forked from SitePen/dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection.js
308 lines (276 loc) · 9.12 KB
/
Selection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
define(["dojo/_base/kernel", "dojo/_base/declare", "dojo/_base/Deferred", "dojo/on", "dojo/has", "dojo/aspect", "./List", "dojo/has!touch?./util/touch", "put-selector/put", "dojo/query"],
function(kernel, declare, Deferred, on, has, aspect, List, touchUtil, put){
var ctrlEquiv = has("mac") ? "metaKey" : "ctrlKey";
return declare([List], {
// summary:
// Add selection capabilities to a grid. The grid will have a selection property and
// fire "dgrid-select" and "dgrid-deselect" events.
// selectionDelegate: String
// Selector to delegate to as target of selection events.
selectionDelegate: ".dgrid-row",
// selectionEvents: String
// Event (or events, comma-delimited) to listen on to trigger select logic.
// Note: this is ignored in the case of touch devices.
selectionEvents: "mousedown,dgrid-cellfocusin",
// deselectOnRefresh: Boolean
// If true, the selection object will be cleared when refresh is called.
deselectOnRefresh: true,
//allowSelectAll: Boolean
// If true, allow ctrl/cmd+A to select all rows.
// Also consulted by the selector plugin for showing select-all checkbox.
allowSelectAll: false,
create: function(){
this.selection = {};
return this.inherited(arguments);
},
postCreate: function(){
this.inherited(arguments);
this._initSelectionEvents(); // first time; set up event hooks
},
// selection:
// An object where the property names correspond to
// object ids and values are true or false depending on whether an item is selected
selection: {},
// selectionMode: String
// The selection mode to use, can be "none", "multiple", "single", or "extended".
selectionMode: "extended",
_setSelectionMode: function(mode){
// summary:
// Updates selectionMode, resetting necessary variables.
if(mode == this.selectionMode){ return; } // prevent unnecessary spinning
// Start selection fresh when switching mode.
this.clearSelection();
this._lastSelected = null;
this.selectionMode = mode;
},
setSelectionMode: function(mode){
kernel.deprecated("setSelectionMode(...)", 'use set("selectionMode", ...) instead', "dgrid 1.0");
this.set("selectionMode", mode);
},
_handleSelect: function(event, currentTarget){
// don't run if selection mode is none,
// or if coming from a dgrid-cellfocusin from a mousedown
if(this.selectionMode == "none" ||
(event.type == "dgrid-cellfocusin" && event.parentType == "mousedown")){
return;
}
var ctrlKey = event.type == "mousedown" ? event[ctrlEquiv] : event.ctrlKey;
if(event.type == "mousedown" || !event.ctrlKey || event.keyCode == 32){
var mode = this.selectionMode,
row = currentTarget,
rowObj = this.row(row),
lastRow = this._lastSelected;
if(mode == "single"){
if(lastRow == row){
if(ctrlKey){
// allow deselection even within single select mode
this.select(row, null, null);
}
}else{
this.clearSelection();
this.select(row);
}
this._lastSelected = row;
}else{
var value;
// clear selection first for non-ctrl-clicks in extended mode,
// as well as for right-clicks on unselected targets
if((event.button != 2 && mode == "extended" && !ctrlKey) ||
(event.button == 2 && !(this.selection[rowObj.id]))){
this.clearSelection(rowObj.id);
}
if(!event.shiftKey){
// null == toggle; undefined == true;
lastRow = value = ctrlKey ? null : undefined;
}
this.select(row, lastRow, value);
if(!lastRow){
// update lastRow reference for potential subsequent shift+select
// (current row was already selected by earlier logic)
this._lastSelected = row;
}
}
if(event.type == "mousedown" && (event.shiftKey || ctrlKey)){
// prevent selection in firefox
event.preventDefault();
}
}
},
_initSelectionEvents: function(){
// summary:
// Performs first-time hookup of event handlers containing logic
// required for selection to operate.
var grid = this,
selector = this.selectionDelegate;
// This is to stop IE8+'s web accelerator and selection.
// It also stops selection in Chrome/Safari.
on(this.domNode, "selectstart", function(event){
// In IE, this also bubbles from text selection inside editor fields;
// we don't want to prevent that!
var tag = event.target && event.target.tagName;
if(tag != "INPUT" && tag != "TEXTAREA"){
event.preventDefault();
}
});
function focus(event){
grid._handleSelect(event, this);
}
if(touchUtil){
// listen for touch taps if available
on(this.contentNode, touchUtil.selector(selector, touchUtil.tap), function(evt){
grid._handleSelect(evt, this);
});
}else{
// listen for actions that should cause selections
on(this.contentNode, on.selector(selector, this.selectionEvents), focus);
}
// If allowSelectAll is true, allow ctrl/cmd+A to (de)select all rows.
// (Handler further checks against _allowSelectAll, which may be updated
// if selectionMode is changed post-init.)
if(this.allowSelectAll){
this.on("keydown", function(event) {
if (event[ctrlEquiv] && event.keyCode == 65) {
event.preventDefault();
grid[grid.allSelected ? "clearSelection" : "selectAll"]();
}
});
}
aspect.before(this, "removeRow", function(rowElement, justCleanup){
if(!justCleanup){
// if it is a real row removal for a selected item, deselect it
if(this.row(rowElement).id in this.selection){ this.deselect(rowElement); }
}
});
},
allowSelect: function(row){
// summary:
// A method that can be overriden to determine whether or not a row (or
// cell) can be selected. By default, all rows (or cells) are selectable.
return true;
},
_selectionEventQueue: function(value, type){
var grid = this,
event = "dgrid-" + (value ? "select" : "deselect"),
rows = this[event]; // current event queue (actually cells for CellSelection)
if(rows){ return rows; } // return existing queue, allowing to push more
// Create a timeout to fire an event for the accumulated rows once everything is done.
// We expose the callback in case the event needs to be fired immediately.
setTimeout(this._fireSelectionEvent = function(){
if(!rows){ return; } // rows will be set only the first time this is called
var eventObject = {
bubbles: true,
grid: grid
};
eventObject[type] = rows;
on.emit(grid.contentNode, event, eventObject);
rows = null;
// clear the queue, so we create a new one as needed
delete grid[event];
}, 0);
return (rows = this[event] = []);
},
select: function(row, toRow, value){
if(value === undefined){
// default to true
value = true;
}
if(!row.element){
row = this.row(row);
}
if(this.allowSelect(row)){
var selection = this.selection;
var previousValue = selection[row.id];
if(value === null){
// indicates a toggle
value = !previousValue;
}
var element = row.element;
if(!value && !this.allSelected){
delete this.selection[row.id];
}else{
selection[row.id] = value;
}
if(element){
// add or remove classes as appropriate
if(value){
put(element, ".dgrid-selected.ui-state-active");
}else{
put(element, "!dgrid-selected!ui-state-active");
}
}
if(value != previousValue && element){
// add to the queue of row events
this._selectionEventQueue(value, "rows").push(row);
}
if(toRow){
if(!toRow.element){
toRow = this.row(toRow);
}
var toElement = toRow.element;
var fromElement = row.element;
// find if it is earlier or later in the DOM
var traverser = (toElement && (toElement.compareDocumentPosition ?
toElement.compareDocumentPosition(fromElement) == 2 :
toElement.sourceIndex > fromElement.sourceIndex)) ? "down" : "up";
while(row.element != toElement && (row = this[traverser](row))){
this.select(row);
}
}
}
},
deselect: function(row, toRow){
this.select(row, toRow, false);
},
clearSelection: function(exceptId){
this.allSelected = false;
for(var id in this.selection){
if(exceptId !== id){
this.deselect(id);
}
}
},
selectAll: function(){
this.allSelected = true;
this.selection = {}; // we do this to clear out pages from previous sorts
for(var i in this._rowIdToObject){
var row = this.row(this._rowIdToObject[i]);
this.select(row.id);
}
},
isSelected: function(object){
if(!object){
return false;
}
if(!object.element){
object = this.row(object);
}
return !!this.selection[object.id];
},
refresh: function(){
if(this.deselectOnRefresh){
this.clearSelection();
// Need to fire the selection event now because after the refresh,
// the nodes that we will fire for will be gone.
this._fireSelectionEvent && this._fireSelectionEvent();
}
this._lastSelected = null;
this.inherited(arguments);
},
renderArray: function(){
var grid = this,
rows = this.inherited(arguments);
Deferred.when(rows, function(rows){
var selection = grid.selection,
i, row, selected;
for(i = 0; i < rows.length; i++){
row = grid.row(rows[i]);
selected = row.id in selection ? selection[row.id] : grid.allSelected;
if(selected){
grid.select(row, null, selected);
}
}
});
return rows;
}
});
});