utils.js 12 KB

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