ソースを参照

Merge pull request #211 from ckeditor/t/190

t/190: Controller.add/remove shorthand.
Piotrek Koszuliński 9 年 前
コミット
f48b38db7a

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

@@ -152,6 +152,27 @@ export default class Controller {
 		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 {core.ui.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 {core.ui.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.
 	 *

+ 1 - 1
packages/ckeditor5-engine/tests/creator/manual/_utils/creator/classiccreator.js

@@ -36,7 +36,7 @@ export default class ClassicCreator extends Creator {
 		const editable = this._createEditable();
 
 		this.editor.editable = editable;
-		this.editor.ui.collections.get( 'main' ).add( editable );
+		this.editor.ui.add( 'main', editable );
 	}
 
 	_createEditable() {

+ 1 - 1
packages/ckeditor5-engine/tests/creator/manual/_utils/creator/inlinecreator.js

@@ -34,7 +34,7 @@ export default class InlineCreator extends Creator {
 	_setupEditable() {
 		this.editor.editable = this._createEditable();
 
-		this.editor.ui.collections.get( 'editable' ).add( this.editor.editable );
+		this.editor.ui.add( 'editable', this.editor.editable );
 	}
 
 	_createEditable() {

+ 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( 'add', () => {
 			beforeEach( defineParentViewClass );