forked from dojo/dojo1-dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColumnSet.js
249 lines (226 loc) · 8.25 KB
/
ColumnSet.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
define(["dojo/_base/kernel", "dojo/_base/declare", "dojo/_base/Deferred", "dojo/on", "dojo/aspect", "dojo/query", "dojo/has", "put-selector/put", "xstyle/has-class", "./Grid", "dojo/_base/sniff", "xstyle/css!./css/columnset.css"],
function(kernel, declare, Deferred, listen, aspect, query, has, put, hasClass, Grid){
has.add("event-mousewheel", function(global, document, element){
return typeof element.onmousewheel !== "undefined";
});
has.add("event-wheel", function(global, document, element){
var supported = false;
// From https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/wheel
try{
WheelEvent("wheel");
supported = true;
}finally{
return supported;
}
});
console.log(has("event-mousewheel"), has("event-wheel"));
var colsetidAttr = "data-dgrid-column-set-id";
hasClass("safari", "ie-7");
function positionScrollers(grid){
var domNode = grid.domNode,
scrollers = grid._columnSetScrollers,
scrollerContents = grid._columnSetScrollerContents,
columnSets = grid.columnSets,
left = 0, scrollerWidth = 0,
i, l, columnSetElement, contentWidth;
for(i = 0, l = columnSets.length; i < l; i++){
// iterate through the columnSets
left += scrollerWidth;
columnSetElement = query('.dgrid-column-set[' + colsetidAttr + '="' + i +'"]', domNode)[0];
scrollerWidth = columnSetElement.offsetWidth;
contentWidth = columnSetElement.firstChild.offsetWidth;
scrollerContents[i].style.width = contentWidth + "px";
scrollers[i].style.width = scrollerWidth + "px";
scrollers[i].style.overflowX = contentWidth > scrollerWidth ? "scroll" : "auto"; // IE seems to need it be set explicitly
scrollers[i].style.left = left + "px";
}
}
function adjustScrollLeft(grid, row){
var scrollLefts = grid._columnSetScrollLefts;
function doAdjustScrollLeft(){
query(".dgrid-column-set", row).forEach(function(element){
element.scrollLeft = scrollLefts[element.getAttribute(colsetidAttr)];
});
}
if(has("ie") < 8 || has("quirks")){
setTimeout(doAdjustScrollLeft, 1);
}else{
doAdjustScrollLeft();
}
}
function scrollColumnSet(grid, columnSetNode, amount){
var id = columnSetNode.getAttribute(colsetidAttr),
scroller = grid._columnSetScrollers[id],
scrollLeft = scroller.scrollLeft + amount;
scroller.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
}
var horizMouseWheel;
if(!has("touch")){
horizMouseWheel = has("event-mousewheel") || has("event-wheel") ? function(grid){
return function(target, listener){
return listen(target, has("event-wheel") ? "wheel" : "mousewheel", function(event){
var node = event.target, deltaX;
// WebKit will invoke mousewheel handlers with an event target of a text
// node; check target and if it's not an element node, start one node higher
// in the tree
if(node.nodeType !== 1){
node = node.parentNode;
}
while(!query.matches(node, ".dgrid-column-set[" + colsetidAttr + "]", target)){
if(node === target || !(node = node.parentNode)){
return;
}
}
// Normalize reported delta value:
// wheelDeltaX (webkit, mousewheel) needs to be negated and divided by 3
// deltaX (FF17+, wheel) can be used exactly as-is
deltaX = event.deltaX || -event.wheelDeltaX / 3;
if(deltaX){
// only respond to horizontal movement
listener.call(null, grid, node, deltaX);
}
});
};
} : function(grid){
return function(target, listener){
return listen(target, ".dgrid-column-set[" + colsetidAttr + "]:MozMousePixelScroll", function(event){
if(event.axis === 1){
// only respond to horizontal movement
listener.call(null, grid, this, event.detail);
}
});
};
};
}
return declare(null, {
// summary:
// Provides column sets to isolate horizontal scroll of sets of
// columns from each other. This mainly serves the purpose of allowing for
// column locking.
postCreate: function(){
this.inherited(arguments);
if(!has("touch")){
this.on(horizMouseWheel(this), function(grid, colsetNode, amount){
var id = colsetNode.getAttribute(colsetidAttr),
scroller = grid._columnSetScrollers[id],
scrollLeft = scroller.scrollLeft + amount;
scroller.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft;
});
}
},
columnSets: [],
createRowCells: function(tag, each){
var row = put("table.dgrid-row-table");
var tr = put(row, "tbody tr");
for(var i = 0, l = this.columnSets.length; i < l; i++){
// iterate through the columnSets
var cell = put(tr, tag + ".dgrid-column-set-cell.dgrid-column-set-" + i +
" div.dgrid-column-set[" + colsetidAttr + "=" + i + "]");
cell.appendChild(this.inherited(arguments, [tag, each, this.columnSets[i]]));
}
return row;
},
renderArray: function(){
var grid = this,
rows = this.inherited(arguments);
Deferred.when(rows, function(rows){
for(var i = 0; i < rows.length; i++){
adjustScrollLeft(grid, rows[i]);
}
});
return rows;
},
renderHeader: function(){
// summary:
// Setup the headers for the grid
this.inherited(arguments);
this.bodyNode.style.bottom = "17px";
var columnSets = this.columnSets,
domNode = this.domNode,
scrollers = this._columnSetScrollers,
scrollerContents = this._columnSetScrollerContents = {},
scrollLefts = this._columnSetScrollLefts = {},
grid = this,
i, l;
function onScroll(){
var scrollLeft = this.scrollLeft;
var colSetId = this.getAttribute(colsetidAttr);
if(scrollLefts[colSetId] != scrollLeft){
scrollLefts[colSetId] = scrollLeft;
query('.dgrid-column-set[' + colsetidAttr + '="' + colSetId + '"],.dgrid-column-set-scroller[' + colsetidAttr + '="' + colSetId + '"]', domNode).
forEach(function(element){
element.scrollLeft = scrollLeft;
});
}
}
function putScroller(columnSet, i){
// function called for each columnSet
var scroller = scrollers[i] =
put(domNode, "div.dgrid-column-set-scroller.dgrid-scrollbar-height.dgrid-column-set-scroller-" + i +
"[" + colsetidAttr + "=" + i +"]");
scrollerContents[i] = put(scroller, "div.dgrid-column-set-scroller-content");
listen(scroller, "scroll", onScroll);
}
function reposition(){
positionScrollers(grid);
}
if (scrollers) {
// this isn't the first time; destroy existing scroller nodes first
for(i in scrollers){
put("!", scrollers[i]);
}
} else {
// first-time-only operations: hook up event/aspected handlers
aspect.after(this, "resize", reposition, true);
aspect.after(this, "styleColumn", reposition, true);
listen(domNode, ".dgrid-column-set:dgrid-cellfocusin", onScroll);
}
// reset to new object to be populated in loop below
scrollers = this._columnSetScrollers = {};
for(i = 0, l = columnSets.length; i < l; i++){
putScroller(columnSets[i], i);
}
positionScrollers(this);
},
styleColumnSet: function(colsetId, css){
// summary:
// Dynamically creates a stylesheet rule to alter a columnset's style.
var rule = this.addCssRule("#" + this.domNode.id + " .dgrid-column-set-" + colsetId, css);
positionScrollers(this);
return rule;
},
_destroyColumns: function(){
var columnSetsLength = this.columnSets.length,
i, j, k, subRowsLength, len, columnSet, subRow, column;
for(i = 0; i < columnSetsLength; i++){
columnSet = this.columnSets[i];
for(j = 0, subRowsLength = columnSet.length; j < subRowsLength; j++){
subRow = columnSet[j];
for(k = 0, len = subRow.length; k < len; k++){
column = subRow[k];
if(typeof column.destroy === "function"){ column.destroy(); }
}
}
}
},
configStructure: function(){
this.columns = {};
for(var i = 0, l = this.columnSets.length; i < l; i++){
// iterate through the columnSets
var columnSet = this.columnSets[i];
for(var j = 0; j < columnSet.length; j++){
columnSet[j] = this._configColumns(i + "-" + j + "-", columnSet[j]);
}
}
},
_setColumnSets: function(columnSets){
this._destroyColumns();
this.columnSets = columnSets;
this._updateColumns();
},
setColumnSets: function(columnSets){
kernel.deprecated("setColumnSets(...)", 'use set("columnSets", ...) instead', "dgrid 1.0");
this.set("columnSets", columnSets);
}
});
});