(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 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 */ 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 keyCodes = { 'C':67, 'V':86 } 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 getDataItemValueForColumn(item, columnDef) { if (_options.dataItemColumnValueExtractor) { return _options.dataItemColumnValueExtractor(item, columnDef); } // if a custom getter is not defined, we call serializeValue of the editor to serialize var editorArgs = { 'container':$(document), // a dummy container 'column':columnDef }; var editor = new columnDef.editor(editorArgs); var retVal = ''; editor.loadValue(item); retVal = editor.serializeValue(); editor.destroy(); return retVal; } function setDataItemValueForColumn(item, columnDef, value) { if (_options.dataItemColumnValueSetter) { return _options.dataItemColumnValueSetter(item, columnDef, value); } // if a custom setter is not defined, we call applyValue of the editor to unserialize var editorArgs = { 'container':$(document), // a dummy container 'column':columnDef }; var editor = new columnDef.editor(editorArgs); editor.loadValue(item); editor.applyValue(item, value); editor.destroy(); } function _createTextBox(innerText){ var ta = document.createElement('textarea'); ta.style.position = 'absolute'; ta.style.left = '-1000px'; ta.style.top = '-1000px'; ta.value = innerText; document.body.appendChild(ta); ta.focus(); return ta; } function _decodeTabularData(_grid, ta){ var columns = _grid.getColumns(); var clipText = ta.value; var clipRows = clipText.split(/[\n\f\r]/); var clippedRange = []; document.body.removeChild(ta); for (var i=0; i