forked from dojo/dojo1-dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
327 lines (285 loc) · 11 KB
/
tree.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
define([
"dojo/_base/declare",
"dojo/_base/array",
"dojo/_base/Deferred",
"dojo/query",
"dojo/on",
"dojo/aspect",
"./util/has-css3",
"./Grid",
"dojo/has!touch?./util/touch",
"put-selector/put"
], function(declare, arrayUtil, Deferred, querySelector, on, aspect, has, Grid, touchUtil, put){
function defaultRenderExpando(level, hasChildren, expanded, object){
// summary:
// Provides default implementation for column.renderExpando.
var dir = this.grid.isRTL ? "right" : "left",
cls = ".dgrid-expando-icon",
node;
if(hasChildren){
cls += ".ui-icon.ui-icon-triangle-1-" + (expanded ? "se" : "e");
}
node = put("div" + cls + "[style=margin-" + dir + ": " +
(level * (this.indentWidth || 9)) + "px; float: " + dir + "]");
node.innerHTML = " "; // for opera to space things properly
return node;
}
function ontransitionend(event){
var container = this,
height = this.style.height;
if(height){
// After expansion, ensure display is correct;
// after collapse, set display to none to improve performance
this.style.display = height == "0px" ? "none" : "block";
}
// Reset height to be auto, so future height changes (from children
// expansions, for example), will expand to the right height.
if(event){
// For browsers with CSS transition support, setting the height to
// auto or "" will cause an animation to zero height for some
// reason, so temporarily set the transition to be zero duration
put(this, ".dgrid-tree-resetting");
setTimeout(function(){
// Turn off the zero duration transition after we have let it render
put(container, "!dgrid-tree-resetting");
});
}
// Now set the height to auto
this.style.height = "";
}
function tree(column){
// summary:
// Adds tree navigation capability to a column.
var originalRenderCell = column.renderCell || Grid.defaultRenderCell;
var currentLevel, // tracks last rendered item level (for aspected insertRow)
clicked; // tracks row that was clicked (for expand dblclick event handling)
if(!column){ column = {}; }
column.shouldExpand = column.shouldExpand || function(row, level, previouslyExpanded){
// summary:
// Function called after each row is inserted to determine whether
// expand(rowElement, true) should be automatically called.
// The default implementation re-expands any rows that were expanded
// the last time they were rendered (if applicable).
return previouslyExpanded;
};
aspect.after(column, "init", function(){
var grid = column.grid,
colSelector = ".dgrid-content .dgrid-column-" + column.id,
listeners = []; // to be removed when this column is destroyed
// Turn off automatic cleanup of empty observers, to prevent confusion
// due to observers operating at multiple hierarchy levels.
grid.cleanEmptyObservers = false;
if(!grid.store){
throw new Error("dgrid tree column plugin requires a store to operate.");
}
if (!column.renderExpando){
column.renderExpando = defaultRenderExpando;
}
// Set up the event listener once and use event delegation for better memory use.
listeners.push(grid.on(
column.expandOn || ".dgrid-expando-icon:click," + colSelector + ":dblclick," + colSelector + ":keydown",
function(event){
var row = grid.row(event);
if((!grid.store.mayHaveChildren || grid.store.mayHaveChildren(row.data)) &&
(event.type != "keydown" || event.keyCode == 32) &&
!(event.type == "dblclick" && clicked && clicked.count > 1 &&
row.id == clicked.id && event.target.className.indexOf("dgrid-expando-icon") > -1)){
grid.expand(row);
}
// If the expando icon was clicked, update clicked object to prevent
// potential over-triggering on dblclick (all tested browsers but IE < 9).
if(event.target.className.indexOf("dgrid-expando-icon") > -1){
if(clicked && clicked.id == grid.row(event).id){
clicked.count++;
}else{
clicked = {
id: grid.row(event).id,
count: 1
};
}
}
})
);
if(has("touch")){
// Also listen on double-taps of the cell.
listeners.push(grid.on(touchUtil.selector(colSelector, touchUtil.dbltap),
function(){ grid.expand(this); }));
}
// Set up hash to store IDs of expanded rows
if(!grid._expanded){ grid._expanded = {}; }
listeners.push(aspect.after(grid, "insertRow", function(rowElement){
// Auto-expand (shouldExpand) considerations
var row = this.row(rowElement),
expanded = column.shouldExpand(row, currentLevel, this._expanded[row.id]);
if(expanded){ this.expand(rowElement, true, true); }
return rowElement; // pass return value through
}));
listeners.push(aspect.before(grid, "removeRow", function(rowElement, justCleanup){
var connected = rowElement.connected;
if(connected){
// if it has a connected expando node, we process the children
querySelector(">.dgrid-row", connected).forEach(function(element){
grid.removeRow(element, true);
});
// now remove the connected container node
if(!justCleanup){
put(connected, "!");
}
}
}));
if(column.collapseOnRefresh){
// Clear out the _expanded hash on each call to cleanup
// (which generally coincides with refreshes, as well as destroy).
listeners.push(aspect.after(grid, "cleanup", function(){
this._expanded = {};
}));
}
grid._calcRowHeight = function(rowElement){
// we override this method so we can provide row height measurements that
// include the children of a row
var connected = rowElement.connected;
// if connected, need to consider this in the total row height
return rowElement.offsetHeight + (connected ? connected.offsetHeight : 0);
};
grid.expand = function(target, expand, noTransition){
// summary:
// Expands the row corresponding to the given target.
// target: Object
// Row object (or something resolvable to one) to expand/collapse.
// expand: Boolean?
// If specified, designates whether to expand or collapse the row;
// if unspecified, toggles the current state.
var row = target.element ? target : grid.row(target),
hasTransitionend = has("transitionend"),
dfd = new Deferred(),
promise = dfd.promise;
// Resolve initial promise immediately;
// promise will be reassigned later if necessary to only resolve
// after data is retrieved
dfd.resolve();
target = row.element;
target = target.className.indexOf("dgrid-expando-icon") > -1 ? target :
querySelector(".dgrid-expando-icon", target)[0];
if(target && target.mayHaveChildren &&
(noTransition || expand !== !!this._expanded[row.id])){
// toggle or set expand/collapsed state based on optional 2nd argument
var expanded = expand === undefined ? !this._expanded[row.id] : expand;
// update the expando display
put(target, ".ui-icon-triangle-1-" + (expanded ? "se" : "e") +
"!ui-icon-triangle-1-" + (expanded ? "e" : "se"));
var preloadNode = target.preloadNode,
rowElement = row.element,
container,
containerStyle,
scrollHeight,
options = {
originalQuery: this.query
};
if(!preloadNode){
// if the children have not been created, create a container, a preload node and do the
// query for the children
container = rowElement.connected = put('div.dgrid-tree-container');//put(rowElement, '+...
preloadNode = target.preloadNode = put(rowElement, '+', container, 'div.dgrid-preload');
var query = function(options){
return grid.store.getChildren(row.data, options);
};
if(column.allowDuplicates){
// If allowDuplicates is specified, include parentId in options
// in order to facilitate unique IDs for each occurrence of the
// same item under multiple different parents.
options.parentId = row.id;
}
// Include level information on query for renderQuery case
if("level" in target){
query.level = target.level;
}
// Add the query to the promise chain.
promise = promise.then(function(){
return grid.renderQuery ?
grid.renderQuery(query, preloadNode, options) :
grid.renderArray(query(options), preloadNode,
"level" in query ? { queryLevel: query.level } : {});
}).then(function(){
// Expand once results are retrieved, if the row is still expanded.
if(grid._expanded[row.id] && hasTransitionend){
var scrollHeight = container.scrollHeight;
container.style.height = scrollHeight ? scrollHeight + "px" : "auto";
}
});
if(hasTransitionend){
on(container, hasTransitionend, ontransitionend);
}else{
ontransitionend.call(container);
}
}
// Show or hide all the children.
container = rowElement.connected;
container.hidden = !expanded;
containerStyle = container.style;
// make sure it is visible so we can measure it
if(!hasTransitionend || noTransition){
containerStyle.display = expanded ? "block" : "none";
containerStyle.height = "";
}else{
if(expanded){
containerStyle.display = "block";
scrollHeight = container.scrollHeight;
containerStyle.height = "0px";
}
else{
// if it will be hidden we need to be able to give a full height
// without animating it, so it has the right starting point to animate to zero
put(container, ".dgrid-tree-resetting");
containerStyle.height = container.scrollHeight + "px";
}
// Perform a transition for the expand or collapse.
setTimeout(function(){
put(container, "!dgrid-tree-resetting");
containerStyle.height =
expanded ? (scrollHeight ? scrollHeight + "px" : "auto") : "0px";
});
}
// Update _expanded map.
if(expanded){
this._expanded[row.id] = true;
}else{
delete this._expanded[row.id];
}
}
return promise;
}; // end function grid.expand
// Set up a destroy function on column to tear down the listeners/aspects
// established above if the grid's columns are redefined later.
aspect.after(column, "destroy", function(){
arrayUtil.forEach(listeners, function(l){ l.remove(); });
// Delete methods we added/overrode on the instance.
delete grid.expand;
delete grid._calcRowHeight;
});
});
column.renderCell = function(object, value, td, options){
// summary:
// Renders a cell that can be expanded, creating more rows
var grid = column.grid,
level = Number(options && options.queryLevel) + 1,
mayHaveChildren = !grid.store.mayHaveChildren || grid.store.mayHaveChildren(object),
parentId = options.parentId,
expando, node;
level = currentLevel = isNaN(level) ? 0 : level;
expando = column.renderExpando(level, mayHaveChildren,
grid._expanded[(parentId ? parentId + "-" : "") + grid.store.getIdentity(object)], object);
expando.level = level;
expando.mayHaveChildren = mayHaveChildren;
node = originalRenderCell.call(column, object, value, td, options);
if(node && node.nodeType){
put(td, expando);
put(td, node);
}else{
td.insertBefore(expando, td.firstChild);
}
};
return column;
}
tree.defaultRenderExpando = defaultRenderExpando;
return tree;
});