8
0

node.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Position from './position.js';
  7. import CKEditorError from '../../utils/ckeditorerror.js';
  8. import EmitterMixin from '../../utils/emittermixin.js';
  9. import mix from '../../utils/mix.js';
  10. /**
  11. * Abstract tree view node class.
  12. *
  13. * @abstract
  14. * @memberOf engine.treeView
  15. */
  16. export default class Node {
  17. /**
  18. * Creates a tree view node.
  19. *
  20. * This is an abstract class, so this constructor should not be used directly.
  21. */
  22. constructor() {
  23. /**
  24. * Parent element. Null by default. Set by {@link engine.treeView.Element#insertChildren}.
  25. *
  26. * @readonly
  27. * @member {engine.treeView.Element|engine.treeView.DocumentFragment|null} engine.treeView.Node#parent
  28. */
  29. this.parent = null;
  30. /**
  31. * {@link engine.treeView.TreeView} reference.
  32. *
  33. * @protected
  34. * @member {engine.treeView.TreeView} engine.treeView.Node#_treeView
  35. */
  36. this._treeView = null;
  37. }
  38. /**
  39. * Returns index of the node in the parent element or null if the node has no parent.
  40. *
  41. * Throws error if the parent element does not contain this node.
  42. *
  43. * @returns {Number|null} Index of the node in the parent element or null if the node has not parent.
  44. */
  45. getIndex() {
  46. let pos;
  47. if ( !this.parent ) {
  48. return null;
  49. }
  50. // No parent or child doesn't exist in parent's children.
  51. if ( ( pos = this.parent.getChildIndex( this ) ) == -1 ) {
  52. /**
  53. * The node's parent does not contain this node. It means that the document tree is corrupted.
  54. *
  55. * @error treeview-node-not-found-in-parent
  56. */
  57. throw new CKEditorError( 'treeview-node-not-found-in-parent: The node\'s parent does not contain this node.' );
  58. }
  59. return pos;
  60. }
  61. /**
  62. * Returns nodes next sibling or `null` if it is the last child.
  63. *
  64. * @returns {engine.treeView.Node|null} Nodes next sibling or `null` if it is the last child.
  65. */
  66. getNextSibling() {
  67. const index = this.getIndex();
  68. return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
  69. }
  70. /**
  71. * Returns nodes previous sibling or `null` if it is the first child.
  72. *
  73. * @returns {engine.treeView.Node|null} Nodes previous sibling or `null` if it is the first child.
  74. */
  75. getPreviousSibling() {
  76. const index = this.getIndex();
  77. return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
  78. }
  79. /**
  80. * Gets {@link engine.treeView.TreeView} reference. If the node has {@link engine.treeView.TreeView}, assign by
  81. * {@link engine.treeView.Node#setTreeView} it will be returned. Otherwise {@link engine.treeView.TreeView} of the parents node
  82. * will be returned. If node has no parent, `null` will be returned.
  83. *
  84. * @returns {engine.treeView.TreeView|null} Tree view of the node, tree view of the parent or null.
  85. */
  86. getTreeView() {
  87. if ( this._treeView ) {
  88. return this._treeView;
  89. } else if ( this.parent ) {
  90. return this.parent.getTreeView();
  91. } else {
  92. return null;
  93. }
  94. }
  95. /**
  96. * Returns ancestors array of this node.
  97. *
  98. * @param {Object} options Options object.
  99. * @param {Boolean} [options.includeNode=false] When set to `true` this node will be also included in parent's array.
  100. * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
  101. * otherwise root element will be the first item in the array.
  102. * @returns {Array} Array with ancestors.
  103. */
  104. getAncestors( options = { includeNode: false, parentFirst: false } ) {
  105. const ancestors = [];
  106. let parent = options.includeNode ? this : this.parent;
  107. while ( parent !== null ) {
  108. ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
  109. parent = parent.parent;
  110. }
  111. return ancestors;
  112. }
  113. /**
  114. * Sets the {@link engine.treeView.TreeView} of the node. Note that not all of nodes need to have {@link engine.treeView.TreeView}
  115. * assigned, see {@link engine.treeView.Node#getTreeView}.
  116. *
  117. * @param {engine.treeView.TreeView} treeView Tree view.
  118. */
  119. setTreeView( treeView ) {
  120. this._treeView = treeView;
  121. }
  122. /**
  123. * Removes node from parent.
  124. */
  125. remove() {
  126. this.parent.removeChildren( this.getIndex() );
  127. }
  128. /**
  129. * @param {engine.treeView.ChangeType} type Type of the change.
  130. * @param {engine.treeView.Node} node Changed node.
  131. * @fires engine.treeView.Node#change
  132. */
  133. _fireChange( type, node ) {
  134. this.fire( 'change', type, node );
  135. if ( this.parent ) {
  136. this.parent._fireChange( type, node );
  137. }
  138. }
  139. positionBefore() {
  140. return Position.createBefore( this );
  141. }
  142. positionAfter() {
  143. return Position.createAfter( this );
  144. }
  145. /**
  146. * Clones this node.
  147. *
  148. * @method treeView.Node#clone
  149. * @returns {treeView.Node} Clone of this node.
  150. */
  151. /**
  152. * Checks if provided node is similar to this node.
  153. *
  154. * @method treeView.Node#isSimilar
  155. * @returns {Boolean} True if nodes are similar.
  156. */
  157. /**
  158. * Fired when a node changes.
  159. *
  160. * * In case of {@link engine.treeView.Text text nodes} it will be a change of the text data.
  161. * * In case of {@link engine.treeView.Element elements} it will be a change of child nodes or attributes.
  162. *
  163. * Change event is bubbling, it is fired on the ancestors chain.
  164. *
  165. * @event engine.treeView.Node#change
  166. * @param {engine.treeView.ChangeType} Type of the change.
  167. * @param {engine.treeView.Node} Changed node.
  168. */
  169. }
  170. mix( Node, EmitterMixin );