js: Speedup collapsible.js by a factor of 10
It's still relatively slow as it forces a reflow in the browser if there are many collapsibles in the view. I didn't manage to identify the issue yet, but I left a TODO at the location that's responsible for it.
This commit is contained in:
parent
1675cc6d74
commit
94a16056d7
public/js/icinga/behavior
|
@ -40,35 +40,46 @@
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.onRendered = function(event) {
|
Collapsible.prototype.onRendered = function(event) {
|
||||||
var _this = event.data.self;
|
var _this = event.data.self;
|
||||||
|
var toCollapse = [];
|
||||||
|
|
||||||
$('.collapsible:not(.can-collapse)', event.target).each(function() {
|
$.each(event.target.querySelectorAll('.collapsible:not(.can-collapse)'), function (_, collapsible) {
|
||||||
var $collapsible = $(this);
|
|
||||||
|
|
||||||
// Assumes that any newly rendered elements are expanded
|
// Assumes that any newly rendered elements are expanded
|
||||||
if (_this.canCollapse($collapsible)) {
|
if (_this.canCollapse(collapsible)) {
|
||||||
var toggleElement = $collapsible.data('toggleElement');
|
var toggleSelector = collapsible.dataset.toggleElement;
|
||||||
if (!! toggleElement) {
|
if (!! toggleSelector) {
|
||||||
var $toggle = $collapsible.children(toggleElement).first();
|
var toggle = $(collapsible).children(toggleSelector)[0];
|
||||||
if (! $toggle.length) {
|
if (! toggle) {
|
||||||
_this.icinga.logger.error(
|
_this.icinga.logger.error(
|
||||||
'[Collapsible] Control `' + toggleElement + '` not found in .collapsible', $collapsible);
|
'[Collapsible] Control `' + toggleSelector + '` not found in .collapsible', collapsible);
|
||||||
} else if (! $toggle.is('.collapsible-control')) {
|
} else if (! toggle.classList.contains('collapsible-control')) {
|
||||||
$toggle.addClass('collapsible-control');
|
toggle.classList.add('collapsible-control');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
|
setTimeout(function () {
|
||||||
|
var collapsibleControl = document
|
||||||
|
.getElementById('collapsible-control-ghost')
|
||||||
|
.cloneNode(true);
|
||||||
|
collapsibleControl.removeAttribute('id');
|
||||||
|
collapsible.parentNode.insertBefore(collapsibleControl, collapsible.nextElementSibling);
|
||||||
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
$collapsible.addClass('can-collapse');
|
collapsible.classList.add('can-collapse');
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$collapsible.is('[data-no-persistence]')
|
typeof collapsible.dataset.noPersistence !== 'undefined'
|
||||||
|| ! _this.state.has(_this.icinga.utils.getCSSPath($collapsible))
|
|| ! _this.state.has(_this.icinga.utils.getCSSPath(collapsible))
|
||||||
) {
|
) {
|
||||||
_this.collapse($collapsible);
|
toCollapse.push([collapsible, _this.calculateCollapsedHeight(collapsible)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
for (var i = 0; i < toCollapse.length; i++) {
|
||||||
|
_this.collapse(toCollapse[i][0], toCollapse[i][1]);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,27 +89,41 @@
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.onLayoutChange = function(event) {
|
Collapsible.prototype.onLayoutChange = function(event) {
|
||||||
var _this = event.data.self;
|
var _this = event.data.self;
|
||||||
|
var toCollapse = [];
|
||||||
|
|
||||||
$('.collapsible').each(function() {
|
$.each(document.querySelectorAll('.collapsible'), function (_, collapsible) {
|
||||||
var $collapsible = $(this);
|
if (collapsible.matches('.can-collapse')) {
|
||||||
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
|
if (! _this.canCollapse(collapsible)) {
|
||||||
|
$(collapsible).next('.collapsible-control').remove();
|
||||||
if ($collapsible.is('.can-collapse')) {
|
collapsible.classList.remove('can-collapse');
|
||||||
if (! _this.canCollapse($collapsible)) {
|
_this.expand(collapsible);
|
||||||
$collapsible.next('.collapsible-control').remove();
|
|
||||||
$collapsible.removeClass('can-collapse');
|
|
||||||
_this.expand($collapsible);
|
|
||||||
}
|
}
|
||||||
} else if (_this.canCollapse($collapsible)) {
|
} else if (_this.canCollapse(collapsible)) {
|
||||||
// It's expanded but shouldn't
|
// It's expanded but shouldn't
|
||||||
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
|
setTimeout(function () {
|
||||||
$collapsible.addClass('can-collapse');
|
var collapsibleControl = document
|
||||||
|
.getElementById('collapsible-control-ghost')
|
||||||
|
.cloneNode(true);
|
||||||
|
collapsibleControl.removeAttribute('id');
|
||||||
|
collapsible.parentNode.insertBefore(collapsibleControl, collapsible.nextElementSibling);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
if ($collapsible.is('[data-no-persistence]') || ! _this.state.has(collapsiblePath)) {
|
collapsible.classList.add('can-collapse');
|
||||||
_this.collapse($collapsible);
|
|
||||||
|
if (
|
||||||
|
typeof collapsible.dataset.noPersistence !== 'undefined'
|
||||||
|
|| ! _this.state.has(_this.icinga.utils.getCSSPath(collapsible))
|
||||||
|
) {
|
||||||
|
toCollapse.push([collapsible, _this.calculateCollapsedHeight(collapsible)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
for (var i = 0; i < toCollapse.length; i++) {
|
||||||
|
_this.collapse(toCollapse[i][0], toCollapse[i][1]);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,10 +132,10 @@
|
||||||
* @param {string} collapsiblePath
|
* @param {string} collapsiblePath
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.onExpand = function(collapsiblePath) {
|
Collapsible.prototype.onExpand = function(collapsiblePath) {
|
||||||
var $collapsible = $(collapsiblePath);
|
var collapsible = $(collapsiblePath)[0];
|
||||||
|
|
||||||
if ($collapsible.length && $collapsible.is('.can-collapse')) {
|
if (collapsible && collapsible.matches('.can-collapse')) {
|
||||||
this.expand($collapsible);
|
this.expand(collapsible);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -120,10 +145,10 @@
|
||||||
* @param {string} collapsiblePath
|
* @param {string} collapsiblePath
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.onCollapse = function(collapsiblePath) {
|
Collapsible.prototype.onCollapse = function(collapsiblePath) {
|
||||||
var $collapsible = $(collapsiblePath);
|
var collapsible = $(collapsiblePath)[0];
|
||||||
|
|
||||||
if ($collapsible.length && this.canCollapse($collapsible)) {
|
if (collapsible && this.canCollapse(collapsible)) {
|
||||||
this.collapse($collapsible);
|
this.collapse(collapsible, this.calculateCollapsedHeight(collapsible));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -136,27 +161,28 @@
|
||||||
var _this = event.data.self;
|
var _this = event.data.self;
|
||||||
var $target = $(event.currentTarget);
|
var $target = $(event.currentTarget);
|
||||||
|
|
||||||
var $collapsible = $target.prev('.collapsible');
|
var collapsible = $target.prev('.collapsible')[0];
|
||||||
if (! $collapsible.length) {
|
if (! collapsible) {
|
||||||
$collapsible = $target.parent('.collapsible');
|
collapsible = $target.parent('.collapsible')[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $collapsible.length) {
|
if (! collapsible) {
|
||||||
_this.icinga.logger.error('[Collapsible] Collapsible control has no associated .collapsible: ', $target);
|
_this.icinga.logger.error(
|
||||||
} else if ($collapsible.is('[data-no-persistence]')) {
|
'[Collapsible] Collapsible control has no associated .collapsible: ', $target[0]);
|
||||||
if ($collapsible.is('.collapsed')) {
|
} else if (typeof collapsible.dataset.noPersistence !== 'undefined') {
|
||||||
_this.expand($collapsible);
|
if (collapsible.matches('.collapsed')) {
|
||||||
|
_this.expand(collapsible);
|
||||||
} else {
|
} else {
|
||||||
_this.collapse($collapsible);
|
_this.collapse(collapsible, _this.calculateCollapsedHeight(collapsible));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
|
var collapsiblePath = _this.icinga.utils.getCSSPath(collapsible);
|
||||||
if (_this.state.has(collapsiblePath)) {
|
if (_this.state.has(collapsiblePath)) {
|
||||||
_this.state.delete(collapsiblePath);
|
_this.state.delete(collapsiblePath);
|
||||||
_this.collapse($collapsible);
|
_this.collapse(collapsible, _this.calculateCollapsedHeight(collapsible));
|
||||||
} else {
|
} else {
|
||||||
_this.state.set(collapsiblePath);
|
_this.state.set(collapsiblePath);
|
||||||
_this.expand($collapsible);
|
_this.expand(collapsible);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -164,18 +190,18 @@
|
||||||
/**
|
/**
|
||||||
* Return an appropriate row element selector
|
* Return an appropriate row element selector
|
||||||
*
|
*
|
||||||
* @param $collapsible jQuery The given collapsible container element
|
* @param collapsible The given collapsible container element
|
||||||
*
|
*
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.getRowSelector = function($collapsible) {
|
Collapsible.prototype.getRowSelector = function(collapsible) {
|
||||||
if (!! $collapsible.data('visibleHeight')) {
|
if (!! collapsible.dataset.visibleHeight) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($collapsible.is('table')) {
|
if (collapsible.matches('table')) {
|
||||||
return '> tbody > tr';
|
return '> tbody > tr';
|
||||||
} else if ($collapsible.is('ul, ol')) {
|
} else if (collapsible.matches('ul, ol')) {
|
||||||
return '> li:not(.collapsible-control)';
|
return '> li:not(.collapsible-control)';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,74 +211,106 @@
|
||||||
/**
|
/**
|
||||||
* Check whether the given collapsible needs to collapse
|
* Check whether the given collapsible needs to collapse
|
||||||
*
|
*
|
||||||
* @param $collapsible jQuery The given collapsible container element
|
* @param collapsible The given collapsible container element
|
||||||
*
|
*
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.canCollapse = function($collapsible) {
|
Collapsible.prototype.canCollapse = function(collapsible) {
|
||||||
var rowSelector = this.getRowSelector($collapsible);
|
var rowSelector = this.getRowSelector(collapsible);
|
||||||
if (!! rowSelector) {
|
if (!! rowSelector) {
|
||||||
var visibleRows = $collapsible.getData('visibleRows', this.defaultVisibleRows);
|
var visibleRows = Number(collapsible.dataset.visibleRows);
|
||||||
|
if (isNaN(visibleRows)) {
|
||||||
|
visibleRows = this.defaultVisibleRows;
|
||||||
|
}
|
||||||
|
|
||||||
return $(rowSelector, $collapsible).length > visibleRows * 2;
|
return $(rowSelector, collapsible).length > visibleRows * 2;
|
||||||
} else {
|
} else {
|
||||||
var actualHeight = $collapsible[0].scrollHeight - parseFloat($collapsible.css('padding-top'));
|
var actualHeight = collapsible.scrollHeight - parseFloat(
|
||||||
var maxHeight = $collapsible.getData('visibleHeight', this.defaultVisibleHeight);
|
window.getComputedStyle(collapsible).getPropertyValue('padding-top')
|
||||||
|
);
|
||||||
|
|
||||||
|
var maxHeight = Number(collapsible.dataset.visibleHeight);
|
||||||
|
if (isNaN(maxHeight)) {
|
||||||
|
maxHeight = this.defaultVisibleHeight;
|
||||||
|
}
|
||||||
|
|
||||||
return actualHeight >= maxHeight * 2;
|
return actualHeight >= maxHeight * 2;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collapse the given collapsible
|
* Calculate the height the given collapsible should have when collapsed
|
||||||
*
|
*
|
||||||
* @param $collapsible jQuery The given collapsible container element
|
* @param collapsible
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.collapse = function($collapsible) {
|
Collapsible.prototype.calculateCollapsedHeight = function (collapsible) {
|
||||||
var height;
|
var height;
|
||||||
|
|
||||||
var rowSelector = this.getRowSelector($collapsible);
|
var rowSelector = this.getRowSelector(collapsible);
|
||||||
if (!! rowSelector) {
|
if (!! rowSelector) {
|
||||||
height = $collapsible[0].scrollHeight;
|
height = collapsible.scrollHeight;
|
||||||
height -= parseFloat($collapsible.css('padding-bottom'));
|
height -= parseFloat(window.getComputedStyle(collapsible).getPropertyValue('padding-bottom'));
|
||||||
|
|
||||||
var $rows = $(rowSelector, $collapsible).slice(
|
var visibleRows = Number(collapsible.dataset.visibleRows);
|
||||||
$collapsible.getData('visibleRows', this.defaultVisibleRows)
|
if (isNaN(visibleRows)) {
|
||||||
);
|
visibleRows = this.defaultVisibleRows;
|
||||||
$rows.outerHeight(function (i, contentHeight) {
|
}
|
||||||
var $el = $(this);
|
|
||||||
var $prev = $el.prev();
|
|
||||||
|
|
||||||
if (i === 0 && ! $prev.length) { // very first element
|
var $rows = $(rowSelector, collapsible).slice(visibleRows);
|
||||||
height -= parseFloat($el.css('margin-top')) + contentHeight;
|
for (var i = 0; i < $rows.length; i++) {
|
||||||
|
var row = $rows[i];
|
||||||
|
|
||||||
|
if (row.previousElementSibling === null) { // very first element
|
||||||
|
height -= row.offsetHeight;
|
||||||
|
height -= parseFloat(window.getComputedStyle(row).getPropertyValue('margin-top'));
|
||||||
} else if (i < $rows.length - 1) { // every element but the last one
|
} else if (i < $rows.length - 1) { // every element but the last one
|
||||||
var prevBottomOffset = $prev.offset().top + $prev.outerHeight();
|
var prevBottomBorderAt = row.previousElementSibling.offsetTop;
|
||||||
height -= ($el.offset().top - prevBottomOffset) + contentHeight;
|
prevBottomBorderAt += row.previousElementSibling.offsetHeight;
|
||||||
|
height -= row.offsetTop - prevBottomBorderAt + row.offsetHeight;
|
||||||
} else { // the last element
|
} else { // the last element
|
||||||
height -= $el.outerHeight(true);
|
height -= row.offsetHeight;
|
||||||
|
height -= parseFloat(window.getComputedStyle(row).getPropertyValue('margin-top'));
|
||||||
|
height -= parseFloat(window.getComputedStyle(row).getPropertyValue('margin-bottom'));
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
} else {
|
} else {
|
||||||
height = $collapsible.getData('visibleHeight', this.defaultVisibleHeight);
|
height = Number(collapsible.dataset.visibleHeight);
|
||||||
height += parseFloat($collapsible.css('padding-top'));
|
if (isNaN(height)) {
|
||||||
|
height = this.defaultVisibleHeight;
|
||||||
|
}
|
||||||
|
|
||||||
if (!! $collapsible.data('toggleElement')) {
|
height += parseFloat(window.getComputedStyle(collapsible).getPropertyValue('padding-top'));
|
||||||
height += $collapsible.children($collapsible.data('toggleElement')).first().outerHeight(true);
|
|
||||||
|
if (!! collapsible.dataset.toggleElement) {
|
||||||
|
var toggle = $(collapsible).children(collapsible.dataset.toggleElement)[0];
|
||||||
|
height += toggle.offsetHeight; // TODO: Very expensive at times. (50ms+) Check why!
|
||||||
|
height += parseFloat(window.getComputedStyle(toggle).getPropertyValue('margin-top'));
|
||||||
|
height += parseFloat(window.getComputedStyle(toggle).getPropertyValue('margin-bottom'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$collapsible.css({display: 'block', height: height, paddingBottom: 0});
|
return height;
|
||||||
$collapsible.addClass('collapsed');
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collapse the given collapsible
|
||||||
|
*
|
||||||
|
* @param collapsible The given collapsible container element
|
||||||
|
* @param toHeight {int} The height in pixels to collapse to
|
||||||
|
*/
|
||||||
|
Collapsible.prototype.collapse = function(collapsible, toHeight) {
|
||||||
|
collapsible.style.cssText = 'display: block; height: ' + toHeight + 'px; padding-bottom: 0';
|
||||||
|
collapsible.classList.add('collapsed');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand the given collapsible
|
* Expand the given collapsible
|
||||||
*
|
*
|
||||||
* @param $collapsible jQuery The given collapsible container element
|
* @param collapsible The given collapsible container element
|
||||||
*/
|
*/
|
||||||
Collapsible.prototype.expand = function($collapsible) {
|
Collapsible.prototype.expand = function(collapsible) {
|
||||||
$collapsible.removeClass('collapsed');
|
collapsible.classList.remove('collapsed');
|
||||||
$collapsible.css({display: '', height: '', paddingBottom: ''});
|
collapsible.style.cssText = '';
|
||||||
};
|
};
|
||||||
|
|
||||||
Icinga.Behaviors.Collapsible = Collapsible;
|
Icinga.Behaviors.Collapsible = Collapsible;
|
||||||
|
|
Loading…
Reference in New Issue