8
0

tablecellpropertiesview.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals Event */
  6. import TableCellPropertiesView from '../../../src/tablecellproperties/ui/tablecellpropertiesview';
  7. import LabeledView from '@ckeditor/ckeditor5-ui/src/labeledview/labeledview';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  15. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  16. import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
  17. describe( 'TableCellPropertiesView', () => {
  18. let view, locale;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. locale = { t: val => val };
  22. view = new TableCellPropertiesView( locale );
  23. view.render();
  24. } );
  25. afterEach( () => {
  26. view.destroy();
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'should set view#locale', () => {
  30. expect( view.locale ).to.equal( locale );
  31. } );
  32. it( 'should create view#children collection', () => {
  33. expect( view.children ).to.be.instanceOf( ViewCollection );
  34. } );
  35. it( 'should define the public data interface (observable properties)', () => {
  36. expect( view ).to.include( {
  37. borderStyle: 'none',
  38. borderWidth: '',
  39. borderColor: '',
  40. padding: '',
  41. backgroundColor: '',
  42. horizontalAlignment: 'left',
  43. verticalAlignment: 'middle'
  44. } );
  45. } );
  46. it( 'should create element from template', () => {
  47. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  48. expect( view.element.classList.contains( 'ck-form' ) ).to.be.true;
  49. expect( view.element.classList.contains( 'ck-table-cell-properties-form' ) ).to.be.true;
  50. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  51. } );
  52. it( 'should create child views (and references)', () => {
  53. expect( view.borderStyleDropdown ).to.be.instanceOf( LabeledView );
  54. expect( view.borderWidthInput ).to.be.instanceOf( LabeledView );
  55. expect( view.borderColorInput ).to.be.instanceOf( LabeledView );
  56. expect( view.backgroundInput ).to.be.instanceOf( LabeledView );
  57. expect( view.paddingInput ).to.be.instanceOf( LabeledView );
  58. expect( view.horizontalAlignmentToolbar ).to.be.instanceOf( ToolbarView );
  59. expect( view.verticalAlignmentToolbar ).to.be.instanceOf( ToolbarView );
  60. expect( view.saveButtonView ).to.be.instanceOf( ButtonView );
  61. expect( view.cancelButtonView ).to.be.instanceOf( ButtonView );
  62. } );
  63. it( 'should have a header', () => {
  64. const header = view.element.firstChild;
  65. expect( header.classList.contains( 'ck' ) ).to.be.true;
  66. expect( header.classList.contains( 'ck-form__header' ) ).to.be.true;
  67. expect( header.textContent ).to.equal( 'Cell properties' );
  68. } );
  69. describe( 'form rows', () => {
  70. describe( 'border row', () => {
  71. it( 'should be defined', () => {
  72. const row = view.element.childNodes[ 1 ];
  73. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  74. expect( row.classList.contains( 'ck-table-cell-properties-form__border-row' ) ).to.be.true;
  75. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Border' );
  76. expect( row.childNodes[ 1 ] ).to.equal( view.borderStyleDropdown.element );
  77. expect( row.childNodes[ 2 ] ).to.equal( view.borderColorInput.element );
  78. expect( row.childNodes[ 3 ] ).to.equal( view.borderWidthInput.element );
  79. } );
  80. describe( 'border style labeled dropdown', () => {
  81. let labeledDropdown;
  82. beforeEach( () => {
  83. labeledDropdown = view.borderStyleDropdown;
  84. } );
  85. it( 'should have properties set', () => {
  86. expect( labeledDropdown.label ).to.equal( 'Style' );
  87. expect( labeledDropdown.class ).to.equal( 'ck-table-cell-properties-form__border-style' );
  88. } );
  89. it( 'should have a button with properties set', () => {
  90. expect( labeledDropdown.view.buttonView.isOn ).to.be.false;
  91. expect( labeledDropdown.view.buttonView.withText ).to.be.true;
  92. expect( labeledDropdown.view.buttonView.tooltip ).to.equal( 'Style' );
  93. } );
  94. it( 'should bind button\'s label to #borderStyle property', () => {
  95. view.borderStyle = 'dotted';
  96. expect( labeledDropdown.view.buttonView.label ).to.equal( 'Dotted' );
  97. view.borderStyle = 'dashed';
  98. expect( labeledDropdown.view.buttonView.label ).to.equal( 'Dashed' );
  99. } );
  100. it( 'should change #borderStyle when executed', () => {
  101. labeledDropdown.view.listView.items.first.children.first.fire( 'execute' );
  102. expect( view.borderStyle ).to.equal( 'none' );
  103. labeledDropdown.view.listView.items.last.children.first.fire( 'execute' );
  104. expect( view.borderStyle ).to.equal( 'outset' );
  105. } );
  106. it( 'should come with a set of pre–defined border styles', () => {
  107. expect( labeledDropdown.view.listView.items.map( item => item.children.first.label ) ).to.have.ordered.members( [
  108. 'None', 'Solid', 'Dotted', 'Dashed', 'Double', 'Groove', 'Ridge', 'Inset', 'Outset',
  109. ] );
  110. } );
  111. } );
  112. describe( 'border width input', () => {
  113. let labeledInput;
  114. beforeEach( () => {
  115. labeledInput = view.borderWidthInput;
  116. } );
  117. it( 'should be created', () => {
  118. expect( labeledInput.view ).to.be.instanceOf( InputTextView );
  119. expect( labeledInput.label ).to.equal( 'Width' );
  120. expect( labeledInput.class ).to.equal( 'ck-table-cell-properties-form__border-width' );
  121. } );
  122. it( 'should reflect #borderWidth property', () => {
  123. view.borderWidth = 'foo';
  124. expect( labeledInput.view.value ).to.equal( 'foo' );
  125. view.borderWidth = 'bar';
  126. expect( labeledInput.view.value ).to.equal( 'bar' );
  127. } );
  128. it( 'should be enabled only when #borderStyle is different than "none"', () => {
  129. view.borderStyle = 'none';
  130. expect( labeledInput.isEnabled ).to.be.false;
  131. view.borderStyle = 'dotted';
  132. expect( labeledInput.isEnabled ).to.be.true;
  133. } );
  134. it( 'should update #borderWidth on DOM "input" event', () => {
  135. labeledInput.view.element.value = 'foo';
  136. labeledInput.view.fire( 'input' );
  137. expect( view.borderWidth ).to.equal( 'foo' );
  138. labeledInput.view.element.value = 'bar';
  139. labeledInput.view.fire( 'input' );
  140. expect( view.borderWidth ).to.equal( 'bar' );
  141. } );
  142. } );
  143. describe( 'border color input', () => {
  144. let labeledInput;
  145. beforeEach( () => {
  146. labeledInput = view.borderColorInput;
  147. } );
  148. it( 'should be created', () => {
  149. expect( labeledInput.view ).to.be.instanceOf( InputTextView );
  150. expect( labeledInput.label ).to.equal( 'Color' );
  151. } );
  152. it( 'should reflect #borderColor property', () => {
  153. view.borderColor = 'foo';
  154. expect( labeledInput.view.value ).to.equal( 'foo' );
  155. view.borderColor = 'bar';
  156. expect( labeledInput.view.value ).to.equal( 'bar' );
  157. } );
  158. it( 'should be enabled only when #borderStyle is different than "none"', () => {
  159. view.borderStyle = 'none';
  160. expect( labeledInput.isEnabled ).to.be.false;
  161. view.borderStyle = 'dotted';
  162. expect( labeledInput.isEnabled ).to.be.true;
  163. } );
  164. it( 'should update #borderColor on DOM "input" event', () => {
  165. labeledInput.view.element.value = 'foo';
  166. labeledInput.view.fire( 'input' );
  167. expect( view.borderColor ).to.equal( 'foo' );
  168. labeledInput.view.element.value = 'bar';
  169. labeledInput.view.fire( 'input' );
  170. expect( view.borderColor ).to.equal( 'bar' );
  171. } );
  172. } );
  173. } );
  174. describe( 'background and padding row', () => {
  175. it( 'should be defined', () => {
  176. const row = view.element.childNodes[ 2 ];
  177. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  178. expect( row.childNodes[ 0 ] ).to.equal( view.backgroundInput.element );
  179. expect( row.childNodes[ 1 ] ).to.equal( view.paddingInput.element );
  180. } );
  181. describe( 'background color input', () => {
  182. let labeledInput;
  183. beforeEach( () => {
  184. labeledInput = view.backgroundInput;
  185. } );
  186. it( 'should be created', () => {
  187. expect( labeledInput.view ).to.be.instanceOf( InputTextView );
  188. expect( labeledInput.label ).to.equal( 'Background' );
  189. expect( labeledInput.class ).to.equal( 'ck-table-cell-properties-form__background' );
  190. } );
  191. it( 'should reflect #backgroundColor property', () => {
  192. view.backgroundColor = 'foo';
  193. expect( labeledInput.view.value ).to.equal( 'foo' );
  194. view.backgroundColor = 'bar';
  195. expect( labeledInput.view.value ).to.equal( 'bar' );
  196. } );
  197. it( 'should update #backgroundColor on DOM "input" event', () => {
  198. labeledInput.view.element.value = 'foo';
  199. labeledInput.view.fire( 'input' );
  200. expect( view.backgroundColor ).to.equal( 'foo' );
  201. labeledInput.view.element.value = 'bar';
  202. labeledInput.view.fire( 'input' );
  203. expect( view.backgroundColor ).to.equal( 'bar' );
  204. } );
  205. } );
  206. describe( 'padding input', () => {
  207. let labeledInput;
  208. beforeEach( () => {
  209. labeledInput = view.paddingInput;
  210. } );
  211. it( 'should be created', () => {
  212. expect( labeledInput.view ).to.be.instanceOf( InputTextView );
  213. expect( labeledInput.label ).to.equal( 'Padding' );
  214. expect( labeledInput.class ).to.equal( 'ck-table-cell-properties-form__padding' );
  215. } );
  216. it( 'should reflect #padding property', () => {
  217. view.padding = 'foo';
  218. expect( labeledInput.view.value ).to.equal( 'foo' );
  219. view.padding = 'bar';
  220. expect( labeledInput.view.value ).to.equal( 'bar' );
  221. } );
  222. it( 'should update #padding on DOM "input" event', () => {
  223. labeledInput.view.element.value = 'foo';
  224. labeledInput.view.fire( 'input' );
  225. expect( view.padding ).to.equal( 'foo' );
  226. labeledInput.view.element.value = 'bar';
  227. labeledInput.view.fire( 'input' );
  228. expect( view.padding ).to.equal( 'bar' );
  229. } );
  230. } );
  231. } );
  232. describe( 'text alignment row', () => {
  233. it( 'should be defined', () => {
  234. const row = view.element.childNodes[ 3 ];
  235. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  236. expect( row.classList.contains( 'ck-table-cell-properties-form__alignment-row' ) ).to.be.true;
  237. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Text alignment' );
  238. expect( row.childNodes[ 1 ] ).to.equal( view.horizontalAlignmentToolbar.element );
  239. expect( row.childNodes[ 2 ] ).to.equal( view.verticalAlignmentToolbar.element );
  240. } );
  241. describe( 'horizontal text alignment toolbar', () => {
  242. let toolbar;
  243. beforeEach( () => {
  244. toolbar = view.horizontalAlignmentToolbar;
  245. } );
  246. it( 'should be defined', () => {
  247. expect( toolbar ).to.be.instanceOf( ToolbarView );
  248. } );
  249. it( 'should have an ARIA label', () => {
  250. expect( toolbar.ariaLabel ).to.equal( 'Horizontal text alignment toolbar' );
  251. } );
  252. it( 'should bring alignment buttons', () => {
  253. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  254. 'Align cell text to the left',
  255. 'Align cell text to the center',
  256. 'Align cell text to the right',
  257. 'Justify cell text',
  258. ] );
  259. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  260. true, false, false, false
  261. ] );
  262. } );
  263. it( 'should change the #horizontalAlignment value', () => {
  264. toolbar.items.last.fire( 'execute' );
  265. expect( view.horizontalAlignment ).to.equal( 'justify' );
  266. expect( toolbar.items.last.isOn ).to.be.true;
  267. toolbar.items.first.fire( 'execute' );
  268. expect( view.horizontalAlignment ).to.equal( 'left' );
  269. expect( toolbar.items.last.isOn ).to.be.false;
  270. expect( toolbar.items.first.isOn ).to.be.true;
  271. } );
  272. } );
  273. describe( 'vertical text alignment toolbar', () => {
  274. let toolbar;
  275. beforeEach( () => {
  276. toolbar = view.verticalAlignmentToolbar;
  277. } );
  278. it( 'should be defined', () => {
  279. expect( toolbar ).to.be.instanceOf( ToolbarView );
  280. } );
  281. it( 'should have an ARIA label', () => {
  282. expect( toolbar.ariaLabel ).to.equal( 'Vertical text alignment toolbar' );
  283. } );
  284. it( 'should bring alignment buttons', () => {
  285. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  286. 'Align cell text to the top',
  287. 'Align cell text to the middle',
  288. 'Align cell text to the bottom',
  289. ] );
  290. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  291. false, true, false
  292. ] );
  293. } );
  294. it( 'should change the #verticalAlignment value', () => {
  295. toolbar.items.last.fire( 'execute' );
  296. expect( view.verticalAlignment ).to.equal( 'bottom' );
  297. expect( toolbar.items.last.isOn ).to.be.true;
  298. toolbar.items.first.fire( 'execute' );
  299. expect( view.verticalAlignment ).to.equal( 'top' );
  300. expect( toolbar.items.last.isOn ).to.be.false;
  301. expect( toolbar.items.first.isOn ).to.be.true;
  302. } );
  303. } );
  304. } );
  305. describe( 'action row', () => {
  306. it( 'should be defined', () => {
  307. const row = view.element.childNodes[ 4 ];
  308. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  309. expect( row.classList.contains( 'ck-table-form__action-row' ) ).to.be.true;
  310. expect( row.childNodes[ 0 ] ).to.equal( view.saveButtonView.element );
  311. expect( row.childNodes[ 1 ] ).to.equal( view.cancelButtonView.element );
  312. } );
  313. it( 'should have buttons with right properties', () => {
  314. expect( view.saveButtonView.label ).to.equal( 'Save' );
  315. expect( view.saveButtonView.type ).to.equal( 'submit' );
  316. expect( view.saveButtonView.withText ).to.be.true;
  317. expect( view.saveButtonView.class ).to.equal( 'ck-button-save' );
  318. expect( view.cancelButtonView.label ).to.equal( 'Cancel' );
  319. expect( view.cancelButtonView.withText ).to.be.true;
  320. expect( view.cancelButtonView.class ).to.equal( 'ck-button-cancel' );
  321. } );
  322. it( 'should make the cancel button fire the #cancel event when executed', () => {
  323. const spy = sinon.spy();
  324. view.on( 'cancel', spy );
  325. view.cancelButtonView.fire( 'execute' );
  326. expect( spy.calledOnce ).to.be.true;
  327. } );
  328. } );
  329. } );
  330. it( 'should create #focusTracker instance', () => {
  331. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  332. } );
  333. it( 'should create #keystrokes instance', () => {
  334. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  335. } );
  336. it( 'should create #_focusCycler instance', () => {
  337. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  338. } );
  339. it( 'should create #_focusables view collection', () => {
  340. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  341. } );
  342. } );
  343. describe( 'render()', () => {
  344. it( 'should register child views in #_focusables', () => {
  345. expect( view._focusables.map( f => f ) ).to.have.members( [
  346. view.borderStyleDropdown,
  347. view.borderColorInput,
  348. view.borderWidthInput,
  349. view.backgroundInput,
  350. view.paddingInput,
  351. view.horizontalAlignmentToolbar,
  352. view.verticalAlignmentToolbar,
  353. view.saveButtonView,
  354. view.cancelButtonView
  355. ] );
  356. } );
  357. it( 'should register child views\' #element in #focusTracker', () => {
  358. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  359. const view = new TableCellPropertiesView( { t: val => val } );
  360. view.render();
  361. sinon.assert.calledWith( spy, view.borderStyleDropdown.element );
  362. sinon.assert.calledWith( spy, view.borderColorInput.element );
  363. sinon.assert.calledWith( spy, view.borderWidthInput.element );
  364. sinon.assert.calledWith( spy, view.backgroundInput.element );
  365. sinon.assert.calledWith( spy, view.paddingInput.element );
  366. sinon.assert.calledWith( spy, view.horizontalAlignmentToolbar.element );
  367. sinon.assert.calledWith( spy, view.verticalAlignmentToolbar.element );
  368. sinon.assert.calledWith( spy, view.saveButtonView.element );
  369. sinon.assert.calledWith( spy, view.cancelButtonView.element );
  370. view.destroy();
  371. } );
  372. it( 'starts listening for #keystrokes coming from #element', () => {
  373. const view = new TableCellPropertiesView( { t: val => val } );
  374. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  375. view.render();
  376. sinon.assert.calledOnce( spy );
  377. sinon.assert.calledWithExactly( spy, view.element );
  378. } );
  379. describe( 'activates keyboard navigation for the form', () => {
  380. it( 'so "tab" focuses the next focusable item', () => {
  381. const keyEvtData = {
  382. keyCode: keyCodes.tab,
  383. preventDefault: sinon.spy(),
  384. stopPropagation: sinon.spy()
  385. };
  386. // Mock the border style dropdown button is focused.
  387. view.focusTracker.isFocused = true;
  388. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  389. const spy = sinon.spy( view.borderColorInput, 'focus' );
  390. view.keystrokes.press( keyEvtData );
  391. sinon.assert.calledOnce( keyEvtData.preventDefault );
  392. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  393. sinon.assert.calledOnce( spy );
  394. } );
  395. it( 'so "shift + tab" focuses the previous focusable item', () => {
  396. const keyEvtData = {
  397. keyCode: keyCodes.tab,
  398. shiftKey: true,
  399. preventDefault: sinon.spy(),
  400. stopPropagation: sinon.spy()
  401. };
  402. // Mock the border style dropdown button is focused.
  403. view.focusTracker.isFocused = true;
  404. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  405. const spy = sinon.spy( view.cancelButtonView, 'focus' );
  406. view.keystrokes.press( keyEvtData );
  407. sinon.assert.calledOnce( keyEvtData.preventDefault );
  408. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  409. sinon.assert.calledOnce( spy );
  410. } );
  411. } );
  412. } );
  413. describe( 'DOM bindings', () => {
  414. describe( 'submit event', () => {
  415. it( 'should trigger submit event', () => {
  416. const spy = sinon.spy();
  417. view.on( 'submit', spy );
  418. view.element.dispatchEvent( new Event( 'submit' ) );
  419. expect( spy.calledOnce ).to.be.true;
  420. } );
  421. } );
  422. } );
  423. describe( 'focus()', () => {
  424. it( 'focuses the #borderStyleDropdown', () => {
  425. const spy = sinon.spy( view.borderStyleDropdown, 'focus' );
  426. view.focus();
  427. sinon.assert.calledOnce( spy );
  428. } );
  429. } );
  430. } );