6aca1413623cfde7e3f8cde7884bb3ec9a017c967f722258dd3b3cd2b641b3d5f825f6b59de63c685401eea108b4bfd8fd110b2539c923ba14ed696ccea52c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. describe('ContextMenuCopyPaste', function () {
  3. var id = 'testContainer';
  4. if (typeof navigator.mimeTypes['application/x-shockwave-flash'] === 'undefined') {
  5. navigator.mimeTypes['application/x-shockwave-flash'] = {}; // mock Adobe Flash plugin so that contextMenuCopyPaste.js does not throw an error in PhantomJS
  6. }
  7. beforeEach(function () {
  8. this.$container = $('<div id="' + id + '"></div>').appendTo('body');
  9. var wrapper = $('<div></div>').css({
  10. width: 400,
  11. height: 200,
  12. overflow: 'scroll'
  13. });
  14. this.$wrapper = this.$container.wrap(wrapper).parent();
  15. });
  16. afterEach(function () {
  17. if (this.$container) {
  18. destroy();
  19. this.$container.remove();
  20. }
  21. this.$wrapper.remove();
  22. $('head').find('script[src*=ZeroClipboard]').remove();
  23. delete window.ZeroClipboard;
  24. });
  25. it('should add Copy and Paste context menu options at the beginning by default', function () {
  26. var hot = handsontable({
  27. data: Handsontable.helper.createSpreadsheetObjectData(10, 5),
  28. rowHeaders: true,
  29. colHeaders: true,
  30. minSpareRows: 1,
  31. contextMenu: true,
  32. contextMenuCopyPaste: {
  33. swfPath: '../../demo/swf/ZeroClipboard.swf'
  34. }
  35. });
  36. selectCell(1, 1);
  37. contextMenu();
  38. var $contextMenuEntries = $('.htContextMenu .ht_master .htCore tbody').find('td');
  39. var $copyButton = $contextMenuEntries.find('div').filter(function () {
  40. return (/Copy/i.test($(this).text())
  41. );
  42. }).parents('td');
  43. var $pasteButton = $contextMenuEntries.find('div').filter(function () {
  44. return (/Paste/i.test($(this).text())
  45. );
  46. }).parents('td');
  47. expect($contextMenuEntries.index($copyButton)).toEqual(0);
  48. expect($contextMenuEntries.index($pasteButton)).toEqual(1);
  49. });
  50. it('should add Copy and Paste context menu options at the provided index', function () {
  51. var hot = handsontable({
  52. data: Handsontable.helper.createSpreadsheetObjectData(10, 5),
  53. rowHeaders: true,
  54. colHeaders: true,
  55. minSpareRows: 1,
  56. contextMenu: ['row_above', 'copy', 'paste', 'row_below'],
  57. contextMenuCopyPaste: {
  58. swfPath: '../../demo/swf/ZeroClipboard.swf'
  59. }
  60. });
  61. selectCell(1, 1);
  62. contextMenu();
  63. var $contextMenuEntries = $('.htContextMenu .ht_master .htCore tbody').find('td');
  64. var $copyButton = $contextMenuEntries.find('div').filter(function () {
  65. return (/Copy/i.test($(this).text())
  66. );
  67. }).parents('td');
  68. var $pasteButton = $contextMenuEntries.find('div').filter(function () {
  69. return (/Paste/i.test($(this).text())
  70. );
  71. }).parents('td');
  72. expect($contextMenuEntries.not('[class*=htSeparator]').index($copyButton)).toEqual(1);
  73. expect($contextMenuEntries.not('[class*=htSeparator]').index($pasteButton)).toEqual(2);
  74. });
  75. it('should disable `Copy` and `Paste` items when context menu was triggered from corner header', function () {
  76. var hot = handsontable({
  77. data: Handsontable.helper.createSpreadsheetObjectData(10, 5),
  78. rowHeaders: true,
  79. colHeaders: true,
  80. minSpareRows: 1,
  81. contextMenu: true,
  82. contextMenuCopyPaste: {
  83. swfPath: '../../demo/swf/ZeroClipboard.swf'
  84. }
  85. });
  86. $('.ht_clone_top_left_corner .htCore').find('thead').find('th').eq(0).simulate('mousedown', { which: 3 });
  87. contextMenu();
  88. expect($('.htContextMenu tbody td.htDisabled').text()).toBe(['Copy', 'Paste', 'Insert column on the left', 'Insert column on the right', 'Remove row', 'Remove column', 'Undo', 'Redo', 'Read only', 'Alignment'].join(''));
  89. });
  90. // see https://github.com/handsontable/handsontable/issues/3140
  91. it('should not throwing error when ContextMenu plugin is disabled', function () {
  92. var spy = jasmine.createSpy();
  93. var prevError = window.onerror;
  94. window.onerror = function () {
  95. spy();
  96. };
  97. var hot = handsontable({
  98. data: Handsontable.helper.createSpreadsheetObjectData(10, 5),
  99. rowHeaders: true,
  100. colHeaders: true,
  101. minSpareRows: 1,
  102. contextMenu: true,
  103. contextMenuCopyPaste: {
  104. swfPath: '../../demo/swf/ZeroClipboard.swf'
  105. }
  106. });
  107. hot.getPlugin('contextMenu').disablePlugin();
  108. $(getCell(0, 0)).simulate('mouseenter');
  109. expect(spy).not.toHaveBeenCalled();
  110. window.onerror = prevError;
  111. });
  112. });