collection.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * Collections are ordered sets of models.
  8. *
  9. * See also {@link core/NamedCollection}.
  10. *
  11. * @class Collection
  12. * @mixins EventEmitter
  13. */
  14. CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], function( EmitterMixin, CKEditorError, utils ) {
  15. class Collection {
  16. /**
  17. * Creates a new Collection instance.
  18. *
  19. * @constructor
  20. */
  21. constructor() {
  22. /**
  23. * The internal list of models in the collection.
  24. *
  25. * @property _models
  26. * @private
  27. */
  28. this._models = [];
  29. }
  30. /**
  31. * The number of items available in the collection.
  32. *
  33. * @property length
  34. */
  35. get length() {
  36. return this._models.length;
  37. }
  38. /**
  39. * Adds an item into the collection.
  40. *
  41. * Note that this is an array-like collection, so the same item can be present more than once. This behavior is
  42. * for performance purposes only and is not guaranteed to be kept in the same way in the future.
  43. *
  44. * @param {Model} model The item to be added.
  45. */
  46. add( model ) {
  47. this._models.push( model );
  48. this.fire( 'add', model );
  49. }
  50. /**
  51. * Gets one item from the collection.
  52. *
  53. * @param {Number} index The index to take the item from.
  54. * @returns {Model} The requested item or `null` if such item does not exist.
  55. */
  56. get( index ) {
  57. return this._models[ index ] || null;
  58. }
  59. /**
  60. * Removes an item from the collection.
  61. *
  62. * @param {Model|Number} modelOrIndex Either the item itself or its index inside the collection.
  63. * @returns {Model} The removed item.
  64. */
  65. remove( modelOrIndex ) {
  66. // If a model has been passed, convert it to its index.
  67. if ( typeof modelOrIndex != 'number' ) {
  68. modelOrIndex = this._models.indexOf( modelOrIndex );
  69. if ( modelOrIndex == -1 ) {
  70. /**
  71. * Model not found.
  72. *
  73. * @error collection-model-404
  74. */
  75. throw new CKEditorError( 'collection-model-404: Model not found.' );
  76. }
  77. }
  78. var removedModel = this._models.splice( modelOrIndex, 1 )[ 0 ];
  79. if ( !removedModel ) {
  80. /**
  81. * Index not found.
  82. *
  83. * @error collection-index-404
  84. */
  85. throw new CKEditorError( 'collection-index-404: Index not found.' );
  86. }
  87. this.fire( 'remove', removedModel );
  88. return removedModel;
  89. }
  90. /**
  91. * Executes the callback for each model in the collection.
  92. *
  93. * @param {Function} callback
  94. * @param {Model} callback.item
  95. * @param {String} callback.index
  96. * @params {Object} ctx Context in which the `callback` will be called.
  97. */
  98. forEach( callback, ctx ) {
  99. this._models.forEach( callback, ctx );
  100. }
  101. /**
  102. * Finds the first item in the collection for which the `callback` returns a true value.
  103. *
  104. * @param {Function} callback
  105. * @param {Model} callback.item
  106. * @param {String} callback.name
  107. * @returns {Model} The item for which `callback` returned a true value.
  108. * @params {Object} ctx Context in which the `callback` will be called.
  109. */
  110. find( callback, ctx ) {
  111. return this._models.find( callback, ctx );
  112. }
  113. /**
  114. * Returns an array with items for which the `callback` returned a true value.
  115. *
  116. * @param {Function} callback
  117. * @param {Model} callback.item
  118. * @param {String} callback.name
  119. * @params {Object} ctx Context in which the `callback` will be called.
  120. * @returns {Model[]} The array with matching items.
  121. */
  122. filter( callback, ctx ) {
  123. return this._models.filter( callback, ctx );
  124. }
  125. }
  126. utils.extend( Collection.prototype, EmitterMixin );
  127. return Collection;
  128. } );
  129. /**
  130. * Fired when an item is added to the collection.
  131. *
  132. * @event add
  133. * @param {Model} model The added item.
  134. */
  135. /**
  136. * Fired when an item is removed from the collection.
  137. *
  138. * @event remove
  139. * @param {Model} model The removed item.
  140. */