UNPKG

slickgrid

Version:

A lightning fast JavaScript grid/spreadsheet

9 lines 5.82 kB
/** * SlickGrid v3.0.2 * (c) 2009-present Michael Leibman * homepage: http://github.com/mleibman/slickgrid * license: MIT * date: 2022-11-14 * file: ./plugins/slick.cellexternalcopymanager.js */ !function(I){I.extend(!0,window,{Slick:{CellExternalCopyManager:function(e){var g,p,y=this,w=e||{},r=w.copiedCellStyleLayerKey||"copy-manager",i=w.copiedCellStyle||"copied",s=0,v=w.bodyElement||document.body,S=w.onCopyInit||null,D=w.onCopySuccess||null,R={C:67,V:86,ESC:27,INSERT:45};function x(e,t,o){var l;return t.denyPaste?null:w.dataItemColumnValueSetter?w.dataItemColumnValueSetter(e,t,o):void(t.editor?(l={container:I("body"),column:t,position:{top:0,left:0},grid:g},(l=new t.editor(l)).loadValue(e),l.applyValue(e,o),l.destroy()):e[t.field]=o)}function E(e){var t=document.createElement("textarea");return t.style.position="absolute",t.style.left="-1000px",t.style.top=document.body.scrollTop+"px",t.value=e,v.appendChild(t),t.select(),t}function t(e,t){var o;if(!g.getEditorLock().isActive()||g.getOptions().autoEdit){if(e.which==R.ESC&&p&&(e.preventDefault(),b(),y.onCopyCancelled.notify({ranges:p}),p=null),(e.which===R.C||e.which===R.INSERT)&&(e.ctrlKey||e.metaKey)&&!e.shiftKey&&(S&&S.call(),0!==(o=g.getSelectionModel().getSelectedRanges()).length)){V(p=o),y.onCopyCells.notify({ranges:o});for(var l,f,n,a=g.getColumns(),r="",i=0;i<o.length;i++){for(var s=o[i],u=[],c=s.fromRow;c<s.toRow+1;c++){var d=[],h=g.getDataItem(c);if(0===u.length&&w.includeHeaderWhenCopying){for(var C=[],m=s.fromCell;m<s.toCell+1;m++)0<a[m].name.length&&C.push(function(e){if(w.headerColumnValueExtractor){var t=w.headerColumnValueExtractor(e);if(t)return t}return e.name}(a[m]));u.push(C.join("\t"))}for(m=s.fromCell;m<s.toCell+1;m++)d.push(function(e,t,o){if(w.dataItemColumnValueExtractor){var l=w.dataItemColumnValueExtractor(e,t);if(l)return l}return l="",t.editor?(o={container:I("<p>"),column:t,position:{top:0,left:0},grid:g,event:o},(o=new t.editor(o)).loadValue(e),l=o.serializeValue(),o.destroy()):l=e[t.field],l}(h,a[m],e));u.push(d.join("\t"))}r+=u.join("\r\n")+"\r\n"}return window.clipboardData?(window.clipboardData.setData("Text",r),!0):(l=document.activeElement,(f=E(r)).focus(),setTimeout(function(){v.removeChild(f),l?l.focus():console.log("Not element to restore focus to after copy?")},100),D&&(n=0,n=1===o.length?o[0].toRow+1-o[0].fromRow:o.length,D.call(this,n)),!1)}if(!w.readOnlyMode&&(e.which===R.V&&(e.ctrlKey||e.metaKey)&&!e.shiftKey||e.which===R.INSERT&&e.shiftKey&&!e.ctrlKey))return f=E(""),setTimeout(function(){var i=g,e=f,s=i.getColumns(),t=e.value.split(/[\n\f\r]/),r=(""===t[t.length-1]&&t.pop(),[]),o=0;v.removeChild(e);for(var l=0;l<t.length;l++)""!==t[l]?r[o++]=t[l].split("\t"):r[o++]=[""];var e=i.getActiveCell(),n=(n=i.getSelectionModel().getSelectedRanges())&&n.length?n[0]:null,u=null,c=null;if(n)u=n.fromRow,c=n.fromCell;else{if(!e)return;u=e.row,c=e.cell}var d=!1,a=r.length,e=r.length?r[0].length:0,h=(1==r.length&&1==r[0].length&&n&&(d=!0,a=n.toRow-n.fromRow+1,e=n.toCell-n.fromCell+1),i.getData().length-u),C=0;if(h<a&&w.newRowCreator){for(var m=i.getData(),C=1;C<=a-h;C++)m.push({});i.setData(m),i.render()}n=u+a>i.getDataLength(),w.newRowCreator&&n&&(n=u+a-i.getDataLength(),w.newRowCreator(n)),n={isClipboardCommand:!0,clippedRange:r,oldValues:[],cellExternalCopyManager:y,_options:w,setDataItemValueForColumn:x,markCopySelection:V,oneCellToMultiple:d,activeRow:u,activeCell:c,destH:a,destW:e,maxDestY:i.getDataLength(),maxDestX:i.getColumns().length,h:0,w:0,execute:function(){for(var e=this.h=0;e<this.destH;e++){this.oldValues[e]=[],this.w=0,this.h++;for(var t=0;t<this.destW;t++){this.w++;var o,l=u+e,n=c+t;l<this.maxDestY&&n<this.maxDestX&&(i.getCellNode(l,n),o=i.getDataItem(l),this.oldValues[e][t]=o[s[n].field],d?this.setDataItemValueForColumn(o,s[n],r[0][0]):this.setDataItemValueForColumn(o,s[n],r[e]?r[e][t]:""),i.updateCell(l,n),i.onCellChange.notify({row:l,cell:n,item:o,grid:i}))}}var a={fromCell:c,fromRow:u,toCell:c+this.w-1,toRow:u+this.h-1};this.markCopySelection([a]),i.getSelectionModel().setSelectedRanges([a]),this.cellExternalCopyManager.onPasteCells.notify({ranges:[a]})},undo:function(){for(var e=0;e<this.destH;e++)for(var t=0;t<this.destW;t++){var o,l=u+e,n=c+t;l<this.maxDestY&&n<this.maxDestX&&(i.getCellNode(l,n),o=i.getDataItem(l),d?this.setDataItemValueForColumn(o,s[n],this.oldValues[0][0]):this.setDataItemValueForColumn(o,s[n],this.oldValues[e][t]),i.updateCell(l,n),i.onCellChange.notify({row:l,cell:n,item:o,grid:i}))}var a={fromCell:c,fromRow:u,toCell:c+this.w-1,toRow:u+this.h-1};if(this.markCopySelection([a]),i.getSelectionModel().setSelectedRanges([a]),this.cellExternalCopyManager.onPasteCells.notify({ranges:[a]}),1<C){for(var r=i.getData();1<C;C--)r.splice(r.length-1,1);i.setData(r),i.render()}}},w.clipboardCommandHandler?w.clipboardCommandHandler(n):n.execute()},100),!1}}function V(e){b();for(var t=g.getColumns(),o={},l=0;l<e.length;l++)for(var n=e[l].fromRow;n<=e[l].toRow;n++){o[n]={};for(var a=e[l].fromCell;a<=e[l].toCell&&a<t.length;a++)o[n][t[a].id]=i}g.setCellCssStyles(r,o),clearTimeout(s),s=setTimeout(function(){y.clearCopySelection()},2e3)}function b(){g.removeCellCssStyles(r)}I.extend(this,{init:function(e){if((g=e).onKeyDown.subscribe(t),!(e=e.getSelectionModel()))throw new Error("Selection model is mandatory for this plugin. Please set a selection model on the grid before adding this plugin: grid.setSelectionModel(new Slick.CellSelectionModel())");e.onSelectedRangesChanged.subscribe(function(e,t){g.focus()})},destroy:function(){g.onKeyDown.unsubscribe(t)},pluginName:"CellExternalCopyManager",clearCopySelection:b,handleKeyDown:t,onCopyCells:new Slick.Event,onCopyCancelled:new Slick.Event,onPasteCells:new Slick.Event,setIncludeHeaderWhenCopying:function(e){w.includeHeaderWhenCopying=e}})}}})}(jQuery);