Переглянути джерело

Extended Collection#add() with optional index parameter.

Aleksander Nowodzinski 10 роки тому
батько
коміт
f62c209484
1 змінених файлів з 7 додано та 2 видалено
  1. 7 2
      packages/ckeditor5-utils/src/collection.js

+ 7 - 2
packages/ckeditor5-utils/src/collection.js

@@ -79,8 +79,10 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 		 *
 		 * @chainable
 		 * @param {Object} item
+		 * @param {Number} [index] The position of the item in the collection. The item
+		 * is pushed to the collection when `index` not specified.
 		 */
-		add( item ) {
+		add( item, index ) {
 			let itemId;
 			const idProperty = this._idProperty;
 
@@ -109,7 +111,10 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], ( EmitterMixin, C
 				item[ idProperty ] = itemId;
 			}
 
-			this._items.push( item );
+			// TODO: Use ES6 default function argument.
+			this._items.splice(
+				index === undefined ? this._items.length : index, 0, item );
+
 			this._itemMap.set( itemId, item );
 
 			this.fire( 'add', item );