8
0

utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 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. const assertBinding = utilsTestUtils.assertBinding;
  22. describe( 'utils', () => {
  23. let locale, dropdownView;
  24. beforeEach( () => {
  25. locale = { t() {} };
  26. } );
  27. describe( 'createDropdown()', () => {
  28. beforeEach( () => {
  29. dropdownView = createDropdown( locale );
  30. } );
  31. it( 'accepts locale', () => {
  32. expect( dropdownView.locale ).to.equal( locale );
  33. expect( dropdownView.panelView.locale ).to.equal( locale );
  34. } );
  35. it( 'returns view', () => {
  36. expect( dropdownView ).to.be.instanceOf( DropdownView );
  37. } );
  38. it( 'creates dropdown#panelView out of DropdownPanelView', () => {
  39. expect( dropdownView.panelView ).to.be.instanceOf( DropdownPanelView );
  40. } );
  41. it( 'creates dropdown#buttonView out of ButtonView', () => {
  42. expect( dropdownView.buttonView ).to.be.instanceOf( ButtonView );
  43. } );
  44. it( 'creates dropdown#buttonView out of passed SplitButtonView', () => {
  45. dropdownView = createDropdown( locale, SplitButtonView );
  46. expect( dropdownView.buttonView ).to.be.instanceOf( SplitButtonView );
  47. } );
  48. it( 'binds #isEnabled to the buttonView', () => {
  49. dropdownView = createDropdown( locale );
  50. assertBinding( dropdownView.buttonView,
  51. { isEnabled: true },
  52. [
  53. [ dropdownView, { isEnabled: false } ]
  54. ],
  55. { isEnabled: false }
  56. );
  57. } );
  58. it( 'binds button#isOn to dropdown #isOpen', () => {
  59. dropdownView = createDropdown( locale );
  60. dropdownView.buttonView.isEnabled = true;
  61. dropdownView.isOpen = false;
  62. expect( dropdownView.buttonView.isOn ).to.be.false;
  63. dropdownView.isOpen = true;
  64. expect( dropdownView.buttonView.isOn ).to.be.true;
  65. } );
  66. describe( '#buttonView', () => {
  67. it( 'accepts locale', () => {
  68. expect( dropdownView.buttonView.locale ).to.equal( locale );
  69. } );
  70. it( 'is a ButtonView instance', () => {
  71. expect( dropdownView.buttonView ).to.be.instanceof( ButtonView );
  72. } );
  73. } );
  74. describe( 'has default behavior', () => {
  75. describe( 'closeDropdownOnBlur()', () => {
  76. beforeEach( () => {
  77. dropdownView.render();
  78. document.body.appendChild( dropdownView.element );
  79. } );
  80. afterEach( () => {
  81. dropdownView.element.remove();
  82. } );
  83. it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
  84. // Open the dropdown.
  85. dropdownView.isOpen = true;
  86. // Fire event from outside of the dropdown.
  87. document.body.dispatchEvent( new Event( 'mousedown', {
  88. bubbles: true
  89. } ) );
  90. // Closed the dropdown.
  91. expect( dropdownView.isOpen ).to.be.false;
  92. // Fire event from outside of the dropdown.
  93. document.body.dispatchEvent( new Event( 'mousedown', {
  94. bubbles: true
  95. } ) );
  96. // Dropdown is still closed.
  97. expect( dropdownView.isOpen ).to.be.false;
  98. } );
  99. it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
  100. // Open the dropdown.
  101. dropdownView.isOpen = true;
  102. // Event from view.element should be discarded.
  103. dropdownView.element.dispatchEvent( new Event( 'mousedown', {
  104. bubbles: true
  105. } ) );
  106. // Dropdown is still open.
  107. expect( dropdownView.isOpen ).to.be.true;
  108. // Event from within view.element should be discarded.
  109. const child = document.createElement( 'div' );
  110. dropdownView.element.appendChild( child );
  111. child.dispatchEvent( new Event( 'mousedown', {
  112. bubbles: true
  113. } ) );
  114. // Dropdown is still open.
  115. expect( dropdownView.isOpen ).to.be.true;
  116. } );
  117. } );
  118. describe( 'closeDropdownOnExecute()', () => {
  119. beforeEach( () => {
  120. dropdownView.render();
  121. document.body.appendChild( dropdownView.element );
  122. } );
  123. afterEach( () => {
  124. dropdownView.element.remove();
  125. } );
  126. it( 'changes view#isOpen on view#execute', () => {
  127. dropdownView.isOpen = true;
  128. dropdownView.fire( 'execute' );
  129. expect( dropdownView.isOpen ).to.be.false;
  130. dropdownView.fire( 'execute' );
  131. expect( dropdownView.isOpen ).to.be.false;
  132. } );
  133. it( 'does not change #isOpen if #execute triggered by a SwitchButtonView', () => {
  134. const items = new Collection();
  135. items.add( {
  136. type: 'switchbutton',
  137. model: new Model( {
  138. label: 'foo'
  139. } )
  140. } );
  141. addListToDropdown( dropdownView, items );
  142. dropdownView.isOpen = true;
  143. dropdownView.listView.items.first.children.first.fire( 'execute' );
  144. expect( dropdownView.isOpen ).to.be.true;
  145. } );
  146. } );
  147. describe( 'focusDropdownContentsOnArrows()', () => {
  148. let panelChildView;
  149. beforeEach( () => {
  150. panelChildView = new View();
  151. panelChildView.setTemplate( { tag: 'div' } );
  152. panelChildView.focus = () => {};
  153. panelChildView.focusLast = () => {};
  154. dropdownView.panelView.children.add( panelChildView );
  155. dropdownView.render();
  156. document.body.appendChild( dropdownView.element );
  157. } );
  158. afterEach( () => {
  159. dropdownView.element.remove();
  160. } );
  161. it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
  162. const keyEvtData = {
  163. keyCode: keyCodes.arrowdown,
  164. preventDefault: sinon.spy(),
  165. stopPropagation: sinon.spy()
  166. };
  167. const spy = sinon.spy( panelChildView, 'focus' );
  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. it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
  176. const keyEvtData = {
  177. keyCode: keyCodes.arrowup,
  178. preventDefault: sinon.spy(),
  179. stopPropagation: sinon.spy()
  180. };
  181. const spy = sinon.spy( panelChildView, 'focusLast' );
  182. dropdownView.isOpen = false;
  183. dropdownView.keystrokes.press( keyEvtData );
  184. sinon.assert.notCalled( spy );
  185. dropdownView.isOpen = true;
  186. dropdownView.keystrokes.press( keyEvtData );
  187. sinon.assert.calledOnce( spy );
  188. } );
  189. } );
  190. } );
  191. } );
  192. describe( 'addToolbarToDropdown()', () => {
  193. let buttons;
  194. beforeEach( () => {
  195. buttons = [ '<svg>foo</svg>', '<svg>bar</svg>' ].map( icon => {
  196. const button = new ButtonView();
  197. button.icon = icon;
  198. return button;
  199. } );
  200. dropdownView = createDropdown( locale );
  201. addToolbarToDropdown( dropdownView, buttons );
  202. dropdownView.render();
  203. document.body.appendChild( dropdownView.element );
  204. } );
  205. afterEach( () => {
  206. dropdownView.element.remove();
  207. } );
  208. it( 'sets view#locale', () => {
  209. expect( dropdownView.locale ).to.equal( locale );
  210. } );
  211. it( 'sets view class', () => {
  212. expect( dropdownView.element.classList.contains( 'ck-toolbar-dropdown' ) ).to.be.true;
  213. } );
  214. describe( 'view#toolbarView', () => {
  215. it( 'is created', () => {
  216. const panelChildren = dropdownView.panelView.children;
  217. expect( panelChildren ).to.have.length( 1 );
  218. expect( panelChildren.first ).to.equal( dropdownView.toolbarView );
  219. expect( dropdownView.toolbarView ).to.be.instanceof( ToolbarView );
  220. } );
  221. it( 'delegates view.toolbarView.items#execute to the view', done => {
  222. dropdownView.on( 'execute', evt => {
  223. expect( evt.source ).to.equal( dropdownView.toolbarView.items.first );
  224. expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.first, dropdownView ] );
  225. done();
  226. } );
  227. dropdownView.toolbarView.items.first.fire( 'execute' );
  228. } );
  229. } );
  230. } );
  231. describe( 'addListToDropdown()', () => {
  232. let definitions, listItems;
  233. beforeEach( () => {
  234. definitions = new Collection();
  235. dropdownView = createDropdown( locale );
  236. dropdownView.buttonView.set( {
  237. isEnabled: true,
  238. isOn: false,
  239. label: 'foo'
  240. } );
  241. addListToDropdown( dropdownView, definitions );
  242. listItems = dropdownView.listView.items;
  243. dropdownView.render();
  244. document.body.appendChild( dropdownView.element );
  245. } );
  246. afterEach( () => {
  247. dropdownView.element.remove();
  248. } );
  249. describe( 'view#listView', () => {
  250. it( 'is created', () => {
  251. const panelChildren = dropdownView.panelView.children;
  252. expect( panelChildren ).to.have.length( 1 );
  253. expect( panelChildren.first ).to.equal( dropdownView.listView );
  254. expect( dropdownView.listView ).to.be.instanceof( ListView );
  255. } );
  256. it( 'ignores unknown definition types', () => {
  257. definitions.add( { type: 'foo' } );
  258. expect( listItems.length ).to.equal( 0 );
  259. } );
  260. describe( 'with ButtonView', () => {
  261. it( 'is populated using item definitions', () => {
  262. definitions.add( {
  263. type: 'button',
  264. model: new Model( { label: 'a', labelStyle: 'b' } )
  265. } );
  266. definitions.add( {
  267. type: 'button',
  268. model: new Model( { label: 'c', labelStyle: 'd' } )
  269. } );
  270. expect( listItems ).to.have.length( 2 );
  271. expect( listItems.first ).to.be.instanceOf( ListItemView );
  272. expect( listItems.first.children.first ).to.be.instanceOf( ButtonView );
  273. expect( listItems.get( 1 ).children.first.label ).to.equal( 'c' );
  274. expect( listItems.get( 1 ).children.first.labelStyle ).to.equal( 'd' );
  275. definitions.remove( 1 );
  276. expect( listItems ).to.have.length( 1 );
  277. expect( listItems.first.children.first.label ).to.equal( 'a' );
  278. expect( listItems.first.children.first.labelStyle ).to.equal( 'b' );
  279. } );
  280. it( 'binds all button properties', () => {
  281. const def = {
  282. type: 'button',
  283. model: new Model( { label: 'a', labelStyle: 'b', foo: 'bar', baz: 'qux' } )
  284. };
  285. definitions.add( def );
  286. const button = listItems.first.children.first;
  287. expect( button.foo ).to.equal( 'bar' );
  288. expect( button.baz ).to.equal( 'qux' );
  289. def.model.baz = 'foo?';
  290. expect( button.baz ).to.equal( 'foo?' );
  291. } );
  292. it( 'delegates ButtonView#execute to the ListItemView', done => {
  293. definitions.add( {
  294. type: 'button',
  295. model: new Model( { label: 'a', labelStyle: 'b' } )
  296. } );
  297. const listItem = listItems.first;
  298. const button = listItem.children.first;
  299. dropdownView.on( 'execute', evt => {
  300. expect( evt.source ).to.equal( button );
  301. expect( evt.path ).to.deep.equal( [ button, listItem, dropdownView ] );
  302. done();
  303. } );
  304. button.fire( 'execute' );
  305. } );
  306. } );
  307. describe( 'with SwitchButtonView', () => {
  308. it( 'is populated using item definitions', () => {
  309. definitions.add( {
  310. type: 'switchbutton',
  311. model: new Model( { label: 'a', labelStyle: 'b' } )
  312. } );
  313. expect( listItems ).to.have.length( 1 );
  314. expect( listItems.first ).to.be.instanceOf( ListItemView );
  315. expect( listItems.first.children.first ).to.be.instanceOf( SwitchButtonView );
  316. expect( listItems ).to.have.length( 1 );
  317. expect( listItems.first.children.first.label ).to.equal( 'a' );
  318. expect( listItems.first.children.first.labelStyle ).to.equal( 'b' );
  319. } );
  320. it( 'binds all button properties', () => {
  321. const def = {
  322. type: 'switchbutton',
  323. model: new Model( { label: 'a', labelStyle: 'b', foo: 'bar', baz: 'qux' } )
  324. };
  325. definitions.add( def );
  326. const button = listItems.first.children.first;
  327. expect( button.foo ).to.equal( 'bar' );
  328. expect( button.baz ).to.equal( 'qux' );
  329. def.model.baz = 'foo?';
  330. expect( button.baz ).to.equal( 'foo?' );
  331. } );
  332. it( 'delegates SwitchButtonView#execute to the ListItemView', done => {
  333. definitions.add( {
  334. type: 'switchbutton',
  335. model: new Model( { label: 'a', labelStyle: 'b' } )
  336. } );
  337. const listItem = listItems.first;
  338. const button = listItem.children.first;
  339. dropdownView.on( 'execute', evt => {
  340. expect( evt.source ).to.equal( button );
  341. expect( evt.path ).to.deep.equal( [ button, listItem, dropdownView ] );
  342. done();
  343. } );
  344. button.fire( 'execute' );
  345. } );
  346. } );
  347. describe( 'with ListSeparatorView', () => {
  348. it( 'creates a separator from the definition', () => {
  349. definitions.add( { type: 'separator' } );
  350. expect( listItems.first ).to.be.instanceOf( ListSeparatorView );
  351. } );
  352. } );
  353. } );
  354. } );
  355. } );