balloonpanelview.js 19 KB

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