contextualballoon.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import ContextualBalloon from '../../../src/panel/balloon/contextualballoon';
  7. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  8. import View from '../../../src/view';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. /* global document, Event */
  14. describe( 'ContextualBalloon', () => {
  15. let editor, editorElement, balloon, viewA, viewB, viewC, viewD;
  16. beforeEach( () => {
  17. editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor
  20. .create( editorElement, {
  21. plugins: [ Paragraph, ContextualBalloon ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. balloon = editor.plugins.get( ContextualBalloon );
  26. // We don't need to execute BalloonPanel pin and attachTo methods
  27. // it's enough to check if was called with the proper data.
  28. sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  29. sinon.stub( balloon.view, 'pin' ).returns( {} );
  30. viewA = new View();
  31. viewB = new View();
  32. viewC = new View();
  33. viewD = new View();
  34. // Add viewA to the pane and init viewB.
  35. balloon.add( {
  36. view: viewA,
  37. position: {
  38. target: 'fake'
  39. }
  40. } );
  41. viewB.render();
  42. } );
  43. } );
  44. afterEach( () => {
  45. editor.destroy();
  46. editorElement.remove();
  47. } );
  48. it( 'should create a plugin instance', () => {
  49. expect( balloon ).to.instanceof( Plugin );
  50. expect( balloon ).to.instanceof( ContextualBalloon );
  51. } );
  52. describe( 'pluginName', () => {
  53. it( 'should return plugin by name', () => {
  54. expect( editor.plugins.get( 'ContextualBalloon' ) ).to.equal( balloon );
  55. } );
  56. } );
  57. describe( 'constructor()', () => {
  58. it( 'should create a plugin instance with properties', () => {
  59. expect( balloon.view ).to.instanceof( BalloonPanelView );
  60. } );
  61. describe( 'positionLimiter', () => {
  62. let model, view, viewDocument, root;
  63. beforeEach( () => {
  64. model = editor.model;
  65. view = editor.editing.view;
  66. viewDocument = view.document;
  67. root = viewDocument.getRoot();
  68. } );
  69. it( 'obtains the root of the selection', () => {
  70. setModelData( model, '<paragraph>[]bar</paragraph>' );
  71. expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
  72. } );
  73. it( 'does not fail if selection has no #editableElement', () => {
  74. sinon.stub( viewDocument.selection, 'editableElement' ).value( null );
  75. expect( balloon.positionLimiter() ).to.equal( null );
  76. } );
  77. it( 'obtains the farthest root of the selection (nested editable)', () => {
  78. model.schema.register( 'widget', {
  79. allowIn: '$root',
  80. isObject: true
  81. } );
  82. model.schema.register( 'nestedEditable', { allowIn: 'widget' } );
  83. model.schema.extend( '$text', { allowIn: 'nestedEditable' } );
  84. editor.conversion.for( 'downcast' ).elementToElement( {
  85. model: 'widget',
  86. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'figure', { contenteditable: 'false' } )
  87. } );
  88. editor.conversion.for( 'downcast' ).elementToElement( {
  89. model: 'nestedEditable',
  90. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'figcaption', { contenteditable: 'true' } )
  91. } );
  92. setModelData( model, '<widget><nestedEditable>[]foo</nestedEditable></widget>' );
  93. expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
  94. } );
  95. } );
  96. it( 'should add balloon panel view to editor `body` collection', () => {
  97. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.above( -1 );
  98. } );
  99. it( 'should register balloon panel element in editor.ui#focusTracker', () => {
  100. editor.ui.focusTracker.isFocused = false;
  101. balloon.add( {
  102. view: viewB,
  103. position: {
  104. target: 'fake',
  105. limiter: balloon.positionLimiter
  106. }
  107. } );
  108. balloon.view.element.dispatchEvent( new Event( 'focus' ) );
  109. expect( editor.ui.focusTracker.isFocused ).to.true;
  110. } );
  111. } );
  112. describe( 'hasView()', () => {
  113. it( 'should return true when given view is in stack', () => {
  114. expect( balloon.hasView( viewA ) ).to.true;
  115. } );
  116. it( 'should return true when given view is in stack but is not visible', () => {
  117. balloon.add( {
  118. view: viewB,
  119. position: {
  120. target: 'fake',
  121. limiter: balloon.positionLimiter
  122. }
  123. } );
  124. expect( balloon.visibleView ).to.equal( viewB );
  125. expect( balloon.hasView( viewA ) ).to.true;
  126. } );
  127. it( 'should return false when given view is not in stack', () => {
  128. expect( balloon.hasView( viewB ) ).to.false;
  129. } );
  130. } );
  131. describe( 'add()', () => {
  132. it( 'should add view to the `main` stack and display in balloon attached using given position options', () => {
  133. const content = balloon.view.content.get( 0 ).content;
  134. expect( content.length ).to.equal( 1 );
  135. expect( content.get( 0 ) ).to.deep.equal( viewA );
  136. expect( balloon.view.pin.calledOnce ).to.true;
  137. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  138. target: 'fake',
  139. limiter: balloon.positionLimiter
  140. } );
  141. } );
  142. it( 'should add view to the custom stack but not display it when other stack is already visible', () => {
  143. balloon.add( {
  144. view: viewB,
  145. stackId: 'second',
  146. position: {
  147. target: 'fake'
  148. }
  149. } );
  150. balloon.add( {
  151. view: viewC,
  152. stackId: 'second',
  153. position: {
  154. target: 'fake'
  155. }
  156. } );
  157. const content = balloon.view.content.get( 0 ).content;
  158. expect( content.length ).to.equal( 1 );
  159. expect( content.get( 0 ) ).to.deep.equal( viewA );
  160. expect( balloon.hasView( viewB ) );
  161. expect( balloon.hasView( viewC ) );
  162. } );
  163. it( 'should add multiple views to he stack and display last one', () => {
  164. balloon.add( {
  165. view: viewB,
  166. position: {
  167. target: 'fake',
  168. limiter: balloon.positionLimiter
  169. }
  170. } );
  171. const content = balloon.view.content.get( 0 ).content;
  172. expect( content.length ).to.equal( 1 );
  173. expect( content.get( 0 ) ).to.deep.equal( viewB );
  174. } );
  175. it( 'should throw an error when try to add the same view more than once', () => {
  176. expect( () => {
  177. balloon.add( {
  178. view: viewA,
  179. position: {
  180. target: 'fake',
  181. limiter: balloon.positionLimiter
  182. }
  183. } );
  184. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  185. } );
  186. it( 'should use a provided limiter instead of #positionLimiter', () => {
  187. balloon.remove( viewA );
  188. balloon.view.pin.resetHistory();
  189. balloon.add( {
  190. view: viewB,
  191. position: {
  192. target: 'foo',
  193. limiter: 'customLimiter'
  194. }
  195. } );
  196. sinon.assert.calledWithMatch( balloon.view.pin, {
  197. target: 'foo',
  198. limiter: 'customLimiter'
  199. } );
  200. } );
  201. it( 'should use a custom #positionLimiter', () => {
  202. balloon.remove( viewA );
  203. balloon.view.pin.resetHistory();
  204. balloon.positionLimiter = 'customLimiter';
  205. balloon.add( {
  206. view: viewB,
  207. position: {
  208. target: 'foo',
  209. }
  210. } );
  211. sinon.assert.calledWithMatch( balloon.view.pin, {
  212. target: 'foo',
  213. limiter: 'customLimiter'
  214. } );
  215. } );
  216. it( 'should not alter the view data if no limiter is provided and the #positionLimiter is used', () => {
  217. const data = {
  218. view: viewB,
  219. position: {
  220. target: 'foo',
  221. }
  222. };
  223. balloon.remove( viewA );
  224. balloon.add( data );
  225. expect( data ).to.deep.equal( {
  226. view: viewB,
  227. position: {
  228. target: 'foo'
  229. }
  230. } );
  231. } );
  232. it( 'should pin balloon to the target element', () => {
  233. sinon.assert.calledOnce( balloon.view.pin );
  234. } );
  235. it( 'should use the position of the last view in the stack', () => {
  236. balloon.add( {
  237. view: viewB,
  238. position: { target: 'other' }
  239. } );
  240. expect( balloon.view.pin.calledTwice ).to.true;
  241. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  242. target: 'fake',
  243. limiter: balloon.positionLimiter
  244. } );
  245. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  246. target: 'other',
  247. limiter: balloon.positionLimiter
  248. } );
  249. } );
  250. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  251. const view = new View();
  252. balloon.add( {
  253. view,
  254. position: {
  255. target: 'fake',
  256. limiter: balloon.positionLimiter
  257. },
  258. balloonClassName: 'foo'
  259. } );
  260. expect( balloon.view.class ).to.equal( 'foo' );
  261. balloon.add( {
  262. view: viewB,
  263. position: {
  264. target: 'fake',
  265. limiter: balloon.positionLimiter
  266. },
  267. balloonClassName: 'bar'
  268. } );
  269. expect( balloon.view.class ).to.equal( 'bar' );
  270. } );
  271. it( 'should hide arrow if `withArrow` option is set to false', () => {
  272. balloon.remove( viewA );
  273. balloon.view.pin.resetHistory();
  274. balloon.add( {
  275. view: viewB,
  276. position: {
  277. target: 'foo'
  278. },
  279. withArrow: false
  280. } );
  281. expect( balloon.view.withArrow ).to.be.false;
  282. } );
  283. it( 'should show arrow if `withArrow` option was not set and previously shown view had hidden arrow', () => {
  284. balloon.remove( viewA );
  285. balloon.view.pin.resetHistory();
  286. balloon.add( {
  287. view: viewB,
  288. position: {
  289. target: 'foo'
  290. },
  291. withArrow: false
  292. } );
  293. expect( balloon.view.withArrow ).to.be.false;
  294. balloon.remove( viewB );
  295. balloon.add( {
  296. view: viewB,
  297. position: {
  298. target: 'foo'
  299. }
  300. } );
  301. expect( balloon.view.withArrow ).to.be.true;
  302. } );
  303. } );
  304. describe( 'visibleView', () => {
  305. it( 'should return data of currently visible view', () => {
  306. expect( balloon.visibleView ).to.equal( viewA );
  307. } );
  308. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  309. balloon.add( {
  310. view: viewB,
  311. position: {
  312. target: 'fake',
  313. limiter: balloon.positionLimiter
  314. }
  315. } );
  316. expect( balloon.visibleView ).to.equal( viewB );
  317. } );
  318. it( 'should return `null` when the stack is empty', () => {
  319. balloon.remove( viewA );
  320. expect( balloon.visibleView ).to.null;
  321. } );
  322. it( 'should be observable', () => {
  323. const spy = sinon.spy();
  324. balloon.on( 'change:visibleView', spy );
  325. balloon.add( { view: viewB } );
  326. sinon.assert.calledOnce( spy );
  327. sinon.assert.calledWith( spy, sinon.match.any, 'visibleView', viewB, viewA );
  328. } );
  329. } );
  330. describe( 'showStack()', () => {
  331. it( 'should hide current view and display last view from the given stack', () => {
  332. balloon.add( {
  333. stackId: 'second',
  334. view: viewB
  335. } );
  336. balloon.add( {
  337. stackId: 'second',
  338. view: viewC
  339. } );
  340. expect( balloon.visibleView ).to.equal( viewA );
  341. balloon.showStack( 'second' );
  342. expect( balloon.visibleView ).to.equal( viewC );
  343. balloon.showStack( 'main' );
  344. expect( balloon.visibleView ).to.equal( viewA );
  345. } );
  346. it( 'should do nothing when given stack is already visible', () => {
  347. expect( () => {
  348. balloon.showStack( 'main' );
  349. } ).to.not.throw();
  350. } );
  351. it( 'should throw an error when there is no stack of given id', () => {
  352. expect( () => {
  353. balloon.showStack( 'second' );
  354. } ).to.throw( CKEditorError, 'contextualballoon-showstack-stack-not-exist: Cannot show not existing stack.' );
  355. } );
  356. } );
  357. describe( 'remove()', () => {
  358. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  359. balloon.view.isVisible = true;
  360. balloon.remove( viewA );
  361. expect( balloon.visibleView ).to.null;
  362. expect( balloon.view.isVisible ).to.false;
  363. } );
  364. it( 'should remove given view from not displayed stack', () => {
  365. balloon.add( {
  366. stackId: 'second',
  367. view: viewB
  368. } );
  369. balloon.add( {
  370. stackId: 'second',
  371. view: viewC
  372. } );
  373. balloon.remove( viewB );
  374. expect( balloon.visibleView ).to.equal( viewA );
  375. expect( () => {
  376. balloon.showStack( 'second' );
  377. } ).to.not.throw();
  378. } );
  379. it( 'should remove not displayed stack if a removed view was the only view in this stack', () => {
  380. balloon.add( {
  381. stackId: 'second',
  382. view: viewB
  383. } );
  384. balloon.remove( viewB );
  385. expect( balloon.visibleView ).to.equal( viewA );
  386. expect( () => {
  387. balloon.showStack( 'second' );
  388. } ).to.throw();
  389. } );
  390. it( 'should switch stack to the next one when removed view was the last one in the visible stack', () => {
  391. balloon.add( {
  392. stackId: 'second',
  393. view: viewB
  394. } );
  395. balloon.remove( viewA );
  396. expect( balloon.visibleView ).to.equal( viewB );
  397. expect( () => {
  398. balloon.showStack( 'main' );
  399. } ).to.throw();
  400. } );
  401. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  402. balloon.add( {
  403. view: viewB,
  404. position: {
  405. target: 'fake',
  406. limiter: balloon.positionLimiter
  407. }
  408. } );
  409. balloon.remove( viewB );
  410. expect( balloon.visibleView ).to.equal( viewA );
  411. } );
  412. it( 'should remove given view from the stack when view is not visible', () => {
  413. balloon.add( {
  414. view: viewB,
  415. position: {
  416. target: 'fake',
  417. limiter: balloon.positionLimiter
  418. }
  419. } );
  420. balloon.remove( viewA );
  421. expect( balloon.visibleView ).to.equal( viewB );
  422. } );
  423. it( 'should remove given view from a not currently visible stack', () => {
  424. balloon.add( {
  425. view: viewB,
  426. stackId: 'second',
  427. position: {
  428. target: 'fake'
  429. }
  430. } );
  431. balloon.add( {
  432. view: viewC,
  433. stackId: 'second',
  434. position: {
  435. target: 'fake'
  436. }
  437. } );
  438. balloon.remove( viewB );
  439. expect( balloon.hasView( viewB ) ).to.false;
  440. expect( balloon.hasView( viewC ) ).to.true;
  441. // Does not throw, so the stack is there.
  442. expect( () => {
  443. balloon.showStack( 'second' );
  444. } ).to.not.throw();
  445. } );
  446. it( 'should remove not displayed stack when removied view was the last one in the stack', () => {
  447. balloon.add( {
  448. view: viewB,
  449. stackId: 'second',
  450. position: {
  451. target: 'fake'
  452. }
  453. } );
  454. balloon.remove( viewB );
  455. expect( balloon.hasView( viewB ) ).to.false;
  456. // Does throw, so the stack is not there.
  457. expect( () => {
  458. balloon.showStack( 'second' );
  459. } ).to.throw();
  460. } );
  461. it( 'should throw an error when there is no given view in the stack', () => {
  462. expect( () => {
  463. balloon.remove( viewB );
  464. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  465. } );
  466. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  467. const view = new View();
  468. balloon.add( {
  469. view,
  470. position: {
  471. target: 'fake',
  472. limiter: balloon.positionLimiter
  473. },
  474. balloonClassName: 'foo'
  475. } );
  476. balloon.add( {
  477. view: viewB,
  478. position: {
  479. target: 'fake',
  480. limiter: balloon.positionLimiter
  481. },
  482. balloonClassName: 'bar'
  483. } );
  484. balloon.remove( viewB );
  485. expect( balloon.view.class ).to.equal( 'foo' );
  486. } );
  487. } );
  488. describe( 'updatePosition()', () => {
  489. it( 'should attach balloon to the target using position option from the last view in the stack', () => {
  490. balloon.add( {
  491. view: viewB,
  492. position: {
  493. target: 'other'
  494. }
  495. } );
  496. balloon.view.pin.resetHistory();
  497. balloon.updatePosition();
  498. expect( balloon.view.pin.calledOnce );
  499. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  500. target: 'other',
  501. limiter: balloon.positionLimiter
  502. } );
  503. } );
  504. it( 'should set given position to the currently visible view and use position from the first view in the stack #1', () => {
  505. balloon.view.pin.resetHistory();
  506. balloon.updatePosition( { target: 'new' } );
  507. expect( balloon.view.pin.calledOnce );
  508. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  509. target: 'new',
  510. limiter: balloon.positionLimiter
  511. } );
  512. } );
  513. it( 'should set given position to the currently visible view and use position from the first view in the stack #2', () => {
  514. balloon.add( {
  515. view: viewB,
  516. position: {
  517. target: 'other'
  518. }
  519. } );
  520. balloon.view.pin.resetHistory();
  521. balloon.updatePosition( { target: 'new' } );
  522. expect( balloon.view.pin.calledOnce );
  523. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  524. target: 'new',
  525. limiter: balloon.positionLimiter
  526. } );
  527. balloon.remove( viewA );
  528. balloon.updatePosition();
  529. expect( balloon.view.pin.calledTwice );
  530. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  531. target: 'new',
  532. limiter: balloon.positionLimiter
  533. } );
  534. } );
  535. it( 'should use a given position limiter instead of the default one', () => {
  536. balloon.view.pin.resetHistory();
  537. balloon.updatePosition( {
  538. target: 'new',
  539. limiter: 'customLimiter'
  540. } );
  541. expect( balloon.view.pin.calledOnce );
  542. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  543. target: 'new',
  544. limiter: 'customLimiter'
  545. } );
  546. } );
  547. it( 'should throw an error when there is no given view in the stack', () => {
  548. expect( () => {
  549. balloon.remove( viewB );
  550. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  551. } );
  552. } );
  553. describe( 'destroy()', () => {
  554. it( 'can be called multiple times', () => {
  555. expect( () => {
  556. balloon.destroy();
  557. balloon.destroy();
  558. } ).to.not.throw();
  559. } );
  560. it( 'should not touch the DOM', () => {
  561. balloon.destroy();
  562. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.not.equal( -1 );
  563. } );
  564. } );
  565. describe( 'rotator view', () => {
  566. let rotatorView;
  567. beforeEach( () => {
  568. rotatorView = balloon.view.content.get( 0 );
  569. } );
  570. it( 'should display navigation when there is more than one stack', () => {
  571. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  572. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.equal( true );
  573. balloon.add( {
  574. view: viewB,
  575. stackId: 'second'
  576. } );
  577. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.equal( false );
  578. } );
  579. it( 'should display counter', () => {
  580. const counterElement = rotatorView.element.querySelector( '.ck-balloon-rotator__counter' );
  581. expect( counterElement.textContent ).to.equal( '' );
  582. balloon.add( {
  583. view: viewB,
  584. stackId: 'second'
  585. } );
  586. expect( counterElement.textContent ).to.equal( '1 of 2' );
  587. balloon.showStack( 'second' );
  588. expect( counterElement.textContent ).to.equal( '2 of 2' );
  589. } );
  590. it( 'should switch stack to the next one after clicking next button', () => {
  591. balloon.add( {
  592. view: viewB,
  593. stackId: 'second'
  594. } );
  595. balloon.add( {
  596. view: viewC,
  597. stackId: 'third'
  598. } );
  599. expect( balloon.visibleView ).to.equal( viewA );
  600. rotatorView.buttonNextView.fire( 'execute' );
  601. expect( balloon.visibleView ).to.equal( viewB );
  602. rotatorView.buttonNextView.fire( 'execute' );
  603. expect( balloon.visibleView ).to.equal( viewC );
  604. rotatorView.buttonNextView.fire( 'execute' );
  605. expect( balloon.visibleView ).to.equal( viewA );
  606. } );
  607. it( 'should not move focus to the editable when switching not focused view to the next one', () => {
  608. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  609. balloon.add( {
  610. view: viewB,
  611. stackId: 'second'
  612. } );
  613. rotatorView.buttonNextView.fire( 'execute' );
  614. sinon.assert.notCalled( editableFocusSpy );
  615. } );
  616. it( 'should move focus to the editable when switching focused view to the next one', () => {
  617. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  618. balloon.add( {
  619. view: viewB,
  620. stackId: 'second'
  621. } );
  622. rotatorView.focusTracker.isFocused = true;
  623. rotatorView.buttonNextView.fire( 'execute' );
  624. sinon.assert.calledOnce( editableFocusSpy );
  625. } );
  626. it( 'should switch stack to the prev one after clicking prev button', () => {
  627. balloon.add( {
  628. view: viewB,
  629. stackId: 'second'
  630. } );
  631. balloon.add( {
  632. view: viewC,
  633. stackId: 'third'
  634. } );
  635. expect( balloon.visibleView ).to.equal( viewA );
  636. rotatorView.buttonPrevView.fire( 'execute' );
  637. expect( balloon.visibleView ).to.equal( viewC );
  638. rotatorView.buttonPrevView.fire( 'execute' );
  639. expect( balloon.visibleView ).to.equal( viewB );
  640. rotatorView.buttonPrevView.fire( 'execute' );
  641. expect( balloon.visibleView ).to.equal( viewA );
  642. } );
  643. it( 'should not move focus to the editable when switching not focused view to the prev one', () => {
  644. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  645. balloon.add( {
  646. view: viewB,
  647. stackId: 'second'
  648. } );
  649. rotatorView.buttonPrevView.fire( 'execute' );
  650. sinon.assert.notCalled( editableFocusSpy );
  651. } );
  652. it( 'should move focus to the editable when switching focused view to the prev one', () => {
  653. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  654. balloon.add( {
  655. view: viewB,
  656. stackId: 'second'
  657. } );
  658. rotatorView.focusTracker.isFocused = true;
  659. rotatorView.buttonPrevView.fire( 'execute' );
  660. sinon.assert.calledOnce( editableFocusSpy );
  661. } );
  662. it( 'should add hidden view with fake panels to editor body collection', () => {
  663. const fakePanelsView = editor.ui.view.body.last;
  664. expect( fakePanelsView.element.classList.contains( 'ck-fake-panel' ) ).to.equal( true );
  665. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( true );
  666. expect( fakePanelsView.element.childElementCount ).to.equal( 0 );
  667. } );
  668. it( 'should show fake panels when more than one stack is added to the balloon (max to 2 panels)', () => {
  669. const fakePanelsView = editor.ui.view.body.last;
  670. const viewD = new View();
  671. balloon.add( {
  672. view: viewB,
  673. stackId: 'second'
  674. } );
  675. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  676. expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
  677. balloon.add( {
  678. view: viewC,
  679. stackId: 'third'
  680. } );
  681. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  682. expect( fakePanelsView.element.childElementCount ).to.equal( 2 );
  683. balloon.add( {
  684. view: viewD,
  685. stackId: 'fourth'
  686. } );
  687. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  688. expect( fakePanelsView.element.childElementCount ).to.equal( 2 );
  689. balloon.remove( viewD );
  690. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  691. expect( fakePanelsView.element.childElementCount ).to.equal( 2 );
  692. balloon.remove( viewC );
  693. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  694. expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
  695. balloon.remove( viewB );
  696. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( true );
  697. expect( fakePanelsView.element.childElementCount ).to.equal( 0 );
  698. } );
  699. it( 'should keep position of fake panels up to date with balloon position when panels are visible', () => {
  700. const fakePanelsView = editor.ui.view.body.last;
  701. let width = 30;
  702. let height = 40;
  703. balloon.view.top = 10;
  704. balloon.view.left = 20;
  705. sinon.stub( balloon.view.element, 'getBoundingClientRect' ).callsFake( () => ( { width, height } ) );
  706. balloon.add( {
  707. view: viewB,
  708. stackId: 'second'
  709. } );
  710. expect( fakePanelsView.element.style.top ).to.equal( '10px' );
  711. expect( fakePanelsView.element.style.left ).to.equal( '20px' );
  712. expect( fakePanelsView.element.style.width ).to.equal( '30px' );
  713. expect( fakePanelsView.element.style.height ).to.equal( '40px' );
  714. balloon.view.top = 15;
  715. balloon.view.left = 25;
  716. width = 35;
  717. height = 45;
  718. balloon.add( {
  719. view: viewC,
  720. stackId: 'third'
  721. } );
  722. expect( fakePanelsView.element.style.top ).to.equal( '15px' );
  723. expect( fakePanelsView.element.style.left ).to.equal( '25px' );
  724. expect( fakePanelsView.element.style.width ).to.equal( '35px' );
  725. expect( fakePanelsView.element.style.height ).to.equal( '45px' );
  726. balloon.view.top = 10;
  727. balloon.view.left = 20;
  728. width = 30;
  729. height = 40;
  730. balloon.updatePosition();
  731. expect( fakePanelsView.element.style.top ).to.equal( '10px' );
  732. expect( fakePanelsView.element.style.left ).to.equal( '20px' );
  733. expect( fakePanelsView.element.style.width ).to.equal( '30px' );
  734. expect( fakePanelsView.element.style.height ).to.equal( '40px' );
  735. // Hide fake panels by removing additional stacks.
  736. balloon.remove( viewC );
  737. balloon.remove( viewB );
  738. balloon.view.top = 15;
  739. balloon.view.left = 25;
  740. width = 35;
  741. height = 45;
  742. balloon.updatePosition();
  743. // Old values because fake panels are hidden.
  744. expect( fakePanelsView.element.style.top ).to.equal( '10px' );
  745. expect( fakePanelsView.element.style.left ).to.equal( '20px' );
  746. expect( fakePanelsView.element.style.width ).to.equal( '30px' );
  747. expect( fakePanelsView.element.style.height ).to.equal( '40px' );
  748. } );
  749. describe( 'singleViewMode', () => {
  750. it( 'should not display navigation when there is more than one stack', () => {
  751. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  752. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  753. balloon.add( {
  754. view: viewB,
  755. stackId: 'second',
  756. singleViewMode: true
  757. } );
  758. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  759. } );
  760. it( 'should hide display navigation after adding view', () => {
  761. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  762. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  763. balloon.add( {
  764. view: viewB,
  765. stackId: 'second'
  766. } );
  767. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  768. balloon.add( {
  769. view: viewC,
  770. stackId: 'third',
  771. singleViewMode: true
  772. } );
  773. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  774. } );
  775. it( 'should display navigation after removing a view', () => {
  776. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  777. balloon.add( {
  778. view: viewB,
  779. stackId: 'second'
  780. } );
  781. balloon.add( {
  782. view: viewC,
  783. stackId: 'third',
  784. singleViewMode: true
  785. } );
  786. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  787. balloon.remove( viewC );
  788. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  789. } );
  790. it( 'should not display navigation after removing a view if there is still some view with singleViewMode', () => {
  791. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  792. balloon.add( {
  793. view: viewB,
  794. stackId: 'second'
  795. } );
  796. balloon.add( {
  797. view: viewC,
  798. stackId: 'third',
  799. singleViewMode: true
  800. } );
  801. balloon.add( {
  802. view: viewD,
  803. stackId: 'third',
  804. singleViewMode: true
  805. } );
  806. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  807. balloon.remove( viewD );
  808. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  809. balloon.remove( viewC );
  810. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  811. } );
  812. it( 'should not show fake panels when more than one stack is added to the balloon (max to 2 panels)', () => {
  813. const fakePanelsView = editor.ui.view.body.last;
  814. balloon.add( {
  815. view: viewB,
  816. stackId: 'second'
  817. } );
  818. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  819. expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
  820. balloon.add( {
  821. view: viewC,
  822. stackId: 'third',
  823. singleViewMode: true
  824. } );
  825. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  826. expect( fakePanelsView.element.childElementCount ).to.equal( 0 );
  827. balloon.remove( viewC );
  828. expect( fakePanelsView.element.classList.contains( 'ck-hidden' ) ).to.equal( false );
  829. expect( fakePanelsView.element.childElementCount ).to.equal( 1 );
  830. balloon.remove( viewB );
  831. } );
  832. it( 'should switch visible view when adding a view to new stack', () => {
  833. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  834. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  835. balloon.add( {
  836. view: viewB,
  837. stackId: 'second'
  838. } );
  839. expect( balloon.visibleView ).to.equal( viewA );
  840. balloon.add( {
  841. view: viewC,
  842. stackId: 'third',
  843. singleViewMode: true
  844. } );
  845. expect( balloon.visibleView ).to.equal( viewC );
  846. const viewD = new View();
  847. balloon.add( {
  848. view: viewD,
  849. stackId: 'fifth',
  850. singleViewMode: true
  851. } );
  852. expect( balloon.visibleView ).to.equal( viewD );
  853. } );
  854. it( 'should switch visible view when adding a view to the same stack', () => {
  855. const navigationElement = rotatorView.element.querySelector( '.ck-balloon-rotator__navigation' );
  856. expect( navigationElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  857. balloon.add( {
  858. view: viewB,
  859. stackId: 'second'
  860. } );
  861. expect( balloon.visibleView ).to.equal( viewA );
  862. balloon.add( {
  863. view: viewC,
  864. stackId: 'third',
  865. singleViewMode: true
  866. } );
  867. expect( balloon.visibleView ).to.equal( viewC );
  868. const viewD = new View();
  869. balloon.add( {
  870. view: viewD,
  871. stackId: 'third',
  872. singleViewMode: true
  873. } );
  874. expect( balloon.visibleView ).to.equal( viewD );
  875. } );
  876. } );
  877. } );
  878. } );