toolbarview.js 28 KB

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