8
0

contextualballoon.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  13. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. /* global document, Event */
  15. describe( 'ContextualBalloon', () => {
  16. let editor, editorElement, balloon, viewA, viewB;
  17. beforeEach( () => {
  18. editorElement = document.createElement( 'div' );
  19. document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ Paragraph, ContextualBalloon ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. balloon = editor.plugins.get( ContextualBalloon );
  27. // We don't need to execute BalloonPanel pin and attachTo methods
  28. // it's enough to check if was called with the proper data.
  29. sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  30. sinon.stub( balloon.view, 'pin' ).returns( {} );
  31. viewA = new View();
  32. viewB = 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( 'init()', () => {
  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' ).add( downcastElementToElement( {
  84. model: 'widget',
  85. view: ( modelElement, viewWriter ) => viewWriter.createContainerElement( 'figure', { contenteditable: 'false' } )
  86. } ) );
  87. editor.conversion.for( 'downcast' ).add( downcastElementToElement( {
  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 stack and display in balloon attached using given position options', () => {
  132. expect( balloon.view.content.length ).to.equal( 1 );
  133. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewA );
  134. expect( balloon.view.pin.calledOnce ).to.true;
  135. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  136. target: 'fake',
  137. limiter: balloon.positionLimiter
  138. } );
  139. } );
  140. it( 'should use a provided limiter instead of #positionLimiter', () => {
  141. balloon.remove( viewA );
  142. balloon.view.pin.resetHistory();
  143. balloon.add( {
  144. view: viewB,
  145. position: {
  146. target: 'foo',
  147. limiter: 'customLimiter'
  148. }
  149. } );
  150. sinon.assert.calledWithMatch( balloon.view.pin, {
  151. target: 'foo',
  152. limiter: 'customLimiter'
  153. } );
  154. } );
  155. it( 'should use a custom #positionLimiter', () => {
  156. balloon.remove( viewA );
  157. balloon.view.pin.resetHistory();
  158. balloon.positionLimiter = 'customLimiter';
  159. balloon.add( {
  160. view: viewB,
  161. position: {
  162. target: 'foo',
  163. }
  164. } );
  165. sinon.assert.calledWithMatch( balloon.view.pin, {
  166. target: 'foo',
  167. limiter: 'customLimiter'
  168. } );
  169. } );
  170. it( 'should not alter the view data if no limiter is provided and the #positionLimiter is used', () => {
  171. const data = {
  172. view: viewB,
  173. position: {
  174. target: 'foo',
  175. }
  176. };
  177. balloon.remove( viewA );
  178. balloon.add( data );
  179. expect( data ).to.deep.equal( {
  180. view: viewB,
  181. position: {
  182. target: 'foo'
  183. }
  184. } );
  185. } );
  186. it( 'should pin balloon to the target element', () => {
  187. sinon.assert.calledOnce( balloon.view.pin );
  188. } );
  189. it( 'should throw an error when try to add the same view more than once', () => {
  190. expect( () => {
  191. balloon.add( {
  192. view: viewA,
  193. position: {
  194. target: 'fake',
  195. limiter: balloon.positionLimiter
  196. }
  197. } );
  198. } ).to.throw( CKEditorError, /^contextualballoon-add-view-exist/ );
  199. } );
  200. it( 'should add multiple views to he stack and display last one', () => {
  201. balloon.add( {
  202. view: viewB,
  203. position: {
  204. target: 'fake',
  205. limiter: balloon.positionLimiter
  206. }
  207. } );
  208. expect( balloon.view.content.length ).to.equal( 1 );
  209. expect( balloon.view.content.get( 0 ) ).to.deep.equal( viewB );
  210. } );
  211. it( 'should use the position of the last view in the stack', () => {
  212. balloon.add( {
  213. view: viewB,
  214. position: { target: 'other' }
  215. } );
  216. expect( balloon.view.pin.calledTwice ).to.true;
  217. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  218. target: 'fake',
  219. limiter: balloon.positionLimiter
  220. } );
  221. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  222. target: 'other',
  223. limiter: balloon.positionLimiter
  224. } );
  225. } );
  226. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  227. const view = new View();
  228. balloon.add( {
  229. view,
  230. position: {
  231. target: 'fake',
  232. limiter: balloon.positionLimiter
  233. },
  234. balloonClassName: 'foo'
  235. } );
  236. expect( balloon.view.className ).to.equal( 'foo' );
  237. balloon.add( {
  238. view: viewB,
  239. position: {
  240. target: 'fake',
  241. limiter: balloon.positionLimiter
  242. },
  243. balloonClassName: 'bar'
  244. } );
  245. expect( balloon.view.className ).to.equal( 'bar' );
  246. } );
  247. } );
  248. describe( 'visibleView', () => {
  249. it( 'should return data of currently visible view', () => {
  250. expect( balloon.visibleView ).to.equal( viewA );
  251. } );
  252. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  253. balloon.add( {
  254. view: viewB,
  255. position: {
  256. target: 'fake',
  257. limiter: balloon.positionLimiter
  258. }
  259. } );
  260. expect( balloon.visibleView ).to.equal( viewB );
  261. } );
  262. it( 'should return `null` when the stack is empty', () => {
  263. balloon.remove( viewA );
  264. expect( balloon.visibleView ).to.null;
  265. } );
  266. } );
  267. describe( 'remove()', () => {
  268. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  269. balloon.view.isVisible = true;
  270. balloon.remove( viewA );
  271. expect( balloon.visibleView ).to.null;
  272. expect( balloon.view.isVisible ).to.false;
  273. } );
  274. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  275. balloon.add( {
  276. view: viewB,
  277. position: {
  278. target: 'fake',
  279. limiter: balloon.positionLimiter
  280. }
  281. } );
  282. balloon.remove( viewB );
  283. expect( balloon.visibleView ).to.equal( viewA );
  284. } );
  285. it( 'should remove given view from the stack when view is not visible', () => {
  286. balloon.add( {
  287. view: viewB,
  288. position: {
  289. target: 'fake',
  290. limiter: balloon.positionLimiter
  291. }
  292. } );
  293. balloon.remove( viewA );
  294. expect( balloon.visibleView ).to.equal( viewB );
  295. } );
  296. it( 'should throw an error when there is no given view in the stack', () => {
  297. expect( () => {
  298. balloon.remove( viewB );
  299. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  300. } );
  301. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  302. const view = new View();
  303. balloon.add( {
  304. view,
  305. position: {
  306. target: 'fake',
  307. limiter: balloon.positionLimiter
  308. },
  309. balloonClassName: 'foo'
  310. } );
  311. balloon.add( {
  312. view: viewB,
  313. position: {
  314. target: 'fake',
  315. limiter: balloon.positionLimiter
  316. },
  317. balloonClassName: 'bar'
  318. } );
  319. balloon.remove( viewB );
  320. expect( balloon.view.className ).to.equal( 'foo' );
  321. } );
  322. } );
  323. describe( 'updatePosition()', () => {
  324. it( 'should attach balloon to the target using position option from the last view in the stack', () => {
  325. balloon.add( {
  326. view: viewB,
  327. position: {
  328. target: 'other'
  329. }
  330. } );
  331. balloon.view.pin.resetHistory();
  332. balloon.updatePosition();
  333. expect( balloon.view.pin.calledOnce );
  334. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  335. target: 'other',
  336. limiter: balloon.positionLimiter
  337. } );
  338. } );
  339. it( 'should set given position to the currently visible view and use position from the first view in the stack #1', () => {
  340. balloon.view.pin.resetHistory();
  341. balloon.updatePosition( { target: 'new' } );
  342. expect( balloon.view.pin.calledOnce );
  343. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  344. target: 'new',
  345. limiter: balloon.positionLimiter
  346. } );
  347. } );
  348. it( 'should set given position to the currently visible view and use position from the first view in the stack #2', () => {
  349. balloon.add( {
  350. view: viewB,
  351. position: {
  352. target: 'other'
  353. }
  354. } );
  355. balloon.view.pin.resetHistory();
  356. balloon.updatePosition( { target: 'new' } );
  357. expect( balloon.view.pin.calledOnce );
  358. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  359. target: 'new',
  360. limiter: balloon.positionLimiter
  361. } );
  362. balloon.remove( viewA );
  363. balloon.updatePosition();
  364. expect( balloon.view.pin.calledTwice );
  365. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  366. target: 'new',
  367. limiter: balloon.positionLimiter
  368. } );
  369. } );
  370. it( 'should use a given position limiter instead of the default one', () => {
  371. balloon.view.pin.resetHistory();
  372. balloon.updatePosition( {
  373. target: 'new',
  374. limiter: 'customLimiter'
  375. } );
  376. expect( balloon.view.pin.calledOnce );
  377. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  378. target: 'new',
  379. limiter: 'customLimiter'
  380. } );
  381. } );
  382. it( 'should throw an error when there is no given view in the stack', () => {
  383. expect( () => {
  384. balloon.remove( viewB );
  385. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  386. } );
  387. } );
  388. describe( 'destroy()', () => {
  389. it( 'can be called multiple times', () => {
  390. expect( () => {
  391. balloon.destroy();
  392. balloon.destroy();
  393. } ).to.not.throw();
  394. } );
  395. it( 'should not touch the DOM', () => {
  396. balloon.destroy();
  397. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.not.equal( -1 );
  398. } );
  399. } );
  400. } );