PageNode.js 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export class PageNode {
  2. constructor(pagePath) {
  3. this.pagePath = pagePath;
  4. this.children = [];
  5. this.page = undefined;
  6. }
  7. /**
  8. * calculate generations number of decendants
  9. *
  10. * ex:
  11. * /foo -2
  12. * /foo/bar -1
  13. * /foo/bar/buz 0
  14. *
  15. * @returns generations num of decendants
  16. *
  17. * @memberOf PageNode
  18. */
  19. /*
  20. * commented out because it became unnecessary -- 2017.05.18 Yuki Takei
  21. *
  22. getDecendantsGenerationsNum() {
  23. if (this.children.length == 0) {
  24. return -1;
  25. }
  26. return -1 + Math.min.apply(null, this.children.map((child) => {
  27. return child.getDecendantsGenerationsNum();
  28. }))
  29. }
  30. */
  31. static instanciateFrom(obj) {
  32. const pageNode = new PageNode(obj.pagePath);
  33. pageNode.page = obj.page;
  34. // instanciate recursively
  35. pageNode.children = obj.children.map((childObj) => {
  36. return PageNode.instanciateFrom(childObj);
  37. });
  38. return pageNode;
  39. }
  40. }