balloonpanelview.js 16 KB

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