From 761eb5bab012c22f4bcabe3091fbef706bf8c662 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Tue, 28 Feb 2017 16:15:26 +0100 Subject: [PATCH] Moved getItemsFromConfig util to ToolbarView#fillFromConfig. --- src/toolbar/getitemsfromconfig.js | 34 ------------ src/toolbar/toolbarview.js | 21 ++++++++ .../contextualtoolbar/contextualtoolbar.js | 3 +- tests/manual/imagetoolbar/imagetoolbar.js | 3 +- tests/toolbar/getitemsfromconfig.js | 52 ------------------- tests/toolbar/toolbarview.js | 41 +++++++++++++++ 6 files changed, 64 insertions(+), 90 deletions(-) delete mode 100644 src/toolbar/getitemsfromconfig.js delete mode 100644 tests/toolbar/getitemsfromconfig.js diff --git a/src/toolbar/getitemsfromconfig.js b/src/toolbar/getitemsfromconfig.js deleted file mode 100644 index 3f884dea..00000000 --- a/src/toolbar/getitemsfromconfig.js +++ /dev/null @@ -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 ); -} diff --git a/src/toolbar/toolbarview.js b/src/toolbar/toolbarview.js index eb59b3c7..858b25e4 100644 --- a/src/toolbar/toolbarview.js +++ b/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 ); + } ) ); + } } diff --git a/tests/manual/contextualtoolbar/contextualtoolbar.js b/tests/manual/contextualtoolbar/contextualtoolbar.js index e1db972c..483857ac 100644 --- a/tests/manual/contextualtoolbar/contextualtoolbar.js +++ b/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 ); diff --git a/tests/manual/imagetoolbar/imagetoolbar.js b/tests/manual/imagetoolbar/imagetoolbar.js index e3d24d4c..8b12092c 100644 --- a/tests/manual/imagetoolbar/imagetoolbar.js +++ b/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 ); diff --git a/tests/toolbar/getitemsfromconfig.js b/tests/toolbar/getitemsfromconfig.js deleted file mode 100644 index 54a5754b..00000000 --- a/tests/toolbar/getitemsfromconfig.js +++ /dev/null @@ -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; - }; -} diff --git a/tests/toolbar/toolbarview.js b/tests/toolbar/toolbarview.js index d386a4fe..f5811b8c 100644 --- a/tests/toolbar/toolbarview.js +++ b/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; + }; +}