8
0

toolbarview.js 28 KB

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