8
0

stickytoolbarview.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import StickyToolbarView from '../../../src/toolbar/sticky/stickytoolbarview';
  9. import ToolbarView from '../../../src/toolbar/toolbarview';
  10. import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
  11. testUtils.createSinonSandbox();
  12. describe( 'StickyToolbarView', () => {
  13. let view, element, limiterElement, locale, windowStub;
  14. beforeEach( () => {
  15. locale = {};
  16. limiterElement = document.createElement( 'div' );
  17. view = new StickyToolbarView( locale );
  18. element = view.element;
  19. // Dummy values just to let non–geometrical tests pass without reference errors.
  20. view._toolbarRect = { top: 10, right: 20, bottom: 30, left: 40, width: 50, height: 60 };
  21. view._limiterRect = { top: 5, right: 10, bottom: 15, left: 20, width: 25, height: 30 };
  22. windowStub = Object.create( DomEmitterMixin );
  23. Object.assign( windowStub, {
  24. scrollX: 0,
  25. scrollY: 0
  26. } );
  27. testUtils.sinon.stub( global, 'window' ).value( windowStub );
  28. document.body.appendChild( element );
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'inherits from ToolbarView', () => {
  32. expect( view ).to.be.instanceof( ToolbarView );
  33. } );
  34. it( 'sets view attributes', () => {
  35. expect( view.isSticky ).to.be.false;
  36. expect( view.limiterElement ).to.be.null;
  37. expect( view.limiterBottomOffset ).to.equal( 50 );
  38. expect( view.viewportTopOffset ).to.equal( 0 );
  39. expect( view._isStickyToTheLimiter ).to.be.false;
  40. expect( view._hasViewportTopOffset ).to.be.false;
  41. expect( view._marginLeft ).to.be.null;
  42. } );
  43. it( 'accepts the locale', () => {
  44. expect( view.locale ).to.equal( locale );
  45. } );
  46. it( 'creates the _elementPlaceholder', () => {
  47. expect( view._elementPlaceholder.classList.contains( 'ck-toolbar__placeholder' ) ).to.be.true;
  48. } );
  49. } );
  50. describe( 'element view bindings', () => {
  51. beforeEach( () => {
  52. view.limiterElement = limiterElement;
  53. } );
  54. it( 'update the class on view#isSticky change', () => {
  55. view.isSticky = false;
  56. expect( element.classList.contains( 'ck-toolbar_sticky' ) ).to.be.false;
  57. view.isSticky = true;
  58. expect( element.classList.contains( 'ck-toolbar_sticky' ) ).to.be.true;
  59. } );
  60. it( 'update the class on view#_isStickyToTheLimiter change', () => {
  61. view._isStickyToTheLimiter = false;
  62. expect( element.classList.contains( 'ck-toolbar_sticky_bottom-limit' ) ).to.be.false;
  63. view._isStickyToTheLimiter = true;
  64. expect( element.classList.contains( 'ck-toolbar_sticky_bottom-limit' ) ).to.be.true;
  65. } );
  66. it( 'update the styles.top on view#_hasViewportTopOffset change', () => {
  67. view.viewportTopOffset = 100;
  68. view._hasViewportTopOffset = false;
  69. expect( element.style.top ).to.equal( '' );
  70. view._hasViewportTopOffset = true;
  71. expect( element.style.top ).to.equal( '100px' );
  72. } );
  73. it( 'update the styles.width on view#isSticky change', () => {
  74. testUtils.sinon.stub( view._elementPlaceholder, 'getBoundingClientRect' ).returns( { width: 100 } );
  75. view.isSticky = false;
  76. expect( element.style.width ).to.equal( '' );
  77. view.isSticky = true;
  78. expect( element.style.width ).to.equal( '100px' );
  79. } );
  80. it( 'update the styles.bottom on view#_isStickyToTheLimiter change', () => {
  81. view._isStickyToTheLimiter = false;
  82. expect( element.style.bottom ).to.equal( '' );
  83. view._isStickyToTheLimiter = true;
  84. expect( element.style.bottom ).to.equal( '50px' );
  85. } );
  86. it( 'update the styles.marginLeft on view#marginLeft change', () => {
  87. view._marginLeft = '30px';
  88. expect( element.style.marginLeft ).to.equal( '30px' );
  89. view._marginLeft = '10px';
  90. expect( element.style.marginLeft ).to.equal( '10px' );
  91. } );
  92. } );
  93. describe( '_elementPlaceholder view bindings', () => {
  94. it( 'update the styles.display on view#isSticky change', () => {
  95. view.isSticky = false;
  96. expect( view._elementPlaceholder.style.display ).to.equal( 'none' );
  97. view.isSticky = true;
  98. expect( view._elementPlaceholder.style.display ).to.equal( 'block' );
  99. } );
  100. it( 'update the styles.height on view#isSticky change', () => {
  101. view._toolbarRect = { height: 50 };
  102. view.isSticky = false;
  103. expect( view._elementPlaceholder.style.height ).to.equal( '' );
  104. view.isSticky = true;
  105. expect( view._elementPlaceholder.style.height ).to.equal( '50px' );
  106. } );
  107. } );
  108. describe( 'init()', () => {
  109. beforeEach( () => {
  110. view.limiterElement = limiterElement;
  111. } );
  112. afterEach( () => {
  113. return view.destroy();
  114. } );
  115. it( 'calls init on parent class', () => {
  116. const spy = testUtils.sinon.spy( ToolbarView.prototype, 'init' );
  117. view.init();
  118. expect( spy.calledOnce ).to.be.true;
  119. } );
  120. it( 'puts the view._elementPlaceholder before view.element', () => {
  121. view.init();
  122. expect( element.previousSibling ).to.equal( view._elementPlaceholder );
  123. } );
  124. it( 'checks if the toolbar should be sticky', () => {
  125. const spy = testUtils.sinon.spy( view, '_checkIfShouldBeSticky' );
  126. expect( spy.notCalled ).to.be.true;
  127. view.init();
  128. expect( spy.calledOnce ).to.be.true;
  129. } );
  130. it( 'listens to window#scroll event and calls view._checkIfShouldBeSticky', () => {
  131. const spy = testUtils.sinon.spy( view, '_checkIfShouldBeSticky' );
  132. expect( spy.notCalled ).to.be.true;
  133. view.init();
  134. global.window.fire( 'scroll' );
  135. expect( spy.calledTwice ).to.be.true;
  136. } );
  137. it( 'listens to view.isActive and calls view._checkIfShouldBeSticky', () => {
  138. const spy = testUtils.sinon.spy( view, '_checkIfShouldBeSticky' );
  139. expect( spy.notCalled ).to.be.true;
  140. view.init();
  141. view.isActive = true;
  142. expect( spy.calledTwice ).to.be.true;
  143. view.isActive = false;
  144. expect( spy.calledThrice ).to.be.true;
  145. } );
  146. } );
  147. describe( 'destroy()', () => {
  148. it( 'can be called multiple times', () => {
  149. expect( () => {
  150. view.destroy();
  151. view.destroy();
  152. } ).to.not.throw();
  153. } );
  154. it( 'calls destroy on parent class', () => {
  155. const spy = testUtils.sinon.spy( ToolbarView.prototype, 'destroy' );
  156. view.destroy();
  157. expect( spy.calledOnce ).to.be.true;
  158. } );
  159. it( 'removes view._elementPlaceholder from DOM', () => {
  160. view.destroy();
  161. expect( view._elementPlaceholder.parentNode ).to.be.null;
  162. } );
  163. } );
  164. describe( '_checkIfShouldBeSticky', () => {
  165. beforeEach( () => {
  166. view.limiterElement = limiterElement;
  167. } );
  168. describe( 'view.isSticky', () => {
  169. beforeEach( () => {
  170. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  171. height: 20
  172. } );
  173. } );
  174. it( 'is true if beyond the top of the viewport (toolbar is active)', () => {
  175. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( { top: -10, height: 100 } );
  176. view.isActive = true;
  177. expect( view.isSticky ).to.be.false;
  178. view._checkIfShouldBeSticky();
  179. expect( view.isSticky ).to.be.true;
  180. } );
  181. it( 'is false if beyond the top of the viewport (toolbar is inactive)', () => {
  182. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( { top: -10, height: 100 } );
  183. view.isActive = false;
  184. expect( view.isSticky ).to.be.false;
  185. view._checkIfShouldBeSticky();
  186. expect( view.isSticky ).to.be.false;
  187. } );
  188. it( 'is false if in the viewport (toolbar is active)', () => {
  189. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( { top: 10, height: 100 } );
  190. view.isActive = true;
  191. expect( view.isSticky ).to.be.false;
  192. view._checkIfShouldBeSticky();
  193. expect( view.isSticky ).to.be.false;
  194. } );
  195. it( 'is false if view.limiterElement is smaller than the toolbar and view.limiterBottomOffset (toolbar is active)', () => {
  196. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( { top: -10, height: 60 } );
  197. view.isActive = true;
  198. view.limiterBottomOffset = 50;
  199. expect( view.isSticky ).to.be.false;
  200. view._checkIfShouldBeSticky();
  201. expect( view.isSticky ).to.be.false;
  202. } );
  203. } );
  204. describe( 'view._isStickyToTheLimiter', () => {
  205. it( 'is true if view.isSticky is true and reached the bottom edge of view.limiterElement', () => {
  206. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  207. top: -10,
  208. bottom: 10,
  209. height: 100
  210. } );
  211. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  212. height: 20
  213. } );
  214. view.isActive = true;
  215. expect( view.isSticky ).to.be.false;
  216. expect( view._isStickyToTheLimiter ).to.be.false;
  217. view._checkIfShouldBeSticky();
  218. expect( view.isSticky ).to.be.true;
  219. expect( view._isStickyToTheLimiter ).to.be.true;
  220. } );
  221. it( 'is false if view.isSticky is true and not reached the bottom edge of view.limiterElement', () => {
  222. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  223. top: -10,
  224. bottom: 90,
  225. height: 100
  226. } );
  227. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  228. height: 20
  229. } );
  230. view.isActive = true;
  231. expect( view.isSticky ).to.be.false;
  232. expect( view._isStickyToTheLimiter ).to.be.false;
  233. view._checkIfShouldBeSticky();
  234. expect( view.isSticky ).to.be.true;
  235. expect( view._isStickyToTheLimiter ).to.be.false;
  236. } );
  237. it( 'is false if view.isSticky is false', () => {
  238. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  239. top: 10,
  240. } );
  241. view.isActive = true;
  242. expect( view.isSticky ).to.be.false;
  243. expect( view._isStickyToTheLimiter ).to.be.false;
  244. view._checkIfShouldBeSticky();
  245. expect( view.isSticky ).to.be.false;
  246. expect( view._isStickyToTheLimiter ).to.be.false;
  247. } );
  248. } );
  249. describe( 'view._hasViewportTopOffset', () => {
  250. it( 'is true if view._isStickyToTheLimiter is false and view.viewportTopOffset has been specified', () => {
  251. view.viewportTopOffset = 100;
  252. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  253. top: 90,
  254. bottom: 190,
  255. height: 100
  256. } );
  257. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  258. height: 20
  259. } );
  260. view.isActive = true;
  261. view._checkIfShouldBeSticky();
  262. expect( view.isSticky ).to.be.true;
  263. expect( view._isStickyToTheLimiter ).to.be.false;
  264. expect( view._hasViewportTopOffset ).to.be.true;
  265. } );
  266. it( 'is false if view._isStickyToTheLimiter is true and view.viewportTopOffset has been specified', () => {
  267. view.viewportTopOffset = 100;
  268. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  269. top: 10,
  270. bottom: 110,
  271. height: 100
  272. } );
  273. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  274. height: 20
  275. } );
  276. view.isActive = true;
  277. view._checkIfShouldBeSticky();
  278. expect( view.isSticky ).to.be.true;
  279. expect( view._isStickyToTheLimiter ).to.be.true;
  280. expect( view._hasViewportTopOffset ).to.be.false;
  281. } );
  282. it( 'is false if view._isStickyToTheLimiter is false and view.viewportTopOffset is 0', () => {
  283. view.viewportTopOffset = 100;
  284. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  285. top: 90,
  286. bottom: 190,
  287. height: 100
  288. } );
  289. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  290. height: 20
  291. } );
  292. view.isActive = true;
  293. view._checkIfShouldBeSticky();
  294. expect( view.isSticky ).to.be.true;
  295. expect( view._isStickyToTheLimiter ).to.be.false;
  296. expect( view._hasViewportTopOffset ).to.be.true;
  297. } );
  298. } );
  299. describe( 'view._marginLeft', () => {
  300. it( 'is set if view.isSticky is true view._isStickyToTheLimiter is false', () => {
  301. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  302. top: -10,
  303. bottom: 80,
  304. height: 100
  305. } );
  306. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  307. height: 20
  308. } );
  309. Object.assign( windowStub, {
  310. scrollX: 10,
  311. scrollY: 0
  312. } );
  313. view.isActive = true;
  314. expect( view.isSticky ).to.be.false;
  315. expect( view._isStickyToTheLimiter ).to.be.false;
  316. expect( view._marginLeft ).to.equal( null );
  317. view._checkIfShouldBeSticky();
  318. expect( view.isSticky ).to.be.true;
  319. expect( view._isStickyToTheLimiter ).to.be.false;
  320. expect( view._marginLeft ).to.equal( '-10px' );
  321. } );
  322. it( 'is not set if view._isStickyToTheLimiter is true', () => {
  323. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  324. top: -10,
  325. bottom: 10,
  326. left: 60,
  327. height: 100
  328. } );
  329. testUtils.sinon.stub( view.element, 'getBoundingClientRect' ).returns( {
  330. height: 20
  331. } );
  332. testUtils.sinon.stub( document.body, 'getBoundingClientRect' ).returns( {
  333. left: 40
  334. } );
  335. view.isActive = true;
  336. expect( view.isSticky ).to.be.false;
  337. expect( view._isStickyToTheLimiter ).to.be.false;
  338. expect( view._marginLeft ).to.equal( null );
  339. view._checkIfShouldBeSticky();
  340. expect( view.isSticky ).to.be.true;
  341. expect( view._isStickyToTheLimiter ).to.be.true;
  342. expect( view._marginLeft ).to.equal( null );
  343. } );
  344. it( 'is not set if view.isSticky is false', () => {
  345. testUtils.sinon.stub( view.limiterElement, 'getBoundingClientRect' ).returns( {
  346. top: 10,
  347. } );
  348. view.isActive = true;
  349. expect( view.isSticky ).to.be.false;
  350. expect( view._isStickyToTheLimiter ).to.be.false;
  351. expect( view._marginLeft ).to.equal( null );
  352. view._checkIfShouldBeSticky();
  353. expect( view.isSticky ).to.be.false;
  354. expect( view._isStickyToTheLimiter ).to.be.false;
  355. expect( view._marginLeft ).to.equal( null );
  356. } );
  357. } );
  358. } );
  359. } );