8
0

balloonpanelview.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document, Event */
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import ViewCollection from '../../../src/viewcollection';
  8. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  9. import ButtonView from '../../../src/button/buttonview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import * as positionUtils from '@ckeditor/ckeditor5-utils/src/dom/position';
  12. testUtils.createSinonSandbox();
  13. describe( 'BalloonPanelView', () => {
  14. let view, windowStub;
  15. beforeEach( () => {
  16. view = new BalloonPanelView();
  17. view.set( 'maxWidth', 200 );
  18. return view.init();
  19. } );
  20. afterEach( () => {
  21. if ( view ) {
  22. return view.destroy();
  23. }
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should create element from template', () => {
  27. expect( view.element.tagName ).to.equal( 'DIV' );
  28. expect( view.element.classList.contains( 'ck-balloon-panel' ) ).to.true;
  29. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  30. } );
  31. it( 'should set default values', () => {
  32. expect( view.top ).to.equal( 0 );
  33. expect( view.left ).to.equal( 0 );
  34. expect( view.position ).to.equal( 'arrow_se' );
  35. expect( view.isVisible ).to.equal( false );
  36. expect( view.withArrow ).to.equal( true );
  37. } );
  38. it( 'creates view#content collection', () => {
  39. expect( view.content ).to.be.instanceOf( ViewCollection );
  40. } );
  41. } );
  42. describe( 'DOM bindings', () => {
  43. describe( 'arrow', () => {
  44. it( 'should react on view#position', () => {
  45. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_se' ) ).to.true;
  46. view.position = 'arrow_sw';
  47. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_sw' ) ).to.true;
  48. } );
  49. it( 'should react on view#withArrow', () => {
  50. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.true;
  51. view.withArrow = false;
  52. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.false;
  53. } );
  54. } );
  55. describe( 'isVisible', () => {
  56. it( 'should react on view#isvisible', () => {
  57. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.false;
  58. view.isVisible = true;
  59. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.true;
  60. } );
  61. } );
  62. describe( 'styles', () => {
  63. it( 'should react on view#top', () => {
  64. expect( view.element.style.top ).to.equal( '0px' );
  65. view.top = 10;
  66. expect( view.element.style.top ).to.equal( '10px' );
  67. } );
  68. it( 'should react on view#left', () => {
  69. expect( view.element.style.left ).to.equal( '0px' );
  70. view.left = 10;
  71. expect( view.element.style.left ).to.equal( '10px' );
  72. } );
  73. it( 'should react on view#maxWidth', () => {
  74. expect( view.element.style.maxWidth ).to.equal( '200px' );
  75. view.maxWidth = 10;
  76. expect( view.element.style.maxWidth ).to.equal( '10px' );
  77. } );
  78. } );
  79. describe( 'children', () => {
  80. it( 'should react on view#content', () => {
  81. expect( view.element.childNodes.length ).to.equal( 0 );
  82. const button = new ButtonView( { t() {} } );
  83. return view.content.add( button ).then( () => {
  84. expect( view.element.childNodes.length ).to.equal( 1 );
  85. } );
  86. } );
  87. } );
  88. } );
  89. describe( 'isVisible', () => {
  90. it( 'should return view#isVisible value', () => {
  91. expect( view.isVisible ).to.false;
  92. view.isVisible = true;
  93. expect( view.isVisible ).to.true;
  94. } );
  95. } );
  96. describe( 'show()', () => {
  97. it( 'should set view#isVisible as true', () => {
  98. view.isVisible = false;
  99. view.show();
  100. expect( view.isVisible ).to.true;
  101. } );
  102. } );
  103. describe( 'hide()', () => {
  104. it( 'should set view#isVisible as false', () => {
  105. view.isVisible = true;
  106. view.hide();
  107. expect( view.isVisible ).to.false;
  108. } );
  109. } );
  110. describe( 'attachTo()', () => {
  111. let target, limiter;
  112. beforeEach( () => {
  113. limiter = document.createElement( 'div' );
  114. target = document.createElement( 'div' );
  115. // Mock balloon panel element dimensions.
  116. mockBoundingBox( view.element, {
  117. top: 0,
  118. left: 0,
  119. width: 100,
  120. height: 100
  121. } );
  122. // Mock window dimensions.
  123. windowStub = {
  124. innerWidth: 500,
  125. innerHeight: 500,
  126. scrollX: 0,
  127. scrollY: 0,
  128. getComputedStyle: ( el ) => {
  129. return window.getComputedStyle( el );
  130. }
  131. };
  132. testUtils.sinon.stub( global, 'window', windowStub );
  133. } );
  134. it( 'should use default options', () => {
  135. const spy = testUtils.sinon.spy( positionUtils, 'getOptimalPosition' );
  136. view.attachTo( { target } );
  137. sinon.assert.calledWithExactly( spy, sinon.match( {
  138. element: view.element,
  139. target: target,
  140. positions: [
  141. BalloonPanelView.defaultPositions.se,
  142. BalloonPanelView.defaultPositions.sw,
  143. BalloonPanelView.defaultPositions.ne,
  144. BalloonPanelView.defaultPositions.nw
  145. ],
  146. limiter: document.body,
  147. fitInViewport: true
  148. } ) );
  149. } );
  150. describe( 'limited by limiter element', () => {
  151. beforeEach( () => {
  152. // Mock limiter element dimensions.
  153. mockBoundingBox( limiter, {
  154. left: 0,
  155. top: 0,
  156. width: 500,
  157. height: 500
  158. } );
  159. } );
  160. it( 'should put balloon on the `south east` side of the target element at default', () => {
  161. // Place target element at the center of the limiter.
  162. mockBoundingBox( target, {
  163. top: 225,
  164. left: 225,
  165. width: 50,
  166. height: 50
  167. } );
  168. view.attachTo( { target, limiter } );
  169. expect( view.position ).to.equal( 'arrow_se' );
  170. } );
  171. it( 'should put balloon on the `south east` side of the target element when target is on the top left side of the limiter', () => {
  172. mockBoundingBox( target, {
  173. top: 0,
  174. left: 0,
  175. width: 50,
  176. height: 50
  177. } );
  178. view.attachTo( { target, limiter } );
  179. expect( view.position ).to.equal( 'arrow_se' );
  180. } );
  181. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  182. mockBoundingBox( target, {
  183. top: 0,
  184. left: 450,
  185. width: 50,
  186. height: 50
  187. } );
  188. view.attachTo( { target, limiter } );
  189. expect( view.position ).to.equal( 'arrow_sw' );
  190. } );
  191. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  192. mockBoundingBox( target, {
  193. top: 450,
  194. left: 0,
  195. width: 50,
  196. height: 50
  197. } );
  198. view.attachTo( { target, limiter } );
  199. expect( view.position ).to.equal( 'arrow_ne' );
  200. } );
  201. it( 'should put balloon on the `north west` side of the target element when target is on the bottom right of the limiter', () => {
  202. mockBoundingBox( target, {
  203. top: 450,
  204. left: 450,
  205. width: 50,
  206. height: 50
  207. } );
  208. view.attachTo( { target, limiter } );
  209. expect( view.position ).to.equal( 'arrow_nw' );
  210. } );
  211. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  212. it( 'works in a positioned ancestor (position: absolute)', () => {
  213. const positionedAncestor = document.createElement( 'div' );
  214. positionedAncestor.style.position = 'absolute';
  215. positionedAncestor.style.top = '100px';
  216. positionedAncestor.style.left = '100px';
  217. positionedAncestor.appendChild( view.element );
  218. document.body.appendChild( positionedAncestor );
  219. positionedAncestor.appendChild( view.element );
  220. mockBoundingBox( positionedAncestor, {
  221. top: 100,
  222. left: 100,
  223. width: 10,
  224. height: 10
  225. } );
  226. mockBoundingBox( target, {
  227. top: 0,
  228. left: 0,
  229. width: 100,
  230. height: 100
  231. } );
  232. view.attachTo( { target, limiter } );
  233. expect( view.top ).to.equal( 15 );
  234. expect( view.left ).to.equal( -80 );
  235. } );
  236. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  237. it( 'works in a positioned ancestor (position: static)', () => {
  238. const positionedAncestor = document.createElement( 'div' );
  239. positionedAncestor.style.position = 'static';
  240. positionedAncestor.appendChild( view.element );
  241. document.body.appendChild( positionedAncestor );
  242. positionedAncestor.appendChild( view.element );
  243. mockBoundingBox( target, {
  244. top: 0,
  245. left: 0,
  246. width: 100,
  247. height: 100
  248. } );
  249. view.attachTo( { target, limiter } );
  250. expect( view.top ).to.equal( 115 );
  251. expect( view.left ).to.equal( 20 );
  252. } );
  253. } );
  254. describe( 'limited by viewport', () => {
  255. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  256. mockBoundingBox( limiter, {
  257. left: 0,
  258. top: 0,
  259. width: 500,
  260. height: 500
  261. } );
  262. mockBoundingBox( target, {
  263. top: 0,
  264. left: 225,
  265. width: 50,
  266. height: 50
  267. } );
  268. Object.assign( windowStub, {
  269. innerWidth: 275
  270. } );
  271. view.attachTo( { target, limiter } );
  272. expect( view.position ).to.equal( 'arrow_sw' );
  273. } );
  274. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  275. mockBoundingBox( limiter, {
  276. top: 0,
  277. left: -400,
  278. width: 500,
  279. height: 500
  280. } );
  281. mockBoundingBox( target, {
  282. top: 0,
  283. left: 0,
  284. width: 50,
  285. height: 50
  286. } );
  287. view.attachTo( { target, limiter } );
  288. expect( view.position ).to.equal( 'arrow_se' );
  289. } );
  290. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  291. mockBoundingBox( limiter, {
  292. left: 0,
  293. top: 0,
  294. width: 500,
  295. height: 500
  296. } );
  297. mockBoundingBox( target, {
  298. top: 225,
  299. left: 0,
  300. width: 50,
  301. height: 50
  302. } );
  303. Object.assign( windowStub, {
  304. innerHeight: 275
  305. } );
  306. view.attachTo( { target, limiter } );
  307. expect( view.position ).to.equal( 'arrow_ne' );
  308. } );
  309. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  310. mockBoundingBox( limiter, {
  311. left: 0,
  312. top: -400,
  313. width: 500,
  314. height: 500
  315. } );
  316. mockBoundingBox( target, {
  317. top: 0,
  318. left: 0,
  319. width: 50,
  320. height: 50
  321. } );
  322. view.attachTo( { target, limiter } );
  323. expect( view.position ).to.equal( 'arrow_se' );
  324. } );
  325. } );
  326. } );
  327. describe( 'pin() and unpin()', () => {
  328. let attachToSpy, target, targetParent, limiter, notRelatedElement;
  329. beforeEach( () => {
  330. attachToSpy = sinon.spy( view, 'attachTo' );
  331. limiter = document.createElement( 'div' );
  332. targetParent = document.createElement( 'div' );
  333. target = document.createElement( 'div' );
  334. notRelatedElement = document.createElement( 'div' );
  335. view.show();
  336. targetParent.appendChild( target );
  337. document.body.appendChild( targetParent );
  338. document.body.appendChild( limiter );
  339. document.body.appendChild( notRelatedElement );
  340. } );
  341. afterEach( () => {
  342. targetParent.remove();
  343. limiter.remove();
  344. notRelatedElement.remove();
  345. } );
  346. describe( 'pin()', () => {
  347. it( 'should show the balloon', () => {
  348. const spy = sinon.spy( view, 'show' );
  349. view.hide();
  350. view.pin( { target, limiter } );
  351. sinon.assert.calledOnce( spy );
  352. } );
  353. it( 'should start pinning when the balloon is visible', () => {
  354. view.pin( { target, limiter } );
  355. sinon.assert.calledOnce( attachToSpy );
  356. view.hide();
  357. targetParent.dispatchEvent( new Event( 'scroll' ) );
  358. view.show();
  359. sinon.assert.calledTwice( attachToSpy );
  360. targetParent.dispatchEvent( new Event( 'scroll' ) );
  361. sinon.assert.calledThrice( attachToSpy );
  362. } );
  363. it( 'should stop pinning when the balloon becomes invisible', () => {
  364. view.show();
  365. view.pin( { target, limiter } );
  366. sinon.assert.calledOnce( attachToSpy );
  367. view.hide();
  368. targetParent.dispatchEvent( new Event( 'scroll' ) );
  369. sinon.assert.calledOnce( attachToSpy );
  370. } );
  371. it( 'should unpin if already pinned', () => {
  372. const unpinSpy = testUtils.sinon.spy( view, 'unpin' );
  373. view.show();
  374. sinon.assert.notCalled( attachToSpy );
  375. view.pin( { target, limiter } );
  376. sinon.assert.calledOnce( attachToSpy );
  377. view.pin( { target, limiter } );
  378. sinon.assert.calledTwice( unpinSpy );
  379. targetParent.dispatchEvent( new Event( 'scroll' ) );
  380. sinon.assert.calledThrice( attachToSpy );
  381. } );
  382. it( 'should keep the balloon pinned to the target when any of the related elements is scrolled', () => {
  383. view.pin( { target, limiter } );
  384. sinon.assert.calledOnce( attachToSpy );
  385. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  386. targetParent.dispatchEvent( new Event( 'scroll' ) );
  387. sinon.assert.calledTwice( attachToSpy );
  388. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  389. limiter.dispatchEvent( new Event( 'scroll' ) );
  390. sinon.assert.calledThrice( attachToSpy );
  391. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  392. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  393. // Nothing's changed.
  394. sinon.assert.calledThrice( attachToSpy );
  395. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  396. } );
  397. it( 'should keep the balloon pinned to the target when the browser window is being resized', () => {
  398. view.pin( { target, limiter } );
  399. sinon.assert.calledOnce( attachToSpy );
  400. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  401. window.dispatchEvent( new Event( 'resize' ) );
  402. sinon.assert.calledTwice( attachToSpy );
  403. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  404. } );
  405. it( 'should stop attaching when the balloon is hidden', () => {
  406. view.pin( { target, limiter } );
  407. sinon.assert.calledOnce( attachToSpy );
  408. view.hide();
  409. window.dispatchEvent( new Event( 'resize' ) );
  410. window.dispatchEvent( new Event( 'scroll' ) );
  411. // Still once.
  412. sinon.assert.calledOnce( attachToSpy );
  413. } );
  414. it( 'should stop attaching once the view is destroyed', () => {
  415. view.pin( { target, limiter } );
  416. sinon.assert.calledOnce( attachToSpy );
  417. return view.destroy().then( () => {
  418. view = null;
  419. window.dispatchEvent( new Event( 'resize' ) );
  420. window.dispatchEvent( new Event( 'scroll' ) );
  421. // Still once.
  422. sinon.assert.calledOnce( attachToSpy );
  423. } );
  424. } );
  425. it( 'should set document.body as the default limiter', () => {
  426. view.pin( { target } );
  427. sinon.assert.calledOnce( attachToSpy );
  428. document.body.dispatchEvent( new Event( 'scroll' ) );
  429. sinon.assert.calledTwice( attachToSpy );
  430. } );
  431. it( 'should work for Range as a target', () => {
  432. const element = document.createElement( 'div' );
  433. const range = document.createRange();
  434. element.appendChild( document.createTextNode( 'foo bar' ) );
  435. document.body.appendChild( element );
  436. range.selectNodeContents( element );
  437. view.pin( { target: range } );
  438. sinon.assert.calledOnce( attachToSpy );
  439. element.dispatchEvent( new Event( 'scroll' ) );
  440. sinon.assert.calledTwice( attachToSpy );
  441. } );
  442. it( 'should work for rect as a target', () => {
  443. // Just check if this normally works without errors.
  444. const rect = {};
  445. view.pin( { target: rect, limiter } );
  446. sinon.assert.calledOnce( attachToSpy );
  447. limiter.dispatchEvent( new Event( 'scroll' ) );
  448. sinon.assert.calledTwice( attachToSpy );
  449. } );
  450. } );
  451. describe( 'unpin()', () => {
  452. it( 'should hide the balloon if pinned', () => {
  453. const spy = sinon.spy( view, 'hide' );
  454. view.pin( { target, limiter } );
  455. view.unpin();
  456. sinon.assert.calledOnce( spy );
  457. } );
  458. it( 'should stop attaching', () => {
  459. view.pin( { target, limiter } );
  460. sinon.assert.calledOnce( attachToSpy );
  461. view.unpin();
  462. view.hide();
  463. window.dispatchEvent( new Event( 'resize' ) );
  464. document.dispatchEvent( new Event( 'scroll' ) );
  465. view.show();
  466. window.dispatchEvent( new Event( 'resize' ) );
  467. document.dispatchEvent( new Event( 'scroll' ) );
  468. sinon.assert.calledOnce( attachToSpy );
  469. } );
  470. } );
  471. } );
  472. } );
  473. function mockBoundingBox( element, data ) {
  474. const boundingBox = Object.assign( {}, data );
  475. boundingBox.right = boundingBox.left + boundingBox.width;
  476. boundingBox.bottom = boundingBox.top + boundingBox.height;
  477. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  478. }