8
0

toolbarview.js 28 KB

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