balloonpanelview.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. return view.init();
  18. } );
  19. afterEach( () => {
  20. if ( view ) {
  21. return view.destroy();
  22. }
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'should create element from template', () => {
  26. expect( view.element.tagName ).to.equal( 'DIV' );
  27. expect( view.element.classList.contains( 'ck-balloon-panel' ) ).to.true;
  28. } );
  29. it( 'should set default values', () => {
  30. expect( view.top ).to.equal( 0 );
  31. expect( view.left ).to.equal( 0 );
  32. expect( view.position ).to.equal( 'arrow_ne' );
  33. expect( view.isVisible ).to.equal( false );
  34. expect( view.withArrow ).to.equal( true );
  35. } );
  36. it( 'creates view#content collection', () => {
  37. expect( view.content ).to.be.instanceOf( ViewCollection );
  38. } );
  39. } );
  40. describe( 'DOM bindings', () => {
  41. describe( 'arrow', () => {
  42. it( 'should react on view#position', () => {
  43. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_ne' ) ).to.true;
  44. view.position = 'arrow_nw';
  45. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_nw' ) ).to.true;
  46. } );
  47. it( 'should react on view#withArrow', () => {
  48. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.true;
  49. view.withArrow = false;
  50. expect( view.element.classList.contains( 'ck-balloon-panel_with-arrow' ) ).to.be.false;
  51. } );
  52. } );
  53. describe( 'isVisible', () => {
  54. it( 'should react on view#isvisible', () => {
  55. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.false;
  56. view.isVisible = true;
  57. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.true;
  58. } );
  59. } );
  60. describe( 'styles', () => {
  61. it( 'should react on view#top', () => {
  62. expect( view.element.style.top ).to.equal( '0px' );
  63. view.top = 10;
  64. expect( view.element.style.top ).to.equal( '10px' );
  65. } );
  66. it( 'should react on view#left', () => {
  67. expect( view.element.style.left ).to.equal( '0px' );
  68. view.left = 10;
  69. expect( view.element.style.left ).to.equal( '10px' );
  70. } );
  71. } );
  72. describe( 'className', () => {
  73. it( 'should set additional class to the view#element', () => {
  74. view.className = 'foo';
  75. expect( view.element.classList.contains( 'foo' ) ).to.true;
  76. view.className = '';
  77. expect( view.element.classList.contains( 'foo' ) ).to.false;
  78. } );
  79. } );
  80. describe( 'children', () => {
  81. it( 'should react on view#content', () => {
  82. expect( view.element.childNodes.length ).to.equal( 0 );
  83. const button = new ButtonView( { t() {} } );
  84. return view.content.add( button ).then( () => {
  85. expect( view.element.childNodes.length ).to.equal( 1 );
  86. } );
  87. } );
  88. } );
  89. describe( 'event listeners', () => {
  90. it( 'prevent default on #mousedown', () => {
  91. const evt = new Event( 'mousedown', { bubbles: true } );
  92. const spy = sinon.spy( evt, 'preventDefault' );
  93. view.element.dispatchEvent( evt );
  94. sinon.assert.calledOnce( spy );
  95. } );
  96. } );
  97. } );
  98. describe( 'show()', () => {
  99. it( 'should set view#isVisible as true', () => {
  100. view.isVisible = false;
  101. view.show();
  102. expect( view.isVisible ).to.true;
  103. } );
  104. } );
  105. describe( 'hide()', () => {
  106. it( 'should set view#isVisible as false', () => {
  107. view.isVisible = true;
  108. view.hide();
  109. expect( view.isVisible ).to.false;
  110. } );
  111. } );
  112. describe( 'attachTo()', () => {
  113. let target, limiter;
  114. beforeEach( () => {
  115. limiter = document.createElement( 'div' );
  116. target = document.createElement( 'div' );
  117. // Mock balloon panel element dimensions.
  118. mockBoundingBox( view.element, {
  119. top: 0,
  120. left: 0,
  121. width: 100,
  122. height: 100
  123. } );
  124. // Mock window dimensions.
  125. windowStub = {
  126. innerWidth: 500,
  127. innerHeight: 500,
  128. scrollX: 0,
  129. scrollY: 0,
  130. getComputedStyle: ( el ) => {
  131. return window.getComputedStyle( el );
  132. }
  133. };
  134. testUtils.sinon.stub( global, 'window', windowStub );
  135. } );
  136. it( 'should use default options', () => {
  137. const spy = testUtils.sinon.spy( positionUtils, 'getOptimalPosition' );
  138. view.attachTo( { target } );
  139. sinon.assert.calledWithExactly( spy, sinon.match( {
  140. element: view.element,
  141. target: target,
  142. positions: [
  143. BalloonPanelView.defaultPositions.southEastArrowNorthEast,
  144. BalloonPanelView.defaultPositions.southWestArrowNorthEast,
  145. BalloonPanelView.defaultPositions.northEastArrowSouthWest,
  146. BalloonPanelView.defaultPositions.northWestArrowSouthEast
  147. ],
  148. limiter: document.body,
  149. fitInViewport: true
  150. } ) );
  151. } );
  152. describe( 'limited by limiter element', () => {
  153. beforeEach( () => {
  154. // Mock limiter element dimensions.
  155. mockBoundingBox( limiter, {
  156. left: 0,
  157. top: 0,
  158. width: 500,
  159. height: 500
  160. } );
  161. } );
  162. it( 'should put balloon on the `south east` side of the target element at default', () => {
  163. // Place target element at the center of the limiter.
  164. mockBoundingBox( target, {
  165. top: 225,
  166. left: 225,
  167. width: 50,
  168. height: 50
  169. } );
  170. view.attachTo( { target, limiter } );
  171. expect( view.position ).to.equal( 'arrow_ne' );
  172. } );
  173. it( 'should put balloon on the `south east` side of the target element when target is on the top left side of the limiter', () => {
  174. mockBoundingBox( target, {
  175. top: 0,
  176. left: 0,
  177. width: 50,
  178. height: 50
  179. } );
  180. view.attachTo( { target, limiter } );
  181. expect( view.position ).to.equal( 'arrow_ne' );
  182. } );
  183. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  184. mockBoundingBox( target, {
  185. top: 0,
  186. left: 450,
  187. width: 50,
  188. height: 50
  189. } );
  190. view.attachTo( { target, limiter } );
  191. expect( view.position ).to.equal( 'arrow_nw' );
  192. } );
  193. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  194. mockBoundingBox( target, {
  195. top: 450,
  196. left: 0,
  197. width: 50,
  198. height: 50
  199. } );
  200. view.attachTo( { target, limiter } );
  201. expect( view.position ).to.equal( 'arrow_se' );
  202. } );
  203. it( 'should put balloon on the `north west` side of the target element when target is on the bottom right of the limiter', () => {
  204. mockBoundingBox( target, {
  205. top: 450,
  206. left: 450,
  207. width: 50,
  208. height: 50
  209. } );
  210. view.attachTo( { target, limiter } );
  211. expect( view.position ).to.equal( 'arrow_sw' );
  212. } );
  213. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  214. it( 'works in a positioned ancestor (position: absolute)', () => {
  215. const positionedAncestor = document.createElement( 'div' );
  216. positionedAncestor.style.position = 'absolute';
  217. positionedAncestor.style.top = '100px';
  218. positionedAncestor.style.left = '100px';
  219. positionedAncestor.appendChild( view.element );
  220. document.body.appendChild( positionedAncestor );
  221. positionedAncestor.appendChild( view.element );
  222. mockBoundingBox( positionedAncestor, {
  223. top: 100,
  224. left: 100,
  225. width: 10,
  226. height: 10
  227. } );
  228. mockBoundingBox( target, {
  229. top: 0,
  230. left: 0,
  231. width: 100,
  232. height: 100
  233. } );
  234. view.attachTo( { target, limiter } );
  235. expect( view.top ).to.equal( 15 );
  236. expect( view.left ).to.equal( -80 );
  237. } );
  238. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  239. it( 'works in a positioned ancestor (position: static)', () => {
  240. const positionedAncestor = document.createElement( 'div' );
  241. positionedAncestor.style.position = 'static';
  242. positionedAncestor.appendChild( view.element );
  243. document.body.appendChild( positionedAncestor );
  244. positionedAncestor.appendChild( view.element );
  245. mockBoundingBox( target, {
  246. top: 0,
  247. left: 0,
  248. width: 100,
  249. height: 100
  250. } );
  251. view.attachTo( { target, limiter } );
  252. expect( view.top ).to.equal( 115 );
  253. expect( view.left ).to.equal( 20 );
  254. } );
  255. } );
  256. describe( 'limited by viewport', () => {
  257. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  258. mockBoundingBox( limiter, {
  259. left: 0,
  260. top: 0,
  261. width: 500,
  262. height: 500
  263. } );
  264. mockBoundingBox( target, {
  265. top: 0,
  266. left: 225,
  267. width: 50,
  268. height: 50
  269. } );
  270. Object.assign( windowStub, {
  271. innerWidth: 275
  272. } );
  273. view.attachTo( { target, limiter } );
  274. expect( view.position ).to.equal( 'arrow_nw' );
  275. } );
  276. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  277. mockBoundingBox( limiter, {
  278. top: 0,
  279. left: -400,
  280. width: 500,
  281. height: 500
  282. } );
  283. mockBoundingBox( target, {
  284. top: 0,
  285. left: 0,
  286. width: 50,
  287. height: 50
  288. } );
  289. view.attachTo( { target, limiter } );
  290. expect( view.position ).to.equal( 'arrow_ne' );
  291. } );
  292. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  293. mockBoundingBox( limiter, {
  294. left: 0,
  295. top: 0,
  296. width: 500,
  297. height: 500
  298. } );
  299. mockBoundingBox( target, {
  300. top: 225,
  301. left: 0,
  302. width: 50,
  303. height: 50
  304. } );
  305. Object.assign( windowStub, {
  306. innerHeight: 275
  307. } );
  308. view.attachTo( { target, limiter } );
  309. expect( view.position ).to.equal( 'arrow_se' );
  310. } );
  311. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  312. mockBoundingBox( limiter, {
  313. left: 0,
  314. top: -400,
  315. width: 500,
  316. height: 500
  317. } );
  318. mockBoundingBox( target, {
  319. top: 0,
  320. left: 0,
  321. width: 50,
  322. height: 50
  323. } );
  324. view.attachTo( { target, limiter } );
  325. expect( view.position ).to.equal( 'arrow_ne' );
  326. } );
  327. } );
  328. } );
  329. describe( 'pin() and unpin()', () => {
  330. let attachToSpy, target, targetParent, limiter, notRelatedElement;
  331. beforeEach( () => {
  332. attachToSpy = sinon.spy( view, 'attachTo' );
  333. limiter = document.createElement( 'div' );
  334. targetParent = document.createElement( 'div' );
  335. target = document.createElement( 'div' );
  336. notRelatedElement = document.createElement( 'div' );
  337. view.show();
  338. targetParent.appendChild( target );
  339. document.body.appendChild( targetParent );
  340. document.body.appendChild( limiter );
  341. document.body.appendChild( notRelatedElement );
  342. } );
  343. afterEach( () => {
  344. targetParent.remove();
  345. limiter.remove();
  346. notRelatedElement.remove();
  347. } );
  348. describe( 'pin()', () => {
  349. it( 'should show the balloon', () => {
  350. const spy = sinon.spy( view, 'show' );
  351. view.hide();
  352. view.pin( { target, limiter } );
  353. sinon.assert.calledOnce( spy );
  354. } );
  355. it( 'should start pinning when the balloon is visible', () => {
  356. view.pin( { target, limiter } );
  357. sinon.assert.calledOnce( attachToSpy );
  358. view.hide();
  359. targetParent.dispatchEvent( new Event( 'scroll' ) );
  360. view.show();
  361. sinon.assert.calledTwice( attachToSpy );
  362. targetParent.dispatchEvent( new Event( 'scroll' ) );
  363. sinon.assert.calledThrice( attachToSpy );
  364. } );
  365. it( 'should stop pinning when the balloon becomes invisible', () => {
  366. view.show();
  367. view.pin( { target, limiter } );
  368. sinon.assert.calledOnce( attachToSpy );
  369. view.hide();
  370. targetParent.dispatchEvent( new Event( 'scroll' ) );
  371. sinon.assert.calledOnce( attachToSpy );
  372. } );
  373. it( 'should unpin if already pinned', () => {
  374. const unpinSpy = testUtils.sinon.spy( view, 'unpin' );
  375. view.show();
  376. sinon.assert.notCalled( attachToSpy );
  377. view.pin( { target, limiter } );
  378. sinon.assert.calledOnce( attachToSpy );
  379. view.pin( { target, limiter } );
  380. sinon.assert.calledTwice( unpinSpy );
  381. targetParent.dispatchEvent( new Event( 'scroll' ) );
  382. sinon.assert.calledThrice( attachToSpy );
  383. } );
  384. it( 'should keep the balloon pinned to the target when any of the related elements is scrolled', () => {
  385. view.pin( { target, limiter } );
  386. sinon.assert.calledOnce( attachToSpy );
  387. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  388. targetParent.dispatchEvent( new Event( 'scroll' ) );
  389. sinon.assert.calledTwice( attachToSpy );
  390. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  391. limiter.dispatchEvent( new Event( 'scroll' ) );
  392. sinon.assert.calledThrice( attachToSpy );
  393. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  394. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  395. // Nothing's changed.
  396. sinon.assert.calledThrice( attachToSpy );
  397. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  398. } );
  399. it( 'should keep the balloon pinned to the target when the browser window is being resized', () => {
  400. view.pin( { target, limiter } );
  401. sinon.assert.calledOnce( attachToSpy );
  402. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  403. window.dispatchEvent( new Event( 'resize' ) );
  404. sinon.assert.calledTwice( attachToSpy );
  405. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  406. } );
  407. it( 'should stop attaching when the balloon is hidden', () => {
  408. view.pin( { target, limiter } );
  409. sinon.assert.calledOnce( attachToSpy );
  410. view.hide();
  411. window.dispatchEvent( new Event( 'resize' ) );
  412. window.dispatchEvent( new Event( 'scroll' ) );
  413. // Still once.
  414. sinon.assert.calledOnce( attachToSpy );
  415. } );
  416. it( 'should stop attaching once the view is destroyed', () => {
  417. view.pin( { target, limiter } );
  418. sinon.assert.calledOnce( attachToSpy );
  419. return view.destroy().then( () => {
  420. view = null;
  421. window.dispatchEvent( new Event( 'resize' ) );
  422. window.dispatchEvent( new Event( 'scroll' ) );
  423. // Still once.
  424. sinon.assert.calledOnce( attachToSpy );
  425. } );
  426. } );
  427. it( 'should set document.body as the default limiter', () => {
  428. view.pin( { target } );
  429. sinon.assert.calledOnce( attachToSpy );
  430. document.body.dispatchEvent( new Event( 'scroll' ) );
  431. sinon.assert.calledTwice( attachToSpy );
  432. } );
  433. it( 'should work for Range as a target', () => {
  434. const element = document.createElement( 'div' );
  435. const range = document.createRange();
  436. element.appendChild( document.createTextNode( 'foo bar' ) );
  437. document.body.appendChild( element );
  438. range.selectNodeContents( element );
  439. view.pin( { target: range } );
  440. sinon.assert.calledOnce( attachToSpy );
  441. element.dispatchEvent( new Event( 'scroll' ) );
  442. sinon.assert.calledTwice( attachToSpy );
  443. } );
  444. it( 'should work for rect as a target', () => {
  445. // Just check if this normally works without errors.
  446. const rect = {};
  447. view.pin( { target: rect, limiter } );
  448. sinon.assert.calledOnce( attachToSpy );
  449. limiter.dispatchEvent( new Event( 'scroll' ) );
  450. sinon.assert.calledTwice( attachToSpy );
  451. } );
  452. } );
  453. describe( 'unpin()', () => {
  454. it( 'should hide the balloon if pinned', () => {
  455. const spy = sinon.spy( view, 'hide' );
  456. view.pin( { target, limiter } );
  457. view.unpin();
  458. sinon.assert.calledOnce( spy );
  459. } );
  460. it( 'should stop attaching', () => {
  461. view.pin( { target, limiter } );
  462. sinon.assert.calledOnce( attachToSpy );
  463. view.unpin();
  464. view.hide();
  465. window.dispatchEvent( new Event( 'resize' ) );
  466. document.dispatchEvent( new Event( 'scroll' ) );
  467. view.show();
  468. window.dispatchEvent( new Event( 'resize' ) );
  469. document.dispatchEvent( new Event( 'scroll' ) );
  470. sinon.assert.calledOnce( attachToSpy );
  471. } );
  472. } );
  473. } );
  474. describe( 'defaultPositions', () => {
  475. let positions, balloonRect, targetRect;
  476. beforeEach( () => {
  477. positions = BalloonPanelView.defaultPositions;
  478. targetRect = {
  479. top: 100,
  480. bottom: 200,
  481. left: 100,
  482. right: 200,
  483. width: 100,
  484. height: 100
  485. };
  486. balloonRect = {
  487. top: 0,
  488. bottom: 0,
  489. left: 0,
  490. right: 0,
  491. width: 50,
  492. height: 50
  493. };
  494. } );
  495. it( 'southEastArrowNorthEast', () => {
  496. expect( positions.southEastArrowNorthEast( targetRect ) ).to.deep.equal( {
  497. top: 215,
  498. left: 120,
  499. name: 'arrow_ne'
  500. } );
  501. } );
  502. it( 'southWestArrowNorthEast', () => {
  503. expect( positions.southWestArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  504. top: 215,
  505. left: 130,
  506. name: 'arrow_nw'
  507. } );
  508. } );
  509. it( 'northEastArrowSouthWest', () => {
  510. expect( positions.northEastArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  511. top: 35,
  512. left: 120,
  513. name: 'arrow_se'
  514. } );
  515. } );
  516. it( 'northWestArrowSouthEast', () => {
  517. expect( positions.northWestArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  518. top: 35,
  519. left: 130,
  520. name: 'arrow_sw'
  521. } );
  522. } );
  523. it( 'southEastArrowNorth', () => {
  524. expect( positions.southEastArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  525. top: 215,
  526. left: 175,
  527. name: 'arrow_n'
  528. } );
  529. } );
  530. it( 'northEastArrowSouth', () => {
  531. expect( positions.northEastArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  532. top: 35,
  533. left: 175,
  534. name: 'arrow_s'
  535. } );
  536. } );
  537. it( 'northWestArrowSouth', () => {
  538. expect( positions.northWestArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  539. top: 35,
  540. left: 75,
  541. name: 'arrow_s'
  542. } );
  543. } );
  544. it( 'southWestArrowNorth', () => {
  545. expect( positions.southWestArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  546. top: 215,
  547. left: 75,
  548. name: 'arrow_n'
  549. } );
  550. } );
  551. it( 'southArrowNorth', () => {
  552. expect( positions.southArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  553. top: 215,
  554. left: 125,
  555. name: 'arrow_n'
  556. } );
  557. } );
  558. it( 'northArrowSouth', () => {
  559. expect( positions.northArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  560. top: 35,
  561. left: 125,
  562. name: 'arrow_s'
  563. } );
  564. } );
  565. } );
  566. } );
  567. function mockBoundingBox( element, data ) {
  568. const boundingBox = Object.assign( {}, data );
  569. boundingBox.right = boundingBox.left + boundingBox.width;
  570. boundingBox.bottom = boundingBox.top + boundingBox.height;
  571. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  572. }