contextualballoon.js 13 KB

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