/
Row.js
486 lines (439 loc) · 13.4 KB
/
Row.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
define([
/*====="../../core/Row", =====*/
"dojo/_base/declare",
"dojo/_base/array",
"dojo/_base/event",
// "dojo/query",
'../../support/query',
"dojo/_base/lang",
"dojo/_base/Deferred",
"dojo/_base/sniff",
"dojo/dom-class",
"dojo/mouse",
"dojo/keys",
"../../core/_Module",
"./_RowCellBase"
], function(/*=====Row, =====*/declare, array, event, query, lang, Deferred, has, domClass, mouse, keys, _Module, _RowCellBase){
/*=====
Row.select = function(){
// summary:
// Select this row.
};
Row.deselect = function(){
// summary:
// Deselect this row.
};
Row.isSelected = function(){
// summary:
// Check whether this row is selected.
};
Row.isSelectable = function(){
// summary:
// Check whether this row is selectable.
};
Row.setSelectable = function(selectable){
// summary:
// Set this row to be selectable or not.
};
return declare(_RowCellBase, {
// summary:
// module name: selectRow.
// Provides advanced row selections.
// description:
// This module provides an advanced way for selecting rows by clicking, swiping, SPACE key, or CTRL/SHIFT CLICK to select multiple rows.
// This module uses gridx/core/model/extensions/Mark.
//
// example:
// 1. Use select api on grid row object obtained from grid.row(i)
// | grid.row(1).select();
// | grid.row(1).deselect();
// | grid.row(1).isSelected();
//
// 2. Use select api on select.row module
// | grid.select.row.selectById(rowId);
// | grid.select.row.deSelectById(rowId);
// | grid.select.row.isSelected(rowId);
// | grid.select.row.getSelected();//[]
// | grid.select.row.clear();
// triggerOnCell: Boolean
// Whether row will be selected by clicking on cell, false by default
triggerOnCell: false,
// treeMode: Boolean
// Whether to apply tri-state selection for child rows.
treeMode: true,
// unselectable: Object
// User can set unselectable rows in this hash object. The hash key is the row ID.
unselectable: {},
selectById: function(rowId){
// summary:
// Select rows by id.
// rowId: String...
// Row IDs
},
deselectById: function(rowId){
// summary:
// Deselect rows by id.
// rowId: String...
// Row ID
},
selectByIndex: function(rowIndex){
// summary:
// Select a row by index
// This function can also select multiple rows.
// | //Select several individual rows:
// | gridx.select.row.selectByIndex(rowIndex1, rowIndex2, rowIndex3, ...);
// | //Select a range of rows:
// | gridx.select.row.selectByIndex([rowStartIndex, rowEndIndex]);
// | //Select multiple ranges of rows:
// | gridx.select.row.selectByIndex([rowStartIndex1, rowEndIndex1], [rowStartIndex2, rowEndIndex2], ...);
// rowIndex: Integer...
// Row visual indexes
},
deSelectByIndex: function(rowIndex){
// summary:
// Deselect a row by index.
// This function can also deselect multiple rows. Please refer to selectByIndex().
// rowIndex: Integer...
// Row visual indexes
},
getSelected: function(){
// summary:
// Get id array of all selected row IDs.
},
isSelected: function(rowId){
// summary:
// Check if the given rows are all selected.
// rowId: String...
// Row IDs
// returns:
// True if all given columns are selected; false if not.
},
clear: function(){
// summary:
// Deselected all selected rows;
},
onHighlightChange: function(){
// summary:
// Fired when row highlight is changed.
// tags:
// private
}
});
=====*/
return declare(_RowCellBase, {
name: 'selectRow',
allowRight: false, //allow mouse right click to trigger select
rowMixin: {
select: function(){
this.grid.select.row.selectById(this.id);
return this;
},
deselect: function(){
this.grid.select.row.deselectById(this.id);
return this;
},
isSelected: function(){
return this.model.getMark(this.id) === true;
},
isSelectable: function(){
return this.grid.select.row._isSelectable(this.id);
},
setSelectable: function(selectable){
this.grid.select.row.setSelectable(this.id, selectable);
}
},
//Public-----------------------------------------------------------------
treeMode: true,
setSelectable: function(rowId, selectable){
var t = this,
m = t.model,
n = t.grid.body.getRowNode({
rowId: rowId
});
m.setMarkable(rowId, selectable);
t.unselectable[rowId] = !selectable;
if(n){
domClass.toggle(n, 'gridxRowUnselectable', !selectable);
t.onHighlightChange({row: parseInt(n.getAttribute('visualindex'), 10)}, m.getMark(rowId));
t.onSelectionChange();
}
},
getSelected: function(){
return this.model.getMarkedIds();
},
isSelected: function(){
return array.every(arguments, function(id){
return this.model.getMark(id) === true;
}, this);
},
clear: function(silent){
query(".gridxRowSelected", this.grid.mainNode).forEach(function(node){
domClass.remove(node, 'gridxRowSelected');
node.removeAttribute('aria-selected');
});
query(".gridxRowPartialSelected", this.grid.mainNode).forEach(function(node){
domClass.remove(node, 'gridxRowPartialSelected');
});
this._clear();
this.model.clearMark();
if(!silent){
this._onSelectionChange();
}
},
onHighlightChange: function(){},
//Private---------------------------------------------------------------------
_type: 'row',
_isSelectable: function(rowId){
return !this.arg('unselectable', {})[rowId];
},
_getUnselectableRows: function(){
var ret = [],
t = this,
unselectable = this.arg('unselectable');
for(var id in unselectable){
if(unselectable[id] && t.model.byId(id)){
ret.push(id);
}
}
return ret;
},
_init: function(){
var t = this,
g = t.grid,
unselectable = t.arg('unselectable', {});
//#12078
//tri-state selection status is meaningless for grid without tree module
t.model.treeMarkMode('', t.arg('treeMode') && g.tree);
for(var id in unselectable){
t.model.setMarkable(id, !unselectable[id]);
}
t.inherited(arguments);
//Use special types to make filtered out rows unselected
t.model._spTypes.select = 1; //1 as true
t.model.setMarkable(lang.hitch(t, t._isSelectable));
function canSelect(e){
if(e.columnId){
var col = g._columnsById[e.columnId];
if(t.arg('triggerOnCell')){
return col.rowSelectable !== false &&
!domClass.contains(e.target, 'gridxTreeExpandoIcon') &&
!domClass.contains(e.target, 'gridxTreeExpandoInner');
}
return col.rowSelectable;
}
return !e.columnId;
}
t.batchConnect(
g.rowHeader && [g.rowHeader, 'onMoveToRowHeaderCell', '_onMoveToRowHeaderCell'],
[g, 'onRowMouseDown', function(e){
if((mouse.isLeft(e) || t.arg('allowRight')) && canSelect(e)){
t._isOnCell = e.columnId;
if(t._isOnCell){
g.body._focusCellCol = e.columnIndex;
}
t._start({row: e.visualIndex}, g._isCtrlKey(e), e.shiftKey);
if(!e.shiftKey && !t.arg('canSwept')){
t._end();
}
}
}],
[g, 'onRowTouchStart', function(e){
if(canSelect(e)){
t._isOnCell = e.columnId;
if(t._isOnCell){
g.body._focusCellCol = e.columnIndex;
}
t._start({row: e.visualIndex}, g._isCtrlKey(e) || e.columnId === '__indirectSelect__', e.shiftKey);
if(!e.shiftKey && !t.arg('canSwept')){
t._end();
}
}
}],
[g, 'onRowTouchEnd', '_end'],
[g.body, 'onAfterRow', function(row){
var unselectable = !row.isSelectable();
domClass.toggle(row.node(), 'gridxRowUnselectable', unselectable);
}],
[g, 'onRowMouseOver', function(e){
if(t._selecting && t.arg('triggerOnCell') && e.columnId){
g.body._focusCellCol = e.columnIndex;
}
t._highlight({row: e.visualIndex});
}],
[g, has('ff') < 4 ? 'onRowKeyUp' : 'onRowKeyDown', function(e){
if(e.keyCode == keys.SPACE && (!e.columnId ||
(g._columnsById[e.columnId].rowSelectable) ||
//When trigger on cell, check if we are navigating on body, reducing the odds of conflictions.
(t.arg('triggerOnCell') && (!g.focus || g.focus.currentArea() == 'body')))){
event.stop(e);
t._isOnCell = e.columnId;
t._start({row: e.visualIndex}, g._isCtrlKey(e), e.shiftKey);
t._end();
}
}],
[g.model, 'setStore', '_syncUnselectable']);
},
_markById: function(args, toSelect){
var m = this.model;
array.forEach(args, function(arg){
m.markById(arg, toSelect);
});
m.when();
},
_markByIndex: function(args, toSelect){
var g = this.grid,
m = this.model,
view = g.view,
rowInfo;
array.forEach(args, function(arg){
if(lang.isArrayLike(arg)){
var start = arg[0],
end = arg[1],
i, count;
if(start >= 0 && start < Infinity){
if(end >= start && end < Infinity){
count = end - start + 1;
}else{
count = view.visualCount - start;
}
rowInfo = view.getRowInfo({visualIndex: start});
start = rowInfo.rowIndex;
for(i = 0; i < count; ++i){
m.markByIndex(i + start, toSelect, '', rowInfo.parentId);
}
}
}else if(arg >= 0 && arg < Infinity){
rowInfo = view.getRowInfo({visualIndex: arg});
m.markByIndex(rowInfo.rowIndex, toSelect, '', rowInfo.parentId);
}
});
return m.when();
},
_onRender: function(start, count){
var t = this, i, end = start + count;
for(i = start; i < end; ++i){
var item = {row: i},
toHighlight = t._isSelected(item) || (t._selecting && t._toSelect &&
t._inRange(i, t._startItem.row, t._currentItem.row, 1));
if(toHighlight){
t._doHighlight(item, toHighlight);
}
}
},
_onMark: function(id, toMark, oldState, type){
if(type == 'select' && this.grid.body.renderedIds[id]){
// if(type == 'select'){
var nodes = query('[rowid="' + this.grid._escapeId(id) + '"]', this.grid.mainNode);
if(nodes.length){
nodes.forEach(function(node){
var selected = toMark && toMark != 'mixed';
domClass.toggle(node, 'gridxRowSelected', selected);
domClass.toggle(node, 'gridxRowPartialSelected', toMark == 'mixed');
node.setAttribute('aria-selected', !!selected);
});
this.onHighlightChange({row: parseInt(nodes[0].getAttribute('visualindex'), 10)}, toMark);
}
}
},
_onMoveToCell: function(rowVisIndex, colIndex, e){
var t = this;
if(t.arg('triggerOnCell') && e.shiftKey && (e.keyCode == keys.UP_ARROW || e.keyCode == keys.DOWN_ARROW)){
t._start({row: rowVisIndex}, t.grid._isCtrlKey(e), 1); //1 as true
t._end();
}
},
_onMoveToRowHeaderCell: function(rowVisIndex, e){
if(e.shiftKey){
this._start({row: rowVisIndex}, this.grid._isCtrlKey(e), 1); //1 as true
this._end();
}
},
_isSelected: function(target){
var t = this,
id = t._getRowId(target.row);
return t._isRange ? array.indexOf(t._refSelectedIds, id) >= 0 : t.model.getMark(id);
},
_beginAutoScroll: function(){
this.grid.autoScroll.horizontal = false;
},
_endAutoScroll: function(){
this.grid.autoScroll.horizontal = true;
},
_doHighlight: function(target, toHighlight){
query('[visualindex="' + target.row + '"]', this.grid.mainNode).forEach(function(node){
var selected = toHighlight && toHighlight != 'mixed';
domClass.toggle(node, 'gridxRowSelected', selected);
domClass.toggle(node, 'gridxRowPartialSelected', toHighlight == 'mixed');
node.setAttribute('aria-selected', !!selected);
});
this.onHighlightChange(target, toHighlight);
},
_end: function(){
this.inherited(arguments);
delete this._isOnCell;
},
_focus: function(target){
var g = this.grid, focus = g.focus;
if(focus){
g.body._focusCellRow = target.row;
focus.focusArea(this._isOnCell ? 'body' : 'rowHeader', true);
}
},
_addToSelected: function(start, end, toSelect){
var t = this,
view = t.grid.view,
m = t.model,
lastEndItem = t._lastEndItem,
a, b, i, d;
if(!t._isRange){
t._refSelectedIds = m.getMarkedIds();
}
if(t._isRange && t._inRange(end.row, start.row, lastEndItem.row)){
a = Math.min(end.row, lastEndItem.row);
b = Math.max(end.row, lastEndItem.row);
start = view.getRowInfo({visualIndex: a}).rowIndex + 1;
end = view.getRowInfo({visualIndex: b}).rowIndex;
d = new Deferred();
m.when({
start: start,
count: end - start + 1
}, function(){
for(i = start; i <= end; ++i){
var id = m.indexToId(i),
selected = array.indexOf(t._refSelectedIds, id) >= 0;
m.markById(id, selected);
}
}).then(function(){
m.when(null, function(){
d.callback();
});
});
return d;
}else{
a = Math.min(start.row, end.row);
b = Math.max(start.row, end.row);
for(i = a; i <= b; ++i){
var rowInfo = view.getRowInfo({visualIndex: i});
m.markByIndex(rowInfo.rowIndex, toSelect, '', rowInfo.parentId);
}
return m.when();
}
},
_highlightSingle: function(target, toHighlight){ //prevent highlight at UI level if a row is not selectable
var rowId = this._getRowId(target.row);
if(!this._isSelectable(rowId)){
toHighlight = this.model.getMark(rowId);
}else{
toHighlight = toHighlight ? this._toSelect : this._isSelected(target);
}
this._doHighlight(target, toHighlight);
},
_syncUnselectable: function(){
var t = this,
unselectable = t.arg('unselectable');
for(var id in unselectable){
t.model.setMarkable(id, !unselectable[id]);
}
}
});
});