element.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Node from './node.js';
  7. import NodeList from './nodelist.js';
  8. import utils from '../utils.js';
  9. /**
  10. * Tree data model element.
  11. *
  12. * @memberOf core.treeModel
  13. */
  14. export default class Element extends Node {
  15. /**
  16. * Creates a tree data model element.
  17. *
  18. * @param {String} name Node name.
  19. * @param {Iterable} attrs Iterable collection of attributes.
  20. * @param {core.treeModel.NodeSet} children List of nodes to be inserted
  21. * into created element. List of nodes can be of any type accepted by the {@link core.treeModel.NodeList} constructor.
  22. */
  23. constructor( name, attrs, children ) {
  24. super( attrs );
  25. /**
  26. * Element name.
  27. *
  28. * @readonly
  29. * @type {String}
  30. */
  31. this.name = name;
  32. /**
  33. * List of children nodes.
  34. *
  35. * @protected
  36. * @type {core.treeModel.NodeList}
  37. */
  38. this._children = new NodeList();
  39. if ( children ) {
  40. this.insertChildren( 0, children );
  41. }
  42. }
  43. /**
  44. * Gets child at the given index.
  45. *
  46. * @method core.treeModel.Element#getChild
  47. * @param {Number} index Index of child.
  48. * @returns {core.treeModel.Node} Child node.
  49. */
  50. getChild( index ) {
  51. return this._children.get( index );
  52. }
  53. /**
  54. * Gets the number of element's children.
  55. *
  56. * @method core.treeModel.Element#getChildCount
  57. * @returns {Number} The number of element's children.
  58. */
  59. getChildCount() {
  60. return this._children.length;
  61. }
  62. /**
  63. * Gets index of the given child node.
  64. *
  65. * @method core.treeModel.Element#getChildIndex
  66. * @param {core.treeModel.Node} node Child node.
  67. * @returns {Number} Index of the child node.
  68. */
  69. getChildIndex( node ) {
  70. return this._children.indexOf( node );
  71. }
  72. /**
  73. * {@link core.treeModel.Element#insert Inserts} a child node or a list of child nodes at the end of this node and sets
  74. * the parent of these nodes to this element.
  75. *
  76. * Note that the list of children can be modified only in elements not yet attached to the document.
  77. * All attached nodes should be modified using the {@link core.treeModel.operation.InsertOperation}.
  78. *
  79. * @method core.treeModel.Element#appendChildren
  80. * @param {core.treeModel.NodeSet} nodes The list of nodes to be inserted.
  81. */
  82. appendChildren( nodes ) {
  83. this.insertChildren( this.getChildCount(), nodes );
  84. }
  85. /**
  86. * Inserts a list of child nodes on the given index and sets the parent of these nodes to this element.
  87. *
  88. * Note that the list of children can be modified only in elements not yet attached to the document.
  89. * All attached nodes should be modified using the {@link core.treeModel.operation.InsertOperation}.
  90. *
  91. * @method core.treeModel.Element#insertChildren
  92. * @param {Number} index Position where nodes should be inserted.
  93. * @param {core.treeModel.NodeSet} nodes The list of nodes to be inserted.
  94. * The list of nodes can be of any type accepted by the {@link core.treeModel.NodeList} constructor.
  95. */
  96. insertChildren( index, nodes ) {
  97. let nodeList = new NodeList( nodes );
  98. for ( let node of nodeList._nodes ) {
  99. node.parent = this;
  100. }
  101. this._children.insert( index, nodeList );
  102. }
  103. /**
  104. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  105. *
  106. * Note that the list of children can be modified only in elements not yet attached to the document.
  107. * All attached nodes should be modified using the {@link core.treeModel.operation.RemoveOperation}.
  108. *
  109. * @method core.treeModel.Element#removeChildren
  110. * @param {Number} index Position of the first node to remove.
  111. * @param {Number} [number] Number of nodes to remove.
  112. * @returns {core.treeModel.NodeList} The list of removed nodes.
  113. */
  114. removeChildren( index, number ) {
  115. if ( typeof number === 'undefined' ) {
  116. number = 1;
  117. }
  118. let nodeList = this._children.remove( index, number );
  119. for ( let node of nodeList._nodes ) {
  120. node.parent = null;
  121. }
  122. return nodeList;
  123. }
  124. /**
  125. * Sets attribute on the element. If attribute with the same key already is set, it overwrites its values.
  126. *
  127. * @method core.treeModel.Element#setAttribute
  128. * @param {String} key Key of attribute to set.
  129. * @param {*} value Attribute value.
  130. */
  131. setAttribute( key, value ) {
  132. this._attrs.set( key, value );
  133. }
  134. /**
  135. * Removes all attributes from the element and sets given attributes.
  136. *
  137. * @method core.treeModel.Element#setAttributesTo
  138. * @param {Iterable|Object} attrs Iterable object containing attributes to be set. See {@link core.treeModel.Node#getAttributes}.
  139. */
  140. setAttributesTo( attrs ) {
  141. this._attrs = utils.toMap( attrs );
  142. }
  143. /**
  144. * Removes an attribute with given key from the element.
  145. *
  146. * @method core.treeModel.Element#removeAttribute
  147. * @param {String} key Key of attribute to remove.
  148. * @returns {Boolean} `true` if the attribute was set on the element, `false` otherwise.
  149. */
  150. removeAttribute( key ) {
  151. return this._attrs.delete( key );
  152. }
  153. /**
  154. * Removes all attributes from the element.
  155. *
  156. * @method core.treeModel.Element#clearAttributes
  157. */
  158. clearAttributes() {
  159. this._attrs.clear();
  160. }
  161. }