2019-06-06 14:39:40 +02:00
|
|
|
/*! Icinga Web 2 | (c) 2019 Icinga GmbH | GPLv2+ */
|
2018-11-21 13:59:54 +01:00
|
|
|
|
|
|
|
;(function(Icinga, $) {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-06-06 10:13:34 +02:00
|
|
|
Icinga.Behaviors = Icinga.Behaviors || {};
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2018-12-06 13:53:52 +01:00
|
|
|
/**
|
2019-06-06 14:39:40 +02:00
|
|
|
* Behavior for collapsible containers.
|
2018-12-06 13:53:52 +01:00
|
|
|
*
|
|
|
|
* @param icinga Icinga The current Icinga Object
|
|
|
|
*/
|
2019-06-06 14:40:48 +02:00
|
|
|
var Collapsible = function (icinga) {
|
2018-11-21 13:59:54 +01:00
|
|
|
Icinga.EventListener.call(this, icinga);
|
|
|
|
|
2019-06-06 14:52:58 +02:00
|
|
|
this.on('rendered', '.container', this.onRendered, this);
|
2019-06-06 11:58:51 +02:00
|
|
|
this.on('click', '.collapsible + .collapsible-control', this.onControlClicked, this);
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2019-06-06 10:13:34 +02:00
|
|
|
this.icinga = icinga;
|
2019-06-07 09:17:13 +02:00
|
|
|
this.expanded = new Set();
|
2019-06-07 08:11:19 +02:00
|
|
|
this.defaultVisibleRows = 2;
|
2019-06-07 08:13:36 +02:00
|
|
|
this.defaultVisibleHeight = 36;
|
2019-06-06 15:53:20 +02:00
|
|
|
|
2019-06-07 09:17:13 +02:00
|
|
|
this.loadStorage();
|
2019-06-06 10:13:34 +02:00
|
|
|
};
|
2019-06-06 14:40:48 +02:00
|
|
|
Collapsible.prototype = new Icinga.EventListener();
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2018-12-06 13:53:52 +01:00
|
|
|
/**
|
2019-06-06 11:58:51 +02: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
|
|
|
|
*/
|
2019-06-06 14:40:48 +02:00
|
|
|
Collapsible.prototype.onRendered = function(event) {
|
2019-06-06 10:13:34 +02:00
|
|
|
var _this = event.data.self;
|
|
|
|
|
2019-06-06 11:58:51 +02:00
|
|
|
$('.collapsible', event.currentTarget).each(function() {
|
|
|
|
var $collapsible = $(this);
|
2019-06-06 15:30:56 +02:00
|
|
|
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2019-06-06 15:30:56 +02:00
|
|
|
// Assumes that any newly rendered elements are expanded
|
2019-06-06 11:58:51 +02:00
|
|
|
if (_this.canCollapse($collapsible)) {
|
|
|
|
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
|
|
|
|
$collapsible.addClass('can-collapse');
|
2019-06-06 15:30:56 +02:00
|
|
|
|
2019-06-07 09:17:13 +02:00
|
|
|
if (! _this.expanded.has(collapsiblePath)) {
|
2019-06-06 15:30:56 +02:00
|
|
|
_this.collapse($collapsible);
|
|
|
|
}
|
2019-06-06 10:16:00 +02:00
|
|
|
}
|
2018-11-21 13:59:54 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-06 13:53:52 +01:00
|
|
|
/**
|
2019-06-06 11:58:51 +02: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
|
|
|
|
*/
|
2019-06-06 14:40:48 +02:00
|
|
|
Collapsible.prototype.onControlClicked = function(event) {
|
2019-06-06 10:13:34 +02:00
|
|
|
var _this = event.data.self;
|
2019-06-06 11:58:51 +02:00
|
|
|
var $target = $(event.currentTarget);
|
|
|
|
var $collapsible = $target.prev('.collapsible');
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2019-06-06 11:58:51 +02:00
|
|
|
if (! $collapsible.length) {
|
|
|
|
_this.icinga.logger.error('[Collapsible] Collapsible control has no associated .collapsible: ', $target);
|
|
|
|
} else {
|
2019-06-06 15:30:56 +02:00
|
|
|
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
|
2019-06-07 09:17:13 +02:00
|
|
|
if (_this.expanded.has(collapsiblePath)) {
|
|
|
|
_this.expanded.delete(collapsiblePath);
|
2019-06-06 15:30:56 +02:00
|
|
|
_this.collapse($collapsible);
|
2019-06-07 09:17:13 +02:00
|
|
|
} else {
|
|
|
|
_this.expanded.add(collapsiblePath);
|
|
|
|
_this.expand($collapsible);
|
2019-06-06 11:58:51 +02:00
|
|
|
}
|
2018-11-21 13:59:54 +01:00
|
|
|
}
|
2019-06-06 11:58:51 +02:00
|
|
|
};
|
2019-06-06 09:51:02 +02:00
|
|
|
|
2019-06-06 14:39:40 +02:00
|
|
|
/**
|
|
|
|
* Return an appropriate row element selector
|
|
|
|
*
|
|
|
|
* @param $collapsible jQuery The given collapsible container element
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2019-06-06 14:40:48 +02:00
|
|
|
Collapsible.prototype.getRowSelector = function ($collapsible) {
|
2019-06-06 11:58:51 +02:00
|
|
|
if ($collapsible.is('table')) {
|
2019-06-07 07:33:28 +02:00
|
|
|
return '> tbody > tr';
|
2019-06-06 11:58:51 +02:00
|
|
|
} else if ($collapsible.is('ul, ol')) {
|
|
|
|
return '> li';
|
2019-06-06 09:51:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 11:58:51 +02:00
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2019-06-06 14:39:40 +02:00
|
|
|
/**
|
|
|
|
* Check whether the given collapsible needs to collapse
|
|
|
|
*
|
|
|
|
* @param $collapsible jQuery The given collapsible container element
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2019-06-06 14:40:48 +02:00
|
|
|
Collapsible.prototype.canCollapse = function ($collapsible) {
|
2019-06-06 11:58:51 +02:00
|
|
|
var rowSelector = this.getRowSelector($collapsible);
|
|
|
|
if (!! rowSelector) {
|
2019-06-07 08:11:19 +02:00
|
|
|
return $(rowSelector, $collapsible).length > ($collapsible.data('visibleRows') || this.defaultVisibleRows);
|
2018-11-21 13:59:54 +01:00
|
|
|
} else {
|
2019-06-07 08:13:36 +02:00
|
|
|
return $collapsible.innerHeight() > ($collapsible.data('visibleHeight') || this.defaultVisibleHeight);
|
2018-11-21 13:59:54 +01:00
|
|
|
}
|
2019-06-06 10:13:34 +02:00
|
|
|
};
|
2018-11-21 13:59:54 +01:00
|
|
|
|
2019-06-06 15:30:56 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2019-06-07 08:11:19 +02:00
|
|
|
var $rows = $(rowSelector, $collapsible).slice(0, $collapsible.data('visibleRows') || this.defaultVisibleRows);
|
2019-06-06 15:30:56 +02:00
|
|
|
|
|
|
|
var totalHeight = $rows.offset().top - $collapsible.offset().top;
|
|
|
|
$rows.outerHeight(function (_, height) {
|
|
|
|
totalHeight += height;
|
|
|
|
});
|
|
|
|
|
|
|
|
$collapsible.css({display: 'block', height: totalHeight});
|
|
|
|
} else {
|
2019-06-07 08:13:36 +02:00
|
|
|
$collapsible.css({display: 'block', height: $collapsible.data('visibleHeight') || this.defaultVisibleHeight});
|
2019-06-06 15:30:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the given collapsible
|
|
|
|
*
|
|
|
|
* @param $collapsible jQuery The given collapsible container element
|
|
|
|
*/
|
|
|
|
Collapsible.prototype.expand = function ($collapsible) {
|
|
|
|
$collapsible.removeClass('collapsed');
|
|
|
|
$collapsible.css({display: '', height: ''});
|
|
|
|
};
|
|
|
|
|
2019-06-06 15:53:20 +02:00
|
|
|
/**
|
2019-06-07 09:17:13 +02:00
|
|
|
* Load state from storage
|
2019-06-06 15:53:20 +02:00
|
|
|
*/
|
2019-06-07 09:17:13 +02:00
|
|
|
Collapsible.prototype.loadStorage = function () {
|
|
|
|
var expanded = localStorage.getItem('collapsible.expanded');
|
|
|
|
if (!! expanded) {
|
|
|
|
this.expanded = new Set(JSON.parse(expanded));
|
2019-06-06 15:53:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-06-07 09:17:13 +02:00
|
|
|
* Save state to storage
|
2019-06-06 15:53:20 +02:00
|
|
|
*/
|
|
|
|
Collapsible.prototype.destroy = function () {
|
2019-06-07 09:17:13 +02:00
|
|
|
if (this.expanded.size > 0) {
|
|
|
|
localStorage.setItem('collapsible.expanded', JSON.stringify(Array.from(this.expanded.values())));
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem('collapsible.expanded');
|
|
|
|
}
|
2019-06-06 15:53:20 +02:00
|
|
|
};
|
|
|
|
|
2019-06-06 14:40:48 +02:00
|
|
|
Icinga.Behaviors.Collapsible = Collapsible;
|
2018-11-21 13:59:54 +01:00
|
|
|
|
|
|
|
})(Icinga, jQuery);
|