contextualballoon.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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;
  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. // Add viewA to the pane and init viewB.
  33. balloon.add( {
  34. view: viewA,
  35. position: {
  36. target: 'fake'
  37. }
  38. } );
  39. viewB.render();
  40. } );
  41. } );
  42. afterEach( () => {
  43. editor.destroy();
  44. editorElement.remove();
  45. } );
  46. it( 'should create a plugin instance', () => {
  47. expect( balloon ).to.instanceof( Plugin );
  48. expect( balloon ).to.instanceof( ContextualBalloon );
  49. } );
  50. describe( 'pluginName', () => {
  51. it( 'should return plugin by name', () => {
  52. expect( editor.plugins.get( 'ContextualBalloon' ) ).to.equal( balloon );
  53. } );
  54. } );
  55. describe( 'init()', () => {
  56. it( 'should create a plugin instance with properties', () => {
  57. expect( balloon.view ).to.instanceof( BalloonPanelView );
  58. } );
  59. describe( 'positionLimiter', () => {
  60. let model, view, viewDocument, root;
  61. beforeEach( () => {
  62. model = editor.model;
  63. view = editor.editing.view;
  64. viewDocument = view.document;
  65. root = viewDocument.getRoot();
  66. } );
  67. it( 'obtains the root of the selection', () => {
  68. setModelData( model, '<paragraph>[]bar</paragraph>' );
  69. expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
  70. } );
  71. it( 'does not fail if selection has no #editableElement', () => {
  72. sinon.stub( viewDocument.selection, 'editableElement' ).value( null );
  73. expect( balloon.positionLimiter() ).to.equal( null );
  74. } );
  75. it( 'obtains the farthest root of the selection (nested editable)', () => {
  76. model.schema.register( 'widget', {
  77. allowIn: '$root',
  78. isObject: true
  79. } );
  80. model.schema.register( 'nestedEditable', { allowIn: 'widget' } );
  81. model.schema.extend( '$text', { allowIn: 'nestedEditable' } );
  82. editor.conversion.for( 'downcast' ).elementToElement( {
  83. model: 'widget',
  84. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'figure', { contenteditable: 'false' } )
  85. } );
  86. editor.conversion.for( 'downcast' ).elementToElement( {
  87. model: 'nestedEditable',
  88. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'figcaption', { contenteditable: 'true' } )
  89. } );
  90. setModelData( model, '<widget><nestedEditable>[]foo</nestedEditable></widget>' );
  91. expect( balloon.positionLimiter() ).to.equal( view.domConverter.mapViewToDom( root ) );
  92. } );
  93. } );
  94. it( 'should add balloon panel view to editor `body` collection', () => {
  95. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.above( -1 );
  96. } );
  97. it( 'should register balloon panel element in editor.ui#focusTracker', () => {
  98. editor.ui.focusTracker.isFocused = false;
  99. balloon.add( {
  100. view: viewB,
  101. position: {
  102. target: 'fake',
  103. limiter: balloon.positionLimiter
  104. }
  105. } );
  106. balloon.view.element.dispatchEvent( new Event( 'focus' ) );
  107. expect( editor.ui.focusTracker.isFocused ).to.true;
  108. } );
  109. } );
  110. describe( 'hasView()', () => {
  111. it( 'should return true when given view is in stack', () => {
  112. expect( balloon.hasView( viewA ) ).to.true;
  113. } );
  114. it( 'should return true when given view is in stack but is not visible', () => {
  115. balloon.add( {
  116. view: viewB,
  117. position: {
  118. target: 'fake',
  119. limiter: balloon.positionLimiter
  120. }
  121. } );
  122. expect( balloon.visibleView ).to.equal( viewB );
  123. expect( balloon.hasView( viewA ) ).to.true;
  124. } );
  125. it( 'should return false when given view is not in stack', () => {
  126. expect( balloon.hasView( viewB ) ).to.false;
  127. } );
  128. } );
  129. describe( 'add()', () => {
  130. it( 'should add view to the stack and display in balloon attached using given position options', () => {
  131. expect( balloon.view.content.length ).to.equal( 1 );
  132. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  133. expect( balloon.view.pin.calledOnce ).to.true;
  134. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  135. target: 'fake',
  136. limiter: balloon.positionLimiter
  137. } );
  138. } );
  139. it( 'should use a provided limiter instead of #positionLimiter', () => {
  140. balloon.remove( viewA );
  141. balloon.view.pin.resetHistory();
  142. balloon.add( {
  143. view: viewB,
  144. position: {
  145. target: 'foo',
  146. limiter: 'customLimiter'
  147. }
  148. } );
  149. sinon.assert.calledWithMatch( balloon.view.pin, {
  150. target: 'foo',
  151. limiter: 'customLimiter'
  152. } );
  153. } );
  154. it( 'should use a custom #positionLimiter', () => {
  155. balloon.remove( viewA );
  156. balloon.view.pin.resetHistory();
  157. balloon.positionLimiter = 'customLimiter';
  158. balloon.add( {
  159. view: viewB,
  160. position: {
  161. target: 'foo',
  162. }
  163. } );
  164. sinon.assert.calledWithMatch( balloon.view.pin, {
  165. target: 'foo',
  166. limiter: 'customLimiter'
  167. } );
  168. } );
  169. it( 'should not alter the view data if no limiter is provided and the #positionLimiter is used', () => {
  170. const data = {
  171. view: viewB,
  172. position: {
  173. target: 'foo',
  174. }
  175. };
  176. balloon.remove( viewA );
  177. balloon.add( data );
  178. expect( data ).to.deep.equal( {
  179. view: viewB,
  180. position: {
  181. target: 'foo'
  182. }
  183. } );
  184. } );
  185. it( 'should pin balloon to the target element', () => {
  186. sinon.assert.calledOnce( balloon.view.pin );
  187. } );
  188. it( 'should throw an error when try to add the same view more than once', () => {
  189. expect( () => {
  190. balloon.add( {
  191. view: viewA,
  192. position: {
  193. target: 'fake',
  194. limiter: balloon.positionLimiter
  195. }
  196. } );
  197. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  198. } );
  199. it( 'should add multiple views to he stack and display last one', () => {
  200. balloon.add( {
  201. view: viewB,
  202. position: {
  203. target: 'fake',
  204. limiter: balloon.positionLimiter
  205. }
  206. } );
  207. expect( balloon.view.content.length ).to.equal( 1 );
  208. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  209. } );
  210. it( 'should use the position of the last view in the stack', () => {
  211. balloon.add( {
  212. view: viewB,
  213. position: { target: 'other' }
  214. } );
  215. expect( balloon.view.pin.calledTwice ).to.true;
  216. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  217. target: 'fake',
  218. limiter: balloon.positionLimiter
  219. } );
  220. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  221. target: 'other',
  222. limiter: balloon.positionLimiter
  223. } );
  224. } );
  225. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  226. const view = new View();
  227. balloon.add( {
  228. view,
  229. position: {
  230. target: 'fake',
  231. limiter: balloon.positionLimiter
  232. },
  233. balloonClassName: 'foo'
  234. } );
  235. expect( balloon.view.class ).to.equal( 'foo' );
  236. balloon.add( {
  237. view: viewB,
  238. position: {
  239. target: 'fake',
  240. limiter: balloon.positionLimiter
  241. },
  242. balloonClassName: 'bar'
  243. } );
  244. expect( balloon.view.class ).to.equal( 'bar' );
  245. } );
  246. it( 'should hide arrow if `withArrow` option is set to false', () => {
  247. balloon.remove( viewA );
  248. balloon.view.pin.resetHistory();
  249. balloon.add( {
  250. view: viewB,
  251. position: {
  252. target: 'foo'
  253. },
  254. withArrow: false
  255. } );
  256. expect( balloon.view.withArrow ).to.be.false;
  257. } );
  258. it( 'should show arrow if `withArrow` option was not set and previously shown view had hidden arrow', () => {
  259. balloon.remove( viewA );
  260. balloon.view.pin.resetHistory();
  261. balloon.add( {
  262. view: viewB,
  263. position: {
  264. target: 'foo'
  265. },
  266. withArrow: false
  267. } );
  268. expect( balloon.view.withArrow ).to.be.false;
  269. balloon.remove( viewB );
  270. balloon.add( {
  271. view: viewB,
  272. position: {
  273. target: 'foo'
  274. }
  275. } );
  276. expect( balloon.view.withArrow ).to.be.true;
  277. } );
  278. } );
  279. describe( 'visibleView', () => {
  280. it( 'should return data of currently visible view', () => {
  281. expect( balloon.visibleView ).to.equal( viewA );
  282. } );
  283. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  284. balloon.add( {
  285. view: viewB,
  286. position: {
  287. target: 'fake',
  288. limiter: balloon.positionLimiter
  289. }
  290. } );
  291. expect( balloon.visibleView ).to.equal( viewB );
  292. } );
  293. it( 'should return `null` when the stack is empty', () => {
  294. balloon.remove( viewA );
  295. expect( balloon.visibleView ).to.null;
  296. } );
  297. } );
  298. describe( 'remove()', () => {
  299. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  300. balloon.view.isVisible = true;
  301. balloon.remove( viewA );
  302. expect( balloon.visibleView ).to.null;
  303. expect( balloon.view.isVisible ).to.false;
  304. } );
  305. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  306. balloon.add( {
  307. view: viewB,
  308. position: {
  309. target: 'fake',
  310. limiter: balloon.positionLimiter
  311. }
  312. } );
  313. balloon.remove( viewB );
  314. expect( balloon.visibleView ).to.equal( viewA );
  315. } );
  316. it( 'should remove given view from the stack when view is not visible', () => {
  317. balloon.add( {
  318. view: viewB,
  319. position: {
  320. target: 'fake',
  321. limiter: balloon.positionLimiter
  322. }
  323. } );
  324. balloon.remove( viewA );
  325. expect( balloon.visibleView ).to.equal( viewB );
  326. } );
  327. it( 'should throw an error when there is no given view in the stack', () => {
  328. expect( () => {
  329. balloon.remove( viewB );
  330. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  331. } );
  332. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  333. const view = new View();
  334. balloon.add( {
  335. view,
  336. position: {
  337. target: 'fake',
  338. limiter: balloon.positionLimiter
  339. },
  340. balloonClassName: 'foo'
  341. } );
  342. balloon.add( {
  343. view: viewB,
  344. position: {
  345. target: 'fake',
  346. limiter: balloon.positionLimiter
  347. },
  348. balloonClassName: 'bar'
  349. } );
  350. balloon.remove( viewB );
  351. expect( balloon.view.class ).to.equal( 'foo' );
  352. } );
  353. } );
  354. describe( 'updatePosition()', () => {
  355. it( 'should attach balloon to the target using position option from the last view in the stack', () => {
  356. balloon.add( {
  357. view: viewB,
  358. position: {
  359. target: 'other'
  360. }
  361. } );
  362. balloon.view.pin.resetHistory();
  363. balloon.updatePosition();
  364. expect( balloon.view.pin.calledOnce );
  365. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  366. target: 'other',
  367. limiter: balloon.positionLimiter
  368. } );
  369. } );
  370. it( 'should set given position to the currently visible view and use position from the first view in the stack #1', () => {
  371. balloon.view.pin.resetHistory();
  372. balloon.updatePosition( { target: 'new' } );
  373. expect( balloon.view.pin.calledOnce );
  374. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  375. target: 'new',
  376. limiter: balloon.positionLimiter
  377. } );
  378. } );
  379. it( 'should set given position to the currently visible view and use position from the first view in the stack #2', () => {
  380. balloon.add( {
  381. view: viewB,
  382. position: {
  383. target: 'other'
  384. }
  385. } );
  386. balloon.view.pin.resetHistory();
  387. balloon.updatePosition( { target: 'new' } );
  388. expect( balloon.view.pin.calledOnce );
  389. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  390. target: 'new',
  391. limiter: balloon.positionLimiter
  392. } );
  393. balloon.remove( viewA );
  394. balloon.updatePosition();
  395. expect( balloon.view.pin.calledTwice );
  396. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  397. target: 'new',
  398. limiter: balloon.positionLimiter
  399. } );
  400. } );
  401. it( 'should use a given position limiter instead of the default one', () => {
  402. balloon.view.pin.resetHistory();
  403. balloon.updatePosition( {
  404. target: 'new',
  405. limiter: 'customLimiter'
  406. } );
  407. expect( balloon.view.pin.calledOnce );
  408. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  409. target: 'new',
  410. limiter: 'customLimiter'
  411. } );
  412. } );
  413. it( 'should throw an error when there is no given view in the stack', () => {
  414. expect( () => {
  415. balloon.remove( viewB );
  416. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  417. } );
  418. } );
  419. describe( 'destroy()', () => {
  420. it( 'can be called multiple times', () => {
  421. expect( () => {
  422. balloon.destroy();
  423. balloon.destroy();
  424. } ).to.not.throw();
  425. } );
  426. it( 'should not touch the DOM', () => {
  427. balloon.destroy();
  428. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.not.equal( -1 );
  429. } );
  430. } );
  431. } );