Browse Source

Added Controller#add/remove() shorthand.

Aleksander Nowodzinski 9 years ago
parent
commit
ea5b05cd51

+ 21 - 0
packages/ckeditor5-engine/src/ui/controller.js

@@ -153,6 +153,27 @@ export default class Controller {
 		return Promise.all( promises );
 		return Promise.all( promises );
 	}
 	}
 
 
+	/**
+	 * Adds a child {@link Controller} instance to {@link #collections} at given index.
+	 *
+	 * @param {String} collectionName Name of the Controller Collection.
+	 * @param {Controller} controller A controller instance to be added.
+	 * @param {Number} [index] An index in the collection.
+	 */
+	add( collectionName, controller, index ) {
+		this.collections.get( collectionName ).add( controller, index );
+	}
+
+	/**
+	 * Removes a child {@link Controller} instance from one of {@link #collections}.
+	 *
+	 * @param {String} collectionName Name of the Controller Collection.
+	 * @param {Controller|Number} toRemove A Controller instance or index to be removed.
+	 */
+	remove( collectionName, toRemove ) {
+		return this.collections.get( collectionName ).remove( toRemove );
+	}
+
 	/**
 	/**
 	 * Initializes the {@link #view} of this controller instance.
 	 * Initializes the {@link #view} of this controller instance.
 	 *
 	 *

+ 74 - 0
packages/ckeditor5-engine/tests/ui/controller.js

@@ -97,6 +97,80 @@ describe( 'Controller', () => {
 		} );
 		} );
 	} );
 	} );
 
 
+	describe( 'add', () => {
+		beforeEach( defineParentControllerClass );
+
+		it( 'should add a controller to specific collection', () => {
+			const parentController = new ParentController();
+			const child1 = new Controller();
+			const child2 = new Controller();
+			const collection = parentController.collections.get( 'x' );
+
+			parentController.add( 'x', child1 );
+			parentController.add( 'x', child2 );
+
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 0 ) ).to.be.equal( child1 );
+			expect( collection.get( 1 ) ).to.be.equal( child2 );
+		} );
+
+		it( 'should add a controller at specific index', () => {
+			const parentController = new ParentController();
+			const child1 = new Controller();
+			const child2 = new Controller();
+			const collection = parentController.collections.get( 'x' );
+
+			parentController.add( 'x', child1 );
+			parentController.add( 'x', child2, 0 );
+
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 0 ) ).to.be.equal( child2 );
+			expect( collection.get( 1 ) ).to.be.equal( child1 );
+		} );
+	} );
+
+	describe( 'remove', () => {
+		beforeEach( defineParentControllerClass );
+
+		it( 'should remove a controller from specific collection – by instance', () => {
+			const parentController = new ParentController();
+			const child1 = new Controller();
+			const child2 = new Controller();
+			const child3 = new Controller();
+			const collection = parentController.collections.get( 'x' );
+
+			parentController.add( 'x', child1 );
+			parentController.add( 'x', child2 );
+			parentController.add( 'x', child3 );
+
+			const removed = parentController.remove( 'x', child2 );
+
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 0 ) ).to.be.equal( child1 );
+			expect( collection.get( 1 ) ).to.be.equal( child3 );
+			expect( removed ).to.be.equal( child2 );
+		} );
+
+		it( 'should remove a controller from specific collection – by index', () => {
+			const parentController = new ParentController();
+			const child1 = new Controller();
+			const child2 = new Controller();
+			const child3 = new Controller();
+			const collection = parentController.collections.get( 'x' );
+
+			parentController.add( 'x', child1 );
+			parentController.add( 'x', child2 );
+			parentController.add( 'x', child3 );
+
+			const removed = parentController.remove( 'x', 1 );
+
+			expect( collection ).to.have.length( 2 );
+			expect( collection.get( 0 ) ).to.be.equal( child1 );
+			expect( collection.get( 1 ) ).to.be.equal( child3 );
+			expect( removed ).to.be.equal( child2 );
+		} );
+	} );
+
 	describe( 'collections', () => {
 	describe( 'collections', () => {
 		describe( 'add', () => {
 		describe( 'add', () => {
 			beforeEach( defineParentViewClass );
 			beforeEach( defineParentViewClass );