contextualballoon.js 13 KB

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