8
0

tablecellpropertiesview.js 19 KB

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