forked from SitePen/dgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.js
443 lines (381 loc) · 13.9 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
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
define([
'dojo/_base/declare',
'dojo/_base/lang',
'dojo/_base/array',
'dojo/aspect',
'dojo/dom-construct',
'dojo/dom-class',
'dojo/on',
'dojo/query',
'dojo/when',
'./util/has-css3',
'./Grid',
'dojo/has!touch?./util/touch'
], function (declare, lang, arrayUtil, aspect, domConstruct, domClass, on, querySelector, when, has, Grid, touchUtil) {
return declare(null, {
// collapseOnRefresh: Boolean
// Whether to collapse all expanded nodes any time refresh is called.
collapseOnRefresh: false,
// enableTreeTransitions: Boolean
// Enables/disables all expand/collapse CSS transitions.
enableTreeTransitions: true,
// treeIndentWidth: Number
// Width (in pixels) of each level of indentation.
treeIndentWidth: 9,
constructor: function () {
this._treeColumnListeners = [];
},
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;
},
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.
if (!this._treeColumn) {
return;
}
var grid = this,
row = target.element ? target : this.row(target),
isExpanded = !!this._expanded[row.id],
hasTransitionend = has('transitionend'),
promise;
target = row.element;
target = target.className.indexOf('dgrid-expando-icon') > -1 ? target :
querySelector('.dgrid-expando-icon', target)[0];
noTransition = noTransition || !this.enableTreeTransitions;
if (target && target.mayHaveChildren && (noTransition || expand !== isExpanded)) {
// toggle or set expand/collapsed state based on optional 2nd argument
var expanded = expand === undefined ? !this._expanded[row.id] : expand;
// update the expando display
domClass.replace(target, 'ui-icon-triangle-1-' + (expanded ? 'se' : 'e'),
'ui-icon-triangle-1-' + (expanded ? 'e' : 'se'));
domClass.toggle(row.element, 'dgrid-row-expanded', expanded);
var rowElement = row.element,
container = rowElement.connected,
containerStyle,
scrollHeight,
options = {};
if (!container) {
// if the children have not been created, create a container, a preload node and do the
// query for the children
container = options.container = rowElement.connected =
domConstruct.create('div', { className: 'dgrid-tree-container' }, rowElement, 'after');
var query = function (options) {
var childCollection = grid._renderedCollection.getChildren(row.data),
results;
if (grid.sort && grid.sort.length > 0) {
childCollection = childCollection.sort(grid.sort);
}
if (childCollection.track && grid.shouldTrackCollection) {
container._rows = options.rows = [];
childCollection = childCollection.track();
// remember observation handles so they can be removed when the parent row is destroyed
container._handles = [
childCollection.tracking,
grid._observeCollection(childCollection, container, options)
];
}
if ('start' in options) {
var rangeArgs = {
start: options.start,
end: options.start + options.count
};
results = childCollection.fetchRange(rangeArgs);
} else {
results = childCollection.fetch();
}
return results;
};
if ('level' in target) {
// Include level information on query for renderQuery case;
// include on container for insertRow to detect in other cases
container.level = query.level = target.level + 1;
}
// Add the query to the promise chain
if (this.renderQuery) {
promise = this.renderQuery(query, options);
}
else {
// If not using OnDemandList, we don't need preload nodes,
// but we still need a beforeNode to pass to renderArray,
// so create a temporary one
var firstChild = domConstruct.create('div', null, container);
promise = this._trackError(function () {
return grid.renderQueryResults(
query(options),
firstChild,
lang.mixin({ rows: options.rows },
'level' in query ? { queryLevel: query.level } : null
)
).then(function (rows) {
domConstruct.destroy(firstChild);
return rows;
});
});
}
if (hasTransitionend) {
// Update height whenever a collapse/expand transition ends.
// (This handler is only registered when each child container is first created.)
on(container, hasTransitionend, this._onTreeTransitionEnd);
}
}
// Show or hide all the children.
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
domClass.add(container, 'dgrid-tree-resetting');
containerStyle.height = container.scrollHeight + 'px';
}
// Perform a transition for the expand or collapse.
setTimeout(function () {
domClass.remove(container, 'dgrid-tree-resetting');
containerStyle.height =
expanded ? (scrollHeight ? scrollHeight + 'px' : 'auto') : '0px';
}, 0);
}
// Update _expanded map.
if (expanded) {
this._expanded[row.id] = true;
}
else {
delete this._expanded[row.id];
}
}
// Always return a promise
return when(promise);
},
_configColumns: function () {
var columnArray = this.inherited(arguments);
// Set up hash to store IDs of expanded rows (here rather than in
// _configureTreeColumn so nothing breaks if no column has renderExpando)
this._expanded = {};
for (var i = 0, l = columnArray.length; i < l; i++) {
if (columnArray[i].renderExpando) {
this._configureTreeColumn(columnArray[i]);
break; // Allow only one tree column.
}
}
return columnArray;
},
insertRow: function (object, container, beforeNode, i, options) {
options = options || {};
var level = options.queryLevel = 'queryLevel' in options ? options.queryLevel :
'level' in container ? container.level : 0;
var rowElement = this.inherited(arguments);
// Auto-expand (shouldExpand) considerations
var row = this.row(rowElement),
expanded = this.shouldExpand(row, level, this._expanded[row.id]);
if (expanded) {
this.expand(rowElement, true, true);
}
if (expanded || (!this.collection.mayHaveChildren || this.collection.mayHaveChildren(object))) {
domClass.add(rowElement, 'dgrid-row-expandable');
}
return rowElement; // pass return value through
},
removeRow: function (rowElement, preserveDom) {
var connected = rowElement.connected,
childOptions = {};
if (connected) {
if (connected._handles) {
arrayUtil.forEach(connected._handles, function (handle) {
handle.remove();
});
delete connected._handles;
}
if (connected._rows) {
childOptions.rows = connected._rows;
}
querySelector('>.dgrid-row', connected).forEach(function (element) {
this.removeRow(element, true, childOptions);
}, this);
if (connected._rows) {
connected._rows.length = 0;
delete connected._rows;
}
if (!preserveDom) {
domConstruct.destroy(connected);
}
}
this.inherited(arguments);
},
_refreshCellFromItem: function (cell, item) {
if (!cell.column.renderExpando) {
return this.inherited(arguments);
}
this.inherited(arguments, [ cell, item, {
queryLevel: querySelector('.dgrid-expando-icon', cell.element)[0].level
}]);
},
cleanup: function () {
this.inherited(arguments);
if (this.collapseOnRefresh) {
// Clear out the _expanded hash on each call to cleanup
// (which generally coincides with refreshes, as well as destroy)
this._expanded = {};
}
},
_destroyColumns: function () {
this.inherited(arguments);
var listeners = this._treeColumnListeners;
for (var i = listeners.length; i--;) {
listeners[i].remove();
}
this._treeColumnListeners = [];
this._treeColumn = null;
},
_calcRowHeight: function (rowElement) {
// Override this method to 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 this.inherited(arguments) + (connected ? connected.offsetHeight : 0);
},
_configureTreeColumn: function (column) {
// summary:
// Adds tree navigation capability to a column.
var grid = this;
var colSelector = '.dgrid-content .dgrid-column-' + column.id;
var clicked; // tracks row that was clicked (for expand dblclick event handling)
this._treeColumn = column;
if (!column._isConfiguredTreeColumn) {
var originalRenderCell = column.renderCell || this._defaultRenderCell;
column._isConfiguredTreeColumn = true;
column.renderCell = function (object, value, td, options) {
// summary:
// Renders a cell that can be expanded, creating more rows
var level = options && 'queryLevel' in options ? options.queryLevel : 0,
mayHaveChildren = !grid.collection.mayHaveChildren || grid.collection.mayHaveChildren(object),
expando, node;
expando = column.renderExpando(level, mayHaveChildren,
grid._expanded[grid.collection.getIdentity(object)], object);
expando.level = level;
expando.mayHaveChildren = mayHaveChildren;
node = originalRenderCell.call(column, object, value, td, options);
if (node && node.nodeType) {
td.appendChild(expando);
td.appendChild(node);
}
else {
td.insertBefore(expando, td.firstChild);
}
};
if (typeof column.renderExpando !== 'function') {
column.renderExpando = this._defaultRenderExpando;
}
}
var treeColumnListeners = this._treeColumnListeners;
if (treeColumnListeners.length === 0) {
// Set up the event listener once and use event delegation for better memory use.
treeColumnListeners.push(this.on(column.expandOn ||
'.dgrid-expando-icon:click,' + colSelector + ':dblclick,' + colSelector + ':keydown',
function (event) {
var row = grid.row(event);
if ((!grid.collection.mayHaveChildren || grid.collection.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.
treeColumnListeners.push(this.on(touchUtil.selector(colSelector, touchUtil.dbltap),
function () {
grid.expand(this);
}));
}
}
},
_defaultRenderExpando: function (level, hasChildren, expanded) {
// summary:
// Default implementation for column.renderExpando.
// NOTE: Called in context of the column definition object.
// level: Number
// Level of indentation for this row (0 for top-level)
// hasChildren: Boolean
// Whether this item may have children (in most cases this determines
// whether an expando icon should be rendered)
// expanded: Boolean
// Whether this item is currently in expanded state
// object: Object
// The item that this expando pertains to
var dir = this.grid.isRTL ? 'right' : 'left',
cls = 'dgrid-expando-icon';
if (hasChildren) {
cls += ' ui-icon ui-icon-triangle-1-' + (expanded ? 'se' : 'e');
}
return domConstruct.create('div', {
className: cls,
innerHTML: ' ',
style: 'margin-' + dir + ': ' + (level * this.grid.treeIndentWidth) + 'px; float: ' + dir + ';'
});
},
_onNotification: function (rows, event) {
if (event.type === 'delete') {
delete this._expanded[event.id];
}
this.inherited(arguments);
},
_onTreeTransitionEnd: function (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
domClass.add(this, 'dgrid-tree-resetting');
setTimeout(function () {
// Turn off the zero duration transition after we have let it render
domClass.remove(container, 'dgrid-tree-resetting');
}, 0);
}
// Now set the height to auto
this.style.height = '';
}
});
});