toolbarview.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, Event, console */
  6. import ToolbarView from '../../src/toolbar/toolbarview';
  7. import ToolbarSeparatorView from '../../src/toolbar/toolbarseparatorview';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import ComponentFactory from '../../src/componentfactory';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '../../src/focuscycler';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import ViewCollection from '../../src/viewcollection';
  14. import View from '../../src/view';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  17. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  18. describe( 'ToolbarView', () => {
  19. let locale, view;
  20. testUtils.createSinonSandbox();
  21. before( () => {
  22. addTranslations( 'pl', {
  23. 'Editor toolbar': 'Pasek narzędzi edytora'
  24. } );
  25. addTranslations( 'en', {
  26. 'Editor toolbar': 'Editor toolbar'
  27. } );
  28. } );
  29. after( () => {
  30. clearTranslations();
  31. } );
  32. beforeEach( () => {
  33. locale = new Locale();
  34. view = new ToolbarView( locale );
  35. view.render();
  36. } );
  37. afterEach( () => {
  38. sinon.restore();
  39. view.destroy();
  40. } );
  41. describe( 'constructor()', () => {
  42. it( 'should set view#locale', () => {
  43. expect( view.locale ).to.equal( locale );
  44. } );
  45. it( 'should set view#isVertical', () => {
  46. expect( view.isVertical ).to.be.false;
  47. } );
  48. it( 'should create view#items collection', () => {
  49. expect( view.items ).to.be.instanceOf( ViewCollection );
  50. } );
  51. it( 'should not create view#groupedItems collection', () => {
  52. expect( view.groupedItems ).to.be.null;
  53. } );
  54. it( 'creates #focusTracker instance', () => {
  55. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  56. } );
  57. it( 'creates #keystrokes instance', () => {
  58. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  59. } );
  60. it( 'should create view#itemsView', () => {
  61. expect( view.itemsView ).to.be.instanceOf( View );
  62. } );
  63. it( 'should not create view#groupedItemsDropdown', () => {
  64. expect( view.groupedItemsDropdown ).to.be.null;
  65. } );
  66. it( 'should set view#shouldGroupWhenFull', () => {
  67. expect( view.shouldGroupWhenFull ).to.be.false;
  68. } );
  69. it( 'should create view#_components collection', () => {
  70. expect( view._components ).to.be.instanceOf( ViewCollection );
  71. } );
  72. it( 'creates #_itemsFocusCycler instance', () => {
  73. expect( view._itemsFocusCycler ).to.be.instanceOf( FocusCycler );
  74. } );
  75. it( 'creates #_componentsFocusCycler instance', () => {
  76. expect( view._componentsFocusCycler ).to.be.instanceOf( FocusCycler );
  77. } );
  78. } );
  79. describe( 'template', () => {
  80. it( 'should create element from template', () => {
  81. expect( view.element.classList.contains( 'ck' ) ).to.true;
  82. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  83. } );
  84. it( 'should create #itemsView from template', () => {
  85. expect( view.element.firstChild ).to.equal( view.itemsView.element );
  86. expect( view.itemsView.element.classList.contains( 'ck' ) ).to.true;
  87. expect( view.itemsView.element.classList.contains( 'ck-toolbar__items' ) ).to.true;
  88. } );
  89. describe( 'attributes', () => {
  90. it( 'should be defined', () => {
  91. expect( view.element.getAttribute( 'role' ) ).to.equal( 'toolbar' );
  92. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Editor toolbar' );
  93. } );
  94. it( 'should allow a custom aria-label', () => {
  95. const view = new ToolbarView( locale );
  96. view.ariaLabel = 'Custom label';
  97. view.render();
  98. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Custom label' );
  99. } );
  100. it( 'should allow the aria-label to be translated', () => {
  101. const view = new ToolbarView( new Locale( { uiLanguage: 'pl' } ) );
  102. view.render();
  103. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Pasek narzędzi edytora' );
  104. } );
  105. } );
  106. describe( 'event listeners', () => {
  107. it( 'prevent default on #mousedown', () => {
  108. const evt = new Event( 'mousedown', { bubbles: true } );
  109. const spy = sinon.spy( evt, 'preventDefault' );
  110. view.element.dispatchEvent( evt );
  111. sinon.assert.calledOnce( spy );
  112. } );
  113. } );
  114. } );
  115. describe( 'element bindings', () => {
  116. describe( 'class', () => {
  117. it( 'reacts on view#isVertical', () => {
  118. view.isVertical = false;
  119. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  120. view.isVertical = true;
  121. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  122. } );
  123. it( 'reacts on view#class', () => {
  124. view.class = 'foo';
  125. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  126. view.class = 'bar';
  127. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  128. view.class = false;
  129. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  130. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  131. } );
  132. it( 'reacts on view#shouldGroupWhenFull', () => {
  133. view.shouldGroupWhenFull = false;
  134. expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.false;
  135. view.shouldGroupWhenFull = true;
  136. expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.true;
  137. } );
  138. } );
  139. } );
  140. describe( 'render()', () => {
  141. it( 'registers #_components in #focusTracker', () => {
  142. const view = new ToolbarView( locale );
  143. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  144. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  145. view._components.add( focusable() );
  146. view._components.add( focusable() );
  147. sinon.assert.notCalled( spyAdd );
  148. view.render();
  149. // First call is for the #itemsView.
  150. sinon.assert.calledThrice( spyAdd );
  151. view._components.remove( 1 );
  152. sinon.assert.calledOnce( spyRemove );
  153. view.destroy();
  154. } );
  155. it( 'starts listening for #keystrokes coming from #element', () => {
  156. const view = new ToolbarView( new Locale() );
  157. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  158. view.render();
  159. sinon.assert.calledOnce( spy );
  160. sinon.assert.calledWithExactly( spy, view.element );
  161. view.destroy();
  162. } );
  163. describe( 'activates keyboard navigation for the toolbar', () => {
  164. it( 'so "arrowup" focuses previous focusable item', () => {
  165. const keyEvtData = getArrowKeyData( 'arrowup' );
  166. // No children to focus.
  167. view.keystrokes.press( keyEvtData );
  168. sinon.assert.calledOnce( keyEvtData.preventDefault );
  169. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  170. view.items.add( nonFocusable() );
  171. view.items.add( nonFocusable() );
  172. // No focusable children.
  173. view.keystrokes.press( keyEvtData );
  174. sinon.assert.calledTwice( keyEvtData.preventDefault );
  175. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  176. view.items.add( focusable() );
  177. view.items.add( nonFocusable() );
  178. view.items.add( focusable() );
  179. // Mock the last item is focused.
  180. view.itemsView.focusTracker.isFocused = true;
  181. view.itemsView.focusTracker.focusedElement = view.items.get( 4 ).element;
  182. view.keystrokes.press( keyEvtData );
  183. sinon.assert.calledThrice( keyEvtData.preventDefault );
  184. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  185. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  186. } );
  187. it( 'so "arrowleft" focuses previous focusable item', () => {
  188. const keyEvtData = getArrowKeyData( 'arrowleft' );
  189. view.items.add( focusable() );
  190. view.items.add( nonFocusable() );
  191. view.items.add( focusable() );
  192. // Mock the last item is focused.
  193. view.itemsView.focusTracker.isFocused = true;
  194. view.itemsView.focusTracker.focusedElement = view.items.get( 2 ).element;
  195. view.keystrokes.press( keyEvtData );
  196. sinon.assert.calledOnce( view.items.get( 0 ).focus );
  197. } );
  198. it( 'so "arrowdown" focuses next focusable item', () => {
  199. const keyEvtData = getArrowKeyData( 'arrowdown' );
  200. // No children to focus.
  201. view.keystrokes.press( keyEvtData );
  202. sinon.assert.calledOnce( keyEvtData.preventDefault );
  203. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  204. view.items.add( nonFocusable() );
  205. view.items.add( nonFocusable() );
  206. // No focusable children.
  207. view.keystrokes.press( keyEvtData );
  208. sinon.assert.calledTwice( keyEvtData.preventDefault );
  209. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  210. view.items.add( focusable() );
  211. view.items.add( nonFocusable() );
  212. view.items.add( focusable() );
  213. // Mock the last item is focused.
  214. view.itemsView.focusTracker.isFocused = true;
  215. view.itemsView.focusTracker.focusedElement = view.items.get( 4 ).element;
  216. view.keystrokes.press( keyEvtData );
  217. sinon.assert.calledThrice( keyEvtData.preventDefault );
  218. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  219. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  220. } );
  221. it( 'so "arrowright" focuses next focusable item', () => {
  222. const keyEvtData = getArrowKeyData( 'arrowright' );
  223. view.items.add( focusable() );
  224. view.items.add( nonFocusable() );
  225. view.items.add( focusable() );
  226. // Mock the last item is focused.
  227. view.itemsView.focusTracker.isFocused = true;
  228. view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
  229. view.keystrokes.press( keyEvtData );
  230. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  231. } );
  232. describe( 'when #shouldGroupWhenFull is true', () => {
  233. beforeEach( () => {
  234. document.body.appendChild( view.element );
  235. view.element.style.width = '200px';
  236. view.shouldGroupWhenFull = true;
  237. } );
  238. afterEach( () => {
  239. view.element.remove();
  240. } );
  241. it( 'navigates from #items to the #groupedItemsDropdown (forwards)', () => {
  242. const keyEvtData = getArrowKeyData( 'arrowright' );
  243. view.items.add( focusable() );
  244. view.items.add( nonFocusable() );
  245. view.items.add( focusable() );
  246. view.updateGroupedItems();
  247. sinon.spy( view.groupedItemsDropdown, 'focus' );
  248. view.focusTracker.isFocused = true;
  249. view.focusTracker.focusedElement = view.itemsView.element;
  250. view.itemsView.focusTracker.isFocused = true;
  251. view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
  252. view.keystrokes.press( keyEvtData );
  253. sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
  254. } );
  255. it( 'navigates from the #groupedItemsDropdown to #items (forwards)', () => {
  256. const keyEvtData = getArrowKeyData( 'arrowright' );
  257. view.items.add( focusable() );
  258. view.items.add( nonFocusable() );
  259. view.items.add( focusable() );
  260. view.updateGroupedItems();
  261. view.focusTracker.isFocused = true;
  262. view.focusTracker.focusedElement = view.groupedItemsDropdown.element;
  263. view.itemsView.focusTracker.isFocused = false;
  264. view.itemsView.focusTracker.focusedElement = null;
  265. view.keystrokes.press( keyEvtData );
  266. sinon.assert.calledOnce( view.items.get( 0 ).focus );
  267. } );
  268. it( 'navigates from #items to the #groupedItemsDropdown (backwards)', () => {
  269. const keyEvtData = getArrowKeyData( 'arrowleft' );
  270. view.items.add( focusable() );
  271. view.items.add( nonFocusable() );
  272. view.items.add( focusable() );
  273. view.updateGroupedItems();
  274. sinon.spy( view.groupedItemsDropdown, 'focus' );
  275. view.focusTracker.isFocused = true;
  276. view.focusTracker.focusedElement = view.itemsView.element;
  277. view.itemsView.focusTracker.isFocused = true;
  278. view.itemsView.focusTracker.focusedElement = view.items.get( 0 ).element;
  279. view.keystrokes.press( keyEvtData );
  280. sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
  281. } );
  282. it( 'navigates from the #groupedItemsDropdown to #items (backwards)', () => {
  283. const keyEvtData = getArrowKeyData( 'arrowleft' );
  284. view.items.add( focusable() );
  285. view.items.add( nonFocusable() );
  286. view.items.add( focusable() );
  287. view.updateGroupedItems();
  288. view.focusTracker.isFocused = true;
  289. view.focusTracker.focusedElement = view.groupedItemsDropdown.element;
  290. view.itemsView.focusTracker.isFocused = false;
  291. view.itemsView.focusTracker.focusedElement = null;
  292. view.keystrokes.press( keyEvtData );
  293. sinon.assert.calledOnce( view.items.get( 0 ).focus );
  294. } );
  295. } );
  296. } );
  297. } );
  298. describe( 'focus()', () => {
  299. it( 'focuses the first focusable of #items in DOM', () => {
  300. // No children to focus.
  301. view.focus();
  302. // The second child is focusable.
  303. view.items.add( nonFocusable() );
  304. view.items.add( focusable() );
  305. view.items.add( nonFocusable() );
  306. view.focus();
  307. sinon.assert.calledOnce( view.items.get( 1 ).focus );
  308. } );
  309. } );
  310. describe( 'focusLast()', () => {
  311. it( 'focuses the last focusable of #items in DOM', () => {
  312. // No children to focus.
  313. view.focusLast();
  314. // The second child is focusable.
  315. view.items.add( nonFocusable() );
  316. view.items.add( focusable() );
  317. view.items.add( focusable() );
  318. view.items.add( focusable() );
  319. view.items.add( nonFocusable() );
  320. view.focusLast();
  321. sinon.assert.calledOnce( view.items.get( 3 ).focus );
  322. } );
  323. it( 'focuses the #groupedItemsDropdown when view#shouldGroupWhenFull is true', () => {
  324. document.body.appendChild( view.element );
  325. view.element.style.width = '200px';
  326. view.shouldGroupWhenFull = true;
  327. view.items.add( focusable() );
  328. view.items.add( focusable() );
  329. view.items.add( focusable() );
  330. view.updateGroupedItems();
  331. sinon.spy( view.groupedItemsDropdown, 'focus' );
  332. view.focusLast();
  333. sinon.assert.calledOnce( view.groupedItemsDropdown.focus );
  334. view.element.remove();
  335. } );
  336. } );
  337. describe( 'fillFromConfig()', () => {
  338. let factory;
  339. beforeEach( () => {
  340. factory = new ComponentFactory( {} );
  341. factory.add( 'foo', namedFactory( 'foo' ) );
  342. factory.add( 'bar', namedFactory( 'bar' ) );
  343. } );
  344. it( 'expands the config into collection', () => {
  345. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  346. const items = view.items;
  347. expect( items ).to.have.length( 4 );
  348. expect( items.get( 0 ).name ).to.equal( 'foo' );
  349. expect( items.get( 1 ).name ).to.equal( 'bar' );
  350. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  351. expect( items.get( 3 ).name ).to.equal( 'foo' );
  352. } );
  353. it( 'warns if there is no such component in the factory', () => {
  354. const items = view.items;
  355. const consoleWarnStub = sinon.stub( console, 'warn' );
  356. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  357. expect( items ).to.have.length( 2 );
  358. expect( items.get( 0 ).name ).to.equal( 'foo' );
  359. expect( items.get( 1 ).name ).to.equal( 'bar' );
  360. sinon.assert.calledOnce( consoleWarnStub );
  361. sinon.assert.calledWithExactly( consoleWarnStub,
  362. sinon.match( /^toolbarview-item-unavailable/ ),
  363. { name: 'baz' }
  364. );
  365. } );
  366. } );
  367. } );
  368. function focusable() {
  369. const view = nonFocusable();
  370. view.label = 'focusable';
  371. view.focus = sinon.stub().callsFake( () => {
  372. view.element.focus();
  373. } );
  374. view.extendTemplate( {
  375. attributes: {
  376. tabindex: -1
  377. }
  378. } );
  379. return view;
  380. }
  381. function nonFocusable() {
  382. const view = new View();
  383. view.set( 'label', 'non-focusable' );
  384. const bind = view.bindTemplate;
  385. view.setTemplate( {
  386. tag: 'div',
  387. attributes: {
  388. style: {
  389. padding: '0',
  390. margin: '0',
  391. width: '100px',
  392. height: '100px',
  393. outline: '1px solid green'
  394. }
  395. },
  396. children: [
  397. {
  398. text: bind.to( 'label' )
  399. }
  400. ]
  401. } );
  402. return view;
  403. }
  404. function namedFactory( name ) {
  405. return locale => {
  406. const view = new View( locale );
  407. view.name = name;
  408. view.element = document.createElement( 'a' );
  409. return view;
  410. };
  411. }
  412. function getArrowKeyData( arrow ) {
  413. return {
  414. keyCode: keyCodes[ arrow ],
  415. preventDefault: sinon.spy(),
  416. stopPropagation: sinon.spy()
  417. };
  418. }