8
0

tableui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import TableEditing from '../src/tableediting';
  10. import TableUI from '../src/tableui';
  11. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  12. testUtils.createSinonSandbox();
  13. describe( 'TableUI', () => {
  14. let editor, element;
  15. before( () => {
  16. addTranslations( 'en', {} );
  17. addTranslations( 'pl', {} );
  18. } );
  19. after( () => {
  20. clearTranslations();
  21. } );
  22. beforeEach( () => {
  23. element = document.createElement( 'div' );
  24. document.body.appendChild( element );
  25. return ClassicTestEditor
  26. .create( element, {
  27. plugins: [ TableEditing, TableUI ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. } );
  32. } );
  33. afterEach( () => {
  34. element.remove();
  35. return editor.destroy();
  36. } );
  37. describe( 'insertTable dropdown', () => {
  38. let insertTable;
  39. beforeEach( () => {
  40. insertTable = editor.ui.componentFactory.create( 'insertTable' );
  41. } );
  42. it( 'should register insertTable button', () => {
  43. expect( insertTable ).to.be.instanceOf( DropdownView );
  44. expect( insertTable.buttonView.label ).to.equal( 'Insert table' );
  45. expect( insertTable.buttonView.icon ).to.match( /<svg / );
  46. } );
  47. it( 'should bind to insertTable command', () => {
  48. const command = editor.commands.get( 'insertTable' );
  49. command.isEnabled = true;
  50. expect( insertTable.buttonView.isOn ).to.be.false;
  51. expect( insertTable.buttonView.isEnabled ).to.be.true;
  52. command.isEnabled = false;
  53. expect( insertTable.buttonView.isEnabled ).to.be.false;
  54. } );
  55. it( 'should execute insertTable command on button execute event', () => {
  56. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  57. const tableSizeView = insertTable.panelView.children.get( 0 );
  58. tableSizeView.rows = 2;
  59. tableSizeView.columns = 7;
  60. insertTable.fire( 'execute' );
  61. sinon.assert.calledOnce( executeSpy );
  62. sinon.assert.calledWithExactly( executeSpy, 'insertTable', { rows: 2, columns: 7 } );
  63. } );
  64. it( 'should reset rows & columns on dropdown open', () => {
  65. const tableSizeView = insertTable.panelView.children.get( 0 );
  66. expect( tableSizeView.rows ).to.equal( 0 );
  67. expect( tableSizeView.columns ).to.equal( 0 );
  68. tableSizeView.rows = 2;
  69. tableSizeView.columns = 2;
  70. insertTable.buttonView.fire( 'open' );
  71. expect( tableSizeView.rows ).to.equal( 0 );
  72. expect( tableSizeView.columns ).to.equal( 0 );
  73. } );
  74. } );
  75. describe( 'tableRow dropdown', () => {
  76. let dropdown;
  77. beforeEach( () => {
  78. dropdown = editor.ui.componentFactory.create( 'tableRow' );
  79. } );
  80. it( 'have button with proper properties set', () => {
  81. expect( dropdown ).to.be.instanceOf( DropdownView );
  82. const button = dropdown.buttonView;
  83. expect( button.isOn ).to.be.false;
  84. expect( button.tooltip ).to.be.true;
  85. expect( button.label ).to.equal( 'Row' );
  86. expect( button.icon ).to.match( /<svg / );
  87. } );
  88. it( 'should have proper items in panel', () => {
  89. const listView = dropdown.listView;
  90. const labels = listView.items.map( ( { label } ) => label );
  91. expect( labels ).to.deep.equal( [ 'Header row', 'Insert row below', 'Insert row above', 'Delete row' ] );
  92. } );
  93. it( 'should bind items in panel to proper commands', () => {
  94. const items = dropdown.listView.items;
  95. const setRowHeaderCommand = editor.commands.get( 'setRowHeader' );
  96. const insertRowBelowCommand = editor.commands.get( 'insertRowBelow' );
  97. const insertRowAboveCommand = editor.commands.get( 'insertRowAbove' );
  98. const removeRowCommand = editor.commands.get( 'removeRow' );
  99. setRowHeaderCommand.isEnabled = true;
  100. insertRowBelowCommand.isEnabled = true;
  101. insertRowAboveCommand.isEnabled = true;
  102. removeRowCommand.isEnabled = true;
  103. expect( items.get( 0 ).isEnabled ).to.be.true;
  104. expect( items.get( 1 ).isEnabled ).to.be.true;
  105. expect( items.get( 2 ).isEnabled ).to.be.true;
  106. expect( items.get( 3 ).isEnabled ).to.be.true;
  107. expect( dropdown.buttonView.isEnabled ).to.be.true;
  108. setRowHeaderCommand.isEnabled = false;
  109. expect( items.get( 0 ).isEnabled ).to.be.false;
  110. expect( dropdown.buttonView.isEnabled ).to.be.true;
  111. insertRowBelowCommand.isEnabled = false;
  112. expect( items.get( 1 ).isEnabled ).to.be.false;
  113. expect( dropdown.buttonView.isEnabled ).to.be.true;
  114. insertRowAboveCommand.isEnabled = false;
  115. expect( items.get( 2 ).isEnabled ).to.be.false;
  116. expect( dropdown.buttonView.isEnabled ).to.be.true;
  117. removeRowCommand.isEnabled = false;
  118. expect( items.get( 3 ).isEnabled ).to.be.false;
  119. expect( dropdown.buttonView.isEnabled ).to.be.false;
  120. } );
  121. it( 'should focus view after command execution', () => {
  122. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  123. dropdown.listView.items.get( 0 ).fire( 'execute' );
  124. sinon.assert.calledOnce( focusSpy );
  125. } );
  126. it( 'executes command when it\'s executed', () => {
  127. const spy = sinon.stub( editor, 'execute' );
  128. dropdown.listView.items.get( 0 ).fire( 'execute' );
  129. expect( spy.calledOnce ).to.be.true;
  130. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setRowHeader' );
  131. } );
  132. it( 'should bind set header row command value to dropdown item', () => {
  133. const items = dropdown.listView.items;
  134. const setRowHeaderCommand = editor.commands.get( 'setRowHeader' );
  135. setRowHeaderCommand.value = false;
  136. expect( items.get( 0 ).isActive ).to.be.false;
  137. setRowHeaderCommand.value = true;
  138. expect( items.get( 0 ).isActive ).to.be.true;
  139. } );
  140. } );
  141. describe( 'tableColumn dropdown', () => {
  142. let dropdown;
  143. beforeEach( () => {
  144. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  145. } );
  146. it( 'have button with proper properties set', () => {
  147. expect( dropdown ).to.be.instanceOf( DropdownView );
  148. const button = dropdown.buttonView;
  149. expect( button.isOn ).to.be.false;
  150. expect( button.tooltip ).to.be.true;
  151. expect( button.label ).to.equal( 'Column' );
  152. expect( button.icon ).to.match( /<svg / );
  153. } );
  154. it( 'should have proper items in panel', () => {
  155. const listView = dropdown.listView;
  156. const labels = listView.items.map( ( { label } ) => label );
  157. expect( labels ).to.deep.equal( [ 'Header column', 'Insert column before', 'Insert column after', 'Delete column' ] );
  158. } );
  159. it( 'should bind items in panel to proper commands', () => {
  160. const items = dropdown.listView.items;
  161. const setColumnHeaderCommand = editor.commands.get( 'setColumnHeader' );
  162. const insertColumnBeforeCommand = editor.commands.get( 'insertColumnBefore' );
  163. const insertColumnAfterCommand = editor.commands.get( 'insertColumnAfter' );
  164. const removeColumnCommand = editor.commands.get( 'removeColumn' );
  165. setColumnHeaderCommand.isEnabled = true;
  166. insertColumnBeforeCommand.isEnabled = true;
  167. insertColumnAfterCommand.isEnabled = true;
  168. removeColumnCommand.isEnabled = true;
  169. expect( items.get( 0 ).isEnabled ).to.be.true;
  170. expect( items.get( 1 ).isEnabled ).to.be.true;
  171. expect( items.get( 2 ).isEnabled ).to.be.true;
  172. expect( items.get( 3 ).isEnabled ).to.be.true;
  173. expect( dropdown.buttonView.isEnabled ).to.be.true;
  174. setColumnHeaderCommand.isEnabled = false;
  175. expect( items.get( 0 ).isEnabled ).to.be.false;
  176. expect( dropdown.buttonView.isEnabled ).to.be.true;
  177. insertColumnBeforeCommand.isEnabled = false;
  178. expect( items.get( 1 ).isEnabled ).to.be.false;
  179. expect( dropdown.buttonView.isEnabled ).to.be.true;
  180. insertColumnAfterCommand.isEnabled = false;
  181. expect( items.get( 2 ).isEnabled ).to.be.false;
  182. removeColumnCommand.isEnabled = false;
  183. expect( items.get( 3 ).isEnabled ).to.be.false;
  184. expect( dropdown.buttonView.isEnabled ).to.be.false;
  185. } );
  186. it( 'should focus view after command execution', () => {
  187. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  188. dropdown.listView.items.get( 0 ).fire( 'execute' );
  189. sinon.assert.calledOnce( focusSpy );
  190. } );
  191. it( 'executes command when it\'s executed', () => {
  192. const spy = sinon.stub( editor, 'execute' );
  193. dropdown.listView.items.get( 0 ).fire( 'execute' );
  194. expect( spy.calledOnce ).to.be.true;
  195. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setColumnHeader' );
  196. } );
  197. it( 'should bind set header column command value to dropdown item', () => {
  198. const items = dropdown.listView.items;
  199. const setColumnHeaderCommand = editor.commands.get( 'setColumnHeader' );
  200. setColumnHeaderCommand.value = false;
  201. expect( items.get( 0 ).isActive ).to.be.false;
  202. setColumnHeaderCommand.value = true;
  203. expect( items.get( 0 ).isActive ).to.be.true;
  204. } );
  205. } );
  206. describe( 'mergeCell dropdown', () => {
  207. let dropdown;
  208. beforeEach( () => {
  209. dropdown = editor.ui.componentFactory.create( 'mergeCell' );
  210. } );
  211. it( 'have button with proper properties set', () => {
  212. expect( dropdown ).to.be.instanceOf( DropdownView );
  213. const button = dropdown.buttonView;
  214. expect( button.isOn ).to.be.false;
  215. expect( button.tooltip ).to.be.true;
  216. expect( button.label ).to.equal( 'Merge cell' );
  217. expect( button.icon ).to.match( /<svg / );
  218. } );
  219. it( 'should have proper items in panel', () => {
  220. const listView = dropdown.listView;
  221. const labels = listView.items.map( ( { label } ) => label );
  222. expect( labels ).to.deep.equal( [ 'Merge cell up', 'Merge cell right', 'Merge cell down', 'Merge cell left' ] );
  223. } );
  224. it( 'should bind items in panel to proper commands', () => {
  225. const items = dropdown.listView.items;
  226. const mergeCellUpCommand = editor.commands.get( 'mergeCellUp' );
  227. const mergeCellRightCommand = editor.commands.get( 'mergeCellRight' );
  228. const mergeCellDownCommand = editor.commands.get( 'mergeCellDown' );
  229. const mergeCellLeftCommand = editor.commands.get( 'mergeCellLeft' );
  230. mergeCellUpCommand.isEnabled = true;
  231. mergeCellRightCommand.isEnabled = true;
  232. mergeCellDownCommand.isEnabled = true;
  233. mergeCellLeftCommand.isEnabled = true;
  234. expect( items.get( 0 ).isEnabled ).to.be.true;
  235. expect( items.get( 1 ).isEnabled ).to.be.true;
  236. expect( items.get( 2 ).isEnabled ).to.be.true;
  237. expect( items.get( 3 ).isEnabled ).to.be.true;
  238. expect( dropdown.buttonView.isEnabled ).to.be.true;
  239. mergeCellUpCommand.isEnabled = false;
  240. expect( items.get( 0 ).isEnabled ).to.be.false;
  241. expect( dropdown.buttonView.isEnabled ).to.be.true;
  242. mergeCellRightCommand.isEnabled = false;
  243. expect( items.get( 1 ).isEnabled ).to.be.false;
  244. expect( dropdown.buttonView.isEnabled ).to.be.true;
  245. mergeCellDownCommand.isEnabled = false;
  246. expect( items.get( 2 ).isEnabled ).to.be.false;
  247. mergeCellLeftCommand.isEnabled = false;
  248. expect( items.get( 3 ).isEnabled ).to.be.false;
  249. expect( dropdown.buttonView.isEnabled ).to.be.false;
  250. } );
  251. it( 'should focus view after command execution', () => {
  252. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  253. dropdown.listView.items.get( 0 ).fire( 'execute' );
  254. sinon.assert.calledOnce( focusSpy );
  255. } );
  256. it( 'executes command when it\'s executed', () => {
  257. const spy = sinon.stub( editor, 'execute' );
  258. dropdown.listView.items.get( 0 ).fire( 'execute' );
  259. expect( spy.calledOnce ).to.be.true;
  260. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeCellUp' );
  261. } );
  262. } );
  263. describe( 'splitCell dropdown', () => {
  264. let dropdown;
  265. beforeEach( () => {
  266. dropdown = editor.ui.componentFactory.create( 'splitCell' );
  267. } );
  268. it( 'have button with proper properties set', () => {
  269. expect( dropdown ).to.be.instanceOf( DropdownView );
  270. const button = dropdown.buttonView;
  271. expect( button.isOn ).to.be.false;
  272. expect( button.tooltip ).to.be.true;
  273. expect( button.label ).to.equal( 'Split cell' );
  274. expect( button.icon ).to.match( /<svg / );
  275. } );
  276. it( 'should have proper items in panel', () => {
  277. const listView = dropdown.listView;
  278. const labels = listView.items.map( ( { label } ) => label );
  279. expect( labels ).to.deep.equal( [ 'Split cell vertically', 'Split cell horizontally' ] );
  280. } );
  281. it( 'should bind items in panel to proper commands', () => {
  282. const items = dropdown.listView.items;
  283. const splitCellVerticallyCommand = editor.commands.get( 'splitCellVertically' );
  284. const splitCellHorizontallyCommand = editor.commands.get( 'splitCellHorizontally' );
  285. splitCellVerticallyCommand.isEnabled = true;
  286. splitCellHorizontallyCommand.isEnabled = true;
  287. expect( items.get( 0 ).isEnabled ).to.be.true;
  288. expect( items.get( 1 ).isEnabled ).to.be.true;
  289. expect( dropdown.buttonView.isEnabled ).to.be.true;
  290. splitCellVerticallyCommand.isEnabled = false;
  291. expect( items.get( 0 ).isEnabled ).to.be.false;
  292. expect( dropdown.buttonView.isEnabled ).to.be.true;
  293. splitCellHorizontallyCommand.isEnabled = false;
  294. expect( items.get( 1 ).isEnabled ).to.be.false;
  295. expect( dropdown.buttonView.isEnabled ).to.be.false;
  296. } );
  297. it( 'should focus view after command execution', () => {
  298. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  299. dropdown.listView.items.get( 0 ).fire( 'execute' );
  300. sinon.assert.calledOnce( focusSpy );
  301. } );
  302. it( 'executes command when it\'s executed', () => {
  303. const spy = sinon.stub( editor, 'execute' );
  304. dropdown.listView.items.get( 0 ).fire( 'execute' );
  305. expect( spy.calledOnce ).to.be.true;
  306. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'splitCellVertically' );
  307. } );
  308. } );
  309. } );