icingaweb2/public/js/icinga/behavior/collapsible.js

150 lines
5.2 KiB
JavaScript
Raw Normal View History

/*! Icinga Web 2 | (c) 2019 Icinga GmbH | GPLv2+ */
;(function(Icinga, $) {
'use strict';
Icinga.Behaviors = Icinga.Behaviors || {};
2018-12-06 13:53:52 +01:00
/**
* Behavior for collapsible containers.
2018-12-06 13:53:52 +01:00
*
* @param icinga Icinga The current Icinga Object
*/
var Collapsible = function (icinga) {
Icinga.EventListener.call(this, icinga);
this.on('rendered', '.container', this.onRendered, this);
this.on('click', '.collapsible + .collapsible-control', this.onControlClicked, this);
this.icinga = icinga;
this.collapsibleStates = {};
this.defaultNumOfRows = 2;
this.defaultHeight = 36;
};
Collapsible.prototype = new Icinga.EventListener();
2018-12-06 13:53:52 +01:00
/**
* Initializes all collapsibles. Triggered on rendering of a container.
2018-12-06 13:53:52 +01:00
*
* @param event Event The `onRender` event triggered by the rendered container
*/
Collapsible.prototype.onRendered = function(event) {
var _this = event.data.self;
$('.collapsible', event.currentTarget).each(function() {
var $collapsible = $(this);
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
// Assumes that any newly rendered elements are expanded
if (_this.canCollapse($collapsible)) {
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
$collapsible.addClass('can-collapse');
if (typeof _this.collapsibleStates[collapsiblePath] === 'undefined') {
_this.collapsibleStates[collapsiblePath] = true;
_this.collapse($collapsible);
} else if (_this.collapsibleStates[collapsiblePath]) {
_this.collapse($collapsible);
}
} else {
// This collapsible is not large enough (anymore)
delete _this.collapsibleStates[collapsiblePath];
}
});
};
2018-12-06 13:53:52 +01:00
/**
* Event handler for toggling collapsibles. Switches the collapsed state of the respective container.
2018-12-06 13:53:52 +01:00
*
* @param event Event The `onClick` event triggered by the clicked collapsible-control element
*/
Collapsible.prototype.onControlClicked = function(event) {
var _this = event.data.self;
var $target = $(event.currentTarget);
var $collapsible = $target.prev('.collapsible');
if (! $collapsible.length) {
_this.icinga.logger.error('[Collapsible] Collapsible control has no associated .collapsible: ', $target);
} else {
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
if (_this.collapsibleStates[collapsiblePath]) {
_this.collapsibleStates[collapsiblePath] = false;
_this.expand($collapsible);
} else {
_this.collapsibleStates[collapsiblePath] = true;
_this.collapse($collapsible);
}
}
};
/**
* Return an appropriate row element selector
*
* @param $collapsible jQuery The given collapsible container element
*
* @returns {string}
*/
Collapsible.prototype.getRowSelector = function ($collapsible) {
if ($collapsible.is('table')) {
return '> tbody > th, > tbody > tr';
} else if ($collapsible.is('ul, ol')) {
return '> li';
}
return '';
};
/**
* Check whether the given collapsible needs to collapse
*
* @param $collapsible jQuery The given collapsible container element
*
* @returns {boolean}
*/
Collapsible.prototype.canCollapse = function ($collapsible) {
var rowSelector = this.getRowSelector($collapsible);
if (!! rowSelector) {
return $(rowSelector, $collapsible).length > ($collapsible.data('numofrows') || this.defaultNumOfRows);
} else {
return $collapsible.innerHeight() > ($collapsible.data('height') || this.defaultHeight);
}
};
/**
* Collapse the given collapsible
*
* @param $collapsible jQuery The given collapsible container element
*/
Collapsible.prototype.collapse = function ($collapsible) {
$collapsible.addClass('collapsed');
var rowSelector = this.getRowSelector($collapsible);
if (!! rowSelector) {
var $rows = $(rowSelector, $collapsible).slice(0, $collapsible.data('numofrows') || this.defaultNumOfRows);
var totalHeight = $rows.offset().top - $collapsible.offset().top;
$rows.outerHeight(function (_, height) {
totalHeight += height;
});
$collapsible.css({display: 'block', height: totalHeight});
} else {
$collapsible.css({display: 'block', height: $collapsible.data('height') || this.defaultHeight});
}
};
/**
* Expand the given collapsible
*
* @param $collapsible jQuery The given collapsible container element
*/
Collapsible.prototype.expand = function ($collapsible) {
$collapsible.removeClass('collapsed');
$collapsible.css({display: '', height: ''});
};
Icinga.Behaviors.Collapsible = Collapsible;
})(Icinga, jQuery);