balloonpanelview.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. describe( 'constructor()', () => {
  21. it( 'should create element from template', () => {
  22. expect( view.element.tagName ).to.equal( 'DIV' );
  23. expect( view.element.classList.contains( 'ck-balloon-panel' ) ).to.true;
  24. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  25. } );
  26. it( 'should set default values', () => {
  27. expect( view.top ).to.equal( 0 );
  28. expect( view.left ).to.equal( 0 );
  29. expect( view.position ).to.equal( 'se' );
  30. expect( view.isVisible ).to.equal( false );
  31. } );
  32. it( 'creates view#content collection', () => {
  33. expect( view.content ).to.be.instanceOf( ViewCollection );
  34. } );
  35. } );
  36. describe( 'DOM bindings', () => {
  37. describe( 'arrow', () => {
  38. it( 'should react on view#position', () => {
  39. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_se' ) ).to.true;
  40. view.position = 'sw';
  41. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_sw' ) ).to.true;
  42. } );
  43. } );
  44. describe( 'isVisible', () => {
  45. it( 'should react on view#isvisible', () => {
  46. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.false;
  47. view.isVisible = true;
  48. expect( view.element.classList.contains( 'ck-balloon-panel_visible' ) ).to.true;
  49. } );
  50. } );
  51. describe( 'styles', () => {
  52. it( 'should react on view#top', () => {
  53. expect( view.element.style.top ).to.equal( '0px' );
  54. view.top = 10;
  55. expect( view.element.style.top ).to.equal( '10px' );
  56. } );
  57. it( 'should react on view#left', () => {
  58. expect( view.element.style.left ).to.equal( '0px' );
  59. view.left = 10;
  60. expect( view.element.style.left ).to.equal( '10px' );
  61. } );
  62. it( 'should react on view#maxWidth', () => {
  63. expect( view.element.style.maxWidth ).to.equal( '200px' );
  64. view.maxWidth = 10;
  65. expect( view.element.style.maxWidth ).to.equal( '10px' );
  66. } );
  67. } );
  68. describe( 'children', () => {
  69. it( 'should react on view#content', () => {
  70. expect( view.element.childNodes.length ).to.equal( 0 );
  71. const button = new ButtonView( { t() {} } );
  72. view.content.add( button );
  73. expect( view.element.childNodes.length ).to.equal( 1 );
  74. } );
  75. } );
  76. } );
  77. describe( 'isVisible', () => {
  78. it( 'should return view#isVisible value', () => {
  79. expect( view.isVisible ).to.false;
  80. view.isVisible = true;
  81. expect( view.isVisible ).to.true;
  82. } );
  83. } );
  84. describe( 'show()', () => {
  85. it( 'should set view#isVisible as true', () => {
  86. view.isVisible = false;
  87. view.show();
  88. expect( view.isVisible ).to.true;
  89. } );
  90. } );
  91. describe( 'hide()', () => {
  92. it( 'should set view#isVisible as false', () => {
  93. view.isVisible = true;
  94. view.hide();
  95. expect( view.isVisible ).to.false;
  96. } );
  97. } );
  98. describe( 'attachTo()', () => {
  99. let target, limiter;
  100. beforeEach( () => {
  101. limiter = document.createElement( 'div' );
  102. target = document.createElement( 'div' );
  103. // Mock balloon panel element dimensions.
  104. mockBoundingBox( view.element, {
  105. top: 0,
  106. left: 0,
  107. width: 100,
  108. height: 100
  109. } );
  110. // Mock window dimensions.
  111. windowStub = {
  112. innerWidth: 500,
  113. innerHeight: 500,
  114. scrollX: 0,
  115. scrollY: 0,
  116. getComputedStyle: ( el ) => {
  117. return window.getComputedStyle( el );
  118. }
  119. };
  120. testUtils.sinon.stub( global, 'window', windowStub );
  121. } );
  122. it( 'should use default options', () => {
  123. const spy = testUtils.sinon.spy( positionUtils, 'getOptimalPosition' );
  124. view.attachTo( { target } );
  125. sinon.assert.calledWithExactly( spy, sinon.match( {
  126. element: view.element,
  127. target: target,
  128. positions: [
  129. BalloonPanelView.defaultPositions.se,
  130. BalloonPanelView.defaultPositions.sw,
  131. BalloonPanelView.defaultPositions.ne,
  132. BalloonPanelView.defaultPositions.nw
  133. ],
  134. limiter: document.body,
  135. fitInViewport: true
  136. } ) );
  137. } );
  138. describe( 'limited by limiter element', () => {
  139. beforeEach( () => {
  140. // Mock limiter element dimensions.
  141. mockBoundingBox( limiter, {
  142. left: 0,
  143. top: 0,
  144. width: 500,
  145. height: 500
  146. } );
  147. } );
  148. it( 'should put balloon on the `south east` side of the target element at default', () => {
  149. // Place target element at the center of the limiter.
  150. mockBoundingBox( target, {
  151. top: 225,
  152. left: 225,
  153. width: 50,
  154. height: 50
  155. } );
  156. view.attachTo( { target, limiter } );
  157. expect( view.position ).to.equal( 'se' );
  158. } );
  159. it( 'should put balloon on the `south east` side of the target element when target is on the top left side of the limiter', () => {
  160. mockBoundingBox( target, {
  161. top: 0,
  162. left: 0,
  163. width: 50,
  164. height: 50
  165. } );
  166. view.attachTo( { target, limiter } );
  167. expect( view.position ).to.equal( 'se' );
  168. } );
  169. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  170. mockBoundingBox( target, {
  171. top: 0,
  172. left: 450,
  173. width: 50,
  174. height: 50
  175. } );
  176. view.attachTo( { target, limiter } );
  177. expect( view.position ).to.equal( 'sw' );
  178. } );
  179. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  180. mockBoundingBox( target, {
  181. top: 450,
  182. left: 0,
  183. width: 50,
  184. height: 50
  185. } );
  186. view.attachTo( { target, limiter } );
  187. expect( view.position ).to.equal( 'ne' );
  188. } );
  189. it( 'should put balloon on the `north west` side of the target element when target is on the bottom right of the limiter', () => {
  190. mockBoundingBox( target, {
  191. top: 450,
  192. left: 450,
  193. width: 50,
  194. height: 50
  195. } );
  196. view.attachTo( { target, limiter } );
  197. expect( view.position ).to.equal( 'nw' );
  198. } );
  199. // #126
  200. it( 'works in a positioned ancestor (position: absolute)', () => {
  201. const positionedAncestor = document.createElement( 'div' );
  202. positionedAncestor.style.position = 'absolute';
  203. positionedAncestor.style.top = '100px';
  204. positionedAncestor.style.left = '100px';
  205. positionedAncestor.appendChild( view.element );
  206. document.body.appendChild( positionedAncestor );
  207. positionedAncestor.appendChild( view.element );
  208. mockBoundingBox( positionedAncestor, {
  209. top: 100,
  210. left: 100,
  211. width: 10,
  212. height: 10
  213. } );
  214. mockBoundingBox( target, {
  215. top: 0,
  216. left: 0,
  217. width: 100,
  218. height: 100
  219. } );
  220. view.attachTo( { target, limiter } );
  221. expect( view.top ).to.equal( 15 );
  222. expect( view.left ).to.equal( -80 );
  223. } );
  224. // #126
  225. it( 'works in a positioned ancestor (position: static)', () => {
  226. const positionedAncestor = document.createElement( 'div' );
  227. positionedAncestor.style.position = 'static';
  228. positionedAncestor.appendChild( view.element );
  229. document.body.appendChild( positionedAncestor );
  230. positionedAncestor.appendChild( view.element );
  231. mockBoundingBox( target, {
  232. top: 0,
  233. left: 0,
  234. width: 100,
  235. height: 100
  236. } );
  237. view.attachTo( { target, limiter } );
  238. expect( view.top ).to.equal( 115 );
  239. expect( view.left ).to.equal( 20 );
  240. } );
  241. } );
  242. describe( 'limited by viewport', () => {
  243. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  244. mockBoundingBox( limiter, {
  245. left: 0,
  246. top: 0,
  247. width: 500,
  248. height: 500
  249. } );
  250. mockBoundingBox( target, {
  251. top: 0,
  252. left: 225,
  253. width: 50,
  254. height: 50
  255. } );
  256. Object.assign( windowStub, {
  257. innerWidth: 275
  258. } );
  259. view.attachTo( { target, limiter } );
  260. expect( view.position ).to.equal( 'sw' );
  261. } );
  262. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  263. mockBoundingBox( limiter, {
  264. top: 0,
  265. left: -400,
  266. width: 500,
  267. height: 500
  268. } );
  269. mockBoundingBox( target, {
  270. top: 0,
  271. left: 0,
  272. width: 50,
  273. height: 50
  274. } );
  275. view.attachTo( { target, limiter } );
  276. expect( view.position ).to.equal( 'se' );
  277. } );
  278. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  279. mockBoundingBox( limiter, {
  280. left: 0,
  281. top: 0,
  282. width: 500,
  283. height: 500
  284. } );
  285. mockBoundingBox( target, {
  286. top: 225,
  287. left: 0,
  288. width: 50,
  289. height: 50
  290. } );
  291. Object.assign( windowStub, {
  292. innerHeight: 275
  293. } );
  294. view.attachTo( { target, limiter } );
  295. expect( view.position ).to.equal( 'ne' );
  296. } );
  297. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  298. mockBoundingBox( limiter, {
  299. left: 0,
  300. top: -400,
  301. width: 500,
  302. height: 500
  303. } );
  304. mockBoundingBox( target, {
  305. top: 0,
  306. left: 0,
  307. width: 50,
  308. height: 50
  309. } );
  310. view.attachTo( { target, limiter } );
  311. expect( view.position ).to.equal( 'se' );
  312. } );
  313. } );
  314. } );
  315. describe( 'keepAttachedTo()', () => {
  316. let attachToSpy, target, limiter, notRelatedElement;
  317. beforeEach( () => {
  318. attachToSpy = testUtils.sinon.spy( view, 'attachTo' );
  319. limiter = document.createElement( 'div' );
  320. target = document.createElement( 'div' );
  321. notRelatedElement = document.createElement( 'div' );
  322. document.body.appendChild( limiter );
  323. document.body.appendChild( notRelatedElement );
  324. } );
  325. afterEach( () => {
  326. attachToSpy.restore();
  327. limiter.remove();
  328. notRelatedElement.remove();
  329. } );
  330. it( 'should attach balloon to the target constantly when any of element containing target element is scrolled', () => {
  331. view.keepAttachedTo( { target, limiter } );
  332. expect( attachToSpy.calledOnce ).to.true;
  333. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  334. document.dispatchEvent( new Event( 'scroll' ) );
  335. expect( attachToSpy.calledTwice ).to.true;
  336. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  337. limiter.dispatchEvent( new Event( 'scroll' ) );
  338. expect( attachToSpy.calledThrice ).to.true;
  339. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  340. notRelatedElement.dispatchEvent( new Event( 'scroll' ) );
  341. // Nothing's changed.
  342. expect( attachToSpy.calledThrice ).to.true;
  343. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  344. } );
  345. it( 'should stop attach when balloon is hidden', () => {
  346. view.keepAttachedTo( { target, limiter } );
  347. expect( attachToSpy.calledOnce ).to.true;
  348. view.hide();
  349. document.dispatchEvent( new Event( 'scroll' ) );
  350. // Still once.
  351. expect( attachToSpy.calledOnce ).to.true;
  352. } );
  353. it( 'should attach balloon to the target constantly when browser window is resized', () => {
  354. view.keepAttachedTo( { target, limiter } );
  355. expect( attachToSpy.calledOnce ).to.true;
  356. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  357. window.dispatchEvent( new Event( 'resize' ) );
  358. expect( attachToSpy.calledTwice ).to.true;
  359. expect( attachToSpy.lastCall.args[ 0 ] ).to.deep.equal( { target, limiter } );
  360. } );
  361. it( 'should stop attach when balloon is hidden', () => {
  362. view.keepAttachedTo( { target, limiter } );
  363. expect( attachToSpy.calledOnce ).to.true;
  364. view.hide();
  365. window.dispatchEvent( new Event( 'resize' ) );
  366. // Still once.
  367. expect( attachToSpy.calledOnce ).to.true;
  368. } );
  369. } );
  370. } );
  371. function mockBoundingBox( element, data ) {
  372. const boundingBox = Object.assign( {}, data );
  373. boundingBox.right = boundingBox.left + boundingBox.width;
  374. boundingBox.bottom = boundingBox.top + boundingBox.height;
  375. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  376. }