tableui.js 13 KB

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