(function ($) { // register namespace $.extend(true, window, { "Slick": { "CellExternalCopyManager": CellExternalCopyManager } }); function CellExternalCopyManager(options) { /* This manager enables users to copy/paste data from/to an external Spreadsheet application such as MS-Excel® or OpenOffice-Spreadsheet. Since it is not possible to access directly the clipboard in javascript, the plugin uses a trick to do it's job. After detecting the keystroke, we dynamically create a textarea where the browser copies/pastes the serialized data. options: copiedCellStyle : sets the css className used for copied cells. default : "copied" copiedCellStyleLayerKey : sets the layer key for setting css values of copied cells. default : "copy-manager" dataItemColumnValueExtractor : option to specify a custom column value extractor function dataItemColumnValueSetter : option to specify a custom column value setter function clipboardCommandHandler : option to specify a custom handler for paste actions includeHeaderWhenCopying : set to true and the plugin will take the name property from each column (which is usually what appears in your header) and put that as the first row of the text that's copied to the clipboard bodyElement: option to specify a custom DOM element which to will be added the hidden textbox. It's useful if the grid is inside a modal dialog. onCopyInit: optional handler to run when copy action initializes onCopySuccess: optional handler to run when copy action is complete newRowCreator: function to add rows to table if paste overflows bottom of table, if this function is not provided new rows will be ignored. readOnlyMode: suppresses paste headerColumnValueExtractor : option to specify a custom column header value extractor function */ var _grid; var _self = this; var _copiedRanges; var _options = options || {}; var _copiedCellStyleLayerKey = _options.copiedCellStyleLayerKey || "copy-manager"; var _copiedCellStyle = _options.copiedCellStyle || "copied"; var _clearCopyTI = 0; var _bodyElement = _options.bodyElement || document.body; var _onCopyInit = _options.onCopyInit || null; var _onCopySuccess = _options.onCopySuccess || null; var keyCodes = { 'C': 67, 'V': 86, 'ESC': 27, 'INSERT': 45 }; function init(grid) { _grid = grid; _grid.onKeyDown.subscribe(handleKeyDown); // we need a cell selection model var cellSelectionModel = grid.getSelectionModel(); if (!cellSelectionModel){ 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())"); } // we give focus on the grid when a selection is done on it. // without this, if the user selects a range of cell without giving focus on a particular cell, the grid doesn't get the focus and key stroke handles (ctrl+c) don't work cellSelectionModel.onSelectedRangesChanged.subscribe(function(e, args){ _grid.focus(); }); } function destroy() { _grid.onKeyDown.unsubscribe(handleKeyDown); } function getHeaderValueForColumn(columnDef) { if (_options.headerColumnValueExtractor) { var val = _options.headerColumnValueExtractor(columnDef); if (val) { return val; } } return columnDef.name; } function getDataItemValueForColumn(item, columnDef, e) { if (_options.dataItemColumnValueExtractor) { var val = _options.dataItemColumnValueExtractor(item, columnDef); if (val) { return val; } } var retVal = ''; // if a custom getter is not defined, we call serializeValue of the editor to serialize if (columnDef.editor){ var editorArgs = { 'container':$("
"), // a dummy container
'column':columnDef,
'position':{'top':0, 'left':0}, // a dummy position required by some editors
'grid':_grid,
'event':e
};
var editor = new columnDef.editor(editorArgs);
editor.loadValue(item);
retVal = editor.serializeValue();
editor.destroy();
} else {
retVal = item[columnDef.field];
}
return retVal;
}
function setDataItemValueForColumn(item, columnDef, value) {
if (columnDef.denyPaste) { return null; }
if (_options.dataItemColumnValueSetter) {
return _options.dataItemColumnValueSetter(item, columnDef, value);
}
// if a custom setter is not defined, we call applyValue of the editor to unserialize
if (columnDef.editor){
var editorArgs = {
'container':$("body"), // a dummy container
'column':columnDef,
'position':{'top':0, 'left':0}, // a dummy position required by some editors
'grid':_grid
};
var editor = new columnDef.editor(editorArgs);
editor.loadValue(item);
editor.applyValue(item, value);
editor.destroy();
} else {
item[columnDef.field] = value;
}
}
function _createTextBox(innerText){
var ta = document.createElement('textarea');
ta.style.position = 'absolute';
ta.style.left = '-1000px';
ta.style.top = document.body.scrollTop + 'px';
ta.value = innerText;
_bodyElement.appendChild(ta);
ta.select();
return ta;
}
function _decodeTabularData(_grid, ta){
var columns = _grid.getColumns();
var clipText = ta.value;
var clipRows = clipText.split(/[\n\f\r]/);
// trim trailing CR if present
if (clipRows[clipRows.length - 1]=="") { clipRows.pop(); }
var clippedRange = [];
var j = 0;
_bodyElement.removeChild(ta);
for (var i=0; i