8
0

balloonpanelview.js 18 KB

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