sheet_contextmenu.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Created by Mai on 2017/5/18.
  3. */
  4. SHEET_CONTEXTMENU = {
  5. /**
  6. * @param spreadObj: Dom Element for create GC.Spread
  7. * @param spread: GC.Spread created in spreadOBj
  8. * @param menuObj: Dom Element for contextMenu, eg.:
  9. * <ul id="spreadContextMenu" class="dropdown-menu" role="menu" style="display: none">
  10. * <li><a class="dropdown-item localize" data-action="cut">剪切</a></li>
  11. * <li><a class="dropdown-item localize" data-action="copy">复制</a></li>
  12. * <li><a class="dropdown-item localize" data-action="paste">粘贴</a></li>
  13. * <li class="context-header divider"></li>
  14. * <li class="context-header"><a class="dropdown-item localize" data-action="insert">插入</a></li>
  15. * <li class="context-header"><a class="dropdown-item localize" data-action="delete">删除</a></li>
  16. * <li class="context-cell divider"></li>
  17. * <li class="context-cell context-merge"><a class="dropdown-item localize" data-action="merge">合并</a></li>
  18. * <li class="context-cell context-unmerge"><a class="dropdown-item localize" data-action="unmerge">取消合并</a></li>
  19. * </ul>
  20. * menu must be a, define menu's event must use bind method
  21. * @returns {control}; use to bind contextmenu event, see eventName
  22. */
  23. createNew: function (spreadObj, spread, menuObj) {
  24. var tools = {
  25. getHitTest: function (pageX, pageY, sheet) {
  26. var offset = spreadObj.offset(),
  27. x = pageX - offset.left,
  28. y = pageY - offset.top;
  29. return sheet.hitTest(x, y);
  30. },
  31. getInfo: function (e) {
  32. var info = {};
  33. info.sheet = spread.getActiveSheet();
  34. info.target = tools.getHitTest(e.pageX, e.pageY, info.sheet);
  35. info.hitTestType = info.target.hitTestType;
  36. info.isHideContextMenu = false;
  37. info.contextMenu = menuObj;
  38. return info;
  39. },
  40. getCellInSelections: function (selections, row, col) {
  41. var count = selections.length, range;
  42. for (var i = 0; i < count; i++) {
  43. range = selections[i];
  44. if (range.contains(row, col)) {
  45. return range;
  46. }
  47. }
  48. return null;
  49. },
  50. hideSpreadContextMenu: function () {
  51. menuObj.hide();
  52. $(document).off("click.contextmenu");
  53. },
  54. processSpreadContextMenu: function (e) {
  55. var info = tools.getInfo(e), selections = info.sheet.getSelections(), row = info.target.row, col = info.target.col;
  56. // reSet Selections when no selections
  57. if (info.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
  58. if (tools.getCellInSelections(selections, row, col) === null) {
  59. info.sheet.setSelection(-1, col, info.sheet.getRowCount(), 1);
  60. }
  61. } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
  62. if (tools.getCellInSelections(selections, row, col) === null) {
  63. info.sheet.setSelection(row, -1, 1, info.sheet.getColumnCount());
  64. }
  65. } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
  66. if (tools.getCellInSelections(selections, row, col) === null) {
  67. info.sheet.clearSelection();
  68. info.sheet.endEdit();
  69. info.sheet.setActiveCell(row, col);
  70. }
  71. } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
  72. info.sheet.setSelection(-1, -1, info.sheet.getRowCount(), info.sheet.getColumnCount());
  73. };
  74. menuObj.data("sheetArea", info.hitTestType);
  75. if (info.isHideContextMenu) {
  76. tools.hideSpreadContextMenu();
  77. } else {
  78. if (event.beforeShowContextMenu) {
  79. event.beforeShowContextMenu(info);
  80. };
  81. menuObj.css({left: e.pageX, top: e.pageY});
  82. menuObj.show();
  83. $(document).on("click.contextmenu", function () {
  84. if ($(event.target).parents(menuObj).length === 0) {
  85. tools.hideSpreadContextMenu();
  86. }
  87. });
  88. }
  89. }
  90. };
  91. var event = {
  92. beforeShowContextMenu: null
  93. };
  94. var control = function () {
  95. spreadObj.bind("contextmenu", tools.processSpreadContextMenu);
  96. spreadObj.mouseup(function (e) {
  97. // hide context menu when the mouse down on SpreadJS
  98. if (e.button !== 2) {
  99. tools.hideSpreadContextMenu();
  100. }
  101. });
  102. $(menuObj, 'a').bind('click', tools.hideSpreadContextMenu);
  103. $(document).on("contextmenu", function (e) {
  104. var evt = window.event || e;
  105. if (!$(evt.target).data('contextmenu')) {
  106. evt.preventDefault();
  107. return false;
  108. }
  109. });
  110. };
  111. control.prototype.bind = function (eventName, event) {
  112. event[eventName] = event;
  113. };
  114. return new control();
  115. },
  116. /**
  117. * beforeShowContextMenu: right click spread, menu has different with spread.sheet.selections
  118. * @Param info: see return of tools.getInfo
  119. */
  120. eventName: {
  121. beforeShowContextMenu: 'beforeShowContextMenu'
  122. }
  123. }