matrix/models/treenode.js

  1. /*
  2. * This file is part of AUX.
  3. *
  4. * AUX is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 3 of the License, or (at your option) any later version.
  8. *
  9. * AUX is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General
  15. * Public License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @module matrix
  21. */
  22. import { MatrixDatum } from './matrixdatum.js';
  23. /**
  24. * Base class for matrix tree objects, e.g. groups and ports.
  25. */
  26. export class TreeNodeData extends MatrixDatum {
  27. /**
  28. * Is true if this object is a group.
  29. */
  30. get isGroup() {
  31. return false;
  32. }
  33. constructor(matrix, o) {
  34. super(matrix, o);
  35. this.parent = null;
  36. }
  37. /**
  38. * The tree node label.
  39. */
  40. set label(value) {
  41. return this.set('label', value);
  42. }
  43. get label() {
  44. return this.get('label');
  45. }
  46. /**
  47. * The tree node icon.
  48. */
  49. set icon(value) {
  50. return this.set('icon', value);
  51. }
  52. get icon() {
  53. return this.get('icon');
  54. }
  55. /**
  56. * The tree node id.
  57. */
  58. get id() {
  59. return this.get('id');
  60. }
  61. setParent(parent) {
  62. if (parent !== null && this.parent !== null) {
  63. throw new Error('Node already has a parent.');
  64. }
  65. if (parent !== null && !parent.isGroup)
  66. throw new TypeError('Parent node must be a group.');
  67. this.parent = parent;
  68. }
  69. /**
  70. * Returns true if this node is a child of the given node.
  71. *
  72. * @param {TreeNodeData} node
  73. */
  74. isChildOf(node) {
  75. for (let _node = this.parent; _node; _node = _node.parent) {
  76. if (_node === node) return true;
  77. }
  78. return false;
  79. }
  80. getPath() {
  81. const parent = this.parent;
  82. if (!parent) return [];
  83. return parent.getPath().concat([parent]);
  84. }
  85. }