MenuClipboard.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. Ext.define('Ext.ux.grid.plugin.MenuClipboard', {
  2. extend: 'Ext.plugin.Abstract',
  3. alias: 'plugin.menuclipboard',
  4. copyCls : 'grid-copy',
  5. formats: {
  6. cell: {
  7. get: 'getCells'
  8. },
  9. html: {
  10. get: 'getCellData'
  11. },
  12. raw: {
  13. get: 'getCellData',
  14. put: 'putCellData'
  15. }
  16. },
  17. constructor: function (config) {
  18. if (config) {
  19. this.pluginConfig = config;
  20. this.cmp = config.cmp;
  21. this.initConfig(config);
  22. }
  23. },
  24. initConfig: function() {
  25. var me = this;
  26. me.applyEventListeners();
  27. me.callParent(arguments);
  28. },
  29. isExecable: function() {
  30. return Ext.isChrome && Number(Ext.userAgent.match(/chrome\/[\d.]+/gi)[0].replace(/[^0-9.]/ig,"").substring(0, 2)) > 42;
  31. },
  32. applyEventListeners: function() {
  33. var me = this,
  34. grid = me.cmp;
  35. me.execable = me.isExecable();
  36. grid.on({
  37. cellcontextmenu (view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
  38. e.stopEvent();
  39. me.getContextMenu(view, td, cellIndex, record, tr, rowIndex, e, eOpts);
  40. return false;
  41. },
  42. });
  43. },
  44. getContextMenu : function(view, td, colIdx, record, tr, rowIdx, e) {
  45. var me = this,
  46. grid = me.cmp,
  47. column = view.getHeaderByCell(td) || view.ownerCt.headerCt.getHeaderAtIndex(colIdx);
  48. if (!column) {
  49. return;
  50. }
  51. var dataIndex = column.dataIndex;
  52. e.preventDefault();
  53. var menu = view.copymenu;
  54. if (!menu) {
  55. menu = view.copymenu = me.createMenu();
  56. }
  57. menu.showAt(e.getXY());
  58. // me.clearCopyCls();
  59. menu.grid = grid;
  60. menu.td = td,
  61. menu.record = record;
  62. menu.column = column;
  63. menu.dataIndex = dataIndex;
  64. menu.cell = view.getCell(menu.record, menu.column);
  65. // menu.cell.addCls(me.copyCls);
  66. },
  67. createMenu : function() {
  68. var me = this;
  69. return Ext.create('Ext.menu.Menu', {
  70. cls: 'x-copy-menu',
  71. items: [{
  72. copyType : 'cell',
  73. iconCls : 'x-fa fa-copy',
  74. text : '复制',
  75. handler: function(item) {
  76. if(me.execable) {
  77. var m = item.ownerCt;
  78. me.onCopy(me.getCellText(m.grid, m.td, m.record, m.column, m.dataIndex, m.cell));
  79. }
  80. }
  81. // },{
  82. // copyType : 'row',
  83. // text : '复制行',
  84. // handler: function(item) {
  85. // if(me.execable) {
  86. // var m = item.ownerCt;
  87. // me.onCopy(me.getRecordText(m.grid, m.td, m.record, m.column, m.dataIndex, m.cell));
  88. // }
  89. // }
  90. // },{
  91. // copyType : 'table',
  92. // text : '复制表格',
  93. // handler: function(item) {
  94. // if(me.execable) {
  95. // var m = item.ownerCt;
  96. // me.onCopy(me.getTableText(m.grid));
  97. // }
  98. // }
  99. // },{
  100. // xtype: 'menuseparator',cls:'x-copymenu-spt'
  101. // },{
  102. // xtype: 'menuseparator',cls:'x-copymenu-spt'
  103. // },{
  104. // text : '粘贴',
  105. // iconCls : 'x-button-icon-paste',
  106. // handler : function() {
  107. // me.onCellPaste();
  108. // }
  109. // }, {
  110. // text : '粘贴行',
  111. // handler : function(t, e) {
  112. // var m = t.up('menu'),
  113. // val = me.getCellText(m.grid, m.record, m.column, m.dataIndex);
  114. // m && me.onColumnPaste(val, m.grid, m.column, m.record, m.dataIndex, m.cell, e);
  115. // }
  116. }]
  117. });
  118. },
  119. getCellText : function(grid, td, record, column, dataIndex, cell) {
  120. var v = record.get(dataIndex);
  121. if(v) {
  122. if(Ext.isDate(v)) {
  123. return Ext.Date.format(v, column.format || Ext.Date.defaultFormat);
  124. }
  125. return v;
  126. }
  127. return '';
  128. },
  129. getRecordText : function(grid, td, record, column, dataIndex, cell) {
  130. var me = this, s = [], columns = grid.headerCt.getGridColumns(), v = null;
  131. Ext.each(columns, function(c){
  132. if(!c.hidden && c.dataIndex && c.getWidth() > 0) {
  133. v = record.get(c.dataIndex);
  134. if(c == null) {
  135. s.push(' ');
  136. } else {
  137. if(Ext.isDate(v)) {
  138. s.push(Ext.Date.format(v, c.format || Ext.Date.defaultFormat));
  139. } else {
  140. s.push(v);
  141. }
  142. }
  143. }
  144. });
  145. return s.join('\t');
  146. },
  147. getTableText : function(grid) {
  148. var me = this, s = [];
  149. grid.store.each(function(){
  150. s.push(me.getRecordText(grid, this));
  151. });
  152. return s.join('\n');
  153. },
  154. onCopy: function(text) {
  155. var target = Ext.DomHelper.append(document.body, {
  156. tag: 'textarea',
  157. style: 'opacity: 0;position: absolute;top: -10000px;right: 0;',
  158. html: text
  159. });
  160. target.focus();
  161. target.select();
  162. document.execCommand('Copy');
  163. target.blur();
  164. document.body.removeChild(target);
  165. },
  166. });