contextualballoon.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.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 doc, viewDocument, root;
  64. beforeEach( () => {
  65. doc = editor.document;
  66. viewDocument = editor.editing.view;
  67. root = viewDocument.getRoot();
  68. } );
  69. it( 'obtains the root of the selection', () => {
  70. setModelData( doc, '<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. doc.schema.registerItem( 'widget' );
  79. doc.schema.registerItem( 'nestededitable' );
  80. doc.schema.objects.add( 'widget' );
  81. doc.schema.allow( { name: 'widget', inside: '$root' } );
  82. doc.schema.allow( { name: 'nestededitable', inside: 'widget' } );
  83. doc.schema.allow( { name: '$inline', inside: 'nestededitable' } );
  84. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView )
  85. .fromElement( 'widget' )
  86. .toElement( () => new ViewContainerElement( 'figure', { contenteditable: 'false' } ) );
  87. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView )
  88. .fromElement( 'nestededitable' )
  89. .toElement( () => new ViewEditableElement( 'figcaption', { contenteditable: 'true' } ) );
  90. setModelData( doc, '<widget><nestededitable>[]foo</nestededitable></widget>' );
  91. expect( balloon.positionLimiter() ).to.equal( viewDocument.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 keep balloon at the same position after adding next view', () => {
  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: 'fake',
  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.className ).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.className ).to.equal( 'bar' );
  245. } );
  246. } );
  247. describe( 'visibleView', () => {
  248. it( 'should return data of currently visible view', () => {
  249. expect( balloon.visibleView ).to.equal( viewA );
  250. } );
  251. it( 'should return data of currently visible view when there is more than one in the stack', () => {
  252. balloon.add( {
  253. view: viewB,
  254. position: {
  255. target: 'fake',
  256. limiter: balloon.positionLimiter
  257. }
  258. } );
  259. expect( balloon.visibleView ).to.equal( viewB );
  260. } );
  261. it( 'should return `null` when the stack is empty', () => {
  262. balloon.remove( viewA );
  263. expect( balloon.visibleView ).to.null;
  264. } );
  265. } );
  266. describe( 'remove()', () => {
  267. it( 'should remove given view and hide balloon when there is no other view to display', () => {
  268. balloon.view.isVisible = true;
  269. balloon.remove( viewA );
  270. expect( balloon.visibleView ).to.null;
  271. expect( balloon.view.isVisible ).to.false;
  272. } );
  273. it( 'should remove given view and set preceding in the stack as visible when removed view was visible', () => {
  274. balloon.add( {
  275. view: viewB,
  276. position: {
  277. target: 'fake',
  278. limiter: balloon.positionLimiter
  279. }
  280. } );
  281. balloon.remove( viewB );
  282. expect( balloon.visibleView ).to.equal( viewA );
  283. } );
  284. it( 'should remove given view from the stack when view is not visible', () => {
  285. balloon.add( {
  286. view: viewB,
  287. position: {
  288. target: 'fake',
  289. limiter: balloon.positionLimiter
  290. }
  291. } );
  292. balloon.remove( viewA );
  293. expect( balloon.visibleView ).to.equal( viewB );
  294. } );
  295. it( 'should throw an error when there is no given view in the stack', () => {
  296. expect( () => {
  297. balloon.remove( viewB );
  298. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  299. } );
  300. it( 'should set additional css class of visible view to BalloonPanelView', () => {
  301. const view = new View();
  302. balloon.add( {
  303. view,
  304. position: {
  305. target: 'fake',
  306. limiter: balloon.positionLimiter
  307. },
  308. balloonClassName: 'foo'
  309. } );
  310. balloon.add( {
  311. view: viewB,
  312. position: {
  313. target: 'fake',
  314. limiter: balloon.positionLimiter
  315. },
  316. balloonClassName: 'bar'
  317. } );
  318. balloon.remove( viewB );
  319. expect( balloon.view.className ).to.equal( 'foo' );
  320. } );
  321. } );
  322. describe( 'updatePosition()', () => {
  323. it( 'should attach balloon to the target using position option from the first view in the stack', () => {
  324. balloon.add( {
  325. view: viewB,
  326. position: {
  327. target: 'other'
  328. }
  329. } );
  330. balloon.view.pin.reset();
  331. balloon.updatePosition();
  332. expect( balloon.view.pin.calledOnce );
  333. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  334. target: 'fake',
  335. limiter: balloon.positionLimiter
  336. } );
  337. } );
  338. it( 'should set given position to the currently visible view and use position from the first view in the stack #1', () => {
  339. balloon.view.pin.reset();
  340. balloon.updatePosition( { target: 'new' } );
  341. expect( balloon.view.pin.calledOnce );
  342. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  343. target: 'new',
  344. limiter: balloon.positionLimiter
  345. } );
  346. } );
  347. it( 'should set given position to the currently visible view and use position from the first view in the stack #2', () => {
  348. balloon.add( {
  349. view: viewB,
  350. position: {
  351. target: 'other'
  352. }
  353. } );
  354. balloon.view.pin.reset();
  355. balloon.updatePosition( { target: 'new' } );
  356. expect( balloon.view.pin.calledOnce );
  357. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  358. target: 'fake',
  359. limiter: balloon.positionLimiter
  360. } );
  361. balloon.remove( viewA );
  362. balloon.updatePosition();
  363. expect( balloon.view.pin.calledTwice );
  364. sinon.assert.calledWithMatch( balloon.view.pin.secondCall, {
  365. target: 'new',
  366. limiter: balloon.positionLimiter
  367. } );
  368. } );
  369. it( 'should use a given position limiter instead of the default one', () => {
  370. balloon.view.pin.reset();
  371. balloon.updatePosition( {
  372. target: 'new',
  373. limiter: 'customLimiter'
  374. } );
  375. expect( balloon.view.pin.calledOnce );
  376. sinon.assert.calledWithMatch( balloon.view.pin.firstCall, {
  377. target: 'new',
  378. limiter: 'customLimiter'
  379. } );
  380. } );
  381. it( 'should throw an error when there is no given view in the stack', () => {
  382. expect( () => {
  383. balloon.remove( viewB );
  384. } ).to.throw( CKEditorError, /^contextualballoon-remove-view-not-exist/ );
  385. } );
  386. } );
  387. describe( 'destroy()', () => {
  388. it( 'can be called multiple times', () => {
  389. expect( () => {
  390. balloon.destroy();
  391. balloon.destroy();
  392. } ).to.not.throw();
  393. } );
  394. it( 'should not touch the DOM', () => {
  395. balloon.destroy();
  396. expect( editor.ui.view.body.getIndex( balloon.view ) ).to.not.equal( -1 );
  397. } );
  398. } );
  399. } );