8
0
Pārlūkot izejas kodu

Moved getItemsFromConfig util to ToolbarView#fillFromConfig.

Aleksander Nowodzinski 8 gadi atpakaļ
vecāks
revīzija
8de17e34ba

+ 0 - 34
packages/ckeditor5-ui/src/toolbar/getitemsfromconfig.js

@@ -1,34 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * @module ui/toolbar/getitemsfromconfig
- */
-
-import ToolbarSeparatorView from './toolbarseparatorview';
-
-/**
- * A utility which expands a plain toolbar configuration into a collection
- * of {@link module:ui/view~View views} using a given factory.
- *
- * @param {Array} config The toolbar config.
- * @param {module:utils/collection~Collection} collection A collection into which the config
- * is expanded.
- * @param {module:ui/componentfactory~ComponentFactory} factory A factory producing toolbar items.
- * @returns {Promise} A promise resolved when all toolbar items are initialized.
- */
-export default function getItemsFromConfig( config, collection, factory ) {
-	let promises = [];
-
-	if ( config ) {
-		promises = config.map( name => {
-			const component = name == '|' ? new ToolbarSeparatorView() : factory.create( name );
-
-			return collection.add( component );
-		} );
-	}
-
-	return Promise.all( promises );
-}

+ 21 - 0
packages/ckeditor5-ui/src/toolbar/toolbarview.js

@@ -12,6 +12,7 @@ import Template from '../template';
 import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
 import FocusCycler from '../focuscycler';
 import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
+import ToolbarSeparatorView from './toolbarseparatorview';
 
 /**
  * The toolbar view class.
@@ -105,5 +106,25 @@ export default class ToolbarView extends View {
 	focus() {
 		this._focusCycler.focusFirst();
 	}
+
+	/**
+	 * A utility which expands a plain toolbar configuration into a collection
+	 * of {@link module:ui/view~View views} using a given factory.
+	 *
+	 * @param {Array} config The toolbar config.
+	 * @param {module:ui/componentfactory~ComponentFactory} factory A factory producing toolbar items.
+	 * @returns {Promise} A promise resolved when all new toolbar items are initialized.
+	 */
+	fillFromConfig( config, factory ) {
+		if ( !config ) {
+			return Promise.resolve();
+		}
+
+		return Promise.all( config.map( name => {
+			const component = name == '|' ? new ToolbarSeparatorView() : factory.create( name );
+
+			return this.items.add( component );
+		} ) );
+	}
 }
 

+ 1 - 2
packages/ckeditor5-ui/tests/manual/contextualtoolbar/contextualtoolbar.js

@@ -13,7 +13,6 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Undo from '@ckeditor/ckeditor5-undo/src/undo';
 import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
 import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
-import getItemsFromConfig from '@ckeditor/ckeditor5-ui/src/toolbar/getitemsfromconfig';
 
 import Template from '../../../src/template';
 import ToolbarView from '../../../src/toolbar/toolbarview';
@@ -78,7 +77,7 @@ function createContextualToolbar( editor ) {
 		const editingView = editor.editing.view;
 
 		// Fill the toolbar with some buttons. Simply copy default editor toolbar.
-		getItemsFromConfig( editor.config.get( 'toolbar' ), toolbar.items, editor.ui.componentFactory );
+		toolbar.fillFromConfig( editor.config.get( 'toolbar' ), editor.ui.componentFactory );
 
 		// Let the focusTracker know about new focusable UI element.
 		editor.ui.focusTracker.add( panel.element );

+ 1 - 2
packages/ckeditor5-ui/tests/manual/imagetoolbar/imagetoolbar.js

@@ -14,7 +14,6 @@ import Undo from '@ckeditor/ckeditor5-undo/src/undo';
 import Image from '@ckeditor/ckeditor5-image/src/image';
 import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
 import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
-import getItemsFromConfig from '@ckeditor/ckeditor5-ui/src/toolbar/getitemsfromconfig';
 
 import Template from '../../../src/template';
 import ToolbarView from '../../../src/toolbar/toolbarview';
@@ -80,7 +79,7 @@ function createImageToolbar( editor ) {
 		const editingView = editor.editing.view;
 
 		// Fill the toolbar with some buttons. Simply copy default editor toolbar.
-		getItemsFromConfig( editor.config.get( 'toolbar' ), toolbar.items, editor.ui.componentFactory );
+		toolbar.fillFromConfig( editor.config.get( 'toolbar' ), editor.ui.componentFactory );
 
 		// Let the focusTracker know about new focusable UI element.
 		editor.ui.focusTracker.add( panel.element );

+ 0 - 52
packages/ckeditor5-ui/tests/toolbar/getitemsfromconfig.js

@@ -1,52 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* global document */
-
-import View from '../../src/view';
-import ComponentFactory from '../../src/componentfactory';
-import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
-import Collection from '@ckeditor/ckeditor5-utils/src/collection';
-import ToolbarSeparatorView from '../../src/toolbar/toolbarseparatorview';
-import getItemsFromConfig from '../../src/toolbar/getitemsfromconfig';
-
-describe( 'getItemsFromConfig()', () => {
-	let factory;
-
-	beforeEach( () => {
-		factory = new ComponentFactory( new Editor() );
-
-		factory.add( 'foo', viewCreator( 'foo' ) );
-		factory.add( 'bar', viewCreator( 'bar' ) );
-	} );
-
-	it( 'returns a promise', () => {
-		expect( getItemsFromConfig() ).to.be.instanceOf( Promise );
-	} );
-
-	it( 'expands the config into collection', () => {
-		const collection = new Collection();
-
-		return getItemsFromConfig( [ 'foo', 'bar', '|', 'foo' ], collection, factory )
-			.then( () => {
-				expect( collection ).to.have.length( 4 );
-				expect( collection.get( 0 ).name ).to.equal( 'foo' );
-				expect( collection.get( 1 ).name ).to.equal( 'bar' );
-				expect( collection.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
-				expect( collection.get( 3 ).name ).to.equal( 'foo' );
-			} );
-	} );
-} );
-
-function viewCreator( name ) {
-	return ( locale ) => {
-		const view = new View( locale );
-
-		view.name = name;
-		view.element = document.createElement( 'a' );
-
-		return view;
-	};
-}

+ 41 - 0
packages/ckeditor5-ui/tests/toolbar/toolbarview.js

@@ -6,7 +6,9 @@
 /* global document */
 
 import ToolbarView from '../../src/toolbar/toolbarview';
+import ToolbarSeparatorView from '../../src/toolbar/toolbarseparatorview';
 import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
+import ComponentFactory from '../../src/componentfactory';
 import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
 import FocusCycler from '../../src/focuscycler';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
@@ -209,6 +211,34 @@ describe( 'ToolbarView', () => {
 			sinon.assert.calledOnce( spy );
 		} );
 	} );
+
+	describe( 'fillFromConfig()', () => {
+		let factory;
+
+		beforeEach( () => {
+			factory = new ComponentFactory( {} );
+
+			factory.add( 'foo', namedFactory( 'foo' ) );
+			factory.add( 'bar', namedFactory( 'bar' ) );
+		} );
+
+		it( 'returns a promise', () => {
+			expect( view.fillFromConfig() ).to.be.instanceOf( Promise );
+		} );
+
+		it( 'expands the config into collection', () => {
+			return view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory )
+				.then( () => {
+					const items = view.items;
+
+					expect( items ).to.have.length( 4 );
+					expect( items.get( 0 ).name ).to.equal( 'foo' );
+					expect( items.get( 1 ).name ).to.equal( 'bar' );
+					expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
+					expect( items.get( 3 ).name ).to.equal( 'foo' );
+				} );
+		} );
+	} );
 } );
 
 function focusable() {
@@ -225,3 +255,14 @@ function nonFocusable() {
 
 	return view;
 }
+
+function namedFactory( name ) {
+	return ( locale ) => {
+		const view = new View( locale );
+
+		view.name = name;
+		view.element = document.createElement( 'a' );
+
+		return view;
+	};
+}