8
0

toolbarview.js 31 KB

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