Sfoglia il codice sorgente

Merge pull request #50 from ckeditor/t/6

t/6: Added Controller.addCollection() method.
Piotrek Koszuliński 9 anni fa
parent
commit
5a5c150a39

+ 0 - 1
packages/ckeditor5-ui/src/componentfactory.js

@@ -16,7 +16,6 @@ import CKEditorError from '../utils/ckeditorerror.js';
  *
  * @memberOf ui
  */
-
 export default class ComponentFactory {
 	/**
 	 * Creates ComponentFactory instance.

+ 18 - 1
packages/ckeditor5-ui/src/controller.js

@@ -4,6 +4,7 @@
  */
 
 import Collection from '../utils/collection.js';
+import ControllerCollection from './controllercollection.js';
 import CKEditorError from '../utils/ckeditorerror.js';
 import EmitterMixin from '../utils/emittermixin.js';
 import mix from '../utils/mix.js';
@@ -14,7 +15,6 @@ import mix from '../utils/mix.js';
  * @memberOf ui
  * @mixes utils.EmitterMixin
  */
-
 export default class Controller {
 	/**
 	 * Creates an instance of the {@link ui.Controller} class.
@@ -153,6 +153,22 @@ export default class Controller {
 	}
 
 	/**
+	 * Adds a new collection to {@link ui.Controller#collections}.
+	 *
+	 * @param {String} collectionName Name of the controller collection.
+	 * @param {utils.Locale} [locale] The {@link ckeditor5.Editor#locale editor's locale} instance.
+	 * See {@link ui.ControllerCollection#locale}.
+	 * @returns {ui.ControllerCollection} The new collection instance.
+	 */
+	addCollection( collectionName, locale ) {
+		const collection = new ControllerCollection( collectionName, locale );
+
+		this.collections.add( collection );
+
+		return collection;
+	}
+
+	/**
 	 * Adds a child {@link Controller} instance to {@link #collections} at given index.
 	 *
 	 * @param {String} collectionName Name of the Controller Collection.
@@ -168,6 +184,7 @@ export default class Controller {
 	 *
 	 * @param {String} collectionName Name of the Controller Collection.
 	 * @param {ui.Controller|Number} toRemove A Controller instance or index to be removed.
+	 * @returns {Object} The removed item.
 	 */
 	remove( collectionName, toRemove ) {
 		return this.collections.get( collectionName ).remove( toRemove );

+ 2 - 3
packages/ckeditor5-ui/src/editorui/boxed/boxededitorui.js

@@ -4,7 +4,6 @@
  */
 
 import EditorUI from '../../../ui/editorui/editorui.js';
-import ControllerCollection from '../../../ui/controllercollection.js';
 
 /**
  * The boxed editor UI controller class. This class controls an editor interface
@@ -27,8 +26,8 @@ export default class BoxedEditorUI extends EditorUI {
 	constructor( editor ) {
 		super( editor );
 
-		this.collections.add( new ControllerCollection( 'top' ) );
-		this.collections.add( new ControllerCollection( 'main' ) );
+		this.addCollection( 'top' );
+		this.addCollection( 'main' );
 
 		const config = editor.config;
 

+ 1 - 2
packages/ckeditor5-ui/src/editorui/editorui.js

@@ -4,7 +4,6 @@
  */
 
 import Controller from '../controller.js';
-import ControllerCollection from '../controllercollection.js';
 import ComponentFactory from '../componentfactory.js';
 import ObservableMixin from '../../utils/observablemixin.js';
 import IconManager from '../iconmanager/iconmanager.js';
@@ -46,7 +45,7 @@ export default class EditorUI extends Controller {
 		 */
 		this.featureComponents = new ComponentFactory( editor );
 
-		this.collections.add( new ControllerCollection( 'body' ) );
+		this.addCollection( 'body' );
 	}
 
 	/**

+ 1 - 2
packages/ckeditor5-ui/tests/_utils/utils.js

@@ -5,7 +5,6 @@
 
 import View from '/ckeditor5/ui/view.js';
 import Controller from '/ckeditor5/ui/controller.js';
-import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
 
 /**
  * Test utils for CKEditor UI.
@@ -44,7 +43,7 @@ const utils = {
 		const controller = new Controller( null, new TestUIView() );
 
 		for ( let r in regions ) {
-			controller.collections.add( new ControllerCollection( r ) );
+			controller.addCollection( r );
 		}
 
 		return controller.init().then( () => {

+ 1 - 2
packages/ckeditor5-ui/tests/bindings/toolbarbindingsmixin.js

@@ -8,7 +8,6 @@
 import mix from '/ckeditor5/utils/mix.js';
 import Editor from '/ckeditor5/editor/editor.js';
 import Controller from '/ckeditor5/ui/controller.js';
-import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
 import ToolbarBindingsMixin from '/ckeditor5/ui/bindings/toolbarbindingsmixin.js';
 
 describe( 'ToolbarBindingsMixin', () => {
@@ -21,7 +20,7 @@ describe( 'ToolbarBindingsMixin', () => {
 			constructor( model, view ) {
 				super( model, view );
 
-				this.collections.add( new ControllerCollection( 'buttons' ) );
+				this.addCollection( 'buttons' );
 			}
 		}
 

+ 28 - 1
packages/ckeditor5-ui/tests/controller.js

@@ -406,6 +406,33 @@ describe( 'Controller', () => {
 				} );
 		} );
 	} );
+
+	describe( 'addCollection', () => {
+		it( 'should add a new collection', () => {
+			const controller = new Controller();
+
+			controller.addCollection( 'foo' );
+
+			expect( controller.collections ).to.have.length( 1 );
+			expect( controller.collections.get( 'foo' ).name ).to.equal( 'foo' );
+		} );
+
+		it( 'should return the collection which has been created (chaining)', () => {
+			const controller = new Controller();
+			const returned = controller.addCollection( 'foo' );
+
+			expect( returned ).to.be.instanceOf( ControllerCollection );
+		} );
+
+		it( 'should pass locale to controller collection', () => {
+			const controller = new Controller();
+			const locale = {};
+
+			controller.addCollection( 'foo', locale );
+
+			expect( controller.collections.get( 'foo' ).locale ).to.equal( locale );
+		} );
+	} );
 } );
 
 function defineParentViewClass() {
@@ -424,7 +451,7 @@ function defineParentControllerClass() {
 		constructor( ...args ) {
 			super( ...args );
 
-			this.collections.add( new ControllerCollection( 'x' ) );
+			this.addCollection( 'x' );
 		}
 	};
 }

+ 4 - 11
packages/ckeditor5-ui/tests/controllercollection.js

@@ -45,9 +45,7 @@ describe( 'ControllerCollection', () => {
 		it( 'should add a child controller and return promise', () => {
 			const parentController = new Controller();
 			const childController = new Controller();
-			const collection = new ControllerCollection( 'x' );
-
-			parentController.collections.add( collection );
+			const collection = parentController.addCollection( 'x' );
 
 			const returned = collection.add( childController );
 
@@ -59,9 +57,7 @@ describe( 'ControllerCollection', () => {
 			const parentController = new Controller();
 			const childController1 = new Controller();
 			const childController2 = new Controller();
-			const collection = new ControllerCollection( 'x' );
-
-			parentController.collections.add( collection );
+			const collection = parentController.addCollection( 'x' );
 
 			collection.add( childController1 );
 			collection.add( childController2, 0 );
@@ -74,9 +70,8 @@ describe( 'ControllerCollection', () => {
 			const parentController = new Controller( null, new ParentView() );
 			const childController = new Controller( null, new View() );
 			const spy = testUtils.sinon.spy( childController, 'init' );
-			const collection = new ControllerCollection( 'x' );
+			const collection = parentController.addCollection( 'x' );
 
-			parentController.collections.add( collection );
 			collection.add( childController );
 			collection.remove( childController );
 
@@ -95,9 +90,7 @@ describe( 'ControllerCollection', () => {
 			const parentController = new Controller( null, new ParentView() );
 			const childController = new Controller( null, new View() );
 			const spy = testUtils.sinon.spy( childController, 'init' );
-			const collection = new ControllerCollection( 'x' );
-
-			parentController.collections.add( collection );
+			const collection = parentController.addCollection( 'x' );
 
 			return parentController.init()
 				.then( () => {