PageNode.js 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. export class PageNode {
  2. constructor(pagePath) {
  3. this.pagePath = pagePath;
  4. this.children = [];
  5. }
  6. /**
  7. * calculate generations number of decendants
  8. *
  9. * ex:
  10. * /foo -2
  11. * /foo/bar -1
  12. * /foo/bar/buz 0
  13. *
  14. * @returns generations num of decendants
  15. *
  16. * @memberOf PageNode
  17. */
  18. /*
  19. * commented out because it became unnecessary -- 2017.05.18 Yuki Takei
  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. */
  30. static instanciateFrom(obj) {
  31. const pageNode = new PageNode(obj.pagePath);
  32. pageNode.page = obj.page;
  33. // instanciate recursively
  34. pageNode.children = obj.children.map((childObj) => {
  35. return PageNode.instanciateFrom(childObj);
  36. });
  37. return pageNode;
  38. }
  39. }