slickgrid added to third-party
[myslice.git] / third-party / slickgrid-2.1 / plugins / slick.cellcopymanager.js
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             clearCopySelection();
50             ranges = _grid.getSelectionModel().getSelectedRanges();
51             _self.onPasteCells.notify({from: _copiedRanges, to: ranges});
52             _copiedRanges = null;
53           }
54         }
55       }
56     }
57
58     function markCopySelection(ranges) {
59       var columns = _grid.getColumns();
60       var hash = {};
61       for (var i = 0; i < ranges.length; i++) {
62         for (var j = ranges[i].fromRow; j <= ranges[i].toRow; j++) {
63           hash[j] = {};
64           for (var k = ranges[i].fromCell; k <= ranges[i].toCell; k++) {
65             hash[j][columns[k].id] = "copied";
66           }
67         }
68       }
69       _grid.setCellCssStyles("copy-manager", hash);
70     }
71
72     function clearCopySelection() {
73       _grid.removeCellCssStyles("copy-manager");
74     }
75
76     $.extend(this, {
77       "init": init,
78       "destroy": destroy,
79       "clearCopySelection": clearCopySelection,
80
81       "onCopyCells": new Slick.Event(),
82       "onCopyCancelled": new Slick.Event(),
83       "onPasteCells": new Slick.Event()
84     });
85   }
86 })(jQuery);