toolbarview.js 27 KB

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