collection.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 EmitterMixin from './emittermixin.js';
  7. import CKEditorError from './ckeditorerror.js';
  8. import utils from './utils.js';
  9. /**
  10. * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
  11. * in the collection (like in an array) or by their ids.
  12. *
  13. * If an object without an `id` property is being added to the collection, the `id` property will be generated
  14. * automatically. Note that the automatically generated id is unique only within this single collection instance.
  15. *
  16. * By default an item in the collection is identified by its `id` property. The name of the identifier can be
  17. * configured through the constructor of the collection.
  18. *
  19. * @memberOf core
  20. * @mixes EventEmitter
  21. */
  22. export default class Collection {
  23. /**
  24. * Creates a new Collection instance.
  25. *
  26. * @param {Iterable} [items] Items to be added to the collection.
  27. * @param {Object} options The options object.
  28. * @param {String} [options.idProperty='id'] The name of the property which is considered to identify an item.
  29. */
  30. constructor( options ) {
  31. /**
  32. * The internal list of items in the collection.
  33. *
  34. * @private
  35. * @type {Object[]}
  36. */
  37. this._items = [];
  38. /**
  39. * The internal map of items in the collection.
  40. *
  41. * @private
  42. * @type {Map}
  43. */
  44. this._itemMap = new Map();
  45. /**
  46. * The name of the property which is considered to identify an item.
  47. *
  48. * @private
  49. * @type {String}
  50. */
  51. this._idProperty = options && options.idProperty || 'id';
  52. }
  53. /**
  54. * The number of items available in the collection.
  55. *
  56. * @property length
  57. */
  58. get length() {
  59. return this._items.length;
  60. }
  61. /**
  62. * Adds an item into the collection.
  63. *
  64. * If the item does not have an id, then it will be automatically generated and set on the item.
  65. *
  66. * @chainable
  67. * @param {Object} item
  68. * @param {Number} [index] The position of the item in the collection. The item
  69. * is pushed to the collection when `index` not specified.
  70. * @fires core.Collection#add
  71. */
  72. add( item, index ) {
  73. let itemId;
  74. const idProperty = this._idProperty;
  75. if ( ( idProperty in item ) ) {
  76. itemId = item[ idProperty ];
  77. if ( typeof itemId != 'string' ) {
  78. /**
  79. * This item's id should be a string.
  80. *
  81. * @error collection-add-invalid-id
  82. */
  83. throw new CKEditorError( 'collection-add-invalid-id' );
  84. }
  85. if ( this.get( itemId ) ) {
  86. /**
  87. * This item already exists in the collection.
  88. *
  89. * @error collection-add-item-already-exists
  90. */
  91. throw new CKEditorError( 'collection-add-item-already-exists' );
  92. }
  93. } else {
  94. itemId = this._getNextId();
  95. item[ idProperty ] = itemId;
  96. }
  97. // TODO: Use ES6 default function argument.
  98. if ( index === undefined ) {
  99. index = this._items.length;
  100. } else if ( index > this._items.length || index < 0 ) {
  101. /**
  102. * The index number has invalid value.
  103. *
  104. * @error collection-add-item-bad-index
  105. */
  106. throw new CKEditorError( 'collection-add-item-invalid-index' );
  107. }
  108. this._items.splice( index, 0, item );
  109. this._itemMap.set( itemId, item );
  110. this.fire( 'add', item, index );
  111. return this;
  112. }
  113. /**
  114. * Gets item by its id or index.
  115. *
  116. * @param {String|Number} idOrIndex The item id or index in the collection.
  117. * @returns {Object} The requested item or `null` if such item does not exist.
  118. */
  119. get( idOrIndex ) {
  120. let item;
  121. if ( typeof idOrIndex == 'string' ) {
  122. item = this._itemMap.get( idOrIndex );
  123. } else if ( typeof idOrIndex == 'number' ) {
  124. item = this._items[ idOrIndex ];
  125. } else {
  126. /**
  127. * Index or id must be given.
  128. *
  129. * @error collection-get-invalid-arg
  130. */
  131. throw new CKEditorError( 'collection-get-invalid-arg: Index or id must be given.' );
  132. }
  133. return item || null;
  134. }
  135. /**
  136. * Removes an item from the collection.
  137. *
  138. * @param {Object|Number|String} subject The item to remove, its id or index in the collection.
  139. * @returns {Object} The removed item.
  140. * @fires core.Collection#remove
  141. */
  142. remove( subject ) {
  143. let index, id, item;
  144. let itemDoesNotExist = false;
  145. const idProperty = this._idProperty;
  146. if ( typeof subject == 'string' ) {
  147. id = subject;
  148. item = this._itemMap.get( id );
  149. itemDoesNotExist = !item;
  150. if ( item ) {
  151. index = this._items.indexOf( item );
  152. }
  153. } else if ( typeof subject == 'number' ) {
  154. index = subject;
  155. item = this._items[ index ];
  156. itemDoesNotExist = !item;
  157. if ( item ) {
  158. id = item[ idProperty ];
  159. }
  160. } else {
  161. item = subject;
  162. id = item[ idProperty ];
  163. index = this._items.indexOf( item );
  164. itemDoesNotExist = ( index == -1 || !this._itemMap.get( id ) );
  165. }
  166. if ( itemDoesNotExist ) {
  167. /**
  168. * Item not found.
  169. *
  170. * @error collection-remove-404
  171. */
  172. throw new CKEditorError( 'collection-remove-404: Item not found.' );
  173. }
  174. this._items.splice( index, 1 );
  175. this._itemMap.delete( id );
  176. this.fire( 'remove', item );
  177. return item;
  178. }
  179. /**
  180. * Executes the callback for each item in the collection and composes an array or values returned by this callback.
  181. *
  182. * @param {Function} callback
  183. * @param {Item} callback.item
  184. * @param {Number} callback.index
  185. * @params {Object} ctx Context in which the `callback` will be called.
  186. * @returns {Array} The result of mapping.
  187. */
  188. map( callback, ctx ) {
  189. return this._items.map( callback, ctx );
  190. }
  191. /**
  192. * Finds the first item in the collection for which the `callback` returns a true value.
  193. *
  194. * @param {Function} callback
  195. * @param {Object} callback.item
  196. * @param {Number} callback.index
  197. * @returns {Object} The item for which `callback` returned a true value.
  198. * @params {Object} ctx Context in which the `callback` will be called.
  199. */
  200. find( callback, ctx ) {
  201. return this._items.find( callback, ctx );
  202. }
  203. /**
  204. * Returns an array with items for which the `callback` returned a true value.
  205. *
  206. * @param {Function} callback
  207. * @param {Object} callback.item
  208. * @param {Number} callback.index
  209. * @params {Object} ctx Context in which the `callback` will be called.
  210. * @returns {Object[]} The array with matching items.
  211. */
  212. filter( callback, ctx ) {
  213. return this._items.filter( callback, ctx );
  214. }
  215. /**
  216. * Collection iterator.
  217. */
  218. [ Symbol.iterator ]() {
  219. return this._items[ Symbol.iterator ]();
  220. }
  221. /**
  222. * Generates next (not yet used) id for unidentified item being add to the collection.
  223. *
  224. * @returns {String} The next id.
  225. */
  226. _getNextId() {
  227. let id;
  228. do {
  229. id = String( utils.uid() );
  230. } while ( this._itemMap.has( id ) );
  231. return id;
  232. }
  233. }
  234. utils.mix( Collection, EmitterMixin );
  235. /**
  236. * Fired when an item is added to the collection.
  237. *
  238. * @event core.Collection#add
  239. * @param {Object} item The added item.
  240. */
  241. /**
  242. * Fired when an item is removed from the collection.
  243. *
  244. * @event core.Collection#remove
  245. * @param {Object} item The removed item.
  246. */