tableui.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. } );
  122. describe( 'tableColumn dropdown', () => {
  123. let dropdown;
  124. beforeEach( () => {
  125. dropdown = editor.ui.componentFactory.create( 'tableColumn' );
  126. } );
  127. it( 'have button with proper properties set', () => {
  128. expect( dropdown ).to.be.instanceOf( DropdownView );
  129. const button = dropdown.buttonView;
  130. expect( button.isOn ).to.be.false;
  131. expect( button.tooltip ).to.be.true;
  132. expect( button.label ).to.equal( 'Column' );
  133. expect( button.icon ).to.match( /<svg / );
  134. } );
  135. it( 'should have proper items in panel', () => {
  136. const listView = dropdown.listView;
  137. const labels = listView.items.map( ( { label } ) => label );
  138. expect( labels ).to.deep.equal( [ 'Header column', 'Insert column before', 'Insert column after', 'Delete column' ] );
  139. } );
  140. it( 'should bind items in panel to proper commands', () => {
  141. const items = dropdown.listView.items;
  142. const setColumnHeaderCommand = editor.commands.get( 'setColumnHeader' );
  143. const insertColumnBeforeCommand = editor.commands.get( 'insertColumnBefore' );
  144. const insertColumnAfterCommand = editor.commands.get( 'insertColumnAfter' );
  145. const removeColumnCommand = editor.commands.get( 'removeColumn' );
  146. setColumnHeaderCommand.isEnabled = true;
  147. insertColumnBeforeCommand.isEnabled = true;
  148. insertColumnAfterCommand.isEnabled = true;
  149. removeColumnCommand.isEnabled = true;
  150. expect( items.get( 0 ).isEnabled ).to.be.true;
  151. expect( items.get( 1 ).isEnabled ).to.be.true;
  152. expect( items.get( 2 ).isEnabled ).to.be.true;
  153. expect( items.get( 3 ).isEnabled ).to.be.true;
  154. expect( dropdown.buttonView.isEnabled ).to.be.true;
  155. setColumnHeaderCommand.isEnabled = false;
  156. expect( items.get( 0 ).isEnabled ).to.be.false;
  157. expect( dropdown.buttonView.isEnabled ).to.be.true;
  158. insertColumnBeforeCommand.isEnabled = false;
  159. expect( items.get( 1 ).isEnabled ).to.be.false;
  160. expect( dropdown.buttonView.isEnabled ).to.be.true;
  161. insertColumnAfterCommand.isEnabled = false;
  162. expect( items.get( 2 ).isEnabled ).to.be.false;
  163. removeColumnCommand.isEnabled = false;
  164. expect( items.get( 3 ).isEnabled ).to.be.false;
  165. expect( dropdown.buttonView.isEnabled ).to.be.false;
  166. } );
  167. it( 'should focus view a+fter command execution', () => {
  168. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  169. dropdown.listView.items.get( 0 ).fire( 'execute' );
  170. sinon.assert.calledOnce( focusSpy );
  171. } );
  172. it( 'executes command when it\'s executed', () => {
  173. const spy = sinon.stub( editor, 'execute' );
  174. dropdown.listView.items.get( 0 ).fire( 'execute' );
  175. expect( spy.calledOnce ).to.be.true;
  176. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'setColumnHeader' );
  177. } );
  178. } );
  179. describe( 'mergeCell dropdown', () => {
  180. let dropdown;
  181. beforeEach( () => {
  182. dropdown = editor.ui.componentFactory.create( 'mergeCell' );
  183. } );
  184. it( 'have button with proper properties set', () => {
  185. expect( dropdown ).to.be.instanceOf( DropdownView );
  186. const button = dropdown.buttonView;
  187. expect( button.isOn ).to.be.false;
  188. expect( button.tooltip ).to.be.true;
  189. expect( button.label ).to.equal( 'Merge cell' );
  190. expect( button.icon ).to.match( /<svg / );
  191. } );
  192. it( 'should have proper items in panel', () => {
  193. const listView = dropdown.listView;
  194. const labels = listView.items.map( ( { label } ) => label );
  195. expect( labels ).to.deep.equal( [ 'Merge cell up', 'Merge cell right', 'Merge cell down', 'Merge cell left' ] );
  196. } );
  197. it( 'should bind items in panel to proper commands', () => {
  198. const items = dropdown.listView.items;
  199. const mergeCellUpCommand = editor.commands.get( 'mergeCellUp' );
  200. const mergeCellRightCommand = editor.commands.get( 'mergeCellRight' );
  201. const mergeCellDownCommand = editor.commands.get( 'mergeCellDown' );
  202. const mergeCellLeftCommand = editor.commands.get( 'mergeCellLeft' );
  203. mergeCellUpCommand.isEnabled = true;
  204. mergeCellRightCommand.isEnabled = true;
  205. mergeCellDownCommand.isEnabled = true;
  206. mergeCellLeftCommand.isEnabled = true;
  207. expect( items.get( 0 ).isEnabled ).to.be.true;
  208. expect( items.get( 1 ).isEnabled ).to.be.true;
  209. expect( items.get( 2 ).isEnabled ).to.be.true;
  210. expect( items.get( 3 ).isEnabled ).to.be.true;
  211. expect( dropdown.buttonView.isEnabled ).to.be.true;
  212. mergeCellUpCommand.isEnabled = false;
  213. expect( items.get( 0 ).isEnabled ).to.be.false;
  214. expect( dropdown.buttonView.isEnabled ).to.be.true;
  215. mergeCellRightCommand.isEnabled = false;
  216. expect( items.get( 1 ).isEnabled ).to.be.false;
  217. expect( dropdown.buttonView.isEnabled ).to.be.true;
  218. mergeCellDownCommand.isEnabled = false;
  219. expect( items.get( 2 ).isEnabled ).to.be.false;
  220. mergeCellLeftCommand.isEnabled = false;
  221. expect( items.get( 3 ).isEnabled ).to.be.false;
  222. expect( dropdown.buttonView.isEnabled ).to.be.false;
  223. } );
  224. it( 'should focus view after command execution', () => {
  225. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  226. dropdown.listView.items.get( 0 ).fire( 'execute' );
  227. sinon.assert.calledOnce( focusSpy );
  228. } );
  229. it( 'executes command when it\'s executed', () => {
  230. const spy = sinon.stub( editor, 'execute' );
  231. dropdown.listView.items.get( 0 ).fire( 'execute' );
  232. expect( spy.calledOnce ).to.be.true;
  233. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'mergeCellUp' );
  234. } );
  235. } );
  236. describe( 'splitCell dropdown', () => {
  237. let dropdown;
  238. beforeEach( () => {
  239. dropdown = editor.ui.componentFactory.create( 'splitCell' );
  240. } );
  241. it( 'have button with proper properties set', () => {
  242. expect( dropdown ).to.be.instanceOf( DropdownView );
  243. const button = dropdown.buttonView;
  244. expect( button.isOn ).to.be.false;
  245. expect( button.tooltip ).to.be.true;
  246. expect( button.label ).to.equal( 'Split cell' );
  247. expect( button.icon ).to.match( /<svg / );
  248. } );
  249. it( 'should have proper items in panel', () => {
  250. const listView = dropdown.listView;
  251. const labels = listView.items.map( ( { label } ) => label );
  252. expect( labels ).to.deep.equal( [ 'Split cell vertically', 'Split cell horizontally' ] );
  253. } );
  254. it( 'should bind items in panel to proper commands', () => {
  255. const items = dropdown.listView.items;
  256. const splitCellVerticallyCommand = editor.commands.get( 'splitCellVertically' );
  257. const splitCellHorizontallyCommand = editor.commands.get( 'splitCellHorizontally' );
  258. splitCellVerticallyCommand.isEnabled = true;
  259. splitCellHorizontallyCommand.isEnabled = true;
  260. expect( items.get( 0 ).isEnabled ).to.be.true;
  261. expect( items.get( 1 ).isEnabled ).to.be.true;
  262. expect( dropdown.buttonView.isEnabled ).to.be.true;
  263. splitCellVerticallyCommand.isEnabled = false;
  264. expect( items.get( 0 ).isEnabled ).to.be.false;
  265. expect( dropdown.buttonView.isEnabled ).to.be.true;
  266. splitCellHorizontallyCommand.isEnabled = false;
  267. expect( items.get( 1 ).isEnabled ).to.be.false;
  268. expect( dropdown.buttonView.isEnabled ).to.be.false;
  269. } );
  270. it( 'should focus view after command execution', () => {
  271. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  272. dropdown.listView.items.get( 0 ).fire( 'execute' );
  273. sinon.assert.calledOnce( focusSpy );
  274. } );
  275. it( 'executes command when it\'s executed', () => {
  276. const spy = sinon.stub( editor, 'execute' );
  277. dropdown.listView.items.get( 0 ).fire( 'execute' );
  278. expect( spy.calledOnce ).to.be.true;
  279. expect( spy.args[ 0 ][ 0 ] ).to.equal( 'splitCellVertically' );
  280. } );
  281. } );
  282. } );