Parcourir la source

Created getItemsFromConfig toolbar util.

Aleksander Nowodzinski il y a 8 ans
Parent
commit
af0d86ac9c

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

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module ui/toolbar/utils
+ */
+
+import ToolbarSeparatorView from './toolbarseparatorview';
+
+/**
+ * An utility which expands a plain toolbar configuration into a collection
+ * of {@link module:ui/view~View views} using a given factory.
+ *
+ * @param {Object} 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 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 );
+}

+ 54 - 0
packages/ckeditor5-ui/tests/toolbar/utils.js

@@ -0,0 +1,54 @@
+/**
+ * @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/utils';
+
+describe( 'utils', () => {
+	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;
+	};
+}