浏览代码

Implemented ToolbarBindingsMixin#bindToolbarItems method and used it in (Sticky)Toolbar bindings instead of #addButtons.

Aleksander Nowodzinski 9 年之前
父节点
当前提交
17d49a9288

+ 6 - 0
packages/ckeditor5-ui/src/bindings/stickytoolbar.js

@@ -28,6 +28,12 @@ export default class StickyToolbar extends BaseStickyToolbar {
 
 		this.editor = editor;
 	}
+
+	init() {
+		this.bindToolbarItems();
+
+		return super.init();
+	}
 }
 
 mix( StickyToolbar, ToolbarBindingsMixin );

+ 6 - 0
packages/ckeditor5-ui/src/bindings/toolbar.js

@@ -28,6 +28,12 @@ export default class Toolbar extends BaseToolbar {
 
 		this.editor = editor;
 	}
+
+	init() {
+		this.bindToolbarItems();
+
+		return super.init();
+	}
 }
 
 mix( Toolbar, ToolbarBindingsMixin );

+ 21 - 7
packages/ckeditor5-ui/src/bindings/toolbarbindingsmixin.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+import Collection from '../../utils/collection.js';
+
 /**
  * Mixin that injects the common Toolbar–like bindings.
  *
@@ -12,15 +14,27 @@
  */
 const ToolbarBindingsMixin = {
 	/**
-	 * Adds buttons to the toolbar. Buttons are taken from the {@link ui.editorUI.EditorUI#featureComponents}
-	 * factory.
-	 *
-	 * @param {String[]} buttons The name of the buttons to add to the toolbar.
+	 * Adds items like buttons to the toolbar. Buttons are generated by the
+	 * {@link ui.editorUI.EditorUI#featureComponents} factory.
 	 */
-	addButtons( buttons ) {
-		for ( let button of buttons ) {
-			this.add( 'buttons', this.editor.ui.featureComponents.create( button ) );
+	bindToolbarItems() {
+		/**
+		 * Collection of toolbar items.
+		 *
+		 * @member {utils.Collection} ui.bindings.ToolbarBindingsMixin#items
+		 */
+		this.items = new Collection();
+
+		if ( this.model.config ) {
+			// Translate config to dynamic item collection.
+			for ( let name of this.model.config ) {
+				this.items.add( { name } );
+			}
 		}
+
+		this.collections.get( 'items' ).bind( this.items ).as( ( { name } ) => {
+			return this.editor.ui.featureComponents.create( name );
+		} );
 	}
 };
 

+ 28 - 5
packages/ckeditor5-ui/tests/bindings/stickytoolbar.js

@@ -3,21 +3,34 @@
  * For licensing, see LICENSE.md.
  */
 
-/* bender-tags: ui, toolbar */
+/* bender-tags: ui, bindings, toolbar */
 
 import Editor from '/ckeditor5/editor/editor.js';
 import Model from '/ckeditor5/ui/model.js';
 import View from '/ckeditor5/ui/view.js';
-import StickyToolbar from '/ckeditor5/ui/bindings/toolbar.js';
+import Controller from '/ckeditor5/ui/controller.js';
+import StickyToolbar from '/ckeditor5/ui/bindings/stickytoolbar.js';
+
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+testUtils.createSinonSandbox();
 
 describe( 'StickyToolbar', () => {
-	let toolbar, model, editor;
+	let toolbar, model;
+
+	const editor = new Editor();
+
+	editor.ui = {
+		featureComponents: {
+			create: () => new Controller()
+		}
+	};
 
 	beforeEach( () => {
-		editor = new Editor();
 		model = new Model( {
-			isActive: false
+			isActive: false,
+			config: [ 'bold', 'italic' ]
 		} );
+
 		toolbar = new StickyToolbar( model, new View(), editor );
 	} );
 
@@ -26,4 +39,14 @@ describe( 'StickyToolbar', () => {
 			expect( toolbar ).to.have.property( 'editor', editor );
 		} );
 	} );
+
+	describe( 'init', () => {
+		it( 'calls bindToolbarItems', () => {
+			const spy = testUtils.sinon.spy( toolbar, 'bindToolbarItems' );
+
+			return toolbar.init().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+	} );
 } );

+ 27 - 4
packages/ckeditor5-ui/tests/bindings/toolbar.js

@@ -3,21 +3,34 @@
  * For licensing, see LICENSE.md.
  */
 
-/* bender-tags: ui, toolbar */
+/* bender-tags: ui, bindings, toolbar */
 
 import Editor from '/ckeditor5/editor/editor.js';
 import Model from '/ckeditor5/ui/model.js';
 import View from '/ckeditor5/ui/view.js';
+import Controller from '/ckeditor5/ui/controller.js';
 import Toolbar from '/ckeditor5/ui/bindings/toolbar.js';
 
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+testUtils.createSinonSandbox();
+
 describe( 'Toolbar', () => {
-	let toolbar, model, editor;
+	let toolbar, model;
+
+	const editor = new Editor();
+
+	editor.ui = {
+		featureComponents: {
+			create: () => new Controller()
+		}
+	};
 
 	beforeEach( () => {
-		editor = new Editor();
 		model = new Model( {
-			isActive: false
+			isActive: false,
+			config: [ 'bold', 'italic' ]
 		} );
+
 		toolbar = new Toolbar( model, new View(), editor );
 	} );
 
@@ -26,4 +39,14 @@ describe( 'Toolbar', () => {
 			expect( toolbar ).to.have.property( 'editor', editor );
 		} );
 	} );
+
+	describe( 'init', () => {
+		it( 'calls bindToolbarItems', () => {
+			const spy = testUtils.sinon.spy( toolbar, 'bindToolbarItems' );
+
+			return toolbar.init().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+	} );
 } );

+ 37 - 34
packages/ckeditor5-ui/tests/bindings/toolbarbindingsmixin.js

@@ -3,63 +3,66 @@
  * For licensing, see LICENSE.md.
  */
 
-/* bender-tags: ui, toolbar */
+/* bender-tags: ui, bindings, toolbar */
 
 import mix from '/ckeditor5/utils/mix.js';
 import Editor from '/ckeditor5/editor/editor.js';
+import Collection from '/ckeditor5/utils/collection.js';
+import Model from '/ckeditor5/ui/model.js';
 import Controller from '/ckeditor5/ui/controller.js';
 import ToolbarBindingsMixin from '/ckeditor5/ui/bindings/toolbarbindingsmixin.js';
 
 describe( 'ToolbarBindingsMixin', () => {
-	let mixinInstance, editor;
+	const editor = new Editor();
+	let mixinInstance;
 
-	beforeEach( () => {
-		editor = new Editor();
+	editor.ui = {
+		featureComponents: {
+			create: () => new Controller()
+		}
+	};
 
-		class MixClass extends Controller {
-			constructor( model, view ) {
-				super( model, view );
+	class MixClass extends Controller {
+		constructor( model, view ) {
+			super( model, view );
 
-				this.addCollection( 'buttons' );
-			}
+			this.editor = editor;
+			this.addCollection( 'items' );
 		}
+	}
 
-		mix( MixClass, ToolbarBindingsMixin );
+	mix( MixClass, ToolbarBindingsMixin );
 
-		mixinInstance = new MixClass();
-		mixinInstance.editor = editor;
+	beforeEach( () => {
+		mixinInstance = new MixClass( new Model( {
+			config: [ 'bold', 'italic' ]
+		} ) );
 	} );
 
-	describe( 'addButtons', () => {
-		it( 'creates buttons for each button name', () => {
-			const createSpy = sinon.spy( () => new Controller() );
+	describe( 'bindToolbarItems', () => {
+		it( 'creates item collection', () => {
+			mixinInstance.bindToolbarItems();
 
-			editor.ui = {
-				featureComponents: {
-					create: createSpy
-				}
-			};
+			expect( mixinInstance.items ).to.be.instanceOf( Collection );
+			expect( mixinInstance.items.map( i => i.name ) ).to.have.members( [ 'bold', 'italic' ] );
+		} );
 
-			mixinInstance.addButtons( [ 'foo', 'bar' ] );
+		it( 'works when no config specified in the model', () => {
+			mixinInstance = new MixClass( new Model( {} ) );
+			mixinInstance.bindToolbarItems();
 
-			expect( createSpy.callCount ).to.equal( 2 );
-			expect( createSpy.firstCall.calledWith( 'foo' ) ).to.be.true;
-			expect( createSpy.secondCall.calledWith( 'bar' ) ).to.be.true;
+			expect( mixinInstance.items ).to.be.instanceOf( Collection );
+			expect( mixinInstance.items ).to.have.length( 0 );
 		} );
 
-		it( 'adds created components to the collection of buttons', () => {
-			const component = new Controller();
-			const createSpy = sinon.spy( () => component );
+		it( 'binds item collection to "items" controller collection', () => {
+			const items = mixinInstance.collections.get( 'items' );
 
-			editor.ui = {
-				featureComponents: {
-					create: createSpy
-				}
-			};
+			expect( items ).to.have.length( 0 );
 
-			mixinInstance.addButtons( [ 'foo' ] );
+			mixinInstance.bindToolbarItems();
 
-			expect( mixinInstance.collections.get( 'buttons' ).get( 0 ) ).to.equal( component );
+			expect( items ).to.have.length( 2 );
 		} );
 	} );
 } );