forked from SitePen/dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellSelection.js
127 lines (122 loc) · 3.78 KB
/
CellSelection.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
define(["dojo/_base/declare", "./Selection", "dojo/on", "put-selector/put", "dojo/has"], function(declare, Selection, listen, put, has){
return declare(Selection, {
// summary:
// Add cell level selection capabilities to a grid. The grid will have a selection property and
// fire "dgrid-select" and "dgrid-deselect" events.
// ensure we don't select when an individual cell is not identifiable
selectionDelegate: ".dgrid-cell",
select: function(cell, toCell, value){
var i, id;
if(value === undefined){
// default to true
value = true;
}
if(typeof cell != "object" || !("element" in cell)){
cell = this.cell(cell);
}else if(!cell.row){
// it is row, with the value being a hash
for(id in value){
this.select(this.cell(cell.id, id), null, value[id]);
}
return;
}
if(this.allowSelect(cell)){
var selection = this.selection,
rowId = cell.row.id,
previousRow = selection[rowId];
if(!cell.column){
for(i in this.columns){
this.select(this.cell(rowId, i), null, value);
}
return;
}
var previous = previousRow && previousRow[cell.column.id];
if(value === null){
// indicates a toggle
value = !previous;
}
var element = cell.element;
previousRow = previousRow || {};
previousRow[cell.column.id] = value;
this.selection[rowId] = previousRow;
// Check for all-false objects to see if it can be deleted.
// This prevents build-up of unnecessary iterations later.
var hasSelected = false;
for(i in previousRow){
if(previousRow[i] === true){
hasSelected = true;
break;
}
}
if(!hasSelected){ delete this.selection[rowId]; }
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 != previous && element){
this._selectionEventQueue(value, "cells").push(cell);
}
if(toCell){
// a range
if(!toCell.element){
toCell = this.cell(toCell);
}
var toElement = toCell.element;
var fromElement = cell.element;
// find if it is earlier or later in the DOM
var traverser = (toElement && (toElement.compareDocumentPosition ?
toElement.compareDocumentPosition(fromElement) == 2 :
toElement.sourceIndex > fromElement.sourceIndex)) ? "nextSibling" : "previousSibling";
// now we determine which columns are in the range
var idFrom = cell.column.id, idTo = toCell.column.id, started, columnIds = [];
for(id in this.columns){
if(started){
columnIds.push(id);
}
if(id == idFrom && (idFrom = columnIds) || // once found, we mark it off so we don't hit it again
id == idTo && (idTo = columnIds)){
columnIds.push(id);
if(started || // last id, we are done
(idFrom == columnIds && id == idTo)){ // the ids are the same, we are done
break;
}
started = true;
}
}
// now we iterate over rows
var row = cell.row, nextNode = row.element;
toElement = toCell.row.element;
do{
// looping through each row..
// and now loop through each column to be selected
for(i = 0; i < columnIds.length; i++){
cell = this.cell(nextNode, columnIds[i]);
this.select(cell);
}
if(nextNode == toElement){
break;
}
}while((nextNode = cell.row.element[traverser]));
}
}
},
isSelected: function(object, columnId){
if(!object){
return false;
}
if(!object.element){
object = this.cell(object, columnId);
}
return this.selection[object.row.id] && !!this.selection[object.row.id][object.column.id];
},
clearSelection: function(exceptId){
// disable exceptId in cell selection, since it would require double parameters
exceptId = false;
this.inherited(arguments);
}
});
});