8
0

toolbarview.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 none 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. } );
  143. describe( 'render()', () => {
  144. it( 'registers #items in #focusTracker', () => {
  145. const view = new ToolbarView( locale );
  146. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  147. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  148. view.items.add( focusable() );
  149. view.items.add( focusable() );
  150. sinon.assert.notCalled( spyAdd );
  151. view.render();
  152. sinon.assert.calledTwice( spyAdd );
  153. view.items.remove( 1 );
  154. sinon.assert.calledOnce( spyRemove );
  155. view.destroy();
  156. } );
  157. it( 'starts listening for #keystrokes coming from #element', () => {
  158. const view = new ToolbarView( new Locale() );
  159. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  160. view.render();
  161. sinon.assert.calledOnce( spy );
  162. sinon.assert.calledWithExactly( spy, view.element );
  163. view.destroy();
  164. } );
  165. describe( 'activates keyboard navigation for the toolbar', () => {
  166. it( 'so "arrowup" focuses previous focusable item', () => {
  167. const keyEvtData = getArrowKeyData( 'arrowup' );
  168. // No children to focus.
  169. view.keystrokes.press( keyEvtData );
  170. sinon.assert.calledOnce( keyEvtData.preventDefault );
  171. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  172. view.items.add( nonFocusable() );
  173. view.items.add( nonFocusable() );
  174. // No focusable children.
  175. view.keystrokes.press( keyEvtData );
  176. sinon.assert.calledTwice( keyEvtData.preventDefault );
  177. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  178. view.items.add( focusable() );
  179. view.items.add( nonFocusable() );
  180. view.items.add( focusable() );
  181. // Mock the last item is focused.
  182. view.focusTracker.isFocused = true;
  183. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  184. view.keystrokes.press( keyEvtData );
  185. sinon.assert.calledThrice( keyEvtData.preventDefault );
  186. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  187. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  188. } );
  189. it( 'so "arrowleft" focuses previous focusable item', () => {
  190. const keyEvtData = getArrowKeyData( 'arrowleft' );
  191. view.items.add( focusable() );
  192. view.items.add( nonFocusable() );
  193. view.items.add( focusable() );
  194. // Mock the last item is focused.
  195. view.focusTracker.isFocused = true;
  196. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  197. view.keystrokes.press( keyEvtData );
  198. sinon.assert.calledOnce( view.items.get( 0 ).focus );
  199. } );
  200. it( 'so "arrowdown" focuses next focusable item', () => {
  201. const keyEvtData = getArrowKeyData( 'arrowdown' );
  202. // No children to focus.
  203. view.keystrokes.press( keyEvtData );
  204. sinon.assert.calledOnce( keyEvtData.preventDefault );
  205. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  206. view.items.add( nonFocusable() );
  207. view.items.add( nonFocusable() );
  208. // No focusable children.
  209. view.keystrokes.press( keyEvtData );
  210. sinon.assert.calledTwice( keyEvtData.preventDefault );
  211. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  212. view.items.add( focusable() );
  213. view.items.add( nonFocusable() );
  214. view.items.add( focusable() );
  215. // Mock the last item is focused.
  216. view.focusTracker.isFocused = true;
  217. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  218. view.keystrokes.press( keyEvtData );
  219. sinon.assert.calledThrice( keyEvtData.preventDefault );
  220. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  221. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  222. } );
  223. it( 'so "arrowright" focuses next focusable item', () => {
  224. const keyEvtData = getArrowKeyData( 'arrowright' );
  225. view.items.add( focusable() );
  226. view.items.add( nonFocusable() );
  227. view.items.add( focusable() );
  228. // Mock the last item is focused.
  229. view.focusTracker.isFocused = true;
  230. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  231. view.keystrokes.press( keyEvtData );
  232. sinon.assert.calledOnce( view.items.get( 2 ).focus );
  233. } );
  234. } );
  235. it( 'calls _behavior#render()', () => {
  236. const view = new ToolbarView( locale );
  237. sinon.spy( view._behavior, 'render' );
  238. view.render();
  239. sinon.assert.calledOnce( view._behavior.render );
  240. sinon.assert.calledWithExactly( view._behavior.render, view );
  241. view.destroy();
  242. } );
  243. } );
  244. describe( 'destroy()', () => {
  245. it( 'destroys the feature', () => {
  246. sinon.spy( view._behavior, 'destroy' );
  247. view.destroy();
  248. sinon.assert.calledOnce( view._behavior.destroy );
  249. } );
  250. it( 'calls _behavior#destroy()', () => {
  251. sinon.spy( view._behavior, 'destroy' );
  252. view.destroy();
  253. sinon.assert.calledOnce( view._behavior.destroy );
  254. } );
  255. } );
  256. describe( 'focus()', () => {
  257. it( 'focuses the first focusable of #items in DOM', () => {
  258. // No children to focus.
  259. view.focus();
  260. // The second child is focusable.
  261. view.items.add( nonFocusable() );
  262. view.items.add( focusable() );
  263. view.items.add( nonFocusable() );
  264. view.focus();
  265. sinon.assert.calledOnce( view.items.get( 1 ).focus );
  266. } );
  267. } );
  268. describe( 'focusLast()', () => {
  269. it( 'focuses the last focusable of #items in DOM', () => {
  270. // No children to focus.
  271. view.focusLast();
  272. // The second child is focusable.
  273. view.items.add( nonFocusable() );
  274. view.items.add( focusable() );
  275. view.items.add( focusable() );
  276. view.items.add( focusable() );
  277. view.items.add( nonFocusable() );
  278. view.focusLast();
  279. sinon.assert.calledOnce( view.items.get( 3 ).focus );
  280. } );
  281. } );
  282. describe( 'fillFromConfig()', () => {
  283. let factory;
  284. beforeEach( () => {
  285. factory = new ComponentFactory( {} );
  286. factory.add( 'foo', namedFactory( 'foo' ) );
  287. factory.add( 'bar', namedFactory( 'bar' ) );
  288. } );
  289. it( 'expands the config into collection', () => {
  290. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  291. const items = view.items;
  292. expect( items ).to.have.length( 4 );
  293. expect( items.get( 0 ).name ).to.equal( 'foo' );
  294. expect( items.get( 1 ).name ).to.equal( 'bar' );
  295. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  296. expect( items.get( 3 ).name ).to.equal( 'foo' );
  297. } );
  298. it( 'warns if there is no such component in the factory', () => {
  299. const items = view.items;
  300. const consoleWarnStub = sinon.stub( console, 'warn' );
  301. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  302. expect( items ).to.have.length( 2 );
  303. expect( items.get( 0 ).name ).to.equal( 'foo' );
  304. expect( items.get( 1 ).name ).to.equal( 'bar' );
  305. sinon.assert.calledOnce( consoleWarnStub );
  306. sinon.assert.calledWithExactly( consoleWarnStub,
  307. sinon.match( /^toolbarview-item-unavailable/ ),
  308. { name: 'baz' }
  309. );
  310. } );
  311. } );
  312. describe( 'toolbar with static items', () => {
  313. describe( 'constructor()', () => {
  314. it( 'should set view#isVertical', () => {
  315. expect( view.isVertical ).to.be.false;
  316. } );
  317. it( 'binds itemsView#children to #items', () => {
  318. const itemA = focusable();
  319. const itemB = focusable();
  320. const itemC = focusable();
  321. view.items.add( itemA );
  322. view.items.add( itemB );
  323. view.items.add( itemC );
  324. expect( view.itemsView.children.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  325. } );
  326. it( 'binds #focusables to #items', () => {
  327. const itemA = focusable();
  328. const itemB = focusable();
  329. const itemC = focusable();
  330. view.items.add( itemA );
  331. view.items.add( itemB );
  332. view.items.add( itemC );
  333. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  334. } );
  335. } );
  336. describe( 'element bindings', () => {
  337. describe( 'class', () => {
  338. it( 'reacts on view#isVertical', () => {
  339. view.isVertical = false;
  340. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  341. view.isVertical = true;
  342. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  343. } );
  344. } );
  345. } );
  346. } );
  347. describe( 'toolbar with a dynamic item grouping', () => {
  348. let locale, view, groupedItems, ungroupedItems, groupedItemsDropdown;
  349. let resizeCallback, observeSpy, unobserveSpy;
  350. beforeEach( () => {
  351. observeSpy = sinon.spy();
  352. unobserveSpy = sinon.spy();
  353. // Make sure other tests of the editor do not affect tests that follow.
  354. // Without it, if an instance of ResizeObserver already exists somewhere undestroyed
  355. // in DOM, the following DOM mock will have no effect.
  356. ResizeObserver._observerInstance = null;
  357. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  358. resizeCallback = callback;
  359. return {
  360. observe: observeSpy,
  361. unobserve: unobserveSpy
  362. };
  363. } );
  364. locale = new Locale();
  365. view = new ToolbarView( locale, {
  366. shouldGroupWhenFull: true
  367. } );
  368. view.render();
  369. view.element.style.width = '200px';
  370. document.body.appendChild( view.element );
  371. groupedItems = view._behavior.groupedItems;
  372. ungroupedItems = view._behavior.ungroupedItems;
  373. groupedItemsDropdown = view._behavior.groupedItemsDropdown;
  374. } );
  375. afterEach( () => {
  376. view.element.remove();
  377. view.destroy();
  378. } );
  379. describe( 'constructor()', () => {
  380. it( 'extends the template with the CSS class', () => {
  381. expect( view.element.classList.contains( 'ck-toolbar_grouping' ) ).to.be.true;
  382. } );
  383. it( 'updates the UI as new #items are added', () => {
  384. sinon.spy( view._behavior, '_updateGrouping' );
  385. const itemA = focusable();
  386. const itemB = focusable();
  387. const itemC = focusable();
  388. const itemD = focusable();
  389. view.element.style.width = '200px';
  390. view.items.add( itemA );
  391. view.items.add( itemB );
  392. sinon.assert.calledTwice( view._behavior._updateGrouping );
  393. expect( ungroupedItems ).to.have.length( 2 );
  394. expect( groupedItems ).to.have.length( 0 );
  395. view.items.add( itemC );
  396. // The dropdown took some extra space.
  397. expect( ungroupedItems ).to.have.length( 1 );
  398. expect( groupedItems ).to.have.length( 2 );
  399. view.items.add( itemD, 2 );
  400. expect( ungroupedItems ).to.have.length( 1 );
  401. expect( groupedItems ).to.have.length( 3 );
  402. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  403. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD, itemC ] );
  404. } );
  405. it( 'updates the UI as #items are removed', () => {
  406. const itemA = focusable();
  407. const itemB = focusable();
  408. const itemC = focusable();
  409. const itemD = focusable();
  410. view.element.style.width = '200px';
  411. view.items.add( itemA );
  412. view.items.add( itemB );
  413. view.items.add( itemC );
  414. view.items.add( itemD );
  415. sinon.spy( view._behavior, '_updateGrouping' );
  416. view.items.remove( 2 );
  417. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  418. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  419. sinon.assert.calledOnce( view._behavior._updateGrouping );
  420. view.items.remove( 0 );
  421. sinon.assert.calledTwice( view._behavior._updateGrouping );
  422. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemD ] );
  423. } );
  424. } );
  425. it( 'groups items that overflow into the dropdown', () => {
  426. const itemA = focusable();
  427. const itemB = focusable();
  428. const itemC = focusable();
  429. const itemD = focusable();
  430. view.items.add( itemA );
  431. view.items.add( itemB );
  432. view.items.add( itemC );
  433. view.items.add( itemD );
  434. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  435. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  436. expect( view.children ).to.have.length( 3 );
  437. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  438. expect( view.children.get( 1 ) ).to.be.instanceOf( ToolbarSeparatorView );
  439. expect( view.children.get( 2 ) ).to.equal( groupedItemsDropdown );
  440. } );
  441. it( 'ungroups items if there is enough space to display them (all)', () => {
  442. const itemA = focusable();
  443. const itemB = focusable();
  444. const itemC = focusable();
  445. const itemD = focusable();
  446. view.items.add( itemA );
  447. view.items.add( itemB );
  448. view.items.add( itemC );
  449. view.items.add( itemD );
  450. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  451. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC, itemD ] );
  452. view.element.style.width = '350px';
  453. // Some grouped items cannot be ungrouped because there is not enough space and they will
  454. // land back in #_behavior.groupedItems after an attempt was made.
  455. view._behavior._updateGrouping();
  456. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  457. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemD ] );
  458. } );
  459. it( 'ungroups items if there is enough space to display them (some)', () => {
  460. const itemA = focusable();
  461. const itemB = focusable();
  462. const itemC = focusable();
  463. view.items.add( itemA );
  464. view.items.add( itemB );
  465. view.items.add( itemC );
  466. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA ] );
  467. expect( groupedItems.map( i => i ) ).to.have.ordered.members( [ itemB, itemC ] );
  468. view.element.style.width = '350px';
  469. // All grouped items will be ungrouped because they fit just alright in the main space.
  470. view._behavior._updateGrouping();
  471. expect( ungroupedItems.map( i => i ) ).to.have.ordered.members( [ itemA, itemB, itemC ] );
  472. expect( groupedItems ).to.have.length( 0 );
  473. expect( view.children ).to.have.length( 1 );
  474. expect( view.children.get( 0 ) ).to.equal( view.itemsView );
  475. } );
  476. describe( 'render()', () => {
  477. let view, groupedItems, ungroupedItems;
  478. beforeEach( () => {
  479. view = new ToolbarView( locale, {
  480. shouldGroupWhenFull: true
  481. } );
  482. observeSpy.resetHistory();
  483. unobserveSpy.resetHistory();
  484. view.render();
  485. groupedItems = view._behavior.groupedItems;
  486. ungroupedItems = view._behavior.ungroupedItems;
  487. document.body.appendChild( view.element );
  488. } );
  489. afterEach( () => {
  490. view.element.remove();
  491. view.destroy();
  492. } );
  493. it( 'starts observing toolbar resize immediatelly after render', () => {
  494. sinon.assert.calledOnce( observeSpy );
  495. sinon.assert.calledWithExactly( observeSpy, view.element );
  496. } );
  497. it( 'updates the UI when the toolbar is being resized (expanding)', () => {
  498. view.element.style.width = '200px';
  499. view.items.add( focusable() );
  500. view.items.add( focusable() );
  501. view.items.add( focusable() );
  502. view.items.add( focusable() );
  503. view.items.add( focusable() );
  504. resizeCallback( [ {
  505. target: view.element,
  506. contentRect: new Rect( view.element )
  507. } ] );
  508. expect( ungroupedItems ).to.have.length( 1 );
  509. expect( groupedItems ).to.have.length( 4 );
  510. view.element.style.width = '500px';
  511. resizeCallback( [ {
  512. target: view.element,
  513. contentRect: new Rect( view.element )
  514. } ] );
  515. expect( ungroupedItems ).to.have.length( 5 );
  516. expect( groupedItems ).to.have.length( 0 );
  517. } );
  518. it( 'updates the UI when the toolbar is being resized (narrowing)', () => {
  519. view.element.style.width = '500px';
  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( 5 );
  530. expect( groupedItems ).to.have.length( 0 );
  531. view.element.style.width = '200px';
  532. resizeCallback( [ {
  533. target: view.element,
  534. contentRect: new Rect( view.element )
  535. } ] );
  536. expect( ungroupedItems ).to.have.length( 1 );
  537. expect( groupedItems ).to.have.length( 4 );
  538. } );
  539. it( 'does not react to changes in height', () => {
  540. view.element.style.width = '500px';
  541. view.element.style.height = '200px';
  542. view.items.add( focusable() );
  543. view.items.add( focusable() );
  544. view.items.add( focusable() );
  545. view.items.add( focusable() );
  546. view.items.add( focusable() );
  547. sinon.spy( view._behavior, '_updateGrouping' );
  548. view.element.style.width = '500px';
  549. resizeCallback( [ {
  550. target: view.element,
  551. contentRect: new Rect( view.element )
  552. } ] );
  553. sinon.assert.calledOnce( view._behavior._updateGrouping );
  554. view.element.style.height = '500px';
  555. resizeCallback( [ {
  556. target: view.element,
  557. contentRect: new Rect( view.element )
  558. } ] );
  559. sinon.assert.calledOnce( view._behavior._updateGrouping );
  560. } );
  561. it( 'updates the state of grouped items upon resize', () => {
  562. testUtils.sinon.spy( view._behavior, '_updateGrouping' );
  563. sinon.assert.notCalled( view._behavior._updateGrouping );
  564. resizeCallback( [ {
  565. target: view.element,
  566. contentRect: new Rect( view.element )
  567. } ] );
  568. sinon.assert.calledOnce( view._behavior._updateGrouping );
  569. } );
  570. } );
  571. describe( 'destroy()', () => {
  572. it( 'destroys the #groupedItemsDropdown', () => {
  573. view.element.style.width = '200px';
  574. const itemA = focusable();
  575. const itemB = focusable();
  576. const itemC = focusable();
  577. const itemD = focusable();
  578. view.items.add( itemA );
  579. view.items.add( itemB );
  580. view.items.add( itemC );
  581. view.items.add( itemD );
  582. sinon.spy( groupedItemsDropdown, 'destroy' );
  583. view.element.style.width = '500px';
  584. // The dropdown hides; it does not belong to any collection but it still exist.
  585. view._behavior._updateGrouping();
  586. view.destroy();
  587. sinon.assert.calledOnce( groupedItemsDropdown.destroy );
  588. } );
  589. it( 'should destroy the #resizeObserver', () => {
  590. view.element.style.width = '200px';
  591. const itemA = focusable();
  592. const itemB = focusable();
  593. const itemC = focusable();
  594. const itemD = focusable();
  595. view.items.add( itemA );
  596. view.items.add( itemB );
  597. view.items.add( itemC );
  598. view.items.add( itemD );
  599. sinon.spy( view._behavior.resizeObserver, 'destroy' );
  600. view.destroy();
  601. sinon.assert.calledOnce( view._behavior.resizeObserver.destroy );
  602. } );
  603. } );
  604. describe( 'dropdown with grouped items', () => {
  605. it( 'has proper DOM structure', () => {
  606. view.items.add( focusable() );
  607. view.items.add( focusable() );
  608. view.items.add( focusable() );
  609. view.items.add( focusable() );
  610. expect( view.children.has( groupedItemsDropdown ) ).to.be.true;
  611. expect( groupedItemsDropdown.element.classList.contains( 'ck-toolbar__grouped-dropdown' ) );
  612. expect( groupedItemsDropdown.buttonView.label ).to.equal( 'Show more items' );
  613. } );
  614. it( 'shares its toolbarView#items with grouped items', () => {
  615. view.items.add( focusable() );
  616. view.items.add( focusable() );
  617. view.items.add( focusable() );
  618. view.items.add( focusable() );
  619. expect( groupedItemsDropdown.toolbarView.items.map( i => i ) )
  620. .to.have.ordered.members( groupedItems.map( i => i ) );
  621. } );
  622. // https://github.com/ckeditor/ckeditor5/issues/5608
  623. it( 'has the proper position depending on the UI language direction (LTR UI)', () => {
  624. const locale = new Locale( { uiLanguage: 'en' } );
  625. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  626. view.render();
  627. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'sw' );
  628. view.destroy();
  629. } );
  630. // https://github.com/ckeditor/ckeditor5/issues/5608
  631. it( 'has the proper position depending on the UI language direction (RTL UI)', () => {
  632. const locale = new Locale( { uiLanguage: 'ar' } );
  633. const view = new ToolbarView( locale, { shouldGroupWhenFull: true } );
  634. view.render();
  635. expect( view._behavior.groupedItemsDropdown.panelPosition ).to.equal( 'se' );
  636. view.destroy();
  637. } );
  638. } );
  639. describe( 'item overflow checking logic', () => {
  640. it( 'considers the right padding of the toolbar (LTR UI)', () => {
  641. view.class = 'ck-reset_all';
  642. view.element.style.width = '210px';
  643. view.element.style.paddingLeft = '0px';
  644. view.element.style.paddingRight = '20px';
  645. view.items.add( focusable() );
  646. view.items.add( focusable() );
  647. expect( view._behavior.groupedItems ).to.have.length( 1 );
  648. } );
  649. it( 'considers the left padding of the toolbar (RTL UI)', () => {
  650. const locale = new Locale( { uiLanguage: 'ar' } );
  651. const view = new ToolbarView( locale, {
  652. shouldGroupWhenFull: true
  653. } );
  654. view.extendTemplate( {
  655. attributes: {
  656. dir: locale.uiLanguageDirection
  657. }
  658. } );
  659. view.render();
  660. document.body.appendChild( view.element );
  661. view.class = 'ck-reset_all';
  662. view.element.style.width = '210px';
  663. view.element.style.paddingLeft = '20px';
  664. view.element.style.paddingRight = '0px';
  665. view.items.add( focusable() );
  666. view.items.add( focusable() );
  667. expect( view._behavior.groupedItems ).to.have.length( 1 );
  668. view.destroy();
  669. view.element.remove();
  670. } );
  671. } );
  672. describe( 'focus management', () => {
  673. it( '#focus() focuses the dropdown when it is the only focusable', () => {
  674. sinon.spy( groupedItemsDropdown, 'focus' );
  675. view.element.style.width = '10px';
  676. const itemA = focusable();
  677. const itemB = focusable();
  678. view.items.add( itemA );
  679. view.items.add( itemB );
  680. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ groupedItemsDropdown ] );
  681. view.focus();
  682. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  683. } );
  684. it( '#focusLast() focuses the dropdown when present', () => {
  685. sinon.spy( groupedItemsDropdown, 'focus' );
  686. view.element.style.width = '200px';
  687. const itemA = focusable();
  688. const itemB = focusable();
  689. const itemC = focusable();
  690. view.items.add( itemA );
  691. view.items.add( itemB );
  692. view.items.add( itemC );
  693. expect( view.focusables.map( i => i ) ).to.have.ordered.members( [ itemA, groupedItemsDropdown ] );
  694. view.focusLast();
  695. sinon.assert.calledOnce( groupedItemsDropdown.focus );
  696. view.element.remove();
  697. } );
  698. } );
  699. } );
  700. } );
  701. function focusable() {
  702. const view = nonFocusable();
  703. view.label = 'focusable';
  704. view.focus = sinon.stub().callsFake( () => {
  705. view.element.focus();
  706. } );
  707. view.extendTemplate( {
  708. attributes: {
  709. tabindex: -1
  710. }
  711. } );
  712. return view;
  713. }
  714. function nonFocusable() {
  715. const view = new View();
  716. view.set( 'label', 'non-focusable' );
  717. const bind = view.bindTemplate;
  718. view.setTemplate( {
  719. tag: 'div',
  720. attributes: {
  721. style: {
  722. padding: '0',
  723. margin: '0',
  724. width: '100px',
  725. height: '100px',
  726. background: 'rgba(255,0,0,.3)'
  727. }
  728. },
  729. children: [
  730. {
  731. text: bind.to( 'label' )
  732. }
  733. ]
  734. } );
  735. return view;
  736. }
  737. function namedFactory( name ) {
  738. return locale => {
  739. const view = new View( locale );
  740. view.name = name;
  741. view.element = document.createElement( 'a' );
  742. return view;
  743. };
  744. }
  745. function getArrowKeyData( arrow ) {
  746. return {
  747. keyCode: keyCodes[ arrow ],
  748. preventDefault: sinon.spy(),
  749. stopPropagation: sinon.spy()
  750. };
  751. }