8
0

contextualballoon.js 13 KB

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