slickgrid added to third-party
[myslice.git] / third-party / slickgrid-2.1 / plugins / slick.checkboxselectcolumn.js
1 (function ($) {
2   // register namespace
3   $.extend(true, window, {
4     "Slick": {
5       "CheckboxSelectColumn": CheckboxSelectColumn
6     }
7   });
8
9
10   function CheckboxSelectColumn(options) {
11     var _grid;
12     var _self = this;
13     var _handler = new Slick.EventHandler();
14     var _selectedRowsLookup = {};
15     var _defaults = {
16       columnId: "_checkbox_selector",
17       cssClass: null,
18       toolTip: "Select/Deselect All",
19       width: 30
20     };
21
22     var _options = $.extend(true, {}, _defaults, options);
23
24     function init(grid) {
25       _grid = grid;
26       _handler
27         .subscribe(_grid.onSelectedRowsChanged, handleSelectedRowsChanged)
28         .subscribe(_grid.onClick, handleClick)
29         .subscribe(_grid.onHeaderClick, handleHeaderClick)
30         .subscribe(_grid.onKeyDown, handleKeyDown);
31     }
32
33     function destroy() {
34       _handler.unsubscribeAll();
35     }
36
37     function handleSelectedRowsChanged(e, args) {
38       var selectedRows = _grid.getSelectedRows();
39       var lookup = {}, row, i;
40       for (i = 0; i < selectedRows.length; i++) {
41         row = selectedRows[i];
42         lookup[row] = true;
43         if (lookup[row] !== _selectedRowsLookup[row]) {
44           _grid.invalidateRow(row);
45           delete _selectedRowsLookup[row];
46         }
47       }
48       for (i in _selectedRowsLookup) {
49         _grid.invalidateRow(i);
50       }
51       _selectedRowsLookup = lookup;
52       _grid.render();
53
54       if (selectedRows.length && selectedRows.length == _grid.getDataLength()) {
55         _grid.updateColumnHeader(_options.columnId, "<input type='checkbox' checked='checked'>", _options.toolTip);
56       } else {
57         _grid.updateColumnHeader(_options.columnId, "<input type='checkbox'>", _options.toolTip);
58       }
59     }
60
61     function handleKeyDown(e, args) {
62       if (e.which == 32) {
63         if (_grid.getColumns()[args.cell].id === _options.columnId) {
64           // if editing, try to commit
65           if (!_grid.getEditorLock().isActive() || _grid.getEditorLock().commitCurrentEdit()) {
66             toggleRowSelection(args.row);
67           }
68           e.preventDefault();
69           e.stopImmediatePropagation();
70         }
71       }
72     }
73
74     function handleClick(e, args) {
75       // clicking on a row select checkbox
76       if (_grid.getColumns()[args.cell].id === _options.columnId && $(e.target).is(":checkbox")) {
77         // if editing, try to commit
78         if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()) {
79           e.preventDefault();
80           e.stopImmediatePropagation();
81           return;
82         }
83
84         toggleRowSelection(args.row);
85         e.stopPropagation();
86         e.stopImmediatePropagation();
87       }
88     }
89
90     function toggleRowSelection(row) {
91       if (_selectedRowsLookup[row]) {
92         _grid.setSelectedRows($.grep(_grid.getSelectedRows(), function (n) {
93           return n != row
94         }));
95       } else {
96         _grid.setSelectedRows(_grid.getSelectedRows().concat(row));
97       }
98     }
99
100     function handleHeaderClick(e, args) {
101       if (args.column.id == _options.columnId && $(e.target).is(":checkbox")) {
102         // if editing, try to commit
103         if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()) {
104           e.preventDefault();
105           e.stopImmediatePropagation();
106           return;
107         }
108
109         if ($(e.target).is(":checked")) {
110           var rows = [];
111           for (var i = 0; i < _grid.getDataLength(); i++) {
112             rows.push(i);
113           }
114           _grid.setSelectedRows(rows);
115         } else {
116           _grid.setSelectedRows([]);
117         }
118         e.stopPropagation();
119         e.stopImmediatePropagation();
120       }
121     }
122
123     function getColumnDefinition() {
124       return {
125         id: _options.columnId,
126         name: "<input type='checkbox'>",
127         toolTip: _options.toolTip,
128         field: "sel",
129         width: _options.width,
130         resizable: false,
131         sortable: false,
132         cssClass: _options.cssClass,
133         formatter: checkboxSelectionFormatter
134       };
135     }
136
137     function checkboxSelectionFormatter(row, cell, value, columnDef, dataContext) {
138       if (dataContext) {
139         return _selectedRowsLookup[row]
140             ? "<input type='checkbox' checked='checked'>"
141             : "<input type='checkbox'>";
142       }
143       return null;
144     }
145
146     $.extend(this, {
147       "init": init,
148       "destroy": destroy,
149
150       "getColumnDefinition": getColumnDefinition
151     });
152   }
153 })(jQuery);