toolbarview.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. sinon.match( { name: 'baz' } ),
  321. sinon.match.string // Link to the documentation.
  322. );
  323. } );
  324. } );
  325. describe( 'toolbar with static items', () => {
  326. describe( 'constructor()', () => {
  327. it( 'should set view#isVertical', () => {
  328. expect( view.isVertical ).to.be.false;
  329. } );
  330. it( 'binds itemsView#children to #items', () => {
  331. const itemA = focusable();
  332. const itemB = focusable();
  333. const itemC = focusable();
  334. view.items.add( itemA );
  335. view.items.add( itemB );
  336. view.items.add( itemC );
  337. expect( view.itemsView.children.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  338. } );
  339. it( 'binds #focusables to #items', () => {
  340. const itemA = focusable();
  341. const itemB = focusable();
  342. const itemC = focusable();
  343. view.items.add( itemA );
  344. view.items.add( itemB );
  345. view.items.add( itemC );
  346. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  347. } );
  348. } );
  349. describe( 'element bindings', () => {
  350. describe( 'class', () => {
  351. it( 'reacts on view#isVertical', () => {
  352. view.isVertical = false;
  353. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  354. view.isVertical = true;
  355. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  356. } );
  357. } );
  358. } );
  359. } );
  360. describe( 'toolbar with a dynamic item grouping', () => {
  361. let locale, view, groupedItems, ungroupedItems, groupedItemsDropdown;
  362. let resizeCallback, observeSpy, unobserveSpy;
  363. beforeEach( () => {
  364. observeSpy = sinon.spy();
  365. unobserveSpy = sinon.spy();
  366. // Make sure other tests of the editor do not affect tests that follow.
  367. // Without it, if an instance of ResizeObserver already exists somewhere undestroyed
  368. // in DOM, the following DOM mock will have no effect.
  369. ResizeObserver._observerInstance = null;
  370. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  371. resizeCallback = callback;
  372. return {
  373. observe: observeSpy,
  374. unobserve: unobserveSpy
  375. };
  376. } );
  377. locale = new Locale();
  378. view = new ToolbarView( locale, {
  379. shouldGroupWhenFull: true
  380. } );
  381. view.render();
  382. view.element.style.width = '200px';
  383. document.body.appendChild( view.element );
  384. groupedItems = view._behavior.groupedItems;
  385. ungroupedItems = view._behavior.ungroupedItems;
  386. groupedItemsDropdown = view._behavior.groupedItemsDropdown;
  387. } );
  388. afterEach( () => {
  389. view.element.remove();
  390. view.destroy();
  391. } );
  392. describe( 'constructor()', () => {
  393. it( 'extends the template with the CSS class', () => {
  394. expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.true;
  395. } );
  396. it( 'updates the UI as new #items are added', () => {
  397. sinon.spy( view._behavior, '_updateGrouping' );
  398. const itemA = focusable();
  399. const itemB = focusable();
  400. const itemC = focusable();
  401. const itemD = focusable();
  402. view.element.style.width = '200px';
  403. view.items.add( itemA );
  404. view.items.add( itemB );
  405. sinon.assert.calledTwice( view._behavior._updateGrouping );
  406. expect( ungroupedItems ).to.have.length( 2 );
  407. expect( groupedItems ).to.have.length( 0 );
  408. view.items.add( itemC );
  409. // The dropdown took some extra space.
  410. expect( ungroupedItems ).to.have.length( 1 );
  411. expect( groupedItems ).to.have.length( 2 );
  412. view.items.add( itemD, 2 );
  413. expect( ungroupedItems ).to.have.length( 1 );
  414. expect( groupedItems ).to.have.length( 3 );
  415. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  416. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD, itemC ] );
  417. } );
  418. it( 'updates the UI as #items are removed', () => {
  419. const itemA = focusable();
  420. const itemB = focusable();
  421. const itemC = focusable();
  422. const itemD = focusable();
  423. view.element.style.width = '200px';
  424. view.items.add( itemA );
  425. view.items.add( itemB );
  426. view.items.add( itemC );
  427. view.items.add( itemD );
  428. sinon.spy( view._behavior, '_updateGrouping' );
  429. view.items.remove( 2 );
  430. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  431. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  432. sinon.assert.calledOnce( view._behavior._updateGrouping );
  433. view.items.remove( 0 );
  434. sinon.assert.calledTwice( view._behavior._updateGrouping );
  435. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  436. } );
  437. it( 'doesn\'t throw when removing the first of grouped items', () => { // (#7655)
  438. const items = [ focusable(), focusable(), focusable(), focusable() ];
  439. view.element.style.width = '200px';
  440. view.items.addMany( items );
  441. view.items.remove( 1 );
  442. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ items[ 0 ] ] );
  443. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ items[ 2 ], items[ 3 ] ] );
  444. } );
  445. } );
  446. it( 'groups items that overflow into the dropdown', () => {
  447. const itemA = focusable();
  448. const itemB = focusable();
  449. const itemC = focusable();
  450. const itemD = focusable();
  451. view.items.add( itemA );
  452. view.items.add( itemB );
  453. view.items.add( itemC );
  454. view.items.add( itemD );
  455. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  456. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  457. expect( view.children ).to.have.length( 3 );
  458. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  459. expect( view.children.get( 1 ) ).to.be.instanceOf( ToolbarSeparatorView );
  460. expect( view.children.get( 2 ) ).to.equal( groupedItemsDropdown );
  461. } );
  462. it( 'ungroups items if there is enough space to display them (all)', () => {
  463. const itemA = focusable();
  464. const itemB = focusable();
  465. const itemC = focusable();
  466. const itemD = focusable();
  467. view.items.add( itemA );
  468. view.items.add( itemB );
  469. view.items.add( itemC );
  470. view.items.add( itemD );
  471. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  472. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  473. view.element.style.width = '350px';
  474. // Some grouped items cannot be ungrouped because there is not enough space and they will
  475. // land back in #_behavior.groupedItems after an attempt was made.
  476. view._behavior._updateGrouping();
  477. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  478. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemD ] );
  479. } );
  480. it( 'ungroups items if there is enough space to display them (some)', () => {
  481. const itemA = focusable();
  482. const itemB = focusable();
  483. const itemC = focusable();
  484. view.items.add( itemA );
  485. view.items.add( itemB );
  486. view.items.add( itemC );
  487. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  488. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC ] );
  489. view.element.style.width = '350px';
  490. // All grouped items will be ungrouped because they fit just alright in the main space.
  491. view._behavior._updateGrouping();
  492. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  493. expect( groupedItems ).to.have.length( 0 );
  494. expect( view.children ).to.have.length( 1 );
  495. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  496. } );
  497. describe( 'render()', () => {
  498. let view, groupedItems, ungroupedItems;
  499. beforeEach( () => {
  500. view = new ToolbarView( locale, {
  501. shouldGroupWhenFull: true
  502. } );
  503. observeSpy.resetHistory();
  504. unobserveSpy.resetHistory();
  505. view.render();
  506. groupedItems = view._behavior.groupedItems;
  507. ungroupedItems = view._behavior.ungroupedItems;
  508. document.body.appendChild( view.element );
  509. } );
  510. afterEach( () => {
  511. view.element.remove();
  512. view.destroy();
  513. } );
  514. it( 'starts observing toolbar resize immediatelly after render', () => {
  515. sinon.assert.calledOnce( observeSpy );
  516. sinon.assert.calledWithExactly( observeSpy, view.element );
  517. } );
  518. it( 'updates the UI when the toolbar is being resized (expanding)', () => {
  519. view.element.style.width = '200px';
  520. view.items.add( focusable() );
  521. view.items.add( focusable() );
  522. view.items.add( focusable() );
  523. view.items.add( focusable() );
  524. view.items.add( focusable() );
  525. resizeCallback( [ {
  526. target: view.element,
  527. contentRect: new Rect( view.element )
  528. } ] );
  529. expect( ungroupedItems ).to.have.length( 1 );
  530. expect( groupedItems ).to.have.length( 4 );
  531. view.element.style.width = '500px';
  532. resizeCallback( [ {
  533. target: view.element,
  534. contentRect: new Rect( view.element )
  535. } ] );
  536. expect( ungroupedItems ).to.have.length( 5 );
  537. expect( groupedItems ).to.have.length( 0 );
  538. } );
  539. it( 'updates the UI when the toolbar is being resized (narrowing)', () => {
  540. view.element.style.width = '500px';
  541. view.items.add( focusable() );
  542. view.items.add( focusable() );
  543. view.items.add( focusable() );
  544. view.items.add( focusable() );
  545. view.items.add( focusable() );
  546. resizeCallback( [ {
  547. target: view.element,
  548. contentRect: new Rect( view.element )
  549. } ] );
  550. expect( ungroupedItems ).to.have.length( 5 );
  551. expect( groupedItems ).to.have.length( 0 );
  552. view.element.style.width = '200px';
  553. resizeCallback( [ {
  554. target: view.element,
  555. contentRect: new Rect( view.element )
  556. } ] );
  557. expect( ungroupedItems ).to.have.length( 1 );
  558. expect( groupedItems ).to.have.length( 4 );
  559. } );
  560. it( 'does not react to changes in height', () => {
  561. view.element.style.width = '500px';
  562. view.element.style.height = '200px';
  563. view.items.add( focusable() );
  564. view.items.add( focusable() );
  565. view.items.add( focusable() );
  566. view.items.add( focusable() );
  567. view.items.add( focusable() );
  568. sinon.spy( view._behavior, '_updateGrouping' );
  569. view.element.style.width = '500px';
  570. resizeCallback( [ {
  571. target: view.element,
  572. contentRect: new Rect( view.element )
  573. } ] );
  574. sinon.assert.calledOnce( view._behavior._updateGrouping );
  575. view.element.style.height = '500px';
  576. resizeCallback( [ {
  577. target: view.element,
  578. contentRect: new Rect( view.element )
  579. } ] );
  580. sinon.assert.calledOnce( view._behavior._updateGrouping );
  581. } );
  582. it( 'updates the state of grouped items upon resize', () => {
  583. testUtils.sinon.spy( view._behavior, '_updateGrouping' );
  584. sinon.assert.notCalled( view._behavior._updateGrouping );
  585. resizeCallback( [ {
  586. target: view.element,
  587. contentRect: new Rect( view.element )
  588. } ] );
  589. sinon.assert.calledOnce( view._behavior._updateGrouping );
  590. } );
  591. it( 'does not update the state of grouped items if invisible', () => {
  592. view.element.style.width = '500px';
  593. view.element.style.height = '200px';
  594. view.items.add( focusable() );
  595. view.items.add( focusable() );
  596. view.items.add( focusable() );
  597. view.items.add( focusable() );
  598. view.items.add( focusable() );
  599. expect( ungroupedItems ).to.have.length( 5 );
  600. expect( groupedItems ).to.have.length( 0 );
  601. view.element.style.display = 'none';
  602. view.maxWidth = '100px';
  603. expect( ungroupedItems ).to.have.length( 5 );
  604. expect( groupedItems ).to.have.length( 0 );
  605. } );
  606. it( 'should queue the update of the grouped items state when invisible (and execute it when visible again)', () => {
  607. view.maxWidth = '200px';
  608. view.items.add( focusable() );
  609. view.items.add( focusable() );
  610. view.items.add( focusable() );
  611. view.items.add( focusable() );
  612. view.items.add( focusable() );
  613. expect( ungroupedItems ).to.have.length( 1 );
  614. expect( groupedItems ).to.have.length( 4 );
  615. view.element.style.display = 'none';
  616. resizeCallback( [ {
  617. target: view.element,
  618. contentRect: new Rect( view.element )
  619. } ] );
  620. // Response to this change will be queued.
  621. view.maxWidth = '500px';
  622. expect( ungroupedItems ).to.have.length( 1 );
  623. expect( groupedItems ).to.have.length( 4 );
  624. // The queued items state should happen after that.
  625. view.element.style.display = 'flex';
  626. resizeCallback( [ {
  627. target: view.element,
  628. contentRect: new Rect( view.element )
  629. } ] );
  630. expect( ungroupedItems ).to.have.length( 5 );
  631. expect( groupedItems ).to.have.length( 0 );
  632. } );
  633. it( 'should fire the "groupedItemsUpdate" event on the toolbar when some item is grouped or ungrouped', () => {
  634. const updateSpy = sinon.spy();
  635. view.on( 'groupedItemsUpdate', updateSpy );
  636. view.element.style.width = '200px';
  637. view.items.add( focusable() );
  638. view.items.add( focusable() );
  639. view.items.add( focusable() );
  640. view.items.add( focusable() );
  641. view.items.add( focusable() );
  642. resizeCallback( [ {
  643. target: view.element,
  644. contentRect: new Rect( view.element )
  645. } ] );
  646. sinon.assert.calledOnce( updateSpy );
  647. // This 10px is not enough to ungroup an item.
  648. view.element.style.width = '210px';
  649. resizeCallback( [ {
  650. target: view.element,
  651. contentRect: new Rect( view.element )
  652. } ] );
  653. sinon.assert.calledOnce( updateSpy );
  654. // But this is not enough to ungroup some items.
  655. view.element.style.width = '300px';
  656. resizeCallback( [ {
  657. target: view.element,
  658. contentRect: new Rect( view.element )
  659. } ] );
  660. sinon.assert.calledTwice( updateSpy );
  661. } );
  662. } );
  663. describe( 'destroy()', () => {
  664. it( 'destroys the #groupedItemsDropdown', () => {
  665. view.element.style.width = '200px';
  666. const itemA = focusable();
  667. const itemB = focusable();
  668. const itemC = focusable();
  669. const itemD = focusable();
  670. view.items.add( itemA );
  671. view.items.add( itemB );
  672. view.items.add( itemC );
  673. view.items.add( itemD );
  674. sinon.spy( groupedItemsDropdown, 'destroy' );
  675. view.element.style.width = '500px';
  676. // The dropdown hides; it does not belong to any collection but it still exist.
  677. view._behavior._updateGrouping();
  678. view.destroy();
  679. sinon.assert.calledOnce( groupedItemsDropdown.destroy );
  680. } );
  681. it( 'should destroy the #resizeObserver', () => {
  682. view.element.style.width = '200px';
  683. const itemA = focusable();
  684. const itemB = focusable();
  685. const itemC = focusable();
  686. const itemD = focusable();
  687. view.items.add( itemA );
  688. view.items.add( itemB );
  689. view.items.add( itemC );
  690. view.items.add( itemD );
  691. sinon.spy( view._behavior.resizeObserver, 'destroy' );
  692. view.destroy();
  693. sinon.assert.calledOnce( view._behavior.resizeObserver.destroy );
  694. } );
  695. } );
  696. describe( 'dropdown with grouped items', () => {
  697. it( 'has proper DOM structure', () => {
  698. view.items.add( focusable() );
  699. view.items.add( focusable() );
  700. view.items.add( focusable() );
  701. view.items.add( focusable() );
  702. expect( view.children.has( groupedItemsDropdown ) ).to.be.true;
  703. expect( groupedItemsDropdown.element.classList.contains( 'ck-toolbar__grouped-dropdown' ) );
  704. expect( groupedItemsDropdown.buttonView.label ).to.equal( 'Show more items' );
  705. } );
  706. it( 'shares its toolbarView#items with grouped items', () => {
  707. view.items.add( focusable() );
  708. view.items.add( focusable() );
  709. view.items.add( focusable() );
  710. view.items.add( focusable() );
  711. expect( groupedItemsDropdown.toolbarView.items.map( i => i ) )
  712. .to.have.ordered.members( groupedItems.map( i => i ) );
  713. } );
  714. // https://github.com/ckeditor/ckeditor5/issues/5608
  715. it( 'has the proper position depending on the UI language direction (LTR UI)', () => {
  716. const locale = new Locale( { uiLanguage: 'en' } );
  717. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  718. view.render();
  719. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'sw' );
  720. view.destroy();
  721. } );
  722. // https://github.com/ckeditor/ckeditor5/issues/5608
  723. it( 'has the proper position depending on the UI language direction (RTL UI)', () => {
  724. const locale = new Locale( { uiLanguage: 'ar' } );
  725. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  726. view.render();
  727. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'se' );
  728. view.destroy();
  729. } );
  730. } );
  731. describe( 'item overflow checking logic', () => {
  732. it( 'considers the right padding of the toolbar (LTR UI)', () => {
  733. view.class = 'ck-reset_all';
  734. view.element.style.width = '210px';
  735. view.element.style.paddingLeft = '0px';
  736. view.element.style.paddingRight = '20px';
  737. view.items.add( focusable() );
  738. view.items.add( focusable() );
  739. expect( view._behavior.groupedItems ).to.have.length( 1 );
  740. } );
  741. it( 'considers the left padding of the toolbar (RTL UI)', () => {
  742. const locale = new Locale( { uiLanguage: 'ar' } );
  743. const view = new ToolbarView( locale, {
  744. shouldGroupWhenFull: true
  745. } );
  746. view.extendTemplate( {
  747. attributes: {
  748. dir: locale.uiLanguageDirection
  749. }
  750. } );
  751. view.render();
  752. document.body.appendChild( view.element );
  753. view.class = 'ck-reset_all';
  754. view.element.style.width = '210px';
  755. view.element.style.paddingLeft = '20px';
  756. view.element.style.paddingRight = '0px';
  757. view.items.add( focusable() );
  758. view.items.add( focusable() );
  759. expect( view._behavior.groupedItems ).to.have.length( 1 );
  760. view.destroy();
  761. view.element.remove();
  762. } );
  763. } );
  764. describe( 'focus management', () => {
  765. it( '#focus() focuses the dropdown when it is the only focusable', () => {
  766. sinon.spy( groupedItemsDropdown, 'focus' );
  767. view.element.style.width = '10px';
  768. const itemA = focusable();
  769. const itemB = focusable();
  770. view.items.add( itemA );
  771. view.items.add( itemB );
  772. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ groupedItemsDropdown ] );
  773. view.focus();
  774. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  775. } );
  776. it( '#focusLast() focuses the dropdown when present', () => {
  777. sinon.spy( groupedItemsDropdown, 'focus' );
  778. view.element.style.width = '200px';
  779. const itemA = focusable();
  780. const itemB = focusable();
  781. const itemC = focusable();
  782. view.items.add( itemA );
  783. view.items.add( itemB );
  784. view.items.add( itemC );
  785. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, groupedItemsDropdown ] );
  786. view.focusLast();
  787. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  788. view.element.remove();
  789. } );
  790. } );
  791. } );
  792. } );
  793. function focusable() {
  794. const view = nonFocusable();
  795. view.label = 'focusable';
  796. view.focus = sinon.stub().callsFake( () => {
  797. view.element.focus();
  798. } );
  799. view.extendTemplate( {
  800. attributes: {
  801. tabindex: -1
  802. }
  803. } );
  804. return view;
  805. }
  806. function nonFocusable() {
  807. const view = new View();
  808. view.set( 'label', 'non-focusable' );
  809. const bind = view.bindTemplate;
  810. view.setTemplate( {
  811. tag: 'div',
  812. attributes: {
  813. style: {
  814. padding: '0',
  815. margin: '0',
  816. width: '100px',
  817. height: '100px',
  818. background: 'rgba(255,0,0,.3)'
  819. }
  820. },
  821. children: [
  822. {
  823. text: bind.to( 'label' )
  824. }
  825. ]
  826. } );
  827. return view;
  828. }
  829. function namedFactory( name ) {
  830. return locale => {
  831. const view = new View( locale );
  832. view.name = name;
  833. view.element = document.createElement( 'a' );
  834. return view;
  835. };
  836. }
  837. function getArrowKeyData( arrow ) {
  838. return {
  839. keyCode: keyCodes[ arrow ],
  840. preventDefault: sinon.spy(),
  841. stopPropagation: sinon.spy()
  842. };
  843. }