8
0

collection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module utils/collection
  7. */
  8. import EmitterMixin from './emittermixin';
  9. import CKEditorError from './ckeditorerror';
  10. import uid from './uid';
  11. import isIterable from './isiterable';
  12. import mix from './mix';
  13. /**
  14. * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
  15. * in the collection (like in an array) or by their ids.
  16. *
  17. * If an object without an `id` property is being added to the collection, the `id` property will be generated
  18. * automatically. Note that the automatically generated id is unique only within this single collection instance.
  19. *
  20. * By default an item in the collection is identified by its `id` property. The name of the identifier can be
  21. * configured through the constructor of the collection.
  22. *
  23. * @mixes module:utils/emittermixin~EmitterMixin
  24. */
  25. export default class Collection {
  26. /**
  27. * Creates a new Collection instance.
  28. *
  29. * You can provide an iterable of initial items the collection will be created with:
  30. *
  31. * const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
  32. *
  33. * console.log( collection.get( 0 ) ); // -> { id: 'John' }
  34. * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
  35. * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
  36. *
  37. * Or you can first create a collection and then add new items using the {@link #add} method:
  38. *
  39. * const collection = new Collection();
  40. *
  41. * collection.add( { id: 'John' } );
  42. * console.log( collection.get( 0 ) ); // -> { id: 'John' }
  43. *
  44. * Whatever option you choose, you can always pass a configuration object as the last argument
  45. * of the constructor:
  46. *
  47. * const emptyCollection = new Collection( { idProperty: 'name' } );
  48. * emptyCollection.add( { name: 'John' } );
  49. * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
  50. *
  51. * const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
  52. * nonEmptyCollection.add( { name: 'George' } );
  53. * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
  54. * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
  55. *
  56. * @param {Iterable.<Object>|Object} initialItemsOrOptions The initial items of the collection or
  57. * the options object.
  58. * @param {Object} [options={}] The options object, when the first argument is an array of initial items.
  59. * @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
  60. * Items that do not have such a property will be assigned one when added to the collection.
  61. */
  62. constructor( initialItemsOrOptions = {}, options = {} ) {
  63. const hasInitialItems = isIterable( initialItemsOrOptions );
  64. if ( !hasInitialItems ) {
  65. options = initialItemsOrOptions;
  66. }
  67. /**
  68. * The internal list of items in the collection.
  69. *
  70. * @private
  71. * @member {Object[]}
  72. */
  73. this._items = [];
  74. /**
  75. * The internal map of items in the collection.
  76. *
  77. * @private
  78. * @member {Map}
  79. */
  80. this._itemMap = new Map();
  81. /**
  82. * The name of the property which is considered to identify an item.
  83. *
  84. * @private
  85. * @member {String}
  86. */
  87. this._idProperty = options.idProperty || 'id';
  88. /**
  89. * A helper mapping external items of a bound collection ({@link #bindTo})
  90. * and actual items of this collection. It provides information
  91. * necessary to properly remove items bound to another collection.
  92. *
  93. * See {@link #_bindToInternalToExternalMap}.
  94. *
  95. * @protected
  96. * @member {WeakMap}
  97. */
  98. this._bindToExternalToInternalMap = new WeakMap();
  99. /**
  100. * A helper mapping items of this collection to external items of a bound collection
  101. * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
  102. * to avoid loops in two–way bindings.
  103. *
  104. * See {@link #_bindToExternalToInternalMap}.
  105. *
  106. * @protected
  107. * @member {WeakMap}
  108. */
  109. this._bindToInternalToExternalMap = new WeakMap();
  110. /**
  111. * Stores indexes of skipped items from bound external collection.
  112. *
  113. * @private
  114. * @member {Array}
  115. */
  116. this._skippedIndexesFromExternal = [];
  117. // Set the initial content of the collection (if provided in the constructor).
  118. if ( hasInitialItems ) {
  119. for ( const item of initialItemsOrOptions ) {
  120. this._items.push( item );
  121. this._itemMap.set( this._getItemIdBeforeAdding( item ), item );
  122. }
  123. }
  124. /**
  125. * A collection instance this collection is bound to as a result
  126. * of calling {@link #bindTo} method.
  127. *
  128. * @protected
  129. * @member {module:utils/collection~Collection} #_bindToCollection
  130. */
  131. }
  132. /**
  133. * The number of items available in the collection.
  134. *
  135. * @member {Number} #length
  136. */
  137. get length() {
  138. return this._items.length;
  139. }
  140. /**
  141. * Returns the first item from the collection or null when collection is empty.
  142. *
  143. * @returns {Object|null} The first item or `null` if collection is empty.
  144. */
  145. get first() {
  146. return this._items[ 0 ] || null;
  147. }
  148. /**
  149. * Returns the last item from the collection or null when collection is empty.
  150. *
  151. * @returns {Object|null} The last item or `null` if collection is empty.
  152. */
  153. get last() {
  154. return this._items[ this.length - 1 ] || null;
  155. }
  156. /**
  157. * Adds an item into the collection.
  158. *
  159. * If the item does not have an id, then it will be automatically generated and set on the item.
  160. *
  161. * @chainable
  162. * @param {Object} item
  163. * @param {Number} [index] The position of the item in the collection. The item
  164. * is pushed to the collection when `index` not specified.
  165. * @fires add
  166. * @fires change
  167. */
  168. add( item, index ) {
  169. return this.addMany( [ item ], index );
  170. }
  171. /**
  172. * Adds multiple items into the collection.
  173. *
  174. * Any item not containing an id will get an automatically generated one.
  175. *
  176. * @chainable
  177. * @param {Iterable.<Object>} item
  178. * @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
  179. * @fires add
  180. * @fires change
  181. */
  182. addMany( items, index ) {
  183. if ( index === undefined ) {
  184. index = this._items.length;
  185. } else if ( index > this._items.length || index < 0 ) {
  186. /**
  187. * The index number has invalid value.
  188. *
  189. * @error collection-add-item-bad-index
  190. */
  191. throw new CKEditorError( 'collection-add-item-invalid-index', this );
  192. }
  193. for ( let offset = 0; offset < items.length; offset++ ) {
  194. const item = items[ offset ];
  195. const itemId = this._getItemIdBeforeAdding( item );
  196. const currentItemIndex = index + offset;
  197. this._items.splice( currentItemIndex, 0, item );
  198. this._itemMap.set( itemId, item );
  199. this.fire( 'add', item, currentItemIndex );
  200. }
  201. this.fire( 'change', {
  202. added: items,
  203. removed: [],
  204. index
  205. } );
  206. return this;
  207. }
  208. /**
  209. * Gets item by its id or index.
  210. *
  211. * @param {String|Number} idOrIndex The item id or index in the collection.
  212. * @returns {Object|null} The requested item or `null` if such item does not exist.
  213. */
  214. get( idOrIndex ) {
  215. let item;
  216. if ( typeof idOrIndex == 'string' ) {
  217. item = this._itemMap.get( idOrIndex );
  218. } else if ( typeof idOrIndex == 'number' ) {
  219. item = this._items[ idOrIndex ];
  220. } else {
  221. /**
  222. * Index or id must be given.
  223. *
  224. * @error collection-get-invalid-arg
  225. */
  226. throw new CKEditorError( 'collection-get-invalid-arg: Index or id must be given.', this );
  227. }
  228. return item || null;
  229. }
  230. /**
  231. * Returns a boolean indicating whether the collection contains an item.
  232. *
  233. * @param {Object|String} itemOrId The item or its id in the collection.
  234. * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
  235. */
  236. has( itemOrId ) {
  237. if ( typeof itemOrId == 'string' ) {
  238. return this._itemMap.has( itemOrId );
  239. } else { // Object
  240. const idProperty = this._idProperty;
  241. const id = itemOrId[ idProperty ];
  242. return this._itemMap.has( id );
  243. }
  244. }
  245. /**
  246. * Gets index of item in the collection.
  247. * When item is not defined in the collection then index will be equal -1.
  248. *
  249. * @param {Object|String} itemOrId The item or its id in the collection.
  250. * @returns {Number} Index of given item.
  251. */
  252. getIndex( itemOrId ) {
  253. let item;
  254. if ( typeof itemOrId == 'string' ) {
  255. item = this._itemMap.get( itemOrId );
  256. } else {
  257. item = itemOrId;
  258. }
  259. return this._items.indexOf( item );
  260. }
  261. /**
  262. * Removes an item from the collection.
  263. *
  264. * @param {Object|Number|String} subject The item to remove, its id or index in the collection.
  265. * @returns {Object} The removed item.
  266. * @fires remove
  267. * @fires change
  268. */
  269. remove( subject ) {
  270. const [ item, index ] = this._remove( subject );
  271. this.fire( 'change', {
  272. added: [],
  273. removed: [ item ],
  274. index
  275. } );
  276. return item;
  277. }
  278. /**
  279. * Executes the callback for each item in the collection and composes an array or values returned by this callback.
  280. *
  281. * @param {Function} callback
  282. * @param {Object} callback.item
  283. * @param {Number} callback.index
  284. * @param {Object} ctx Context in which the `callback` will be called.
  285. * @returns {Array} The result of mapping.
  286. */
  287. map( callback, ctx ) {
  288. return this._items.map( callback, ctx );
  289. }
  290. /**
  291. * Finds the first item in the collection for which the `callback` returns a true value.
  292. *
  293. * @param {Function} callback
  294. * @param {Object} callback.item
  295. * @param {Number} callback.index
  296. * @param {Object} ctx Context in which the `callback` will be called.
  297. * @returns {Object} The item for which `callback` returned a true value.
  298. */
  299. find( callback, ctx ) {
  300. return this._items.find( callback, ctx );
  301. }
  302. /**
  303. * Returns an array with items for which the `callback` returned a true value.
  304. *
  305. * @param {Function} callback
  306. * @param {Object} callback.item
  307. * @param {Number} callback.index
  308. * @param {Object} ctx Context in which the `callback` will be called.
  309. * @returns {Object[]} The array with matching items.
  310. */
  311. filter( callback, ctx ) {
  312. return this._items.filter( callback, ctx );
  313. }
  314. /**
  315. * Removes all items from the collection and destroys the binding created using
  316. * {@link #bindTo}.
  317. *
  318. * @fires remove
  319. * @fires change
  320. */
  321. clear() {
  322. if ( this._bindToCollection ) {
  323. this.stopListening( this._bindToCollection );
  324. this._bindToCollection = null;
  325. }
  326. const removedItems = Array.from( this._items );
  327. while ( this.length ) {
  328. this._remove( 0 );
  329. }
  330. this.fire( 'change', {
  331. added: [],
  332. removed: removedItems,
  333. index: 0
  334. } );
  335. }
  336. /**
  337. * Binds and synchronizes the collection with another one.
  338. *
  339. * The binding can be a simple factory:
  340. *
  341. * class FactoryClass {
  342. * constructor( data ) {
  343. * this.label = data.label;
  344. * }
  345. * }
  346. *
  347. * const source = new Collection( { idProperty: 'label' } );
  348. * const target = new Collection();
  349. *
  350. * target.bindTo( source ).as( FactoryClass );
  351. *
  352. * source.add( { label: 'foo' } );
  353. * source.add( { label: 'bar' } );
  354. *
  355. * console.log( target.length ); // 2
  356. * console.log( target.get( 1 ).label ); // 'bar'
  357. *
  358. * source.remove( 0 );
  359. * console.log( target.length ); // 1
  360. * console.log( target.get( 0 ).label ); // 'bar'
  361. *
  362. * or the factory driven by a custom callback:
  363. *
  364. * class FooClass {
  365. * constructor( data ) {
  366. * this.label = data.label;
  367. * }
  368. * }
  369. *
  370. * class BarClass {
  371. * constructor( data ) {
  372. * this.label = data.label;
  373. * }
  374. * }
  375. *
  376. * const source = new Collection( { idProperty: 'label' } );
  377. * const target = new Collection();
  378. *
  379. * target.bindTo( source ).using( ( item ) => {
  380. * if ( item.label == 'foo' ) {
  381. * return new FooClass( item );
  382. * } else {
  383. * return new BarClass( item );
  384. * }
  385. * } );
  386. *
  387. * source.add( { label: 'foo' } );
  388. * source.add( { label: 'bar' } );
  389. *
  390. * console.log( target.length ); // 2
  391. * console.log( target.get( 0 ) instanceof FooClass ); // true
  392. * console.log( target.get( 1 ) instanceof BarClass ); // true
  393. *
  394. * or the factory out of property name:
  395. *
  396. * const source = new Collection( { idProperty: 'label' } );
  397. * const target = new Collection();
  398. *
  399. * target.bindTo( source ).using( 'label' );
  400. *
  401. * source.add( { label: { value: 'foo' } } );
  402. * source.add( { label: { value: 'bar' } } );
  403. *
  404. * console.log( target.length ); // 2
  405. * console.log( target.get( 0 ).value ); // 'foo'
  406. * console.log( target.get( 1 ).value ); // 'bar'
  407. *
  408. * It's possible to skip specified items by returning falsy value:
  409. *
  410. * const source = new Collection();
  411. * const target = new Collection();
  412. *
  413. * target.bindTo( source ).using( item => {
  414. * if ( item.hidden ) {
  415. * return null;
  416. * }
  417. *
  418. * return item;
  419. * } );
  420. *
  421. * source.add( { hidden: true } );
  422. * source.add( { hidden: false } );
  423. *
  424. * console.log( source.length ); // 2
  425. * console.log( target.length ); // 1
  426. *
  427. * **Note**: {@link #clear} can be used to break the binding.
  428. *
  429. * @param {module:utils/collection~Collection} externalCollection A collection to be bound.
  430. * @returns {Object}
  431. * @returns {module:utils/collection~CollectionBindToChain} The binding chain object.
  432. */
  433. bindTo( externalCollection ) {
  434. if ( this._bindToCollection ) {
  435. /**
  436. * The collection cannot be bound more than once.
  437. *
  438. * @error collection-bind-to-rebind
  439. */
  440. throw new CKEditorError( 'collection-bind-to-rebind: The collection cannot be bound more than once.', this );
  441. }
  442. this._bindToCollection = externalCollection;
  443. return {
  444. as: Class => {
  445. this._setUpBindToBinding( item => new Class( item ) );
  446. },
  447. using: callbackOrProperty => {
  448. if ( typeof callbackOrProperty == 'function' ) {
  449. this._setUpBindToBinding( item => callbackOrProperty( item ) );
  450. } else {
  451. this._setUpBindToBinding( item => item[ callbackOrProperty ] );
  452. }
  453. }
  454. };
  455. }
  456. /**
  457. * Finalizes and activates a binding initiated by {#bindTo}.
  458. *
  459. * @protected
  460. * @param {Function} factory A function which produces collection items.
  461. */
  462. _setUpBindToBinding( factory ) {
  463. const externalCollection = this._bindToCollection;
  464. // Adds the item to the collection once a change has been done to the external collection.
  465. //
  466. // @private
  467. const addItem = ( evt, externalItem, index ) => {
  468. const isExternalBoundToThis = externalCollection._bindToCollection == this;
  469. const externalItemBound = externalCollection._bindToInternalToExternalMap.get( externalItem );
  470. // If an external collection is bound to this collection, which makes it a 2–way binding,
  471. // and the particular external collection item is already bound, don't add it here.
  472. // The external item has been created **out of this collection's item** and (re)adding it will
  473. // cause a loop.
  474. if ( isExternalBoundToThis && externalItemBound ) {
  475. this._bindToExternalToInternalMap.set( externalItem, externalItemBound );
  476. this._bindToInternalToExternalMap.set( externalItemBound, externalItem );
  477. } else {
  478. const item = factory( externalItem );
  479. // When there is no item we need to remember skipped index first and then we can skip this item.
  480. if ( !item ) {
  481. this._skippedIndexesFromExternal.push( index );
  482. return;
  483. }
  484. // Lets try to put item at the same index as index in external collection
  485. // but when there are a skipped items in one or both collections we need to recalculate this index.
  486. let finalIndex = index;
  487. // When we try to insert item after some skipped items from external collection we need
  488. // to include this skipped items and decrease index.
  489. //
  490. // For the following example:
  491. // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal' ]
  492. // internal -> [ A ]
  493. //
  494. // Another item is been added at the end of external collection:
  495. // external.add( 'D' )
  496. // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal', 'D' ]
  497. //
  498. // We can't just add 'D' to internal at the same index as index in external because
  499. // this will produce empty indexes what is invalid:
  500. // internal -> [ 'A', empty, empty, 'D' ]
  501. //
  502. // So we need to include skipped items and decrease index
  503. // internal -> [ 'A', 'D' ]
  504. for ( const skipped of this._skippedIndexesFromExternal ) {
  505. if ( index > skipped ) {
  506. finalIndex--;
  507. }
  508. }
  509. // We need to take into consideration that external collection could skip some items from
  510. // internal collection.
  511. //
  512. // For the following example:
  513. // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external' ]
  514. // external -> [ A ]
  515. //
  516. // Another item is been added at the end of external collection:
  517. // external.add( 'D' )
  518. // external -> [ 'A', 'D' ]
  519. //
  520. // We need to include skipped items and place new item after them:
  521. // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external', 'D' ]
  522. for ( const skipped of externalCollection._skippedIndexesFromExternal ) {
  523. if ( finalIndex >= skipped ) {
  524. finalIndex++;
  525. }
  526. }
  527. this._bindToExternalToInternalMap.set( externalItem, item );
  528. this._bindToInternalToExternalMap.set( item, externalItem );
  529. this.add( item, finalIndex );
  530. // After adding new element to internal collection we need update indexes
  531. // of skipped items in external collection.
  532. for ( let i = 0; i < externalCollection._skippedIndexesFromExternal.length; i++ ) {
  533. if ( finalIndex <= externalCollection._skippedIndexesFromExternal[ i ] ) {
  534. externalCollection._skippedIndexesFromExternal[ i ]++;
  535. }
  536. }
  537. }
  538. };
  539. // Load the initial content of the collection.
  540. for ( const externalItem of externalCollection ) {
  541. addItem( null, externalItem, externalCollection.getIndex( externalItem ) );
  542. }
  543. // Synchronize the with collection as new items are added.
  544. this.listenTo( externalCollection, 'add', addItem );
  545. // Synchronize the with collection as new items are removed.
  546. this.listenTo( externalCollection, 'remove', ( evt, externalItem, index ) => {
  547. const item = this._bindToExternalToInternalMap.get( externalItem );
  548. if ( item ) {
  549. this.remove( item );
  550. }
  551. // After removing element from external collection we need update/remove indexes
  552. // of skipped items in internal collection.
  553. this._skippedIndexesFromExternal = this._skippedIndexesFromExternal.reduce( ( result, skipped ) => {
  554. if ( index < skipped ) {
  555. result.push( skipped - 1 );
  556. }
  557. if ( index > skipped ) {
  558. result.push( skipped );
  559. }
  560. return result;
  561. }, [] );
  562. } );
  563. }
  564. /**
  565. * Returns an unique id property for a given `item`.
  566. *
  567. * The method will generate new id and assign it to the `item` if it doesn't have any.
  568. *
  569. * @private
  570. * @param {Object} item Item to be added.
  571. * @returns {String}
  572. */
  573. _getItemIdBeforeAdding( item ) {
  574. const idProperty = this._idProperty;
  575. let itemId;
  576. if ( ( idProperty in item ) ) {
  577. itemId = item[ idProperty ];
  578. if ( typeof itemId != 'string' ) {
  579. /**
  580. * This item's id should be a string.
  581. *
  582. * @error collection-add-invalid-id
  583. */
  584. throw new CKEditorError( 'collection-add-invalid-id', this );
  585. }
  586. if ( this.get( itemId ) ) {
  587. /**
  588. * This item already exists in the collection.
  589. *
  590. * @error collection-add-item-already-exists
  591. */
  592. throw new CKEditorError( 'collection-add-item-already-exists', this );
  593. }
  594. } else {
  595. item[ idProperty ] = itemId = uid();
  596. }
  597. return itemId;
  598. }
  599. /**
  600. * Core {@link #remove} method implementation shared in other functions.
  601. *
  602. * In contrast this method **does not** fire the {@link #event:change} event.
  603. *
  604. * @private
  605. * @param {Object} subject The item to remove, its id or index in the collection.
  606. * @returns {Array} Returns an array with the removed item and its index.
  607. * @fires remove
  608. */
  609. _remove( subject ) {
  610. let index, id, item;
  611. let itemDoesNotExist = false;
  612. const idProperty = this._idProperty;
  613. if ( typeof subject == 'string' ) {
  614. id = subject;
  615. item = this._itemMap.get( id );
  616. itemDoesNotExist = !item;
  617. if ( item ) {
  618. index = this._items.indexOf( item );
  619. }
  620. } else if ( typeof subject == 'number' ) {
  621. index = subject;
  622. item = this._items[ index ];
  623. itemDoesNotExist = !item;
  624. if ( item ) {
  625. id = item[ idProperty ];
  626. }
  627. } else {
  628. item = subject;
  629. id = item[ idProperty ];
  630. index = this._items.indexOf( item );
  631. itemDoesNotExist = ( index == -1 || !this._itemMap.get( id ) );
  632. }
  633. if ( itemDoesNotExist ) {
  634. /**
  635. * Item not found.
  636. *
  637. * @error collection-remove-404
  638. */
  639. throw new CKEditorError( 'collection-remove-404: Item not found.', this );
  640. }
  641. this._items.splice( index, 1 );
  642. this._itemMap.delete( id );
  643. const externalItem = this._bindToInternalToExternalMap.get( item );
  644. this._bindToInternalToExternalMap.delete( item );
  645. this._bindToExternalToInternalMap.delete( externalItem );
  646. this.fire( 'remove', item, index );
  647. return [ item, index ];
  648. }
  649. /**
  650. * Iterable interface.
  651. *
  652. * @returns {Iterable.<*>}
  653. */
  654. [ Symbol.iterator ]() {
  655. return this._items[ Symbol.iterator ]();
  656. }
  657. /**
  658. * Fired when an item is added to the collection.
  659. *
  660. * @event add
  661. * @param {Object} item The added item.
  662. */
  663. /**
  664. * Fired when the collection was changed due to adding or removing items.
  665. *
  666. * @event change
  667. * @param {Iterable.<Object>} added A list of added items.
  668. * @param {Iterable.<Object>} removed A list of removed items.
  669. * @param {Number} index An index where the addition or removal occurred.
  670. */
  671. /**
  672. * Fired when an item is removed from the collection.
  673. *
  674. * @event remove
  675. * @param {Object} item The removed item.
  676. * @param {Number} index Index from which item was removed.
  677. */
  678. }
  679. mix( Collection, EmitterMixin );
  680. /**
  681. * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
  682. * providing functions that specify the type of the binding.
  683. *
  684. * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
  685. *
  686. * @interface module:utils/collection~CollectionBindToChain
  687. */
  688. /**
  689. * Creates a callback or a property binding.
  690. *
  691. * @method #using
  692. * @param {Function|String} callbackOrProperty When the function is passed, it should return
  693. * the collection items. When the string is provided, the property value is used to create the bound collection items.
  694. */
  695. /**
  696. * Creates the class factory binding in which items of the source collection are passed to
  697. * the constructor of the specified class.
  698. *
  699. * @method #as
  700. * @param {Function} Class The class constructor used to create instances in the factory.
  701. */