| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* global document, Event, console, setTimeout */
- 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';
- import ViewCollection from '../../src/viewcollection';
- import global from '@ckeditor/ckeditor5-utils/src/dom/global';
- import View from '../../src/view';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
- import Locale from '@ckeditor/ckeditor5-utils/src/locale';
- describe( 'ToolbarView', () => {
- let locale, view;
- testUtils.createSinonSandbox();
- before( () => {
- addTranslations( 'pl', {
- 'Editor toolbar': 'Pasek narzędzi edytora'
- } );
- addTranslations( 'en', {
- 'Editor toolbar': 'Editor toolbar'
- } );
- } );
- after( () => {
- clearTranslations();
- } );
- beforeEach( () => {
- locale = new Locale();
- view = new ToolbarView( locale );
- view.render();
- } );
- afterEach( () => {
- sinon.restore();
- view.destroy();
- } );
- describe( 'constructor()', () => {
- it( 'should set view#locale', () => {
- expect( view.locale ).to.equal( locale );
- } );
- it( 'should set view#isVertical', () => {
- expect( view.isVertical ).to.be.false;
- } );
- it( 'should create view#items collection', () => {
- expect( view.items ).to.be.instanceOf( ViewCollection );
- } );
- it( 'should not create view#groupedItems collection', () => {
- expect( view.groupedItems ).to.be.null;
- } );
- it( 'creates #focusTracker instance', () => {
- expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
- } );
- it( 'creates #keystrokes instance', () => {
- expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
- } );
- it( 'should create view#itemsView', () => {
- expect( view.itemsView ).to.be.instanceOf( View );
- } );
- it( 'should not create view#groupedItemsDropdown', () => {
- expect( view.groupedItemsDropdown ).to.be.null;
- } );
- it( 'should set view#shouldGroupWhenFull', () => {
- expect( view.shouldGroupWhenFull ).to.be.false;
- } );
- it( 'should create view#_components collection', () => {
- expect( view._components ).to.be.instanceOf( ViewCollection );
- } );
- it( 'creates #_itemsFocusCycler instance', () => {
- expect( view._itemsFocusCycler ).to.be.instanceOf( FocusCycler );
- } );
- it( 'creates #_componentsFocusCycler instance', () => {
- expect( view._componentsFocusCycler ).to.be.instanceOf( FocusCycler );
- } );
- describe( '#shouldGroupWhenFull', () => {
- it( 'updates the state of grouped items immediatelly when set true', () => {
- sinon.spy( view, 'updateGroupedItems' );
- view.shouldGroupWhenFull = true;
- sinon.assert.calledOnce( view.updateGroupedItems );
- } );
- // Possibly in the future a possibility to turn the automatic grouping off could be required.
- // As for now, there is no such need, so there is no such functionality.
- it( 'does nothing if toggled false', () => {
- view.shouldGroupWhenFull = true;
- expect( () => {
- view.shouldGroupWhenFull = false;
- } ).to.not.throw();
- } );
- it( 'starts observing toolbar resize immediatelly when set true', () => {
- function FakeResizeObserver( callback ) {
- this.callback = callback;
- }
- FakeResizeObserver.prototype.observe = sinon.spy();
- FakeResizeObserver.prototype.disconnect = sinon.spy();
- testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( FakeResizeObserver );
- expect( view._groupWhenFullResizeObserver ).to.be.null;
- view.shouldGroupWhenFull = true;
- sinon.assert.calledOnce( view._groupWhenFullResizeObserver.observe );
- sinon.assert.calledWithExactly( view._groupWhenFullResizeObserver.observe, view.element );
- } );
- it( 'updates the state of grouped items upon resize', () => {
- sinon.spy( view, 'updateGroupedItems' );
- function FakeResizeObserver( callback ) {
- this.callback = callback;
- }
- FakeResizeObserver.prototype.observe = sinon.spy();
- FakeResizeObserver.prototype.disconnect = sinon.spy();
- testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( FakeResizeObserver );
- expect( view._groupWhenFullResizeObserver ).to.be.null;
- view.shouldGroupWhenFull = true;
- view._groupWhenFullResizeObserver.callback( [
- { contentRect: { width: 42 } }
- ] );
- sinon.assert.calledTwice( view.updateGroupedItems );
- } );
- } );
- } );
- describe( 'template', () => {
- it( 'should create element from template', () => {
- expect( view.element.classList.contains( 'ck' ) ).to.true;
- expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
- } );
- it( 'should create #itemsView from template', () => {
- expect( view.element.firstChild ).to.equal( view.itemsView.element );
- expect( view.itemsView.element.classList.contains( 'ck' ) ).to.true;
- expect( view.itemsView.element.classList.contains( 'ck-toolbar__items' ) ).to.true;
- } );
- describe( 'attributes', () => {
- it( 'should be defined', () => {
- expect( view.element.getAttribute( 'role' ) ).to.equal( 'toolbar' );
- expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Editor toolbar' );
- } );
- it( 'should allow a custom aria-label', () => {
- const view = new ToolbarView( locale );
- view.ariaLabel = 'Custom label';
- view.render();
- expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Custom label' );
- } );
- it( 'should allow the aria-label to be translated', () => {
- const view = new ToolbarView( new Locale( { uiLanguage: 'pl' } ) );
- view.render();
- expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Pasek narzędzi edytora' );
- } );
- } );
- describe( 'event listeners', () => {
- it( 'prevent default on #mousedown', () => {
- const evt = new Event( 'mousedown', { bubbles: true } );
- const spy = sinon.spy( evt, 'preventDefault' );
- view.element.dispatchEvent( evt );
- sinon.assert.calledOnce( spy );
- } );
- } );
- } );
- describe( 'element bindings', () => {
- describe( 'class', () => {
- it( 'reacts on view#isVertical', () => {
- view.isVertical = false;
- expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
- view.isVertical = true;
- expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
- } );
- it( 'reacts on view#class', () => {
- view.class = 'foo';
- expect( view.element.classList.contains( 'foo' ) ).to.be.true;
- view.class = 'bar';
- expect( view.element.classList.contains( 'bar' ) ).to.be.true;
- view.class = false;
- expect( view.element.classList.contains( 'foo' ) ).to.be.false;
- expect( view.element.classList.contains( 'bar' ) ).to.be.false;
- } );
- it( 'reacts on view#shouldGroupWhenFull', () => {
- view.shouldGroupWhenFull = false;
- expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.false;
- view.shouldGroupWhenFull = true;
- expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.true;
- } );
- } );
- } );
- describe( 'render()', () => {
- it( 'registers #_components in #focusTracker', () => {
- const view = new ToolbarView( locale );
- const spyAdd = sinon.spy( view.focusTracker, 'add' );
- const spyRemove = sinon.spy( view.focusTracker, 'remove' );
- view._components.add( focusable() );
- view._components.add( focusable() );
- sinon.assert.notCalled( spyAdd );
- view.render();
- // First call is for the #itemsView.
- sinon.assert.calledThrice( spyAdd );
- view._components.remove( 1 );
- sinon.assert.calledOnce( spyRemove );
- view.destroy();
- } );
- it( 'starts listening for #keystrokes coming from #element', () => {
- const view = new ToolbarView( new Locale() );
- const spy = sinon.spy( view.keystrokes, 'listenTo' );
- view.render();
- sinon.assert.calledOnce( spy );
- sinon.assert.calledWithExactly( spy, view.element );
- view.destroy();
- } );
- describe( 'activates keyboard navigation for the toolbar', () => {
- it( 'so "arrowup" focuses previous focusable item', () => {
- const keyEvtData = getArrowKeyData( 'arrowup' );
- // No children to focus.
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( keyEvtData.preventDefault );
- sinon.assert.calledOnce( keyEvtData.stopPropagation );
- view.items.add( nonFocusable() );
- view.items.add( nonFocusable() );
- // No focusable children.
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledTwice( keyEvtData.preventDefault );
- sinon.assert.calledTwice( keyEvtData.stopPropagation );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- // Mock the last item is focused.
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 4 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledThrice( keyEvtData.preventDefault );
- sinon.assert.calledThrice( keyEvtData.stopPropagation );
- sinon.assert.calledOnce( view.items.get( 2 ).focus );
- } );
- it( 'so "arrowleft" focuses previous focusable item', () => {
- const keyEvtData = getArrowKeyData( 'arrowleft' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- // Mock the last item is focused.
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 2 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.items.get( 0 ).focus );
- } );
- it( 'so "arrowdown" focuses next focusable item', () => {
- const keyEvtData = getArrowKeyData( 'arrowdown' );
- // No children to focus.
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( keyEvtData.preventDefault );
- sinon.assert.calledOnce( keyEvtData.stopPropagation );
- view.items.add( nonFocusable() );
- view.items.add( nonFocusable() );
- // No focusable children.
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledTwice( keyEvtData.preventDefault );
- sinon.assert.calledTwice( keyEvtData.stopPropagation );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- // Mock the last item is focused.
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 4 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledThrice( keyEvtData.preventDefault );
- sinon.assert.calledThrice( keyEvtData.stopPropagation );
- sinon.assert.calledOnce( view.items.get( 2 ).focus );
- } );
- it( 'so "arrowright" focuses next focusable item', () => {
- const keyEvtData = getArrowKeyData( 'arrowright' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- // Mock the last item is focused.
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.items.get( 2 ).focus );
- } );
- describe( 'when #shouldGroupWhenFull is true', () => {
- beforeEach( () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- view.shouldGroupWhenFull = true;
- } );
- afterEach( () => {
- view.element.remove();
- } );
- it( 'navigates from #items to the #groupedItemsDropdown (forwards)', () => {
- const keyEvtData = getArrowKeyData( 'arrowright' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.updateGroupedItems();
- sinon.spy( view.groupedItemsDropdown, 'focus' );
- view.focusTracker.isFocused = true;
- view.focusTracker.focusedElement = view.itemsView.element;
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
- } );
- it( 'navigates from the #groupedItemsDropdown to #items (forwards)', () => {
- const keyEvtData = getArrowKeyData( 'arrowright' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.updateGroupedItems();
- view.focusTracker.isFocused = true;
- view.focusTracker.focusedElement = view.groupedItemsDropdown.element;
- view.itemsView.focusTracker.isFocused = false;
- view.itemsView.focusTracker.focusedElement = null;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.items.get( 0 ).focus );
- } );
- it( 'navigates from #items to the #groupedItemsDropdown (backwards)', () => {
- const keyEvtData = getArrowKeyData( 'arrowleft' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.updateGroupedItems();
- sinon.spy( view.groupedItemsDropdown, 'focus' );
- view.focusTracker.isFocused = true;
- view.focusTracker.focusedElement = view.itemsView.element;
- view.itemsView.focusTracker.isFocused = true;
- view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
- } );
- it( 'navigates from the #groupedItemsDropdown to #items (backwards)', () => {
- const keyEvtData = getArrowKeyData( 'arrowleft' );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.updateGroupedItems();
- view.focusTracker.isFocused = true;
- view.focusTracker.focusedElement = view.groupedItemsDropdown.element;
- view.itemsView.focusTracker.isFocused = false;
- view.itemsView.focusTracker.focusedElement = null;
- view.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( view.items.get( 0 ).focus );
- } );
- } );
- } );
- } );
- describe( 'destroy()', () => {
- it( 'destroys the #groupedItemsDropdown', () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- const itemA = focusable();
- const itemB = focusable();
- const itemC = focusable();
- const itemD = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.items.add( itemC );
- view.items.add( itemD );
- // The dropdown shows up.
- view.shouldGroupWhenFull = true;
- sinon.spy( view.groupedItemsDropdown, 'destroy' );
- view.element.style.width = '500px';
- // The dropdown hides; it does not belong to any collection but it still exist.
- view.updateGroupedItems();
- view.destroy();
- sinon.assert.calledOnce( view.groupedItemsDropdown.destroy );
- view.element.remove();
- } );
- it( 'disconnects the #_groupWhenFullResizeObserver', () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- const itemA = focusable();
- const itemB = focusable();
- const itemC = focusable();
- const itemD = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.items.add( itemC );
- view.items.add( itemD );
- view.shouldGroupWhenFull = true;
- sinon.spy( view._groupWhenFullResizeObserver, 'disconnect' );
- view.destroy();
- sinon.assert.calledOnce( view._groupWhenFullResizeObserver.disconnect );
- view.element.remove();
- } );
- } );
- describe( 'focus()', () => {
- it( 'focuses the first focusable of #items in DOM', () => {
- // No children to focus.
- view.focus();
- // The second child is focusable.
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.focus();
- sinon.assert.calledOnce( view.items.get( 1 ).focus );
- } );
- it( 'if no items the first focusable of #items in DOM', () => {
- document.body.appendChild( view.element );
- view.element.style.width = '10px';
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- sinon.spy( view.groupedItemsDropdown, 'focus' );
- view.focus();
- sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
- } );
- } );
- describe( 'focusLast()', () => {
- it( 'focuses the last focusable of #items in DOM', () => {
- // No children to focus.
- view.focusLast();
- // The second child is focusable.
- view.items.add( nonFocusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( nonFocusable() );
- view.focusLast();
- sinon.assert.calledOnce( view.items.get( 3 ).focus );
- } );
- it( 'focuses the #groupedItemsDropdown when view#shouldGroupWhenFull is true', () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- view.shouldGroupWhenFull = true;
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- sinon.spy( view.groupedItemsDropdown, 'focus' );
- view.focusLast();
- sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
- view.element.remove();
- } );
- } );
- describe( 'fillFromConfig()', () => {
- let factory;
- beforeEach( () => {
- factory = new ComponentFactory( {} );
- factory.add( 'foo', namedFactory( 'foo' ) );
- factory.add( 'bar', namedFactory( 'bar' ) );
- } );
- it( 'expands the config into collection', () => {
- view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
- 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' );
- } );
- it( 'warns if there is no such component in the factory', () => {
- const items = view.items;
- const consoleWarnStub = sinon.stub( console, 'warn' );
- view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
- expect( items ).to.have.length( 2 );
- expect( items.get( 0 ).name ).to.equal( 'foo' );
- expect( items.get( 1 ).name ).to.equal( 'bar' );
- sinon.assert.calledOnce( consoleWarnStub );
- sinon.assert.calledWithExactly( consoleWarnStub,
- sinon.match( /^toolbarview-item-unavailable/ ),
- { name: 'baz' }
- );
- } );
- } );
- describe( 'updateGroupedItems()', () => {
- beforeEach( () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- } );
- afterEach( () => {
- view.element.remove();
- } );
- it( 'only works when #shouldGroupWhenFull', () => {
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.updateGroupedItems();
- expect( view.items ).to.have.length( 4 );
- expect( view.groupedItems ).to.be.null;
- } );
- it( 'stays silent if the toolbar is detached from visible DOM', () => {
- testUtils.sinon.spy( console, 'warn' );
- view.element.remove();
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- sinon.assert.notCalled( console.warn );
- } );
- it( 'does not group when items fit', () => {
- const itemA = focusable();
- const itemB = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.shouldGroupWhenFull = true;
- expect( view.groupedItems ).to.be.null;
- expect( view.groupedItemsDropdown ).to.be.null;
- } );
- it( 'groups items that overflow into #groupedItemsDropdown', () => {
- const itemA = focusable();
- const itemB = focusable();
- const itemC = focusable();
- const itemD = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.items.add( itemC );
- view.items.add( itemD );
- view.shouldGroupWhenFull = true;
- expect( view.items.map( i => i ) ).to.have.members( [ itemA ] );
- expect( view.groupedItems.map( i => i ) ).to.have.members( [ itemB, itemC, itemD ] );
- expect( view._components ).to.have.length( 3 );
- expect( view._components.get( 0 ) ).to.equal( view.itemsView );
- expect( view._components.get( 1 ) ).to.be.instanceOf( ToolbarSeparatorView );
- expect( view._components.get( 2 ) ).to.equal( view.groupedItemsDropdown );
- } );
- it( 'ungroups items from #groupedItemsDropdown if there is enough space to display them (all)', () => {
- const itemA = focusable();
- const itemB = focusable();
- const itemC = focusable();
- const itemD = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.items.add( itemC );
- view.items.add( itemD );
- view.shouldGroupWhenFull = true;
- expect( view.items.map( i => i ) ).to.have.members( [ itemA ] );
- expect( view.groupedItems.map( i => i ) ).to.have.members( [ itemB, itemC, itemD ] );
- view.element.style.width = '350px';
- // Some grouped items cannot be ungrouped because there is not enough space and they will
- // land back in #groupedItems after an attempt was made.
- view.updateGroupedItems();
- expect( view.items.map( i => i ) ).to.have.members( [ itemA, itemB, itemC ] );
- expect( view.groupedItems.map( i => i ) ).to.have.members( [ itemD ] );
- } );
- it( 'ungroups items from #groupedItemsDropdown if there is enough space to display them (some)', () => {
- const itemA = focusable();
- const itemB = focusable();
- const itemC = focusable();
- view.items.add( itemA );
- view.items.add( itemB );
- view.items.add( itemC );
- view.shouldGroupWhenFull = true;
- expect( view.items.map( i => i ) ).to.have.members( [ itemA ] );
- expect( view.groupedItems.map( i => i ) ).to.have.members( [ itemB, itemC ] );
- view.element.style.width = '350px';
- // All grouped items will be ungrouped because they fit just alright in the main space.
- view.updateGroupedItems();
- expect( view.items.map( i => i ) ).to.have.members( [ itemA, itemB, itemC ] );
- expect( view.groupedItems ).to.have.length( 0 );
- expect( view._components ).to.have.length( 1 );
- expect( view._components.get( 0 ) ).to.equal( view.itemsView );
- } );
- describe( '#groupedItemsDropdown', () => {
- it( 'has proper DOM structure', () => {
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- const dropdown = view.groupedItemsDropdown;
- expect( view._components.has( view.groupedItemsDropdown ) ).to.be.true;
- expect( dropdown.element.classList.contains( 'ck-toolbar__grouped-dropdown' ) );
- expect( dropdown.buttonView.label ).to.equal( 'Show more items' );
- } );
- it( 'shares its toolbarView#items with ToolbarView#groupedItems', () => {
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- expect( view.groupedItemsDropdown.toolbarView.items ).to.equal( view.groupedItems );
- } );
- } );
- describe( '#items overflow checking logic', () => {
- it( 'considers the right padding of the toolbar (LTR UI)', () => {
- view.class = 'ck-reset_all';
- view.element.style.width = '210px';
- view.element.style.paddingLeft = '0px';
- view.element.style.paddingRight = '20px';
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- expect( view.groupedItems ).to.have.length( 1 );
- } );
- it( 'considers the left padding of the toolbar (RTL UI)', () => {
- const locale = new Locale( { uiLanguage: 'ar' } );
- const view = new ToolbarView( locale );
- view.extendTemplate( {
- attributes: {
- dir: locale.uiLanguageDirection
- }
- } );
- view.render();
- document.body.appendChild( view.element );
- view.class = 'ck-reset_all';
- view.element.style.width = '210px';
- view.element.style.paddingLeft = '20px';
- view.element.style.paddingRight = '0px';
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- expect( view.groupedItems ).to.have.length( 1 );
- view.destroy();
- view.element.remove();
- } );
- } );
- } );
- describe( 'automatic toolbar grouping (#shouldGroupWhenFull = true)', () => {
- beforeEach( () => {
- document.body.appendChild( view.element );
- view.element.style.width = '200px';
- } );
- afterEach( () => {
- view.element.remove();
- } );
- it( 'updates the UI as new #items are added', () => {
- sinon.spy( view, 'updateGroupedItems' );
- sinon.assert.notCalled( view.updateGroupedItems );
- view.items.add( focusable() );
- view.items.add( focusable() );
- sinon.assert.calledTwice( view.updateGroupedItems );
- } );
- it( 'updates the UI as #items are removed', () => {
- sinon.spy( view, 'updateGroupedItems' );
- sinon.assert.notCalled( view.updateGroupedItems );
- view.items.add( focusable() );
- sinon.assert.calledOnce( view.updateGroupedItems );
- view.items.remove( 0 );
- sinon.assert.calledTwice( view.updateGroupedItems );
- } );
- it( 'updates the UI when the toolbar is being resized (expanding)', done => {
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.element.style.width = '200px';
- view.shouldGroupWhenFull = true;
- expect( view.items ).to.have.length( 1 );
- expect( view.groupedItems ).to.have.length( 4 );
- view.element.style.width = '500px';
- setTimeout( () => {
- expect( view.items ).to.have.length( 5 );
- expect( view.groupedItems ).to.have.length( 0 );
- done();
- }, 100 );
- } );
- it( 'updates the UI when the toolbar is being resized (narrowing)', done => {
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.element.style.width = '500px';
- view.shouldGroupWhenFull = true;
- expect( view.items ).to.have.length( 5 );
- expect( view.groupedItems ).to.be.null;
- view.element.style.width = '200px';
- setTimeout( () => {
- expect( view.items ).to.have.length( 1 );
- expect( view.groupedItems ).to.have.length( 4 );
- done();
- }, 100 );
- } );
- it( 'does not react to changes in height', done => {
- view.element.style.width = '500px';
- view.element.style.height = '200px';
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.items.add( focusable() );
- view.shouldGroupWhenFull = true;
- sinon.spy( view, 'updateGroupedItems' );
- expect( view.items ).to.have.length( 5 );
- expect( view.groupedItems ).to.be.null;
- setTimeout( () => {
- view.element.style.height = '500px';
- setTimeout( () => {
- sinon.assert.calledOnce( view.updateGroupedItems );
- done();
- }, 100 );
- }, 100 );
- } );
- } );
- } );
- function focusable() {
- const view = nonFocusable();
- view.label = 'focusable';
- view.focus = sinon.stub().callsFake( () => {
- view.element.focus();
- } );
- view.extendTemplate( {
- attributes: {
- tabindex: -1
- }
- } );
- return view;
- }
- function nonFocusable() {
- const view = new View();
- view.set( 'label', 'non-focusable' );
- const bind = view.bindTemplate;
- view.setTemplate( {
- tag: 'div',
- attributes: {
- style: {
- padding: '0',
- margin: '0',
- width: '100px',
- height: '100px',
- background: 'rgba(255,0,0,.3)'
- }
- },
- children: [
- {
- text: bind.to( 'label' )
- }
- ]
- } );
- return view;
- }
- function namedFactory( name ) {
- return locale => {
- const view = new View( locale );
- view.name = name;
- view.element = document.createElement( 'a' );
- return view;
- };
- }
- function getArrowKeyData( arrow ) {
- return {
- keyCode: keyCodes[ arrow ],
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- }
|