dropdownview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import DropdownView from '../../src/dropdown/dropdownview';
  6. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  7. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import ButtonView from '../../src/button/buttonview';
  10. import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'DropdownView', () => {
  14. let view, buttonView, panelView, locale;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = {
  18. uiLanguageDirection: 'ltr',
  19. t() {}
  20. };
  21. buttonView = new ButtonView( locale );
  22. panelView = new DropdownPanelView( locale );
  23. view = new DropdownView( locale, buttonView, panelView );
  24. view.render();
  25. // The #panelView positioning depends on the utility that uses DOM Rects.
  26. // To avoid an avalanche of warnings (DOM Rects do not work until
  27. // the element is in DOM), let's allow the dropdown to render in DOM.
  28. global.document.body.appendChild( view.element );
  29. } );
  30. afterEach( () => {
  31. view.element.remove();
  32. } );
  33. describe( 'constructor()', () => {
  34. it( 'sets view#locale', () => {
  35. expect( view.locale ).to.equal( locale );
  36. } );
  37. it( 'sets view#buttonView', () => {
  38. expect( view.buttonView ).to.equal( buttonView );
  39. } );
  40. it( 'sets view#panelView', () => {
  41. expect( view.panelView ).to.equal( panelView );
  42. } );
  43. it( 'sets view#isOpen false', () => {
  44. expect( view.isOpen ).to.be.false;
  45. } );
  46. it( 'sets view#isEnabled true', () => {
  47. expect( view.isEnabled ).to.be.true;
  48. } );
  49. it( 'sets view#panelPosition "auto"', () => {
  50. expect( view.panelPosition ).to.equal( 'auto' );
  51. } );
  52. it( 'creates #focusTracker instance', () => {
  53. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  54. } );
  55. it( 'creates #keystrokeHandler instance', () => {
  56. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  57. } );
  58. it( 'creates #element from template', () => {
  59. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  60. expect( view.element.classList.contains( 'ck-dropdown' ) ).to.be.true;
  61. expect( view.element.children ).to.have.length( 2 );
  62. expect( view.element.children[ 0 ] ).to.equal( buttonView.element );
  63. expect( view.element.children[ 1 ] ).to.equal( panelView.element );
  64. } );
  65. it( 'sets view#buttonView class', () => {
  66. expect( view.buttonView.element.classList.contains( 'ck-dropdown__button' ) ).to.be.true;
  67. } );
  68. describe( 'bindings', () => {
  69. describe( 'view#isOpen to view.buttonView#select', () => {
  70. it( 'is activated', () => {
  71. const values = [];
  72. view.on( 'change:isOpen', () => {
  73. values.push( view.isOpen );
  74. } );
  75. view.buttonView.fire( 'open' );
  76. view.buttonView.fire( 'open' );
  77. view.buttonView.fire( 'open' );
  78. expect( values ).to.have.members( [ true, false, true ] );
  79. } );
  80. } );
  81. describe( 'view.panelView#isVisible to view#isOpen', () => {
  82. it( 'is activated', () => {
  83. const values = [];
  84. view.listenTo( view.panelView, 'change:isVisible', () => {
  85. values.push( view.isOpen );
  86. } );
  87. view.isOpen = true;
  88. view.isOpen = false;
  89. view.isOpen = true;
  90. expect( values ).to.have.members( [ true, false, true ] );
  91. } );
  92. } );
  93. describe( 'view.panelView#position to view#panelPosition', () => {
  94. it( 'does not update until the dropdown is open', () => {
  95. view.isOpen = false;
  96. view.panelPosition = 'nw';
  97. expect( panelView.position ).to.equal( 'se' );
  98. view.isOpen = true;
  99. expect( panelView.position ).to.equal( 'nw' );
  100. } );
  101. describe( 'in "auto" mode', () => {
  102. it( 'uses _getOptimalPosition() and a dedicated set of positions (LTR)', () => {
  103. const spy = testUtils.sinon.spy( DropdownView, '_getOptimalPosition' );
  104. const { southEast, southWest, northEast, northWest } = DropdownView.defaultPanelPositions;
  105. view.isOpen = true;
  106. sinon.assert.calledWithExactly( spy, sinon.match( {
  107. element: panelView.element,
  108. target: buttonView.element,
  109. positions: [
  110. southEast, southWest, northEast, northWest
  111. ],
  112. fitInViewport: true
  113. } ) );
  114. } );
  115. it( 'uses _getOptimalPosition() and a dedicated set of positions (RTL)', () => {
  116. const spy = testUtils.sinon.spy( DropdownView, '_getOptimalPosition' );
  117. const { southEast, southWest, northEast, northWest } = DropdownView.defaultPanelPositions;
  118. view.locale.uiLanguageDirection = 'rtl';
  119. view.isOpen = true;
  120. sinon.assert.calledWithExactly( spy, sinon.match( {
  121. element: panelView.element,
  122. target: buttonView.element,
  123. positions: [
  124. southWest, southEast, northWest, northEast
  125. ],
  126. fitInViewport: true
  127. } ) );
  128. } );
  129. } );
  130. } );
  131. describe( 'DOM element bindings', () => {
  132. describe( 'class', () => {
  133. it( 'reacts on view#isEnabled', () => {
  134. view.isEnabled = true;
  135. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.false;
  136. view.isEnabled = false;
  137. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true;
  138. } );
  139. it( 'reacts on view#class', () => {
  140. view.class = 'custom-css-class';
  141. expect( view.element.classList.contains( 'custom-css-class' ) ).to.be.true;
  142. } );
  143. } );
  144. } );
  145. } );
  146. } );
  147. describe( 'render()', () => {
  148. it( 'starts listening for #keystrokes coming from #element', () => {
  149. view = new DropdownView( locale,
  150. new ButtonView( locale ),
  151. new DropdownPanelView( locale ) );
  152. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  153. view.render();
  154. sinon.assert.calledOnce( spy );
  155. sinon.assert.calledWithExactly( spy, view.element );
  156. } );
  157. it( 'adds #element to #focusTracker', () => {
  158. view = new DropdownView( locale,
  159. new ButtonView( locale ),
  160. new DropdownPanelView( locale ) );
  161. const spy = sinon.spy( view.focusTracker, 'add' );
  162. view.render();
  163. sinon.assert.calledOnce( spy );
  164. sinon.assert.calledWithExactly( spy, view.element );
  165. } );
  166. describe( 'activates keyboard navigation for the dropdown', () => {
  167. it( 'so "arrowdown" opens the #panelView', () => {
  168. const keyEvtData = {
  169. keyCode: keyCodes.arrowdown,
  170. preventDefault: sinon.spy(),
  171. stopPropagation: sinon.spy()
  172. };
  173. view.buttonView.isEnabled = true;
  174. view.isOpen = true;
  175. view.keystrokes.press( keyEvtData );
  176. sinon.assert.notCalled( keyEvtData.preventDefault );
  177. sinon.assert.notCalled( keyEvtData.stopPropagation );
  178. expect( view.isOpen ).to.be.true;
  179. view.isOpen = false;
  180. view.keystrokes.press( keyEvtData );
  181. sinon.assert.calledOnce( keyEvtData.preventDefault );
  182. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  183. expect( view.isOpen ).to.be.true;
  184. } );
  185. it( 'so "arrowdown" won\'t open the #panelView when #isEnabled is false', () => {
  186. const keyEvtData = {
  187. keyCode: keyCodes.arrowdown,
  188. preventDefault: sinon.spy(),
  189. stopPropagation: sinon.spy()
  190. };
  191. view.buttonView.isEnabled = false;
  192. view.isOpen = false;
  193. view.keystrokes.press( keyEvtData );
  194. sinon.assert.notCalled( keyEvtData.preventDefault );
  195. sinon.assert.notCalled( keyEvtData.stopPropagation );
  196. expect( view.isOpen ).to.be.false;
  197. } );
  198. it( 'so "arrowright" is blocked', () => {
  199. const keyEvtData = {
  200. keyCode: keyCodes.arrowright,
  201. preventDefault: sinon.spy(),
  202. stopPropagation: sinon.spy()
  203. };
  204. view.false = true;
  205. view.keystrokes.press( keyEvtData );
  206. sinon.assert.notCalled( keyEvtData.preventDefault );
  207. sinon.assert.notCalled( keyEvtData.stopPropagation );
  208. expect( view.isOpen ).to.be.false;
  209. view.isOpen = true;
  210. view.keystrokes.press( keyEvtData );
  211. sinon.assert.calledOnce( keyEvtData.preventDefault );
  212. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  213. expect( view.isOpen ).to.be.true;
  214. } );
  215. it( 'so "arrowleft" closes the #panelView', () => {
  216. const keyEvtData = {
  217. keyCode: keyCodes.arrowleft,
  218. preventDefault: sinon.spy(),
  219. stopPropagation: sinon.spy()
  220. };
  221. const spy = sinon.spy( view.buttonView, 'focus' );
  222. view.isOpen = false;
  223. view.keystrokes.press( keyEvtData );
  224. sinon.assert.notCalled( keyEvtData.preventDefault );
  225. sinon.assert.notCalled( keyEvtData.stopPropagation );
  226. sinon.assert.notCalled( spy );
  227. expect( view.isOpen ).to.be.false;
  228. view.isOpen = true;
  229. view.keystrokes.press( keyEvtData );
  230. sinon.assert.calledOnce( keyEvtData.preventDefault );
  231. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  232. sinon.assert.calledOnce( spy );
  233. expect( view.isOpen ).to.be.false;
  234. } );
  235. it( 'so "esc" closes the #panelView', () => {
  236. const keyEvtData = {
  237. keyCode: keyCodes.esc,
  238. preventDefault: sinon.spy(),
  239. stopPropagation: sinon.spy()
  240. };
  241. const spy = sinon.spy( view.buttonView, 'focus' );
  242. view.isOpen = false;
  243. view.keystrokes.press( keyEvtData );
  244. sinon.assert.notCalled( keyEvtData.preventDefault );
  245. sinon.assert.notCalled( keyEvtData.stopPropagation );
  246. sinon.assert.notCalled( spy );
  247. expect( view.isOpen ).to.be.false;
  248. view.isOpen = true;
  249. view.keystrokes.press( keyEvtData );
  250. sinon.assert.calledOnce( keyEvtData.preventDefault );
  251. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  252. sinon.assert.calledOnce( spy );
  253. expect( view.isOpen ).to.be.false;
  254. } );
  255. } );
  256. } );
  257. describe( 'focus()', () => {
  258. it( 'focuses the #buttonView in DOM', () => {
  259. const spy = sinon.spy( view.buttonView, 'focus' );
  260. view.focus();
  261. sinon.assert.calledOnce( spy );
  262. } );
  263. } );
  264. describe( 'DropdownView.defaultPanelPositions', () => {
  265. let positions, buttonRect, panelRect;
  266. beforeEach( () => {
  267. positions = DropdownView.defaultPanelPositions;
  268. buttonRect = {
  269. top: 100,
  270. bottom: 200,
  271. left: 100,
  272. right: 200,
  273. width: 100,
  274. height: 100
  275. };
  276. panelRect = {
  277. top: 0,
  278. bottom: 0,
  279. left: 0,
  280. right: 0,
  281. width: 50,
  282. height: 50
  283. };
  284. } );
  285. it( 'should have a proper length', () => {
  286. expect( Object.keys( positions ) ).to.have.length( 4 );
  287. } );
  288. it( 'should define the "southEast" position', () => {
  289. expect( positions.southEast( buttonRect, panelRect ) ).to.deep.equal( {
  290. top: 200,
  291. left: 100,
  292. name: 'se'
  293. } );
  294. } );
  295. it( 'should define the "southWest" position', () => {
  296. expect( positions.southWest( buttonRect, panelRect ) ).to.deep.equal( {
  297. top: 200,
  298. left: 150,
  299. name: 'sw'
  300. } );
  301. } );
  302. it( 'should define the "northEast" position', () => {
  303. expect( positions.northEast( buttonRect, panelRect ) ).to.deep.equal( {
  304. top: 50,
  305. left: 100,
  306. name: 'ne'
  307. } );
  308. } );
  309. it( 'should define the "northWest" position', () => {
  310. expect( positions.northWest( buttonRect, panelRect ) ).to.deep.equal( {
  311. top: 150,
  312. left: 150,
  313. name: 'nw'
  314. } );
  315. } );
  316. } );
  317. } );