contextualballoon.js 29 KB

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