8
0

balloonpanelview.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window, document */
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import ViewCollection from '../../src/viewcollection';
  8. import BalloonPanelView from '../../src/balloonpanel/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. windowStub = {
  19. innerWidth: 1000,
  20. innerHeight: 1000,
  21. scrollX: 0,
  22. scrollY: 0,
  23. getComputedStyle: ( el ) => {
  24. return window.getComputedStyle( el );
  25. }
  26. };
  27. testUtils.sinon.stub( global, 'window', windowStub );
  28. return view.init();
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'should create element from template', () => {
  32. expect( view.element.tagName ).to.equal( 'DIV' );
  33. expect( view.element.classList.contains( 'ck-balloon-panel' ) ).to.true;
  34. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  35. } );
  36. it( 'should set default values', () => {
  37. expect( view.top ).to.equal( 0 );
  38. expect( view.left ).to.equal( 0 );
  39. expect( view.position ).to.equal( 'se' );
  40. expect( view.isVisible ).to.equal( false );
  41. } );
  42. it( 'creates view#content collection', () => {
  43. expect( view.content ).to.be.instanceOf( ViewCollection );
  44. } );
  45. } );
  46. describe( 'DOM bindings', () => {
  47. describe( 'arrow', () => {
  48. it( 'should react on view#position', () => {
  49. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_se' ) ).to.true;
  50. view.position = 'sw';
  51. expect( view.element.classList.contains( 'ck-balloon-panel_arrow_sw' ) ).to.true;
  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( 'children', () => {
  79. it( 'should react on view#content', () => {
  80. expect( view.element.childNodes.length ).to.equal( 0 );
  81. const button = new ButtonView( { t() {} } );
  82. view.content.add( button );
  83. expect( view.element.childNodes.length ).to.equal( 1 );
  84. } );
  85. } );
  86. } );
  87. describe( 'isVisible', () => {
  88. it( 'should return view#isVisible value', () => {
  89. expect( view.isVisible ).to.false;
  90. view.isVisible = true;
  91. expect( view.isVisible ).to.true;
  92. } );
  93. } );
  94. describe( 'show()', () => {
  95. it( 'should set view#isVisible as true', () => {
  96. view.isVisible = false;
  97. view.show();
  98. expect( view.isVisible ).to.true;
  99. } );
  100. } );
  101. describe( 'hide()', () => {
  102. it( 'should set view#isVisible as false', () => {
  103. view.isVisible = true;
  104. view.hide();
  105. expect( view.isVisible ).to.false;
  106. } );
  107. } );
  108. describe( 'attachTo()', () => {
  109. let target, limiter;
  110. beforeEach( () => {
  111. limiter = document.createElement( 'div' );
  112. target = document.createElement( 'div' );
  113. // Mock balloon panel element dimensions.
  114. mockBoundingBox( view.element, {
  115. top: 0,
  116. left: 0,
  117. width: 100,
  118. height: 100
  119. } );
  120. // Make sure that limiter is fully visible in viewport.
  121. Object.assign( windowStub, {
  122. innerWidth: 500,
  123. innerHeight: 500
  124. } );
  125. } );
  126. it( 'should use default options', () => {
  127. const spy = testUtils.sinon.spy( positionUtils, 'getOptimalPosition' );
  128. view.attachTo( { target } );
  129. sinon.assert.calledWithExactly( spy, sinon.match( {
  130. element: view.element,
  131. target: target,
  132. positions: [
  133. BalloonPanelView.defaultPositions.se,
  134. BalloonPanelView.defaultPositions.sw,
  135. BalloonPanelView.defaultPositions.ne,
  136. BalloonPanelView.defaultPositions.nw
  137. ],
  138. limiter: document.body,
  139. fitInViewport: true
  140. } ) );
  141. } );
  142. describe( 'limited by limiter element', () => {
  143. beforeEach( () => {
  144. // Mock limiter element dimensions.
  145. mockBoundingBox( limiter, {
  146. left: 0,
  147. top: 0,
  148. width: 500,
  149. height: 500
  150. } );
  151. } );
  152. it( 'should put balloon on the `south east` side of the target element at default', () => {
  153. // Place target element at the center of the limiter.
  154. mockBoundingBox( target, {
  155. top: 225,
  156. left: 225,
  157. width: 50,
  158. height: 50
  159. } );
  160. view.attachTo( { target, limiter } );
  161. expect( view.position ).to.equal( 'se' );
  162. } );
  163. it( 'should put balloon on the `south east` side of the target element when target is on the top left side of the limiter', () => {
  164. mockBoundingBox( target, {
  165. top: 0,
  166. left: 0,
  167. width: 50,
  168. height: 50
  169. } );
  170. view.attachTo( { target, limiter } );
  171. expect( view.position ).to.equal( 'se' );
  172. } );
  173. it( 'should put balloon on the `south west` side of the target element when target is on the right side of the limiter', () => {
  174. mockBoundingBox( target, {
  175. top: 0,
  176. left: 450,
  177. width: 50,
  178. height: 50
  179. } );
  180. view.attachTo( { target, limiter } );
  181. expect( view.position ).to.equal( 'sw' );
  182. } );
  183. it( 'should put balloon on the `north east` side of the target element when target is on the bottom of the limiter ', () => {
  184. mockBoundingBox( target, {
  185. top: 450,
  186. left: 0,
  187. width: 50,
  188. height: 50
  189. } );
  190. view.attachTo( { target, limiter } );
  191. expect( view.position ).to.equal( 'ne' );
  192. } );
  193. it( 'should put balloon on the `north west` side of the target element when target is on the bottom right of the limiter', () => {
  194. mockBoundingBox( target, {
  195. top: 450,
  196. left: 450,
  197. width: 50,
  198. height: 50
  199. } );
  200. view.attachTo( { target, limiter } );
  201. expect( view.position ).to.equal( 'nw' );
  202. } );
  203. // #126
  204. it( 'works in a positioned ancestor (position: absolute)', () => {
  205. const positionedAncestor = document.createElement( 'div' );
  206. positionedAncestor.style.position = 'absolute';
  207. positionedAncestor.style.top = '100px';
  208. positionedAncestor.style.left = '100px';
  209. positionedAncestor.appendChild( view.element );
  210. document.body.appendChild( positionedAncestor );
  211. positionedAncestor.appendChild( view.element );
  212. mockBoundingBox( positionedAncestor, {
  213. top: 100,
  214. left: 100,
  215. width: 10,
  216. height: 10
  217. } );
  218. mockBoundingBox( target, {
  219. top: 0,
  220. left: 0,
  221. width: 100,
  222. height: 100
  223. } );
  224. view.attachTo( { target, limiter } );
  225. expect( view.top ).to.equal( 15 );
  226. expect( view.left ).to.equal( -80 );
  227. } );
  228. // #126
  229. it( 'works in a positioned ancestor (position: static)', () => {
  230. const positionedAncestor = document.createElement( 'div' );
  231. positionedAncestor.style.position = 'static';
  232. positionedAncestor.appendChild( view.element );
  233. document.body.appendChild( positionedAncestor );
  234. positionedAncestor.appendChild( view.element );
  235. mockBoundingBox( target, {
  236. top: 0,
  237. left: 0,
  238. width: 100,
  239. height: 100
  240. } );
  241. view.attachTo( { target, limiter } );
  242. expect( view.top ).to.equal( 115 );
  243. expect( view.left ).to.equal( 20 );
  244. } );
  245. } );
  246. describe( 'limited by viewport', () => {
  247. it( 'should put balloon on the `south west` position when `south east` is limited', () => {
  248. mockBoundingBox( limiter, {
  249. left: 0,
  250. top: 0,
  251. width: 500,
  252. height: 500
  253. } );
  254. mockBoundingBox( target, {
  255. top: 0,
  256. left: 225,
  257. width: 50,
  258. height: 50
  259. } );
  260. Object.assign( windowStub, {
  261. innerWidth: 275
  262. } );
  263. view.attachTo( { target, limiter } );
  264. expect( view.position ).to.equal( 'sw' );
  265. } );
  266. it( 'should put balloon on the `south east` position when `south west` is limited', () => {
  267. mockBoundingBox( limiter, {
  268. top: 0,
  269. left: -400,
  270. width: 500,
  271. height: 500
  272. } );
  273. mockBoundingBox( target, {
  274. top: 0,
  275. left: 0,
  276. width: 50,
  277. height: 50
  278. } );
  279. view.attachTo( { target, limiter } );
  280. expect( view.position ).to.equal( 'se' );
  281. } );
  282. it( 'should put balloon on the `north east` position when `south east` is limited', () => {
  283. mockBoundingBox( limiter, {
  284. left: 0,
  285. top: 0,
  286. width: 500,
  287. height: 500
  288. } );
  289. mockBoundingBox( target, {
  290. top: 225,
  291. left: 0,
  292. width: 50,
  293. height: 50
  294. } );
  295. Object.assign( windowStub, {
  296. innerHeight: 275
  297. } );
  298. view.attachTo( { target, limiter } );
  299. expect( view.position ).to.equal( 'ne' );
  300. } );
  301. it( 'should put balloon on the `south east` position when `north east` is limited', () => {
  302. mockBoundingBox( limiter, {
  303. left: 0,
  304. top: -400,
  305. width: 500,
  306. height: 500
  307. } );
  308. mockBoundingBox( target, {
  309. top: 0,
  310. left: 0,
  311. width: 50,
  312. height: 50
  313. } );
  314. view.attachTo( { target, limiter } );
  315. expect( view.position ).to.equal( 'se' );
  316. } );
  317. } );
  318. } );
  319. } );
  320. function mockBoundingBox( element, data ) {
  321. const boundingBox = Object.assign( {}, data );
  322. boundingBox.right = boundingBox.left + boundingBox.width;
  323. boundingBox.bottom = boundingBox.top + boundingBox.height;
  324. testUtils.sinon.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  325. }