contextualballoon.js 31 KB

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