8
0

utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document Event */
  6. import utilsTestUtils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  7. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  8. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  9. import Model from '../../src/model';
  10. import ButtonView from '../../src/button/buttonview';
  11. import DropdownView from '../../src/dropdown/dropdownview';
  12. import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
  13. import SplitButtonView from '../../src/dropdown/button/splitbuttonview';
  14. import View from '../../src/view';
  15. import ToolbarView from '../../src/toolbar/toolbarview';
  16. import { createDropdown, addToolbarToDropdown, addListToDropdown } from '../../src/dropdown/utils';
  17. import ListItemView from '../../src/list/listitemview';
  18. import ListSeparatorView from '../../src/list/listseparatorview';
  19. import ListView from '../../src/list/listview';
  20. const assertBinding = utilsTestUtils.assertBinding;
  21. describe( 'utils', () => {
  22. let locale, dropdownView;
  23. beforeEach( () => {
  24. locale = { t() {} };
  25. } );
  26. describe( 'createDropdown()', () => {
  27. beforeEach( () => {
  28. dropdownView = createDropdown( locale );
  29. } );
  30. it( 'accepts locale', () => {
  31. expect( dropdownView.locale ).to.equal( locale );
  32. expect( dropdownView.panelView.locale ).to.equal( locale );
  33. } );
  34. it( 'returns view', () => {
  35. expect( dropdownView ).to.be.instanceOf( DropdownView );
  36. } );
  37. it( 'creates dropdown#panelView out of DropdownPanelView', () => {
  38. expect( dropdownView.panelView ).to.be.instanceOf( DropdownPanelView );
  39. } );
  40. it( 'creates dropdown#buttonView out of ButtonView', () => {
  41. expect( dropdownView.buttonView ).to.be.instanceOf( ButtonView );
  42. } );
  43. it( 'creates dropdown#buttonView out of passed SplitButtonView', () => {
  44. dropdownView = createDropdown( locale, SplitButtonView );
  45. expect( dropdownView.buttonView ).to.be.instanceOf( SplitButtonView );
  46. } );
  47. it( 'binds #isEnabled to the buttonView', () => {
  48. dropdownView = createDropdown( locale );
  49. assertBinding( dropdownView.buttonView,
  50. { isEnabled: true },
  51. [
  52. [ dropdownView, { isEnabled: false } ]
  53. ],
  54. { isEnabled: false }
  55. );
  56. } );
  57. it( 'binds button#isOn to dropdown #isOpen', () => {
  58. dropdownView = createDropdown( locale );
  59. dropdownView.buttonView.isEnabled = true;
  60. dropdownView.isOpen = false;
  61. expect( dropdownView.buttonView.isOn ).to.be.false;
  62. dropdownView.isOpen = true;
  63. expect( dropdownView.buttonView.isOn ).to.be.true;
  64. } );
  65. describe( '#buttonView', () => {
  66. it( 'accepts locale', () => {
  67. expect( dropdownView.buttonView.locale ).to.equal( locale );
  68. } );
  69. it( 'is a ButtonView instance', () => {
  70. expect( dropdownView.buttonView ).to.be.instanceof( ButtonView );
  71. } );
  72. } );
  73. describe( 'has default behavior', () => {
  74. describe( 'closeDropdownOnBlur()', () => {
  75. beforeEach( () => {
  76. dropdownView.render();
  77. document.body.appendChild( dropdownView.element );
  78. } );
  79. afterEach( () => {
  80. dropdownView.element.remove();
  81. } );
  82. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  83. // Open the dropdown.
  84. dropdownView.isOpen = true;
  85. // Fire event from outside of the dropdown.
  86. document.body.dispatchEvent( new Event( 'mousedown', {
  87. bubbles: true
  88. } ) );
  89. // Closed the dropdown.
  90. expect( dropdownView.isOpen ).to.be.false;
  91. // Fire event from outside of the dropdown.
  92. document.body.dispatchEvent( new Event( 'mousedown', {
  93. bubbles: true
  94. } ) );
  95. // Dropdown is still closed.
  96. expect( dropdownView.isOpen ).to.be.false;
  97. } );
  98. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  99. // Open the dropdown.
  100. dropdownView.isOpen = true;
  101. // Event from view.element should be discarded.
  102. dropdownView.element.dispatchEvent( new Event( 'mousedown', {
  103. bubbles: true
  104. } ) );
  105. // Dropdown is still open.
  106. expect( dropdownView.isOpen ).to.be.true;
  107. // Event from within view.element should be discarded.
  108. const child = document.createElement( 'div' );
  109. dropdownView.element.appendChild( child );
  110. child.dispatchEvent( new Event( 'mousedown', {
  111. bubbles: true
  112. } ) );
  113. // Dropdown is still open.
  114. expect( dropdownView.isOpen ).to.be.true;
  115. } );
  116. } );
  117. describe( 'closeDropdownOnExecute()', () => {
  118. beforeEach( () => {
  119. dropdownView.render();
  120. document.body.appendChild( dropdownView.element );
  121. } );
  122. afterEach( () => {
  123. dropdownView.element.remove();
  124. } );
  125. it( 'changes view#isOpen on view#execute', () => {
  126. dropdownView.isOpen = true;
  127. dropdownView.fire( 'execute' );
  128. expect( dropdownView.isOpen ).to.be.false;
  129. dropdownView.fire( 'execute' );
  130. expect( dropdownView.isOpen ).to.be.false;
  131. } );
  132. } );
  133. describe( 'focusDropdownContentsOnArrows()', () => {
  134. let panelChildView;
  135. beforeEach( () => {
  136. panelChildView = new View();
  137. panelChildView.setTemplate( { tag: 'div' } );
  138. panelChildView.focus = () => {};
  139. panelChildView.focusLast = () => {};
  140. dropdownView.panelView.children.add( panelChildView );
  141. dropdownView.render();
  142. document.body.appendChild( dropdownView.element );
  143. } );
  144. afterEach( () => {
  145. dropdownView.element.remove();
  146. } );
  147. it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
  148. const keyEvtData = {
  149. keyCode: keyCodes.arrowdown,
  150. preventDefault: sinon.spy(),
  151. stopPropagation: sinon.spy()
  152. };
  153. const spy = sinon.spy( panelChildView, 'focus' );
  154. dropdownView.isOpen = false;
  155. dropdownView.keystrokes.press( keyEvtData );
  156. sinon.assert.notCalled( spy );
  157. dropdownView.isOpen = true;
  158. dropdownView.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( spy );
  160. } );
  161. it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
  162. const keyEvtData = {
  163. keyCode: keyCodes.arrowup,
  164. preventDefault: sinon.spy(),
  165. stopPropagation: sinon.spy()
  166. };
  167. const spy = sinon.spy( panelChildView, 'focusLast' );
  168. dropdownView.isOpen = false;
  169. dropdownView.keystrokes.press( keyEvtData );
  170. sinon.assert.notCalled( spy );
  171. dropdownView.isOpen = true;
  172. dropdownView.keystrokes.press( keyEvtData );
  173. sinon.assert.calledOnce( spy );
  174. } );
  175. } );
  176. } );
  177. } );
  178. describe( 'addToolbarToDropdown()', () => {
  179. let buttons;
  180. beforeEach( () => {
  181. buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
  182. const button = new ButtonView();
  183. button.icon = icon;
  184. return button;
  185. } );
  186. dropdownView = createDropdown( locale );
  187. addToolbarToDropdown( dropdownView, buttons );
  188. dropdownView.render();
  189. document.body.appendChild( dropdownView.element );
  190. } );
  191. afterEach( () => {
  192. dropdownView.element.remove();
  193. } );
  194. it( 'sets view#locale', () => {
  195. expect( dropdownView.locale ).to.equal( locale );
  196. } );
  197. it( 'sets view class', () => {
  198. expect( dropdownView.element.classList.contains( 'ck-toolbar-dropdown' ) ).to.be.true;
  199. } );
  200. describe( 'view#toolbarView', () => {
  201. it( 'is created', () => {
  202. const panelChildren = dropdownView.panelView.children;
  203. expect( panelChildren ).to.have.length( 1 );
  204. expect( panelChildren.first ).to.equal( dropdownView.toolbarView );
  205. expect( dropdownView.toolbarView ).to.be.instanceof( ToolbarView );
  206. } );
  207. it( 'delegates view.toolbarView.items#execute to the view', done => {
  208. dropdownView.on( 'execute', evt => {
  209. expect( evt.source ).to.equal( dropdownView.toolbarView.items.first );
  210. expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.first, dropdownView ] );
  211. done();
  212. } );
  213. dropdownView.toolbarView.items.first.fire( 'execute' );
  214. } );
  215. } );
  216. } );
  217. describe( 'addListToDropdown()', () => {
  218. let definitions, listItems;
  219. beforeEach( () => {
  220. definitions = new Collection();
  221. dropdownView = createDropdown( locale );
  222. dropdownView.buttonView.set( {
  223. isEnabled: true,
  224. isOn: false,
  225. label: 'foo'
  226. } );
  227. addListToDropdown( dropdownView, definitions );
  228. listItems = dropdownView.listView.items;
  229. dropdownView.render();
  230. document.body.appendChild( dropdownView.element );
  231. } );
  232. afterEach( () => {
  233. dropdownView.element.remove();
  234. } );
  235. describe( 'view#listView', () => {
  236. it( 'is created', () => {
  237. const panelChildren = dropdownView.panelView.children;
  238. expect( panelChildren ).to.have.length( 1 );
  239. expect( panelChildren.first ).to.equal( dropdownView.listView );
  240. expect( dropdownView.listView ).to.be.instanceof( ListView );
  241. } );
  242. it( 'ignores unknown definition types', () => {
  243. definitions.add( { type: 'foo' } );
  244. expect( listItems.length ).to.equal( 0 );
  245. } );
  246. describe( 'with ButtonView', () => {
  247. it( 'is populated using item definitions', () => {
  248. definitions.add( {
  249. type: 'button',
  250. model: new Model( { label: 'a', style: 'b' } )
  251. } );
  252. definitions.add( {
  253. type: 'button',
  254. model: new Model( { label: 'c', style: 'd' } )
  255. } );
  256. expect( listItems ).to.have.length( 2 );
  257. expect( listItems.first ).to.be.instanceOf( ListItemView );
  258. expect( listItems.first.children.first ).to.be.instanceOf( ButtonView );
  259. expect( listItems.get( 1 ).children.first.label ).to.equal( 'c' );
  260. expect( listItems.get( 1 ).children.first.style ).to.equal( 'd' );
  261. definitions.remove( 1 );
  262. expect( listItems ).to.have.length( 1 );
  263. expect( listItems.first.children.first.label ).to.equal( 'a' );
  264. expect( listItems.first.children.first.style ).to.equal( 'b' );
  265. } );
  266. it( 'binds all button properties', () => {
  267. const def = {
  268. type: 'button',
  269. model: new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } )
  270. };
  271. definitions.add( def );
  272. const button = listItems.first.children.first;
  273. expect( button.foo ).to.equal( 'bar' );
  274. expect( button.baz ).to.equal( 'qux' );
  275. def.model.baz = 'foo?';
  276. expect( button.baz ).to.equal( 'foo?' );
  277. } );
  278. it( 'delegates ButtonView#execute to the ListItemView', done => {
  279. definitions.add( {
  280. type: 'button',
  281. model: new Model( { label: 'a', style: 'b' } )
  282. } );
  283. const listItem = listItems.first;
  284. const button = listItem.children.first;
  285. dropdownView.on( 'execute', evt => {
  286. expect( evt.source ).to.equal( button );
  287. expect( evt.path ).to.deep.equal( [ button, listItem, dropdownView ] );
  288. done();
  289. } );
  290. button.fire( 'execute' );
  291. } );
  292. } );
  293. describe( 'with ListSeparatorView', () => {
  294. it( 'creates a separator from the definition', () => {
  295. definitions.add( { type: 'separator' } );
  296. expect( listItems.first ).to.be.instanceOf( ListSeparatorView );
  297. } );
  298. } );
  299. } );
  300. } );
  301. } );