| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* globals document, Event */
- import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
- import Collection from '@ckeditor/ckeditor5-utils/src/collection';
- import utilsTestUtils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- import Model from '../../src/model';
- import View from '../../src/view';
- import ButtonView from '../../src/button/buttonview';
- import ToolbarView from '../../src/toolbar/toolbarview';
- import ListItemView from '../../src/list/listitemview';
- import ListView from '../../src/list/listview';
- import DropdownView from '../../src/dropdown/dropdownview';
- import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
- import SplitButtonView from '../../src/button/splitbuttonview';
- import {
- addListViewToDropdown,
- addToolbarToDropdown,
- closeDropdownOnBlur,
- closeDropdownOnExecute,
- createButtonForDropdown,
- createDropdownView,
- createSingleButtonDropdown,
- createSplitButtonForDropdown,
- enableModelIfOneIsEnabled,
- focusDropdownContentsOnArrows
- } from '../../src/dropdown/utils';
- const assertBinding = utilsTestUtils.assertBinding;
- describe( 'utils', () => {
- let dropdownView, buttonView, model, locale;
- beforeEach( () => {
- locale = { t() {} };
- model = new Model();
- buttonView = createButtonForDropdown( model, locale );
- dropdownView = createDropdownView( model, buttonView, locale );
- } );
- afterEach( () => {
- dropdownView.element.remove();
- } );
- describe( 'focusDropdownContentsOnArrows()', () => {
- let panelChildView;
- beforeEach( () => {
- panelChildView = new View();
- panelChildView.setTemplate( { tag: 'div' } );
- panelChildView.focus = () => {};
- panelChildView.focusLast = () => {};
- // TODO: describe this as #contentView instaed of #listView and #toolbarView
- dropdownView.panelView.children.add( panelChildView );
- focusDropdownContentsOnArrows( dropdownView );
- dropdownView.render();
- document.body.appendChild( dropdownView.element );
- } );
- it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
- const keyEvtData = {
- keyCode: keyCodes.arrowdown,
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- const spy = sinon.spy( panelChildView, 'focus' );
- dropdownView.isOpen = false;
- dropdownView.keystrokes.press( keyEvtData );
- sinon.assert.notCalled( spy );
- dropdownView.isOpen = true;
- dropdownView.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( spy );
- } );
- it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
- const keyEvtData = {
- keyCode: keyCodes.arrowup,
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- const spy = sinon.spy( panelChildView, 'focusLast' );
- dropdownView.isOpen = false;
- dropdownView.keystrokes.press( keyEvtData );
- sinon.assert.notCalled( spy );
- dropdownView.isOpen = true;
- dropdownView.keystrokes.press( keyEvtData );
- sinon.assert.calledOnce( spy );
- } );
- } );
- describe( 'closeDropdownOnExecute()', () => {
- beforeEach( () => {
- closeDropdownOnExecute( dropdownView );
- dropdownView.render();
- document.body.appendChild( dropdownView.element );
- } );
- it( 'changes view#isOpen on view#execute', () => {
- dropdownView.isOpen = true;
- dropdownView.fire( 'execute' );
- expect( dropdownView.isOpen ).to.be.false;
- dropdownView.fire( 'execute' );
- expect( dropdownView.isOpen ).to.be.false;
- } );
- } );
- describe( 'closeDropdownOnBlur()', () => {
- beforeEach( () => {
- closeDropdownOnBlur( dropdownView );
- dropdownView.render();
- document.body.appendChild( dropdownView.element );
- } );
- it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
- // Open the dropdown.
- dropdownView.isOpen = true;
- // Fire event from outside of the dropdown.
- document.body.dispatchEvent( new Event( 'mousedown', {
- bubbles: true
- } ) );
- // Closed the dropdown.
- expect( dropdownView.isOpen ).to.be.false;
- // Fire event from outside of the dropdown.
- document.body.dispatchEvent( new Event( 'mousedown', {
- bubbles: true
- } ) );
- // Dropdown is still closed.
- expect( dropdownView.isOpen ).to.be.false;
- } );
- it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
- // Open the dropdown.
- dropdownView.isOpen = true;
- // Event from view.element should be discarded.
- dropdownView.element.dispatchEvent( new Event( 'mousedown', {
- bubbles: true
- } ) );
- // Dropdown is still open.
- expect( dropdownView.isOpen ).to.be.true;
- // Event from within view.element should be discarded.
- const child = document.createElement( 'div' );
- dropdownView.element.appendChild( child );
- child.dispatchEvent( new Event( 'mousedown', {
- bubbles: true
- } ) );
- // Dropdown is still open.
- expect( dropdownView.isOpen ).to.be.true;
- } );
- } );
- describe( 'createButtonForDropdown()', () => {
- beforeEach( () => {
- buttonView = createButtonForDropdown( new Model(), locale );
- } );
- it( 'accepts locale', () => {
- expect( buttonView.locale ).to.equal( locale );
- } );
- it( 'returns ButtonView instance', () => {
- expect( buttonView ).to.be.instanceof( ButtonView );
- } );
- it( 'delegates "execute" to "select" event', () => {
- const spy = sinon.spy();
- buttonView.on( 'select', spy );
- buttonView.fire( 'exec' );
- sinon.assert.calledOnce( spy );
- } );
- } );
- describe( 'createSplitButtonForDropdown()', () => {
- beforeEach( () => {
- buttonView = createSplitButtonForDropdown( new Model(), locale );
- } );
- it( 'accepts locale', () => {
- expect( buttonView.locale ).to.equal( locale );
- } );
- it( 'returns SplitButtonView instance', () => {
- expect( buttonView ).to.be.instanceof( SplitButtonView );
- } );
- it( 'binds actionView "execute" to "select" event', () => {
- const spy = sinon.spy();
- buttonView.on( 'select', spy );
- buttonView.fire( 'exec' );
- sinon.assert.calledOnce( spy );
- } );
- } );
- describe( 'createDropdownView()', () => {
- it( 'returns view', () => {
- model = new Model();
- buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- expect( dropdownView ).to.be.instanceOf( DropdownView );
- } );
- it( 'creates dropdown#panelView out of DropdownPanelView', () => {
- model = new Model();
- buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- expect( dropdownView.panelView ).to.be.instanceOf( DropdownPanelView );
- } );
- it( 'creates dropdown#buttonView out of buttonView', () => {
- model = new Model();
- buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- expect( dropdownView.buttonView ).to.equal( buttonView );
- } );
- it( 'accepts locale', () => {
- const buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- expect( dropdownView.locale ).to.equal( locale );
- expect( dropdownView.panelView.locale ).to.equal( locale );
- } );
- it( 'binds button attributes to the model', () => {
- const modelDef = {
- label: 'foo',
- isOn: false,
- isEnabled: true,
- withText: false,
- tooltip: false
- };
- model = new Model( modelDef );
- buttonView = new ButtonView();
- createDropdownView( model, buttonView, locale );
- assertBinding( buttonView,
- modelDef,
- [
- [ model, { label: 'bar', isEnabled: false, isOn: true, withText: true, tooltip: true } ]
- ],
- { label: 'bar', isEnabled: false, isOn: true, withText: true, tooltip: true }
- );
- } );
- it( 'binds button#isOn do dropdown #isOpen and model #isOn', () => {
- const modelDef = {
- label: 'foo',
- isOn: false,
- isEnabled: true,
- withText: false,
- tooltip: false
- };
- model = new Model( modelDef );
- buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- dropdownView.isOpen = false;
- expect( buttonView.isOn ).to.be.false;
- model.isOn = true;
- expect( buttonView.isOn ).to.be.true;
- dropdownView.isOpen = true;
- expect( buttonView.isOn ).to.be.true;
- model.isOn = false;
- expect( buttonView.isOn ).to.be.true;
- } );
- it( 'binds dropdown#isEnabled to the model', () => {
- const modelDef = {
- label: 'foo',
- isEnabled: true,
- withText: false,
- tooltip: false
- };
- model = new Model( modelDef );
- buttonView = new ButtonView();
- dropdownView = createDropdownView( model, buttonView, locale );
- assertBinding( dropdownView,
- { isEnabled: true },
- [
- [ model, { isEnabled: false } ]
- ],
- { isEnabled: false }
- );
- } );
- } );
- describe( 'createSplitButtonDropdown()', () => { } );
- describe( 'createSingleButtonDropdown()', () => {} );
- describe( 'enableModelIfOneIsEnabled()', () => {
- it( 'Bind to #isEnabled of each observable and set it true if any observable #isEnabled is true', () => {
- const observables = [
- new Model( { isEnabled: false } ),
- new Model( { isEnabled: false } ),
- new Model( { isEnabled: false } )
- ];
- enableModelIfOneIsEnabled( model, observables );
- expect( model.isEnabled ).to.be.false;
- observables[ 0 ].isEnabled = true;
- expect( model.isEnabled ).to.be.true;
- observables[ 0 ].isEnabled = false;
- expect( model.isEnabled ).to.be.false;
- observables[ 1 ].isEnabled = true;
- expect( model.isEnabled ).to.be.true;
- } );
- } );
- describe( 'addListViewToDropdown()', () => {
- let items;
- beforeEach( () => {
- items = new Collection();
- model = new Model( {
- isEnabled: true,
- items,
- isOn: false,
- label: 'foo'
- } );
- dropdownView = createSingleButtonDropdown( model, locale );
- addListViewToDropdown( dropdownView, model, locale );
- dropdownView.render();
- document.body.appendChild( dropdownView.element );
- } );
- it( 'sets view#locale', () => {
- expect( dropdownView.locale ).to.equal( locale );
- } );
- describe( 'view#listView', () => {
- it( 'is created', () => {
- const panelChildren = dropdownView.panelView.children;
- expect( panelChildren ).to.have.length( 1 );
- expect( panelChildren.get( 0 ) ).to.equal( dropdownView.listView );
- expect( dropdownView.listView ).to.be.instanceof( ListView );
- } );
- it( 'is bound to model#items', () => {
- items.add( new Model( { label: 'a', style: 'b' } ) );
- items.add( new Model( { label: 'c', style: 'd' } ) );
- expect( dropdownView.listView.items ).to.have.length( 2 );
- expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
- expect( dropdownView.listView.items.get( 1 ).label ).to.equal( 'c' );
- expect( dropdownView.listView.items.get( 1 ).style ).to.equal( 'd' );
- items.remove( 1 );
- expect( dropdownView.listView.items ).to.have.length( 1 );
- expect( dropdownView.listView.items.get( 0 ).label ).to.equal( 'a' );
- expect( dropdownView.listView.items.get( 0 ).style ).to.equal( 'b' );
- } );
- it( 'binds all attributes in model#items', () => {
- const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
- items.add( itemModel );
- const item = dropdownView.listView.items.get( 0 );
- expect( item.foo ).to.equal( 'bar' );
- expect( item.baz ).to.equal( 'qux' );
- itemModel.baz = 'foo?';
- expect( item.baz ).to.equal( 'foo?' );
- } );
- it( 'delegates view.listView#execute to the view', done => {
- items.add( new Model( { label: 'a', style: 'b' } ) );
- dropdownView.on( 'execute', evt => {
- expect( evt.source ).to.equal( dropdownView.listView.items.get( 0 ) );
- expect( evt.path ).to.deep.equal( [ dropdownView.listView.items.get( 0 ), dropdownView ] );
- done();
- } );
- dropdownView.listView.items.get( 0 ).fire( 'execute' );
- } );
- } );
- } );
- describe( 'addToolbarToDropdown()', () => {
- let buttons;
- beforeEach( () => {
- buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
- const button = new ButtonView();
- button.icon = icon;
- return button;
- } );
- model = new Model( {
- isVertical: true,
- buttons
- } );
- dropdownView = createSingleButtonDropdown( model, locale );
- addToolbarToDropdown( dropdownView, model );
- dropdownView.render();
- document.body.appendChild( dropdownView.element );
- } );
- it( 'sets view#locale', () => {
- expect( dropdownView.locale ).to.equal( locale );
- } );
- it( 'sets view class', () => {
- expect( dropdownView.element.classList.contains( 'ck-toolbar-dropdown' ) ).to.be.true;
- } );
- describe( 'view#toolbarView', () => {
- it( 'is created', () => {
- const panelChildren = dropdownView.panelView.children;
- expect( panelChildren ).to.have.length( 1 );
- expect( panelChildren.get( 0 ) ).to.equal( dropdownView.toolbarView );
- expect( dropdownView.toolbarView ).to.be.instanceof( ToolbarView );
- } );
- it.skip( 'delegates view.toolbarView.items#execute to the view', done => {
- dropdownView.on( 'execute', evt => {
- expect( evt.source ).to.equal( dropdownView.toolbarView.items.get( 0 ) );
- expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.get( 0 ), dropdownView ] );
- done();
- } );
- dropdownView.toolbarView.items.get( 0 ).fire( 'execute' );
- } );
- it( 'reacts on model#isVertical', () => {
- model.isVertical = false;
- expect( dropdownView.toolbarView.isVertical ).to.be.false;
- model.isVertical = true;
- expect( dropdownView.toolbarView.isVertical ).to.be.true;
- } );
- } );
- describe( 'buttons', () => {
- // TODO: test me!
- } );
- } );
- describe( 'getBindingTargets()', () => {} );
- } );
|