forked from SitePen/dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.js
166 lines (152 loc) · 5.81 KB
/
selector.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
define(["dojo/_base/kernel", "dojo/_base/array", "dojo/on", "dojo/aspect", "dojo/_base/sniff", "put-selector/put"],
function(kernel, arrayUtil, on, aspect, has, put){
return function(column, type){
var listeners = [],
grid, headerCheckbox;
if(column.type){
column.selectorType = column.type;
kernel.deprecated("columndef.type", "use columndef.selectorType instead", "dgrid 1.0");
}
// accept type as argument to Selector function, or from column def
column.selectorType = type = type || column.selectorType || "checkbox";
column.sortable = false;
function changeInput(value){
// creates a function that modifies the input on an event
return function(event){
var rows = event.rows,
len = rows.length,
state = "false",
selection, mixed, i;
for(i = 0; i < len; i++){
var element = grid.cell(rows[i], column.id).element;
if(!element){ continue; } // skip if row has been entirely removed
element = (element.contents || element).input;
if(!element.disabled){
// only change the value if it is not disabled
element.checked = value;
element.setAttribute("aria-checked", value);
}
}
if(headerCheckbox.type == "checkbox"){
selection = grid.selection;
mixed = false;
// see if the header checkbox needs to be indeterminate
for(i in selection){
// if there is anything in the selection, than it is indeterminate
if(selection[i] != grid.allSelected){
mixed = true;
break;
}
}
headerCheckbox.indeterminate = mixed;
headerCheckbox.checked = grid.allSelected;
if (mixed) {
state = "mixed";
} else if (grid.allSelected) {
state = "true";
}
headerCheckbox.setAttribute("aria-checked", state);
}
};
}
function onSelect(event){
// we would really only care about click, since other input sources, like spacebar
// trigger a click, but the click event doesn't provide access to the shift key in firefox, so
// listen for keydown's as well to get an event in firefox that we can properly retrieve
// the shiftKey property from
if(event.type == "click" || event.keyCode == 32 || (!has("opera") && event.keyCode == 13) || event.keyCode === 0){
var row = grid.row(event), lastRow = grid._lastSelected && grid.row(grid._lastSelected);
grid._selectionTriggerEvent = event;
if(type == "radio"){
if(!lastRow || lastRow.id != row.id){
grid.clearSelection();
grid.select(row, null, true);
grid._lastSelected = row.element;
}
}else{
if(row){
if(event.shiftKey){
// make sure the last input always ends up checked for shift key
changeInput(true)({rows: [row]});
}else{
// no shift key, so no range selection
lastRow = null;
}
lastRow = event.shiftKey ? lastRow : null;
grid.select(lastRow || row, row, lastRow ? undefined : null);
grid._lastSelected = row.element;
}else{
put(this, (grid.allSelected ? "!" : ".") + "dgrid-select-all");
grid[grid.allSelected ? "clearSelection" : "selectAll"]();
}
}
grid._selectionTriggerEvent = null;
}
}
function setupSelectionEvents(){
// register one listener at the top level that receives events delegated
grid._hasSelectorInputListener = true;
listeners.push(aspect.before(grid, "_initSelectionEvents", function(){
// listen for clicks and keydown as the triggers
this.on(".dgrid-selector:click,.dgrid-selector:keydown", onSelect);
}));
var handleSelect = grid._handleSelect;
grid._handleSelect = function(event){
// ignore the default select handler for events that originate from the selector column
if(this.cell(event).column != column){
handleSelect.apply(this, arguments);
}
};
if(typeof column.disabled == "function"){
// we override this method to have selections follow the disabled method for selectability
var originalAllowSelect = grid.allowSelect;
grid.allowSelect = function(row){
return originalAllowSelect.call(this, row) && !column.disabled(row.data);
};
}
// register listeners to the select and deselect events to change the input checked value
listeners.push(grid.on("dgrid-select", changeInput(true)));
listeners.push(grid.on("dgrid-deselect", changeInput(false)));
}
var disabled = column.disabled;
var renderInput = typeof type == "function" ? type : function(value, cell, object){
var parent = cell.parentNode;
// must set the class name on the outer cell in IE for keystrokes to be intercepted
put(parent && parent.contents ? parent : cell, ".dgrid-selector");
var input = cell.input || (cell.input = put(cell, "input[type="+type + "]", {
tabIndex: isNaN(column.tabIndex) ? -1 : column.tabIndex,
disabled: disabled && (typeof disabled == "function" ? disabled(object) : disabled),
checked: value
}));
input.setAttribute("aria-checked", !!value);
if(!grid._hasSelectorInputListener){
setupSelectionEvents();
}
return input;
};
aspect.after(column, "init", function(){
grid = column.grid;
});
aspect.after(column, "destroy", function(){
arrayUtil.forEach(listeners, function(l){ l.remove(); });
grid._hasSelectorInputListener = false;
});
column.renderCell = function(object, value, cell, options, header){
var row = object && grid.row(object);
value = row && grid.selection[row.id];
if(header && (type == "radio" || typeof object == "string" || !grid.allowSelectAll)){
cell.appendChild(document.createTextNode(object||""));
if(!grid._hasSelectorInputListener){
setupSelectionEvents();
}
}else{
renderInput(value, cell, object);
}
};
column.renderHeaderCell = function(th){
column.renderCell(column.label || {}, null, th, null, true);
headerCheckbox = th.lastChild;
};
return column;
};
});