Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.

Commit c00ebf2

Browse files
committed
Clean up queue functions in group redraw
1 parent 329d981 commit c00ebf2

File tree

1 file changed

+162
-142
lines changed

1 file changed

+162
-142
lines changed

lib/timeline/component/Group.js

Lines changed: 162 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -207,157 +207,177 @@ Group.prototype.getLabelWidth = function() {
207207
};
208208

209209

210+
211+
212+
/************************************************************************/
213+
214+
215+
Group.prototype._didMarkerHeightChange = function() {
216+
var markerHeight = this.dom.marker.clientHeight;
217+
if (markerHeight != this.lastMarkerHeight) {
218+
this.lastMarkerHeight = markerHeight;
219+
util.forEach(this.items, function (item) {
220+
item.dirty = true;
221+
if (item.displayed) item.redraw();
222+
});
223+
}
224+
return true;
225+
}
226+
227+
Group.prototype._calculateGroupSizeAndPosition = function() {
228+
var foreground = this.dom.foreground;
229+
this.top = foreground.offsetTop;
230+
this.right = foreground.offsetLeft;
231+
this.width = foreground.offsetWidth;
232+
}
233+
234+
Group.prototype._redrawItems = function(forceRestack, lastIsVisible, margin, range) {
235+
var restack = forceRestack || this.stackDirty || this.isVisible && !lastIsVisible;
236+
237+
// if restacking, reposition visible items vertically
238+
if (restack) {
239+
if (typeof this.itemSet.options.order === 'function') {
240+
// a custom order function
241+
// brute force restack of all items
242+
243+
// show all items
244+
var me = this;
245+
var limitSize = false;
246+
util.forEach(this.items, function (item) {
247+
if (!item.displayed) {
248+
item.redraw();
249+
me.visibleItems.push(item);
250+
}
251+
item.repositionX(limitSize);
252+
});
253+
254+
// order all items and force a restacking
255+
var customOrderedItems = this.orderedItems.byStart.slice().sort(function (a, b) {
256+
return me.itemSet.options.order(a.data, b.data);
257+
});
258+
stack.stack(customOrderedItems, margin, true /* restack=true */);
259+
this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
260+
} else {
261+
// no custom order function, lazy stacking
262+
this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
263+
264+
if (this.itemSet.options.stack) {
265+
// TODO: ugly way to access options...
266+
stack.stack(this.visibleItems, margin, true /* restack=true */);
267+
} else {
268+
// no stacking
269+
stack.nostack(this.visibleItems, margin, this.subgroups, this.itemSet.options.stackSubgroups);
270+
}
271+
}
272+
273+
this.stackDirty = false;
274+
}
275+
}
276+
277+
Group.prototype._didResize = function(resized, height) {
278+
resized = util.updateProperty(this, 'height', height) || resized;
279+
// recalculate size of label
280+
resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
281+
resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
282+
return resized
283+
}
284+
285+
Group.prototype._applyGroupHeight = function(height) {
286+
this.dom.background.style.height = height + 'px';
287+
this.dom.foreground.style.height = height + 'px';
288+
this.dom.label.style.height = height + 'px';
289+
}
290+
291+
Group.prototype._updateItemsVerticalPosition = function(resized, margin) {
292+
// update vertical position of items after they are re-stacked and the height of the group is calculated
293+
for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
294+
var item = this.visibleItems[i];
295+
item.repositionY(margin);
296+
if (!this.isVisible && this.groupId != "__background__") {
297+
if (item.displayed) item.hide();
298+
}
299+
}
300+
301+
if (!this.isVisible && this.height) {
302+
return resized = false;
303+
}
304+
305+
return resized;
306+
}
307+
210308
/**
211309
* Repaint this group
212310
* @param {{start: number, end: number}} range
213311
* @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
214312
* @param {boolean} [forceRestack=false] Force restacking of all items
215-
* @param {boolean} [returnQueue=false] return the queue or the result
313+
* @param {boolean} [returnQueue=false] return the queue or if the group resized
216314
* @return {boolean} Returns true if the group is resized or the redraw queue if returnQueue=true
217315
*/
218316
Group.prototype.redraw = function(range, margin, forceRestack, returnQueue) {
219-
var queue = [];
220-
221-
queue.push((function () {
222-
// force recalculation of the height of the items when the marker height changed
223-
// (due to the Timeline being attached to the DOM or changed from display:none to visible)
224-
var markerHeight = this.dom.marker.clientHeight;
225-
if (markerHeight != this.lastMarkerHeight) {
226-
this.lastMarkerHeight = markerHeight;
227-
util.forEach(this.items, function (item) {
228-
item.dirty = true;
229-
if (item.displayed) item.redraw();
230-
});
231-
232-
forceRestack = true;
233-
}
234-
}).bind(this));
235-
236-
queue.push((function () {
237-
// recalculate the height of the subgroups
238-
this._updateSubGroupHeights(margin);
239-
}).bind(this));
240-
241-
queue.push((function () {
242-
// calculate actual size and position
243-
var foreground = this.dom.foreground;
244-
this.top = foreground.offsetTop;
245-
this.right = foreground.offsetLeft;
246-
this.width = foreground.offsetWidth;
247-
}).bind(this));
248-
249-
var resized;
250-
var lastIsVisible;
251-
252-
queue.push((function () {
253-
resized = false;
254-
lastIsVisible = this.isVisible;
255-
this.isVisible = this._isGroupVisible(range, margin);
256-
}).bind(this));
257-
258-
queue.push((function () {
259-
var restack = forceRestack || this.stackDirty || this.isVisible && !lastIsVisible;
260-
261-
// if restacking, reposition visible items vertically
262-
if (restack) {
263-
if (typeof this.itemSet.options.order === 'function') {
264-
// a custom order function
265-
// brute force restack of all items
266-
267-
// show all items
268-
var me = this;
269-
var limitSize = false;
270-
util.forEach(this.items, function (item) {
271-
if (!item.displayed) {
272-
item.redraw();
273-
me.visibleItems.push(item);
274-
}
275-
item.repositionX(limitSize);
276-
});
277-
278-
// order all items and force a restacking
279-
var customOrderedItems = this.orderedItems.byStart.slice().sort(function (a, b) {
280-
return me.itemSet.options.order(a.data, b.data);
281-
});
282-
stack.stack(customOrderedItems, margin, true /* restack=true */);
283-
this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
284-
} else {
285-
// no custom order function, lazy stacking
286-
this.visibleItems = this._updateItemsInRange(this.orderedItems, this.visibleItems, range);
287-
288-
if (this.itemSet.options.stack) {
289-
// TODO: ugly way to access options...
290-
stack.stack(this.visibleItems, margin, true /* restack=true */);
291-
} else {
292-
// no stacking
293-
stack.nostack(this.visibleItems, margin, this.subgroups, this.itemSet.options.stackSubgroups);
294-
}
295-
}
296-
297-
this.stackDirty = false;
298-
}
299-
}).bind(this));
300-
301-
var height;
302-
303-
queue.push((function () {
304-
this._updateSubgroupsSizes();
305-
}).bind(this));
306-
307-
queue.push((function () {
308-
// recalculate the height of the group
309-
height = this._calculateHeight(margin);
310-
}).bind(this));
311-
312-
queue.push((function () {
313-
// calculate actual size and position
314-
var foreground = this.dom.foreground;
315-
this.top = foreground.offsetTop;
316-
this.right = foreground.offsetLeft;
317-
this.width = foreground.offsetWidth;
318-
}).bind(this));
319-
320-
queue.push((function () {
321-
resized = util.updateProperty(this, 'height', height) || resized;
322-
// recalculate size of label
323-
resized = util.updateProperty(this.props.label, 'width', this.dom.inner.clientWidth) || resized;
324-
resized = util.updateProperty(this.props.label, 'height', this.dom.inner.clientHeight) || resized;
325-
}).bind(this));
326-
327-
queue.push((function () {
328-
// apply new height
329-
this.dom.background.style.height = height + 'px';
330-
this.dom.foreground.style.height = height + 'px';
331-
this.dom.label.style.height = height + 'px';
332-
}).bind(this));
333-
334-
queue.push((function () {
335-
// update vertical position of items after they are re-stacked and the height of the group is calculated
336-
for (var i = 0, ii = this.visibleItems.length; i < ii; i++) {
337-
var item = this.visibleItems[i];
338-
item.repositionY(margin);
339-
if (!this.isVisible && this.groupId != "__background__") {
340-
if (item.displayed) item.hide();
341-
}
342-
}
343-
344-
if (!this.isVisible && this.height) {
345-
return resized = false;
346-
}
347-
348-
return resized;
349-
}).bind(this));
317+
var resized = false;
318+
var lastIsVisible = this.isVisible;
319+
var height;
350320

351-
if (returnQueue) {
352-
return queue;
353-
} else {
354-
var result;
355-
queue.forEach(function (fn) {
356-
result = fn();
357-
});
358-
return result;
359-
}
360-
};
321+
var queue = [
322+
// force recalculation of the height of the items when the marker height changed
323+
// (due to the Timeline being attached to the DOM or changed from display:none to visible)
324+
(function () {
325+
forceRestack = this._didMarkerHeightChange.bind(this);
326+
}).bind(this),
327+
328+
// recalculate the height of the subgroups
329+
this._updateSubGroupHeights.bind(this, margin),
330+
331+
// calculate actual size and position
332+
this._calculateGroupSizeAndPosition.bind(this),
333+
334+
// check if group is visible
335+
(function() {
336+
this.isVisible = this._isGroupVisible.bind(this)(range, margin);
337+
}).bind(this),
338+
339+
// redraw Items if needed
340+
(function() {
341+
this._redrawItems.bind(this)(forceRestack, lastIsVisible, margin, range)
342+
}).bind(this),
343+
344+
// update subgroups
345+
this._updateSubgroupsSizes.bind(this),
346+
347+
// recalculate the height of the group
348+
(function() {
349+
height = this._calculateHeight.bind(this)(margin);
350+
}).bind(this),
351+
352+
// calculate actual size and position again
353+
this._calculateGroupSizeAndPosition.bind(this),
354+
355+
// check if resized
356+
(function() {
357+
resized = this._didResize.bind(this)(resized, height)
358+
}).bind(this),
359+
360+
// apply group height
361+
(function() {
362+
this._applyGroupHeight.bind(this)(height)
363+
}).bind(this),
364+
365+
// update vertical position of items after they are re-stacked and the height of the group is calculated
366+
(function() {
367+
return resized = this._updateItemsVerticalPosition.bind(this)(resized, margin)
368+
}).bind(this)
369+
]
370+
371+
if (returnQueue) {
372+
return queue;
373+
} else {
374+
var result;
375+
queue.forEach(function (fn) {
376+
result = fn();
377+
});
378+
return result;
379+
}
380+
};
361381

362382
/**
363383
* recalculate the height of the subgroups

0 commit comments

Comments
 (0)