toolbarview.js 28 KB

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