toolbarview.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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, setTimeout */
  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 global from '@ckeditor/ckeditor5-utils/src/dom/global';
  15. import View from '../../src/view';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  18. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  19. describe( 'ToolbarView', () => {
  20. let locale, view;
  21. testUtils.createSinonSandbox();
  22. before( () => {
  23. addTranslations( 'pl', {
  24. 'Editor toolbar': 'Pasek narzędzi edytora'
  25. } );
  26. addTranslations( 'en', {
  27. 'Editor toolbar': 'Editor toolbar'
  28. } );
  29. } );
  30. after( () => {
  31. clearTranslations();
  32. } );
  33. beforeEach( () => {
  34. locale = new Locale();
  35. view = new ToolbarView( locale );
  36. view.render();
  37. } );
  38. afterEach( () => {
  39. sinon.restore();
  40. view.destroy();
  41. } );
  42. describe( 'constructor()', () => {
  43. it( 'should set view#locale', () => {
  44. expect( view.locale ).to.equal( locale );
  45. } );
  46. describe( '#options', () => {
  47. it( 'should be an empty object if none were passed', () => {
  48. expect( view.options ).to.deep.equal( {} );
  49. } );
  50. it( 'should be an empty object if none were passed', () => {
  51. const options = {
  52. foo: 'bar'
  53. };
  54. const toolbar = new ToolbarView( locale, options );
  55. expect( toolbar.options ).to.equal( options );
  56. toolbar.destroy();
  57. } );
  58. } );
  59. it( 'should create view#items collection', () => {
  60. expect( view.items ).to.be.instanceOf( ViewCollection );
  61. } );
  62. it( 'creates #focusTracker instance', () => {
  63. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  64. } );
  65. it( 'creates #keystrokes instance', () => {
  66. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  67. } );
  68. it( 'should create view#itemsView', () => {
  69. expect( view.itemsView ).to.be.instanceOf( View );
  70. } );
  71. it( 'should create view#children collection', () => {
  72. expect( view.children ).to.be.instanceOf( ViewCollection );
  73. } );
  74. it( 'creates #_focusCycler instance', () => {
  75. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  76. } );
  77. it( 'creates #_behavior', () => {
  78. expect( view._behavior ).to.be.an( 'object' );
  79. } );
  80. } );
  81. describe( 'template', () => {
  82. it( 'should create element from template', () => {
  83. expect( view.element.classList.contains( 'ck' ) ).to.true;
  84. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  85. } );
  86. it( 'should create #itemsView from template', () => {
  87. expect( view.element.firstChild ).to.equal( view.itemsView.element );
  88. expect( view.itemsView.element.classList.contains( 'ck' ) ).to.true;
  89. expect( view.itemsView.element.classList.contains( 'ck-toolbar__items' ) ).to.true;
  90. } );
  91. describe( 'attributes', () => {
  92. it( 'should be defined', () => {
  93. expect( view.element.getAttribute( 'role' ) ).to.equal( 'toolbar' );
  94. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Editor toolbar' );
  95. } );
  96. it( 'should allow a custom aria-label', () => {
  97. const view = new ToolbarView( locale );
  98. view.ariaLabel = 'Custom label';
  99. view.render();
  100. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Custom label' );
  101. view.destroy();
  102. } );
  103. it( 'should allow the aria-label to be translated', () => {
  104. const view = new ToolbarView( new Locale( { uiLanguage: 'pl' } ) );
  105. view.render();
  106. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Pasek narzędzi edytora' );
  107. view.destroy();
  108. } );
  109. } );
  110. describe( 'event listeners', () => {
  111. it( 'prevent default on #mousedown', () => {
  112. const evt = new Event( 'mousedown', { bubbles: true } );
  113. const spy = sinon.spy( evt, 'preventDefault' );
  114. view.element.dispatchEvent( evt );
  115. sinon.assert.calledOnce( spy );
  116. } );
  117. } );
  118. } );
  119. describe( 'element bindings', () => {
  120. describe( 'class', () => {
  121. it( 'reacts on view#class', () => {
  122. view.class = 'foo';
  123. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  124. view.class = 'bar';
  125. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  126. view.class = false;
  127. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  128. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  129. } );
  130. } );
  131. } );
  132. describe( 'render()', () => {
  133. it( 'registers #items in #focusTracker', () => {
  134. const view = new ToolbarView( locale );
  135. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  136. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  137. view.items.add( focusable() );
  138. view.items.add( focusable() );
  139. sinon.assert.notCalled( spyAdd );
  140. view.render();
  141. sinon.assert.calledTwice( spyAdd );
  142. view.items.remove( 1 );
  143. sinon.assert.calledOnce( spyRemove );
  144. view.destroy();
  145. } );
  146. it( 'starts listening for #keystrokes coming from #element', () => {
  147. const view = new ToolbarView( new Locale() );
  148. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  149. view.render();
  150. sinon.assert.calledOnce( spy );
  151. sinon.assert.calledWithExactly( spy, view.element );
  152. view.destroy();
  153. } );
  154. describe( 'activates keyboard navigation for the toolbar', () => {
  155. it( 'so "arrowup" focuses previous focusable item', () => {
  156. const keyEvtData = getArrowKeyData( 'arrowup' );
  157. // No children to focus.
  158. view.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( keyEvtData.preventDefault );
  160. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  161. view.items.add( nonFocusable() );
  162. view.items.add( nonFocusable() );
  163. // No focusable children.
  164. view.keystrokes.press( keyEvtData );
  165. sinon.assert.calledTwice( keyEvtData.preventDefault );
  166. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  167. view.items.add( focusable() );
  168. view.items.add( nonFocusable() );
  169. view.items.add( focusable() );
  170. // Mock the last item is focused.
  171. view.focusTracker.isFocused = true;
  172. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  173. view.keystrokes.press( keyEvtData );
  174. sinon.assert.calledThrice( keyEvtData.preventDefault );
  175. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  176. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  177. } );
  178. it( 'so "arrowleft" focuses previous focusable item', () => {
  179. const keyEvtData = getArrowKeyData( 'arrowleft' );
  180. view.items.add( focusable() );
  181. view.items.add( nonFocusable() );
  182. view.items.add( focusable() );
  183. // Mock the last item is focused.
  184. view.focusTracker.isFocused = true;
  185. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  186. view.keystrokes.press( keyEvtData );
  187. sinon.assert.calledOnce( view.items.get( 0 ).focus );
  188. } );
  189. it( 'so "arrowdown" focuses next focusable item', () => {
  190. const keyEvtData = getArrowKeyData( 'arrowdown' );
  191. // No children to focus.
  192. view.keystrokes.press( keyEvtData );
  193. sinon.assert.calledOnce( keyEvtData.preventDefault );
  194. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  195. view.items.add( nonFocusable() );
  196. view.items.add( nonFocusable() );
  197. // No focusable children.
  198. view.keystrokes.press( keyEvtData );
  199. sinon.assert.calledTwice( keyEvtData.preventDefault );
  200. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  201. view.items.add( focusable() );
  202. view.items.add( nonFocusable() );
  203. view.items.add( focusable() );
  204. // Mock the last item is focused.
  205. view.focusTracker.isFocused = true;
  206. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  207. view.keystrokes.press( keyEvtData );
  208. sinon.assert.calledThrice( keyEvtData.preventDefault );
  209. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  210. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  211. } );
  212. it( 'so "arrowright" focuses next focusable item', () => {
  213. const keyEvtData = getArrowKeyData( 'arrowright' );
  214. view.items.add( focusable() );
  215. view.items.add( nonFocusable() );
  216. view.items.add( focusable() );
  217. // Mock the last item is focused.
  218. view.focusTracker.isFocused = true;
  219. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  220. view.keystrokes.press( keyEvtData );
  221. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  222. } );
  223. } );
  224. it( 'calls _behavior#render()', () => {
  225. const view = new ToolbarView( locale );
  226. sinon.spy( view._behavior, 'render' );
  227. view.render();
  228. sinon.assert.calledOnce( view._behavior.render );
  229. sinon.assert.calledWithExactly( view._behavior.render, view );
  230. view.destroy();
  231. } );
  232. } );
  233. describe( 'destroy()', () => {
  234. it( 'destroys the feature', () => {
  235. sinon.spy( view._behavior, 'destroy' );
  236. view.destroy();
  237. sinon.assert.calledOnce( view._behavior.destroy );
  238. } );
  239. it( 'calls _behavior#destroy()', () => {
  240. sinon.spy( view._behavior, 'destroy' );
  241. view.destroy();
  242. sinon.assert.calledOnce( view._behavior.destroy );
  243. } );
  244. } );
  245. describe( 'focus()', () => {
  246. it( 'focuses the first focusable of #items in DOM', () => {
  247. // No children to focus.
  248. view.focus();
  249. // The second child is focusable.
  250. view.items.add( nonFocusable() );
  251. view.items.add( focusable() );
  252. view.items.add( nonFocusable() );
  253. view.focus();
  254. sinon.assert.calledOnce( view.items.get( 1 ).focus );
  255. } );
  256. } );
  257. describe( 'focusLast()', () => {
  258. it( 'focuses the last focusable of #items in DOM', () => {
  259. // No children to focus.
  260. view.focusLast();
  261. // The second child is focusable.
  262. view.items.add( nonFocusable() );
  263. view.items.add( focusable() );
  264. view.items.add( focusable() );
  265. view.items.add( focusable() );
  266. view.items.add( nonFocusable() );
  267. view.focusLast();
  268. sinon.assert.calledOnce( view.items.get( 3 ).focus );
  269. } );
  270. } );
  271. describe( 'fillFromConfig()', () => {
  272. let factory;
  273. beforeEach( () => {
  274. factory = new ComponentFactory( {} );
  275. factory.add( 'foo', namedFactory( 'foo' ) );
  276. factory.add( 'bar', namedFactory( 'bar' ) );
  277. } );
  278. it( 'expands the config into collection', () => {
  279. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  280. const items = view.items;
  281. expect( items ).to.have.length( 4 );
  282. expect( items.get( 0 ).name ).to.equal( 'foo' );
  283. expect( items.get( 1 ).name ).to.equal( 'bar' );
  284. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  285. expect( items.get( 3 ).name ).to.equal( 'foo' );
  286. } );
  287. it( 'warns if there is no such component in the factory', () => {
  288. const items = view.items;
  289. const consoleWarnStub = sinon.stub( console, 'warn' );
  290. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  291. expect( items ).to.have.length( 2 );
  292. expect( items.get( 0 ).name ).to.equal( 'foo' );
  293. expect( items.get( 1 ).name ).to.equal( 'bar' );
  294. sinon.assert.calledOnce( consoleWarnStub );
  295. sinon.assert.calledWithExactly( consoleWarnStub,
  296. sinon.match( /^toolbarview-item-unavailable/ ),
  297. { name: 'baz' }
  298. );
  299. } );
  300. } );
  301. describe( 'toolbar with static items', () => {
  302. describe( 'constructor()', () => {
  303. it( 'should set view#isVertical', () => {
  304. expect( view.isVertical ).to.be.false;
  305. } );
  306. it( 'binds itemsView#children to #items', () => {
  307. const itemA = focusable();
  308. const itemB = focusable();
  309. const itemC = focusable();
  310. view.items.add( itemA );
  311. view.items.add( itemB );
  312. view.items.add( itemC );
  313. expect( view.itemsView.children.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  314. } );
  315. it( 'binds #focusables to #items', () => {
  316. const itemA = focusable();
  317. const itemB = focusable();
  318. const itemC = focusable();
  319. view.items.add( itemA );
  320. view.items.add( itemB );
  321. view.items.add( itemC );
  322. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  323. } );
  324. } );
  325. describe( 'element bindings', () => {
  326. describe( 'class', () => {
  327. it( 'reacts on view#isVertical', () => {
  328. view.isVertical = false;
  329. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  330. view.isVertical = true;
  331. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  332. } );
  333. } );
  334. } );
  335. } );
  336. describe( 'toolbar with a dynamic item grouping', () => {
  337. let locale, view, groupedItems, ungroupedItems, groupedItemsDropdown;
  338. beforeEach( () => {
  339. locale = new Locale();
  340. view = new ToolbarView( locale, {
  341. shouldGroupWhenFull: true
  342. } );
  343. view.render();
  344. view.element.style.width = '200px';
  345. document.body.appendChild( view.element );
  346. groupedItems = view._behavior.groupedItems;
  347. ungroupedItems = view._behavior.ungroupedItems;
  348. groupedItemsDropdown = view._behavior.groupedItemsDropdown;
  349. } );
  350. afterEach( () => {
  351. sinon.restore();
  352. view.element.remove();
  353. view.destroy();
  354. } );
  355. describe( 'constructor()', () => {
  356. it( 'extends the template with the CSS class', () => {
  357. expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.true;
  358. } );
  359. it( 'updates the UI as new #items are added', () => {
  360. sinon.spy( view._behavior, '_updateGrouping' );
  361. const itemA = focusable();
  362. const itemB = focusable();
  363. const itemC = focusable();
  364. const itemD = focusable();
  365. view.element.style.width = '200px';
  366. view.items.add( itemA );
  367. view.items.add( itemB );
  368. sinon.assert.calledTwice( view._behavior._updateGrouping );
  369. expect( ungroupedItems ).to.have.length( 2 );
  370. expect( groupedItems ).to.have.length( 0 );
  371. view.items.add( itemC );
  372. // The dropdown took some extra space.
  373. expect( ungroupedItems ).to.have.length( 1 );
  374. expect( groupedItems ).to.have.length( 2 );
  375. view.items.add( itemD, 2 );
  376. expect( ungroupedItems ).to.have.length( 1 );
  377. expect( groupedItems ).to.have.length( 3 );
  378. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  379. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD, itemC ] );
  380. } );
  381. it( 'updates the UI as #items are removed', () => {
  382. const itemA = focusable();
  383. const itemB = focusable();
  384. const itemC = focusable();
  385. const itemD = focusable();
  386. view.element.style.width = '200px';
  387. view.items.add( itemA );
  388. view.items.add( itemB );
  389. view.items.add( itemC );
  390. view.items.add( itemD );
  391. sinon.spy( view._behavior, '_updateGrouping' );
  392. view.items.remove( 2 );
  393. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  394. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  395. sinon.assert.calledOnce( view._behavior._updateGrouping );
  396. view.items.remove( 0 );
  397. sinon.assert.calledTwice( view._behavior._updateGrouping );
  398. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  399. } );
  400. } );
  401. it( 'groups items that overflow into the dropdown', () => {
  402. const itemA = focusable();
  403. const itemB = focusable();
  404. const itemC = focusable();
  405. const itemD = focusable();
  406. view.items.add( itemA );
  407. view.items.add( itemB );
  408. view.items.add( itemC );
  409. view.items.add( itemD );
  410. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  411. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  412. expect( view.children ).to.have.length( 3 );
  413. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  414. expect( view.children.get( 1 ) ).to.be.instanceOf( ToolbarSeparatorView );
  415. expect( view.children.get( 2 ) ).to.equal( groupedItemsDropdown );
  416. } );
  417. it( 'ungroups items if there is enough space to display them (all)', () => {
  418. const itemA = focusable();
  419. const itemB = focusable();
  420. const itemC = focusable();
  421. const itemD = focusable();
  422. view.items.add( itemA );
  423. view.items.add( itemB );
  424. view.items.add( itemC );
  425. view.items.add( itemD );
  426. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  427. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  428. view.element.style.width = '350px';
  429. // Some grouped items cannot be ungrouped because there is not enough space and they will
  430. // land back in #_behavior.groupedItems after an attempt was made.
  431. view._behavior._updateGrouping();
  432. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  433. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemD ] );
  434. } );
  435. it( 'ungroups items if there is enough space to display them (some)', () => {
  436. const itemA = focusable();
  437. const itemB = focusable();
  438. const itemC = focusable();
  439. view.items.add( itemA );
  440. view.items.add( itemB );
  441. view.items.add( itemC );
  442. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  443. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC ] );
  444. view.element.style.width = '350px';
  445. // All grouped items will be ungrouped because they fit just alright in the main space.
  446. view._behavior._updateGrouping();
  447. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  448. expect( groupedItems ).to.have.length( 0 );
  449. expect( view.children ).to.have.length( 1 );
  450. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  451. } );
  452. describe( 'render()', () => {
  453. it( 'starts observing toolbar resize immediatelly after render', () => {
  454. function FakeResizeObserver( callback ) {
  455. this.callback = callback;
  456. }
  457. FakeResizeObserver.prototype.observe = sinon.spy();
  458. FakeResizeObserver.prototype.disconnect = sinon.spy();
  459. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( FakeResizeObserver );
  460. const view = new ToolbarView( locale, {
  461. shouldGroupWhenFull: true
  462. } );
  463. view.render();
  464. sinon.assert.calledOnce( view._behavior.resizeObserver.observe );
  465. sinon.assert.calledWithExactly( view._behavior.resizeObserver.observe, view.element );
  466. view.destroy();
  467. } );
  468. it( 'updates the UI when the toolbar is being resized (expanding)', done => {
  469. view.element.style.width = '200px';
  470. view.items.add( focusable() );
  471. view.items.add( focusable() );
  472. view.items.add( focusable() );
  473. view.items.add( focusable() );
  474. view.items.add( focusable() );
  475. expect( ungroupedItems ).to.have.length( 1 );
  476. expect( groupedItems ).to.have.length( 4 );
  477. view.element.style.width = '500px';
  478. setTimeout( () => {
  479. expect( ungroupedItems ).to.have.length( 5 );
  480. expect( groupedItems ).to.have.length( 0 );
  481. done();
  482. }, 100 );
  483. } );
  484. it( 'updates the UI when the toolbar is being resized (narrowing)', done => {
  485. view.element.style.width = '500px';
  486. view.items.add( focusable() );
  487. view.items.add( focusable() );
  488. view.items.add( focusable() );
  489. view.items.add( focusable() );
  490. view.items.add( focusable() );
  491. expect( ungroupedItems ).to.have.length( 5 );
  492. expect( groupedItems ).to.have.length( 0 );
  493. view.element.style.width = '200px';
  494. setTimeout( () => {
  495. expect( ungroupedItems ).to.have.length( 1 );
  496. expect( groupedItems ).to.have.length( 4 );
  497. done();
  498. }, 100 );
  499. } );
  500. it( 'does not react to changes in height', done => {
  501. view.element.style.width = '500px';
  502. view.element.style.height = '200px';
  503. view.items.add( focusable() );
  504. view.items.add( focusable() );
  505. view.items.add( focusable() );
  506. view.items.add( focusable() );
  507. view.items.add( focusable() );
  508. sinon.spy( view._behavior, '_updateGrouping' );
  509. view.element.style.width = '500px';
  510. setTimeout( () => {
  511. sinon.assert.calledOnce( view._behavior._updateGrouping );
  512. view.element.style.height = '500px';
  513. setTimeout( () => {
  514. sinon.assert.calledOnce( view._behavior._updateGrouping );
  515. done();
  516. }, 100 );
  517. }, 100 );
  518. } );
  519. it( 'updates the state of grouped items upon resize', () => {
  520. function FakeResizeObserver( callback ) {
  521. this.callback = callback;
  522. }
  523. FakeResizeObserver.prototype.observe = sinon.spy();
  524. FakeResizeObserver.prototype.disconnect = sinon.spy();
  525. testUtils.sinon.stub( global.window, 'ResizeObserver' ).value( FakeResizeObserver );
  526. const view = new ToolbarView( locale, {
  527. shouldGroupWhenFull: true
  528. } );
  529. testUtils.sinon.spy( view._behavior, '_updateGrouping' );
  530. view.render();
  531. view._behavior.resizeObserver.callback( [
  532. { contentRect: { width: 42 } }
  533. ] );
  534. sinon.assert.calledTwice( view._behavior._updateGrouping );
  535. view.destroy();
  536. } );
  537. } );
  538. describe( 'destroy()', () => {
  539. it( 'destroys the #groupedItemsDropdown', () => {
  540. view.element.style.width = '200px';
  541. const itemA = focusable();
  542. const itemB = focusable();
  543. const itemC = focusable();
  544. const itemD = focusable();
  545. view.items.add( itemA );
  546. view.items.add( itemB );
  547. view.items.add( itemC );
  548. view.items.add( itemD );
  549. sinon.spy( groupedItemsDropdown, 'destroy' );
  550. view.element.style.width = '500px';
  551. // The dropdown hides; it does not belong to any collection but it still exist.
  552. view._behavior._updateGrouping();
  553. view.destroy();
  554. sinon.assert.calledOnce( groupedItemsDropdown.destroy );
  555. } );
  556. it( 'disconnects the #resizeObserver', () => {
  557. view.element.style.width = '200px';
  558. const itemA = focusable();
  559. const itemB = focusable();
  560. const itemC = focusable();
  561. const itemD = focusable();
  562. view.items.add( itemA );
  563. view.items.add( itemB );
  564. view.items.add( itemC );
  565. view.items.add( itemD );
  566. sinon.spy( view._behavior.resizeObserver, 'disconnect' );
  567. view.destroy();
  568. sinon.assert.calledOnce( view._behavior.resizeObserver.disconnect );
  569. } );
  570. } );
  571. describe( 'dropdown with grouped items', () => {
  572. it( 'has proper DOM structure', () => {
  573. view.items.add( focusable() );
  574. view.items.add( focusable() );
  575. view.items.add( focusable() );
  576. view.items.add( focusable() );
  577. expect( view.children.has( groupedItemsDropdown ) ).to.be.true;
  578. expect( groupedItemsDropdown.element.classList.contains( 'ck-toolbar__grouped-dropdown' ) );
  579. expect( groupedItemsDropdown.buttonView.label ).to.equal( 'Show more items' );
  580. } );
  581. it( 'shares its toolbarView#items with grouped items', () => {
  582. view.items.add( focusable() );
  583. view.items.add( focusable() );
  584. view.items.add( focusable() );
  585. view.items.add( focusable() );
  586. expect( groupedItemsDropdown.toolbarView.items.map( i => i ) )
  587. .to.have.ordered.members( groupedItems.map( i => i ) );
  588. } );
  589. // https://github.com/ckeditor/ckeditor5/issues/5608
  590. it( 'has the proper position depending on the UI language direction (LTR UI)', () => {
  591. const locale = new Locale( { uiLanguage: 'en' } );
  592. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  593. view.render();
  594. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'sw' );
  595. view.destroy();
  596. } );
  597. // https://github.com/ckeditor/ckeditor5/issues/5608
  598. it( 'has the proper position depending on the UI language direction (RTL UI)', () => {
  599. const locale = new Locale( { uiLanguage: 'ar' } );
  600. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  601. view.render();
  602. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'se' );
  603. view.destroy();
  604. } );
  605. } );
  606. describe( 'item overflow checking logic', () => {
  607. it( 'considers the right padding of the toolbar (LTR UI)', () => {
  608. view.class = 'ck-reset_all';
  609. view.element.style.width = '210px';
  610. view.element.style.paddingLeft = '0px';
  611. view.element.style.paddingRight = '20px';
  612. view.items.add( focusable() );
  613. view.items.add( focusable() );
  614. expect( view._behavior.groupedItems ).to.have.length( 1 );
  615. } );
  616. it( 'considers the left padding of the toolbar (RTL UI)', () => {
  617. const locale = new Locale( { uiLanguage: 'ar' } );
  618. const view = new ToolbarView( locale, {
  619. shouldGroupWhenFull: true
  620. } );
  621. view.extendTemplate( {
  622. attributes: {
  623. dir: locale.uiLanguageDirection
  624. }
  625. } );
  626. view.render();
  627. document.body.appendChild( view.element );
  628. view.class = 'ck-reset_all';
  629. view.element.style.width = '210px';
  630. view.element.style.paddingLeft = '20px';
  631. view.element.style.paddingRight = '0px';
  632. view.items.add( focusable() );
  633. view.items.add( focusable() );
  634. expect( view._behavior.groupedItems ).to.have.length( 1 );
  635. view.destroy();
  636. view.element.remove();
  637. } );
  638. } );
  639. describe( 'focus management', () => {
  640. it( '#focus() focuses the dropdown when it is the only focusable', () => {
  641. sinon.spy( groupedItemsDropdown, 'focus' );
  642. view.element.style.width = '10px';
  643. const itemA = focusable();
  644. const itemB = focusable();
  645. view.items.add( itemA );
  646. view.items.add( itemB );
  647. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ groupedItemsDropdown ] );
  648. view.focus();
  649. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  650. } );
  651. it( '#focusLast() focuses the dropdown when present', () => {
  652. sinon.spy( groupedItemsDropdown, 'focus' );
  653. view.element.style.width = '200px';
  654. const itemA = focusable();
  655. const itemB = focusable();
  656. const itemC = focusable();
  657. view.items.add( itemA );
  658. view.items.add( itemB );
  659. view.items.add( itemC );
  660. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, groupedItemsDropdown ] );
  661. view.focusLast();
  662. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  663. view.element.remove();
  664. } );
  665. } );
  666. } );
  667. } );
  668. function focusable() {
  669. const view = nonFocusable();
  670. view.label = 'focusable';
  671. view.focus = sinon.stub().callsFake( () => {
  672. view.element.focus();
  673. } );
  674. view.extendTemplate( {
  675. attributes: {
  676. tabindex: -1
  677. }
  678. } );
  679. return view;
  680. }
  681. function nonFocusable() {
  682. const view = new View();
  683. view.set( 'label', 'non-focusable' );
  684. const bind = view.bindTemplate;
  685. view.setTemplate( {
  686. tag: 'div',
  687. attributes: {
  688. style: {
  689. padding: '0',
  690. margin: '0',
  691. width: '100px',
  692. height: '100px',
  693. background: 'rgba(255,0,0,.3)'
  694. }
  695. },
  696. children: [
  697. {
  698. text: bind.to( 'label' )
  699. }
  700. ]
  701. } );
  702. return view;
  703. }
  704. function namedFactory( name ) {
  705. return locale => {
  706. const view = new View( locale );
  707. view.name = name;
  708. view.element = document.createElement( 'a' );
  709. return view;
  710. };
  711. }
  712. function getArrowKeyData( arrow ) {
  713. return {
  714. keyCode: keyCodes[ arrow ],
  715. preventDefault: sinon.spy(),
  716. stopPropagation: sinon.spy()
  717. };
  718. }