collection.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.
  55. */
  56. get( index ) {
  57. var model = this._models[ index ];
  58. if ( !model ) {
  59. throw new Error( 'Index not found' );
  60. }
  61. return model;
  62. }
  63. /**
  64. * Removes an item from the collection.
  65. *
  66. * @param {Model|Number} modelOrIndex Either the item itself or its index inside the collection.
  67. * @returns {Model} The removed item.
  68. */
  69. remove( modelOrIndex ) {
  70. // If a model has been passed, convert it to its index.
  71. if ( typeof modelOrIndex != 'number' ) {
  72. modelOrIndex = this._models.indexOf( modelOrIndex );
  73. if ( modelOrIndex == -1 ) {
  74. /**
  75. * Model not found.
  76. *
  77. * @error collection-model-404
  78. */
  79. throw new CKEditorError( 'collection-model-404: Model not found.' );
  80. }
  81. }
  82. var removedModel = this._models.splice( modelOrIndex, 1 )[ 0 ];
  83. if ( !removedModel ) {
  84. /**
  85. * Index not found.
  86. *
  87. * @error collection-index-404
  88. */
  89. throw new CKEditorError( 'collection-index-404: Index not found.' );
  90. }
  91. this.fire( 'remove', removedModel );
  92. return removedModel;
  93. }
  94. }
  95. utils.extend( Collection.prototype, EmitterMixin );
  96. return Collection;
  97. } );
  98. /**
  99. * Fired when an item is added to the collection.
  100. *
  101. * @event add
  102. * @param {Model} model The added item.
  103. */
  104. /**
  105. * Fired when an item is removed from the collection.
  106. *
  107. * @event remove
  108. * @param {Model} model The removed item.
  109. */