8
0

tableui.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 SwitchButtonView from '@ckeditor/ckeditor5-ui/src/button/switchbuttonview';
  12. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  13. import ListSeparatorView from '@ckeditor/ckeditor5-ui/src/list/listseparatorview';
  14. import TableSelection from '../src/tableselection';
  15. describe( 'TableUI', () => {
  16. let editor, element;
  17. testUtils.createSinonSandbox();
  18. before( () => {
  19. addTranslations( 'en', {} );
  20. addTranslations( 'pl', {} );
  21. } );
  22. after( () => {
  23. clearTranslations();
  24. } );
  25. beforeEach( () => {
  26. element = document.createElement( 'div' );
  27. document.body.appendChild( element );
  28. return ClassicTestEditor
  29. .create( element, {
  30. plugins: [ TableEditing, TableSelection, TableUI ]
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. } );
  35. } );
  36. afterEach( () => {
  37. element.remove();
  38. return editor.destroy();
  39. } );
  40. describe( 'insertTable dropdown', () => {
  41. let insertTable;
  42. beforeEach( () => {
  43. insertTable = editor.ui.componentFactory.create( 'insertTable' );
  44. } );
  45. it( 'should register insertTable button', () => {
  46. expect( insertTable ).to.be.instanceOf( DropdownView );
  47. expect( insertTable.buttonView.label ).to.equal( 'Insert table' );
  48. expect( insertTable.buttonView.icon ).to.match( /<svg / );
  49. } );
  50. it( 'should bind to insertTable command', () => {
  51. const command = editor.commands.get( 'insertTable' );
  52. command.isEnabled = true;
  53. expect( insertTable.buttonView.isOn ).to.be.false;
  54. expect( insertTable.buttonView.isEnabled ).to.be.true;
  55. command.isEnabled = false;
  56. expect( insertTable.buttonView.isEnabled ).to.be.false;
  57. } );
  58. it( 'should execute insertTable command on button execute event', () => {
  59. const executeSpy = testUtils.sinon.spy( editor, 'execute' );
  60. const tableSizeView = insertTable.panelView.children.first;
  61. tableSizeView.rows = 2;
  62. tableSizeView.columns = 7;
  63. insertTable.fire( 'execute' );
  64. sinon.assert.calledOnce( executeSpy );
  65. sinon.assert.calledWithExactly( executeSpy, 'insertTable', { rows: 2, columns: 7 } );
  66. } );
  67. it( 'should reset rows & columns on dropdown open', () => {
  68. const tableSizeView = insertTable.panelView.children.first;
  69. expect( tableSizeView.rows ).to.equal( 0 );
  70. expect( tableSizeView.columns ).to.equal( 0 );
  71. tableSizeView.rows = 2;
  72. tableSizeView.columns = 2;
  73. insertTable.buttonView.fire( 'open' );
  74. expect( tableSizeView.rows ).to.equal( 0 );
  75. expect( tableSizeView.columns ).to.equal( 0 );
  76. } );
  77. } );
  78. describe( 'tableRow dropdown', () => {
  79. let dropdown;
  80. beforeEach( () => {
  81. dropdown = editor.ui.componentFactory.create( 'tableRow' );
  82. } );
  83. it( 'have button with proper properties set', () => {
  84. expect( dropdown ).to.be.instanceOf( DropdownView );
  85. const button = dropdown.buttonView;
  86. expect( button.isOn ).to.be.false;
  87. expect( button.tooltip ).to.be.true;
  88. expect( button.label ).to.equal( 'Row' );
  89. expect( button.icon ).to.match( /<svg / );
  90. } );
  91. it( 'should have proper items in panel', () => {
  92. const listView = dropdown.listView;
  93. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  94. expect( labels ).to.deep.equal( [ 'Header row', '|', 'Insert row below', 'Insert row above', 'Delete row' ] );
  95. } );
  96. it( 'should bind items in panel to proper commands', () => {
  97. const items = dropdown.listView.items;
  98. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  99. const insertRowBelowCommand = editor.commands.get( 'insertTableRowBelow' );
  100. const insertRowAboveCommand = editor.commands.get( 'insertTableRowAbove' );
  101. const removeRowCommand = editor.commands.get( 'removeTableRow' );
  102. setRowHeaderCommand.isEnabled = true;
  103. insertRowBelowCommand.isEnabled = true;
  104. insertRowAboveCommand.isEnabled = true;
  105. removeRowCommand.isEnabled = true;
  106. expect( items.first.children.first.isEnabled ).to.be.true;
  107. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  108. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  109. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  110. expect( dropdown.buttonView.isEnabled ).to.be.true;
  111. setRowHeaderCommand.isEnabled = false;
  112. expect( items.first.children.first.isEnabled ).to.be.false;
  113. expect( dropdown.buttonView.isEnabled ).to.be.true;
  114. insertRowBelowCommand.isEnabled = false;
  115. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  116. expect( dropdown.buttonView.isEnabled ).to.be.true;
  117. insertRowAboveCommand.isEnabled = false;
  118. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  119. expect( dropdown.buttonView.isEnabled ).to.be.true;
  120. removeRowCommand.isEnabled = false;
  121. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  122. expect( dropdown.buttonView.isEnabled ).to.be.false;
  123. } );
  124. it( 'should focus view after command execution', () => {
  125. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  126. dropdown.listView.items.first.children.first.fire( 'execute' );
  127. sinon.assert.calledOnce( focusSpy );
  128. } );
  129. it( 'executes command when it\'s executed', () => {
  130. const spy = sinon.stub( editor, 'execute' );
  131. dropdown.listView.items.first.children.first.fire( 'execute' );
  132. expect( spy.calledOnce ).to.be.true;
  133. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableRowHeader' );
  134. } );
  135. it( 'should use a toggle switch for the setTableRowHeader item', () => {
  136. const items = dropdown.listView.items;
  137. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  138. } );
  139. it( 'should bind set header row command value to dropdown item', () => {
  140. const items = dropdown.listView.items;
  141. const setRowHeaderCommand = editor.commands.get( 'setTableRowHeader' );
  142. setRowHeaderCommand.value = false;
  143. expect( items.first.children.first.isOn ).to.be.false;
  144. setRowHeaderCommand.value = true;
  145. expect( items.first.children.first.isOn ).to.be.true;
  146. } );
  147. } );
  148. describe( 'tableColumn dropdown', () => {
  149. let dropdown;
  150. beforeEach( () => {
  151. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  152. } );
  153. it( 'have button with proper properties set', () => {
  154. expect( dropdown ).to.be.instanceOf( DropdownView );
  155. const button = dropdown.buttonView;
  156. expect( button.isOn ).to.be.false;
  157. expect( button.tooltip ).to.be.true;
  158. expect( button.label ).to.equal( 'Column' );
  159. expect( button.icon ).to.match( /<svg / );
  160. } );
  161. it( 'should have proper items in panel', () => {
  162. const listView = dropdown.listView;
  163. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  164. expect( labels ).to.deep.equal( [ 'Header column', '|', 'Insert column before', 'Insert column after', 'Delete column' ] );
  165. } );
  166. it( 'should bind items in panel to proper commands', () => {
  167. const items = dropdown.listView.items;
  168. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  169. const insertColumnBeforeCommand = editor.commands.get( 'insertTableColumnBefore' );
  170. const insertColumnAfterCommand = editor.commands.get( 'insertTableColumnAfter' );
  171. const removeColumnCommand = editor.commands.get( 'removeTableColumn' );
  172. setColumnHeaderCommand.isEnabled = true;
  173. insertColumnBeforeCommand.isEnabled = true;
  174. insertColumnAfterCommand.isEnabled = true;
  175. removeColumnCommand.isEnabled = true;
  176. expect( items.first.children.first.isEnabled ).to.be.true;
  177. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  178. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  179. expect( items.get( 4 ).children.first.isEnabled ).to.be.true;
  180. expect( dropdown.buttonView.isEnabled ).to.be.true;
  181. setColumnHeaderCommand.isEnabled = false;
  182. expect( items.first.children.first.isEnabled ).to.be.false;
  183. expect( dropdown.buttonView.isEnabled ).to.be.true;
  184. insertColumnBeforeCommand.isEnabled = false;
  185. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  186. expect( dropdown.buttonView.isEnabled ).to.be.true;
  187. insertColumnAfterCommand.isEnabled = false;
  188. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  189. removeColumnCommand.isEnabled = false;
  190. expect( items.get( 4 ).children.first.isEnabled ).to.be.false;
  191. expect( dropdown.buttonView.isEnabled ).to.be.false;
  192. } );
  193. it( 'should focus view after command execution', () => {
  194. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  195. dropdown.listView.items.first.children.first.fire( 'execute' );
  196. sinon.assert.calledOnce( focusSpy );
  197. } );
  198. it( 'executes command when it\'s executed', () => {
  199. const spy = sinon.stub( editor, 'execute' );
  200. dropdown.listView.items.first.children.first.fire( 'execute' );
  201. expect( spy.calledOnce ).to.be.true;
  202. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setTableColumnHeader' );
  203. } );
  204. it( 'should use a toggle switch for the setTableColumnHeader item', () => {
  205. const items = dropdown.listView.items;
  206. expect( items.first.children.first ).to.be.instanceOf( SwitchButtonView );
  207. } );
  208. it( 'should bind set header column command value to dropdown item', () => {
  209. const items = dropdown.listView.items;
  210. const setColumnHeaderCommand = editor.commands.get( 'setTableColumnHeader' );
  211. setColumnHeaderCommand.value = false;
  212. expect( items.first.children.first.isOn ).to.be.false;
  213. setColumnHeaderCommand.value = true;
  214. expect( items.first.children.first.isOn ).to.be.true;
  215. } );
  216. } );
  217. describe( 'mergeTableCell dropdown', () => {
  218. let dropdown;
  219. beforeEach( () => {
  220. dropdown = editor.ui.componentFactory.create( 'mergeTableCells' );
  221. } );
  222. it( 'have button with proper properties set', () => {
  223. expect( dropdown ).to.be.instanceOf( DropdownView );
  224. const button = dropdown.buttonView;
  225. expect( button.isOn ).to.be.false;
  226. expect( button.tooltip ).to.be.true;
  227. expect( button.label ).to.equal( 'Merge cells' );
  228. expect( button.icon ).to.match( /<svg / );
  229. } );
  230. it( 'should have proper items in panel', () => {
  231. const listView = dropdown.listView;
  232. const labels = listView.items.map( item => item instanceof ListSeparatorView ? '|' : item.children.first.label );
  233. expect( labels ).to.deep.equal( [
  234. 'Merge cells',
  235. '|',
  236. 'Split cell vertically',
  237. 'Split cell horizontally'
  238. ] );
  239. } );
  240. it( 'should bind items in panel to proper commands', () => {
  241. const items = dropdown.listView.items;
  242. const mergeCellsCommand = editor.commands.get( 'mergeTableCells' );
  243. const splitCellVerticallyCommand = editor.commands.get( 'splitTableCellVertically' );
  244. const splitCellHorizontallyCommand = editor.commands.get( 'splitTableCellHorizontally' );
  245. mergeCellsCommand.isEnabled = true;
  246. splitCellVerticallyCommand.isEnabled = true;
  247. splitCellHorizontallyCommand.isEnabled = true;
  248. expect( items.get( 0 ).children.first.isEnabled ).to.be.true;
  249. expect( items.get( 2 ).children.first.isEnabled ).to.be.true;
  250. expect( items.get( 3 ).children.first.isEnabled ).to.be.true;
  251. expect( dropdown.buttonView.isEnabled ).to.be.true;
  252. mergeCellsCommand.isEnabled = false;
  253. expect( items.get( 0 ).children.first.isEnabled ).to.be.false;
  254. expect( dropdown.buttonView.isEnabled ).to.be.true;
  255. splitCellVerticallyCommand.isEnabled = false;
  256. expect( items.get( 2 ).children.first.isEnabled ).to.be.false;
  257. splitCellHorizontallyCommand.isEnabled = false;
  258. expect( items.get( 3 ).children.first.isEnabled ).to.be.false;
  259. expect( dropdown.buttonView.isEnabled ).to.be.false;
  260. } );
  261. it( 'should focus view after command execution', () => {
  262. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  263. dropdown.listView.items.first.children.first.fire( 'execute' );
  264. sinon.assert.calledOnce( focusSpy );
  265. } );
  266. it( 'executes command when it\'s executed', () => {
  267. const spy = sinon.stub( editor, 'execute' );
  268. dropdown.listView.items.first.children.first.fire( 'execute' );
  269. expect( spy.calledOnce ).to.be.true;
  270. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeTableCells' );
  271. } );
  272. } );
  273. } );