8
0

utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.get( 0 ) ).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.get( 0 ) );
  210. expect( evt.path ).to.deep.equal( [ dropdownView.toolbarView.items.get( 0 ), dropdownView ] );
  211. done();
  212. } );
  213. dropdownView.toolbarView.items.get( 0 ).fire( 'execute' );
  214. } );
  215. } );
  216. } );
  217. describe( 'addListToDropdown()', () => {
  218. let items;
  219. beforeEach( () => {
  220. items = new Collection();
  221. dropdownView = createDropdown( locale );
  222. dropdownView.buttonView.set( {
  223. isEnabled: true,
  224. isOn: false,
  225. label: 'foo'
  226. } );
  227. addListToDropdown( dropdownView, items );
  228. dropdownView.render();
  229. document.body.appendChild( dropdownView.element );
  230. } );
  231. afterEach( () => {
  232. dropdownView.element.remove();
  233. } );
  234. describe( 'view#listView', () => {
  235. it( 'is created', () => {
  236. const panelChildren = dropdownView.panelView.children;
  237. expect( panelChildren ).to.have.length( 1 );
  238. expect( panelChildren.get( 0 ) ).to.equal( dropdownView.listView );
  239. expect( dropdownView.listView ).to.be.instanceof( ListView );
  240. } );
  241. it( 'is bound to model#items', () => {
  242. items.add( new Model( { label: 'a', style: 'b' } ) );
  243. items.add( new Model( { label: 'c', style: 'd' } ) );
  244. expect( dropdownView.listView.items ).to.have.length( 2 );
  245. expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListItemView );
  246. expect( dropdownView.listView.items.get( 1 ).label ).to.equal( 'c' );
  247. expect( dropdownView.listView.items.get( 1 ).style ).to.equal( 'd' );
  248. items.remove( 1 );
  249. expect( dropdownView.listView.items ).to.have.length( 1 );
  250. expect( dropdownView.listView.items.get( 0 ).label ).to.equal( 'a' );
  251. expect( dropdownView.listView.items.get( 0 ).style ).to.equal( 'b' );
  252. } );
  253. it( 'binds all attributes in model#items', () => {
  254. const itemModel = new Model( { label: 'a', style: 'b', foo: 'bar', baz: 'qux' } );
  255. items.add( itemModel );
  256. const item = dropdownView.listView.items.get( 0 );
  257. expect( item.foo ).to.equal( 'bar' );
  258. expect( item.baz ).to.equal( 'qux' );
  259. itemModel.baz = 'foo?';
  260. expect( item.baz ).to.equal( 'foo?' );
  261. } );
  262. it( 'delegates view.listView#execute to the view', done => {
  263. items.add( new Model( { label: 'a', style: 'b' } ) );
  264. dropdownView.on( 'execute', evt => {
  265. expect( evt.source ).to.equal( dropdownView.listView.items.get( 0 ) );
  266. expect( evt.path ).to.deep.equal( [ dropdownView.listView.items.get( 0 ), dropdownView ] );
  267. done();
  268. } );
  269. dropdownView.listView.items.get( 0 ).fire( 'execute' );
  270. } );
  271. it( 'is filled with separators', () => {
  272. const itemModel = new Model( { isSeparator: true } );
  273. items.add( itemModel );
  274. expect( dropdownView.listView.items.get( 0 ) ).to.be.instanceOf( ListSeparatorView );
  275. } );
  276. } );
  277. } );
  278. } );