collection.js 2.5 KB

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