8
0

balloonpanelview.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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,
  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 ' +
  174. 'target is on the top left side of the limiter', () => {
  175. mockBoundingBox( target, {
  176. top: 0,
  177. left: 0,
  178. width: 50,
  179. height: 50
  180. } );
  181. view.attachTo( { target, limiter } );
  182. expect( view.position ).to.equal( 'arrow_ne' );
  183. } );
  184. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  185. mockBoundingBox( target, {
  186. top: 0,
  187. left: 450,
  188. width: 50,
  189. height: 50
  190. } );
  191. view.attachTo( { target, limiter } );
  192. expect( view.position ).to.equal( 'arrow_nw' );
  193. } );
  194. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  195. mockBoundingBox( target, {
  196. top: 450,
  197. left: 0,
  198. width: 50,
  199. height: 50
  200. } );
  201. view.attachTo( { target, limiter } );
  202. expect( view.position ).to.equal( 'arrow_se' );
  203. } );
  204. it( 'should put balloon on the `north west` side of the target element when ' +
  205. 'target is on the bottom right of the limiter', () => {
  206. mockBoundingBox( target, {
  207. top: 450,
  208. left: 450,
  209. width: 50,
  210. height: 50
  211. } );
  212. view.attachTo( { target, limiter } );
  213. expect( view.position ).to.equal( 'arrow_sw' );
  214. } );
  215. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  216. it( 'works in a positioned ancestor (position: absolute)', () => {
  217. const positionedAncestor = document.createElement( 'div' );
  218. positionedAncestor.style.position = 'absolute';
  219. positionedAncestor.style.top = '100px';
  220. positionedAncestor.style.left = '100px';
  221. positionedAncestor.appendChild( view.element );
  222. document.body.appendChild( positionedAncestor );
  223. positionedAncestor.appendChild( view.element );
  224. mockBoundingBox( positionedAncestor, {
  225. top: 100,
  226. left: 100,
  227. width: 10,
  228. height: 10
  229. } );
  230. mockBoundingBox( target, {
  231. top: 0,
  232. left: 0,
  233. width: 100,
  234. height: 100
  235. } );
  236. view.attachTo( { target, limiter } );
  237. expect( view.top ).to.equal( 15 );
  238. expect( view.left ).to.equal( -80 );
  239. } );
  240. // https://github.com/ckeditor/ckeditor5-ui-default/issues/126
  241. it( 'works in a positioned ancestor (position: static)', () => {
  242. const positionedAncestor = document.createElement( 'div' );
  243. positionedAncestor.style.position = 'static';
  244. positionedAncestor.appendChild( view.element );
  245. document.body.appendChild( positionedAncestor );
  246. positionedAncestor.appendChild( view.element );
  247. mockBoundingBox( target, {
  248. top: 0,
  249. left: 0,
  250. width: 100,
  251. height: 100
  252. } );
  253. view.attachTo( { target, limiter } );
  254. expect( view.top ).to.equal( 115 );
  255. expect( view.left ).to.equal( 20 );
  256. } );
  257. } );
  258. describe( 'limited by viewport', () => {
  259. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  260. mockBoundingBox( limiter, {
  261. left: 0,
  262. top: 0,
  263. width: 500,
  264. height: 500
  265. } );
  266. mockBoundingBox( target, {
  267. top: 0,
  268. left: 225,
  269. width: 50,
  270. height: 50
  271. } );
  272. Object.assign( windowStub, {
  273. innerWidth: 275
  274. } );
  275. view.attachTo( { target, limiter } );
  276. expect( view.position ).to.equal( 'arrow_nw' );
  277. } );
  278. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  279. mockBoundingBox( limiter, {
  280. top: 0,
  281. left: -400,
  282. width: 500,
  283. height: 500
  284. } );
  285. mockBoundingBox( target, {
  286. top: 0,
  287. left: 0,
  288. width: 50,
  289. height: 50
  290. } );
  291. view.attachTo( { target, limiter } );
  292. expect( view.position ).to.equal( 'arrow_ne' );
  293. } );
  294. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  295. mockBoundingBox( limiter, {
  296. left: 0,
  297. top: 0,
  298. width: 500,
  299. height: 500
  300. } );
  301. mockBoundingBox( target, {
  302. top: 225,
  303. left: 0,
  304. width: 50,
  305. height: 50
  306. } );
  307. Object.assign( windowStub, {
  308. innerHeight: 275
  309. } );
  310. view.attachTo( { target, limiter } );
  311. expect( view.position ).to.equal( 'arrow_se' );
  312. } );
  313. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  314. mockBoundingBox( limiter, {
  315. left: 0,
  316. top: -400,
  317. width: 500,
  318. height: 500
  319. } );
  320. mockBoundingBox( target, {
  321. top: 0,
  322. left: 0,
  323. width: 50,
  324. height: 50
  325. } );
  326. view.attachTo( { target, limiter } );
  327. expect( view.position ).to.equal( 'arrow_ne' );
  328. } );
  329. } );
  330. } );
  331. describe( 'pin() and unpin()', () => {
  332. let attachToSpy, target, targetParent, limiter, notRelatedElement;
  333. beforeEach( () => {
  334. attachToSpy = sinon.spy( view, 'attachTo' );
  335. limiter = document.createElement( 'div' );
  336. targetParent = document.createElement( 'div' );
  337. target = document.createElement( 'div' );
  338. notRelatedElement = document.createElement( 'div' );
  339. view.show();
  340. targetParent.appendChild( target );
  341. document.body.appendChild( targetParent );
  342. document.body.appendChild( limiter );
  343. document.body.appendChild( notRelatedElement );
  344. } );
  345. afterEach( () => {
  346. targetParent.remove();
  347. limiter.remove();
  348. notRelatedElement.remove();
  349. } );
  350. describe( 'pin()', () => {
  351. it( 'should show the balloon', () => {
  352. const spy = sinon.spy( view, 'show' );
  353. view.hide();
  354. view.pin( { target, limiter } );
  355. sinon.assert.calledOnce( spy );
  356. } );
  357. it( 'should start pinning when the balloon is visible', () => {
  358. view.pin( { target, limiter } );
  359. sinon.assert.calledOnce( attachToSpy );
  360. view.hide();
  361. targetParent.dispatchEvent( new Event( 'scroll' ) );
  362. view.show();
  363. sinon.assert.calledTwice( attachToSpy );
  364. targetParent.dispatchEvent( new Event( 'scroll' ) );
  365. sinon.assert.calledThrice( attachToSpy );
  366. } );
  367. it( 'should stop pinning when the balloon becomes invisible', () => {
  368. view.show();
  369. view.pin( { target, limiter } );
  370. sinon.assert.calledOnce( attachToSpy );
  371. view.hide();
  372. targetParent.dispatchEvent( new Event( 'scroll' ) );
  373. sinon.assert.calledOnce( attachToSpy );
  374. } );
  375. it( 'should unpin if already pinned', () => {
  376. const unpinSpy = testUtils.sinon.spy( view, 'unpin' );
  377. view.show();
  378. sinon.assert.notCalled( attachToSpy );
  379. view.pin( { target, limiter } );
  380. sinon.assert.calledOnce( attachToSpy );
  381. view.pin( { target, limiter } );
  382. sinon.assert.calledTwice( unpinSpy );
  383. targetParent.dispatchEvent( new Event( 'scroll' ) );
  384. sinon.assert.calledThrice( attachToSpy );
  385. } );
  386. it( 'should keep the balloon pinned to the target when any of the related elements is scrolled', () => {
  387. view.pin( { target, limiter } );
  388. sinon.assert.calledOnce( attachToSpy );
  389. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  390. targetParent.dispatchEvent( new Event( 'scroll' ) );
  391. sinon.assert.calledTwice( attachToSpy );
  392. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  393. limiter.dispatchEvent( new Event( 'scroll' ) );
  394. sinon.assert.calledThrice( attachToSpy );
  395. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  396. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  397. // Nothing's changed.
  398. sinon.assert.calledThrice( attachToSpy );
  399. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  400. } );
  401. it( 'should keep the balloon pinned to the target when the browser window is being resized', () => {
  402. view.pin( { target, limiter } );
  403. sinon.assert.calledOnce( attachToSpy );
  404. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  405. window.dispatchEvent( new Event( 'resize' ) );
  406. sinon.assert.calledTwice( attachToSpy );
  407. sinon.assert.calledWith( attachToSpy.lastCall, { target, limiter } );
  408. } );
  409. it( 'should stop attaching when the balloon is hidden', () => {
  410. view.pin( { target, limiter } );
  411. sinon.assert.calledOnce( attachToSpy );
  412. view.hide();
  413. window.dispatchEvent( new Event( 'resize' ) );
  414. window.dispatchEvent( new Event( 'scroll' ) );
  415. // Still once.
  416. sinon.assert.calledOnce( attachToSpy );
  417. } );
  418. it( 'should stop attaching once the view is destroyed', () => {
  419. view.pin( { target, limiter } );
  420. sinon.assert.calledOnce( attachToSpy );
  421. return view.destroy().then( () => {
  422. view = null;
  423. window.dispatchEvent( new Event( 'resize' ) );
  424. window.dispatchEvent( new Event( 'scroll' ) );
  425. // Still once.
  426. sinon.assert.calledOnce( attachToSpy );
  427. } );
  428. } );
  429. it( 'should set document.body as the default limiter', () => {
  430. view.pin( { target } );
  431. sinon.assert.calledOnce( attachToSpy );
  432. document.body.dispatchEvent( new Event( 'scroll' ) );
  433. sinon.assert.calledTwice( attachToSpy );
  434. } );
  435. it( 'should work for Range as a target', () => {
  436. const element = document.createElement( 'div' );
  437. const range = document.createRange();
  438. element.appendChild( document.createTextNode( 'foo bar' ) );
  439. document.body.appendChild( element );
  440. range.selectNodeContents( element );
  441. view.pin( { target: range } );
  442. sinon.assert.calledOnce( attachToSpy );
  443. element.dispatchEvent( new Event( 'scroll' ) );
  444. sinon.assert.calledTwice( attachToSpy );
  445. } );
  446. it( 'should work for rect as a target', () => {
  447. // Just check if this normally works without errors.
  448. const rect = {};
  449. view.pin( { target: rect, limiter } );
  450. sinon.assert.calledOnce( attachToSpy );
  451. limiter.dispatchEvent( new Event( 'scroll' ) );
  452. sinon.assert.calledTwice( attachToSpy );
  453. } );
  454. } );
  455. describe( 'unpin()', () => {
  456. it( 'should hide the balloon if pinned', () => {
  457. const spy = sinon.spy( view, 'hide' );
  458. view.pin( { target, limiter } );
  459. view.unpin();
  460. sinon.assert.calledOnce( spy );
  461. } );
  462. it( 'should stop attaching', () => {
  463. view.pin( { target, limiter } );
  464. sinon.assert.calledOnce( attachToSpy );
  465. view.unpin();
  466. view.hide();
  467. window.dispatchEvent( new Event( 'resize' ) );
  468. document.dispatchEvent( new Event( 'scroll' ) );
  469. view.show();
  470. window.dispatchEvent( new Event( 'resize' ) );
  471. document.dispatchEvent( new Event( 'scroll' ) );
  472. sinon.assert.calledOnce( attachToSpy );
  473. } );
  474. } );
  475. } );
  476. describe( 'defaultPositions', () => {
  477. let positions, balloonRect, targetRect;
  478. beforeEach( () => {
  479. positions = BalloonPanelView.defaultPositions;
  480. targetRect = {
  481. top: 100,
  482. bottom: 200,
  483. left: 100,
  484. right: 200,
  485. width: 100,
  486. height: 100
  487. };
  488. balloonRect = {
  489. top: 0,
  490. bottom: 0,
  491. left: 0,
  492. right: 0,
  493. width: 50,
  494. height: 50
  495. };
  496. } );
  497. it( 'southEastArrowNorthEast', () => {
  498. expect( positions.southEastArrowNorthEast( targetRect ) ).to.deep.equal( {
  499. top: 215,
  500. left: 120,
  501. name: 'arrow_ne'
  502. } );
  503. } );
  504. it( 'southWestArrowNorthEast', () => {
  505. expect( positions.southWestArrowNorthEast( targetRect, balloonRect ) ).to.deep.equal( {
  506. top: 215,
  507. left: 130,
  508. name: 'arrow_nw'
  509. } );
  510. } );
  511. it( 'northEastArrowSouthWest', () => {
  512. expect( positions.northEastArrowSouthWest( targetRect, balloonRect ) ).to.deep.equal( {
  513. top: 35,
  514. left: 120,
  515. name: 'arrow_se'
  516. } );
  517. } );
  518. it( 'northWestArrowSouthEast', () => {
  519. expect( positions.northWestArrowSouthEast( targetRect, balloonRect ) ).to.deep.equal( {
  520. top: 35,
  521. left: 130,
  522. name: 'arrow_sw'
  523. } );
  524. } );
  525. it( 'southEastArrowNorth', () => {
  526. expect( positions.southEastArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  527. top: 215,
  528. left: 175,
  529. name: 'arrow_n'
  530. } );
  531. } );
  532. it( 'northEastArrowSouth', () => {
  533. expect( positions.northEastArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  534. top: 35,
  535. left: 175,
  536. name: 'arrow_s'
  537. } );
  538. } );
  539. it( 'northWestArrowSouth', () => {
  540. expect( positions.northWestArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  541. top: 35,
  542. left: 75,
  543. name: 'arrow_s'
  544. } );
  545. } );
  546. it( 'southWestArrowNorth', () => {
  547. expect( positions.southWestArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  548. top: 215,
  549. left: 75,
  550. name: 'arrow_n'
  551. } );
  552. } );
  553. it( 'southArrowNorth', () => {
  554. expect( positions.southArrowNorth( targetRect, balloonRect ) ).to.deep.equal( {
  555. top: 215,
  556. left: 125,
  557. name: 'arrow_n'
  558. } );
  559. } );
  560. it( 'northArrowSouth', () => {
  561. expect( positions.northArrowSouth( targetRect, balloonRect ) ).to.deep.equal( {
  562. top: 35,
  563. left: 125,
  564. name: 'arrow_s'
  565. } );
  566. } );
  567. } );
  568. } );
  569. function mockBoundingBox( element, data ) {
  570. const boundingBox = Object.assign( {}, data );
  571. boundingBox.right = boundingBox.left + boundingBox.width;
  572. boundingBox.bottom = boundingBox.top + boundingBox.height;
  573. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  574. }