utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document Event */
  6. import { assertBinding } 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 SwitchButtonView from '../../src/button/switchbuttonview';
  12. import DropdownView from '../../src/dropdown/dropdownview';
  13. import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
  14. import SplitButtonView from '../../src/dropdown/button/splitbuttonview';
  15. import View from '../../src/view';
  16. import ToolbarView from '../../src/toolbar/toolbarview';
  17. import { createDropdown, addToolbarToDropdown, addListToDropdown } from '../../src/dropdown/utils';
  18. import ListItemView from '../../src/list/listitemview';
  19. import ListSeparatorView from '../../src/list/listseparatorview';
  20. import ListView from '../../src/list/listview';
  21. describe( 'utils', () => {
  22. let locale, dropdownView;
  23. beforeEach( () => {
  24. locale = { t: langString => langString };
  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. it( 'does not change #isOpen if #execute triggered by a SwitchButtonView', () => {
  133. const items = new Collection();
  134. items.add( {
  135. type: 'switchbutton',
  136. model: new Model( {
  137. label: 'foo'
  138. } )
  139. } );
  140. addListToDropdown( dropdownView, items );
  141. dropdownView.isOpen = true;
  142. dropdownView.listView.items.first.children.first.fire( 'execute' );
  143. expect( dropdownView.isOpen ).to.be.true;
  144. } );
  145. } );
  146. describe( 'focusDropdownContentsOnArrows()', () => {
  147. let panelChildView;
  148. beforeEach( () => {
  149. panelChildView = new View();
  150. panelChildView.setTemplate( { tag: 'div' } );
  151. panelChildView.focus = () => {};
  152. panelChildView.focusLast = () => {};
  153. dropdownView.panelView.children.add( panelChildView );
  154. dropdownView.render();
  155. document.body.appendChild( dropdownView.element );
  156. } );
  157. afterEach( () => {
  158. dropdownView.element.remove();
  159. } );
  160. it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
  161. const keyEvtData = {
  162. keyCode: keyCodes.arrowdown,
  163. preventDefault: sinon.spy(),
  164. stopPropagation: sinon.spy()
  165. };
  166. const spy = sinon.spy( panelChildView, 'focus' );
  167. dropdownView.isOpen = false;
  168. dropdownView.keystrokes.press( keyEvtData );
  169. sinon.assert.notCalled( spy );
  170. dropdownView.isOpen = true;
  171. dropdownView.keystrokes.press( keyEvtData );
  172. sinon.assert.calledOnce( spy );
  173. } );
  174. it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
  175. const keyEvtData = {
  176. keyCode: keyCodes.arrowup,
  177. preventDefault: sinon.spy(),
  178. stopPropagation: sinon.spy()
  179. };
  180. const spy = sinon.spy( panelChildView, 'focusLast' );
  181. dropdownView.isOpen = false;
  182. dropdownView.keystrokes.press( keyEvtData );
  183. sinon.assert.notCalled( spy );
  184. dropdownView.isOpen = true;
  185. dropdownView.keystrokes.press( keyEvtData );
  186. sinon.assert.calledOnce( spy );
  187. } );
  188. } );
  189. } );
  190. } );
  191. describe( 'addToolbarToDropdown()', () => {
  192. let buttons;
  193. beforeEach( () => {
  194. buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
  195. const button = new ButtonView();
  196. button.icon = icon;
  197. return button;
  198. } );
  199. dropdownView = createDropdown( locale );
  200. addToolbarToDropdown( dropdownView, buttons );
  201. dropdownView.render();
  202. document.body.appendChild( dropdownView.element );
  203. } );
  204. afterEach( () => {
  205. dropdownView.element.remove();
  206. } );
  207. it( 'sets view#locale', () => {
  208. expect( dropdownView.locale ).to.equal( locale );
  209. } );
  210. it( 'sets view class', () => {
  211. expect( dropdownView.element.classList.contains( 'ck-toolbar-dropdown' ) ).to.be.true;
  212. } );
  213. it( 'sets aria-label', () => {
  214. expect( dropdownView.toolbarView.element.getAttribute( 'aria-label' ) ).to.equal( 'Dropdown toolbar' );
  215. } );
  216. describe( 'view#toolbarView', () => {
  217. it( 'is created', () => {
  218. const panelChildren = dropdownView.panelView.children;
  219. expect( panelChildren ).to.have.length( 1 );
  220. expect( panelChildren.first ).to.equal( dropdownView.toolbarView );
  221. expect( dropdownView.toolbarView ).to.be.instanceof( ToolbarView );
  222. } );
  223. it( 'delegates view.toolbarView.items#execute to the view', done => {
  224. dropdownView.on( 'execute', evt => {
  225. expect( evt.source ).to.equal( dropdownView.toolbarView.items.first );
  226. expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.first, dropdownView ] );
  227. done();
  228. } );
  229. dropdownView.toolbarView.items.first.fire( 'execute' );
  230. } );
  231. } );
  232. } );
  233. describe( 'addListToDropdown()', () => {
  234. let definitions, listItems;
  235. beforeEach( () => {
  236. definitions = new Collection();
  237. dropdownView = createDropdown( locale );
  238. dropdownView.buttonView.set( {
  239. isEnabled: true,
  240. isOn: false,
  241. label: 'foo'
  242. } );
  243. addListToDropdown( dropdownView, definitions );
  244. listItems = dropdownView.listView.items;
  245. dropdownView.render();
  246. document.body.appendChild( dropdownView.element );
  247. } );
  248. afterEach( () => {
  249. dropdownView.element.remove();
  250. } );
  251. describe( 'view#listView', () => {
  252. it( 'is created', () => {
  253. const panelChildren = dropdownView.panelView.children;
  254. expect( panelChildren ).to.have.length( 1 );
  255. expect( panelChildren.first ).to.equal( dropdownView.listView );
  256. expect( dropdownView.listView ).to.be.instanceof( ListView );
  257. } );
  258. it( 'ignores unknown definition types', () => {
  259. definitions.add( { type: 'foo' } );
  260. expect( listItems.length ).to.equal( 0 );
  261. } );
  262. describe( 'with ButtonView', () => {
  263. it( 'is populated using item definitions', () => {
  264. definitions.add( {
  265. type: 'button',
  266. model: new Model( { label: 'a', labelStyle: 'b' } )
  267. } );
  268. definitions.add( {
  269. type: 'button',
  270. model: new Model( { label: 'c', labelStyle: 'd' } )
  271. } );
  272. expect( listItems ).to.have.length( 2 );
  273. expect( listItems.first ).to.be.instanceOf( ListItemView );
  274. expect( listItems.first.children.first ).to.be.instanceOf( ButtonView );
  275. expect( listItems.get( 1 ).children.first.label ).to.equal( 'c' );
  276. expect( listItems.get( 1 ).children.first.labelStyle ).to.equal( 'd' );
  277. definitions.remove( 1 );
  278. expect( listItems ).to.have.length( 1 );
  279. expect( listItems.first.children.first.label ).to.equal( 'a' );
  280. expect( listItems.first.children.first.labelStyle ).to.equal( 'b' );
  281. } );
  282. it( 'binds all button properties', () => {
  283. const def = {
  284. type: 'button',
  285. model: new Model( { label: 'a', labelStyle: 'b', foo: 'bar', baz: 'qux' } )
  286. };
  287. definitions.add( def );
  288. const button = listItems.first.children.first;
  289. expect( button.foo ).to.equal( 'bar' );
  290. expect( button.baz ).to.equal( 'qux' );
  291. def.model.baz = 'foo?';
  292. expect( button.baz ).to.equal( 'foo?' );
  293. } );
  294. it( 'delegates ButtonView#execute to the ListItemView', done => {
  295. definitions.add( {
  296. type: 'button',
  297. model: new Model( { label: 'a', labelStyle: 'b' } )
  298. } );
  299. const listItem = listItems.first;
  300. const button = listItem.children.first;
  301. dropdownView.on( 'execute', evt => {
  302. expect( evt.source ).to.equal( button );
  303. expect( evt.path ).to.deep.equal( [ button, listItem, dropdownView ] );
  304. done();
  305. } );
  306. button.fire( 'execute' );
  307. } );
  308. } );
  309. describe( 'with SwitchButtonView', () => {
  310. it( 'is populated using item definitions', () => {
  311. definitions.add( {
  312. type: 'switchbutton',
  313. model: new Model( { label: 'a', labelStyle: 'b' } )
  314. } );
  315. expect( listItems ).to.have.length( 1 );
  316. expect( listItems.first ).to.be.instanceOf( ListItemView );
  317. expect( listItems.first.children.first ).to.be.instanceOf( SwitchButtonView );
  318. expect( listItems ).to.have.length( 1 );
  319. expect( listItems.first.children.first.label ).to.equal( 'a' );
  320. expect( listItems.first.children.first.labelStyle ).to.equal( 'b' );
  321. } );
  322. it( 'binds all button properties', () => {
  323. const def = {
  324. type: 'switchbutton',
  325. model: new Model( { label: 'a', labelStyle: 'b', foo: 'bar', baz: 'qux' } )
  326. };
  327. definitions.add( def );
  328. const button = listItems.first.children.first;
  329. expect( button.foo ).to.equal( 'bar' );
  330. expect( button.baz ).to.equal( 'qux' );
  331. def.model.baz = 'foo?';
  332. expect( button.baz ).to.equal( 'foo?' );
  333. } );
  334. it( 'delegates SwitchButtonView#execute to the ListItemView', done => {
  335. definitions.add( {
  336. type: 'switchbutton',
  337. model: new Model( { label: 'a', labelStyle: 'b' } )
  338. } );
  339. const listItem = listItems.first;
  340. const button = listItem.children.first;
  341. dropdownView.on( 'execute', evt => {
  342. expect( evt.source ).to.equal( button );
  343. expect( evt.path ).to.deep.equal( [ button, listItem, dropdownView ] );
  344. done();
  345. } );
  346. button.fire( 'execute' );
  347. } );
  348. } );
  349. describe( 'with ListSeparatorView', () => {
  350. it( 'creates a separator from the definition', () => {
  351. definitions.add( { type: 'separator' } );
  352. expect( listItems.first ).to.be.instanceOf( ListSeparatorView );
  353. } );
  354. } );
  355. } );
  356. } );
  357. } );