8
0

collection.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * @class Collection
  10. * @mixins EventEmitter
  11. */
  12. CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
  13. class Collection {
  14. /**
  15. * Creates a new Collection instance.
  16. *
  17. * @constructor
  18. */
  19. constructor() {
  20. /**
  21. * The internal list of models in the collection.
  22. *
  23. * @property _models
  24. * @private
  25. */
  26. this._models = [];
  27. }
  28. /**
  29. * The number of items available in the collection.
  30. *
  31. * @property length
  32. */
  33. get length() {
  34. return this._models.length;
  35. }
  36. /**
  37. * Adds an item into the collection.
  38. *
  39. * Note that this is an array-like collection, so the same item can be present more than once. This behavior is
  40. * for performance purposes only and is not guaranteed to be kept in the same way in the future.
  41. *
  42. * @param {Model} model The item to be added.
  43. */
  44. add( model ) {
  45. this._models.push( model );
  46. this.fire( 'add', model );
  47. }
  48. /**
  49. * Gets one item from the collection.
  50. *
  51. * @param {Number} index The index to take the item from.
  52. * @returns {Model} The requested item.
  53. */
  54. get( index ) {
  55. var model = this._models[ index ];
  56. if ( !model ) {
  57. throw new Error( 'Index not found' );
  58. }
  59. return model;
  60. }
  61. /**
  62. * Removes an item from the collection.
  63. *
  64. * @param {Model|Number} modelOrIndex Either the item itself or its index inside the collection.
  65. * @returns {Model} The removed item.
  66. */
  67. remove( modelOrIndex ) {
  68. // If a model has been passed, convert it to its index.
  69. if ( typeof modelOrIndex != 'number' ) {
  70. modelOrIndex = this._models.indexOf( modelOrIndex );
  71. if ( modelOrIndex == -1 ) {
  72. throw new Error( 'Model not found' );
  73. }
  74. }
  75. var removedModel = this._models.splice( modelOrIndex, 1 )[ 0 ];
  76. if ( !removedModel ) {
  77. throw new Error( 'Index not found' );
  78. }
  79. this.fire( 'remove', removedModel );
  80. return removedModel;
  81. }
  82. }
  83. utils.extend( Collection.prototype, EmitterMixin );
  84. return Collection;
  85. } );
  86. /**
  87. * Fired when an item is added to the collection.
  88. *
  89. * @event add
  90. * @param {Model} model The added item.
  91. */
  92. /**
  93. * Fired when an item is removed from the collection.
  94. *
  95. * @event remove
  96. * @param {Model} model The removed item.
  97. */