PageNode.js 865 B

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