UNPKG

2.52 kBJavaScriptView Raw
1(function ($) {
2 // register namespace
3 $.extend(true, window, {
4 "Slick": {
5 "CellCopyManager": CellCopyManager
6 }
7 });
8
9
10 function CellCopyManager() {
11 var _grid;
12 var _self = this;
13 var _copiedRanges;
14
15 function init(grid) {
16 _grid = grid;
17 _grid.onKeyDown.subscribe(handleKeyDown);
18 }
19
20 function destroy() {
21 _grid.onKeyDown.unsubscribe(handleKeyDown);
22 }
23
24 function handleKeyDown(e, args) {
25 var ranges;
26 if (!_grid.getEditorLock().isActive()) {
27 if (e.which == $.ui.keyCode.ESCAPE) {
28 if (_copiedRanges) {
29 e.preventDefault();
30 clearCopySelection();
31 _self.onCopyCancelled.notify({ranges: _copiedRanges});
32 _copiedRanges = null;
33 }
34 }
35
36 if (e.which == 67 && (e.ctrlKey || e.metaKey)) {
37 ranges = _grid.getSelectionModel().getSelectedRanges();
38 if (ranges.length !== 0) {
39 e.preventDefault();
40 _copiedRanges = ranges;
41 markCopySelection(ranges);
42 _self.onCopyCells.notify({ranges: ranges});
43 }
44 }
45
46 if (e.which == 86 && (e.ctrlKey || e.metaKey)) {
47 if (_copiedRanges) {
48 e.preventDefault();
49 ranges = _grid.getSelectionModel().getSelectedRanges();
50 _self.onPasteCells.notify({from: _copiedRanges, to: ranges});
51 if (!_grid.getOptions().preserveCopiedSelectionOnPaste) {
52 clearCopySelection();
53 _copiedRanges = null;
54 }
55 }
56 }
57 }
58 }
59
60 function markCopySelection(ranges) {
61 var columns = _grid.getColumns();
62 var hash = {};
63 for (var i = 0; i < ranges.length; i++) {
64 for (var j = ranges[i].fromRow; j <= ranges[i].toRow; j++) {
65 hash[j] = {};
66 for (var k = ranges[i].fromCell; k <= ranges[i].toCell; k++) {
67 hash[j][columns[k].id] = "copied";
68 }
69 }
70 }
71 _grid.setCellCssStyles("copy-manager", hash);
72 }
73
74 function clearCopySelection() {
75 _grid.removeCellCssStyles("copy-manager");
76 }
77
78 $.extend(this, {
79 "init": init,
80 "destroy": destroy,
81 "pluginName": "CellCopyManager",
82
83 "clearCopySelection": clearCopySelection,
84
85 "onCopyCells": new Slick.Event(),
86 "onCopyCancelled": new Slick.Event(),
87 "onPasteCells": new Slick.Event()
88 });
89 }
90})(jQuery);
\No newline at end of file