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-25 13:46:39 +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-25 14:36:03 +02:00
|
|
|
this.state = new StateStorage();
|
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 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-28 10:49:14 +02:00
|
|
|
$('.collapsible:not(.can-collapse)', event.currentTarget).each(function() {
|
2019-06-06 11:58:51 +02:00
|
|
|
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-25 14:36:03 +02:00
|
|
|
if (! _this.state.isExpanded(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-25 14:36:03 +02:00
|
|
|
if (_this.state.isExpanded(collapsiblePath)) {
|
|
|
|
_this.state.collapse(collapsiblePath);
|
2019-06-06 15:30:56 +02:00
|
|
|
_this.collapse($collapsible);
|
2019-06-07 09:17:13 +02:00
|
|
|
} else {
|
2019-06-25 14:36:03 +02:00
|
|
|
_this.state.expand(collapsiblePath);
|
2019-06-07 09:17:13 +02:00
|
|
|
_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-25 13:46:39 +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-25 13:46:39 +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-28 08:29:01 +02:00
|
|
|
var actualHeight = $collapsible.innerHeight(),
|
|
|
|
maxHeight = $collapsible.data('visibleHeight') || this.defaultVisibleHeight;
|
|
|
|
|
|
|
|
if (actualHeight <= maxHeight) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Although the height seems larger than what it should be, make sure it's not just a small fraction
|
|
|
|
// i.e. more than 12 pixel and at least 10% difference
|
|
|
|
return actualHeight - maxHeight > 12 && actualHeight / maxHeight >= 1.1;
|
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
|
|
|
|
*/
|
2019-06-25 13:46:39 +02:00
|
|
|
Collapsible.prototype.collapse = function($collapsible) {
|
2019-06-06 15:30:56 +02:00
|
|
|
$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;
|
2019-06-25 13:46:39 +02:00
|
|
|
$rows.outerHeight(function(_, height) {
|
2019-06-06 15:30:56 +02:00
|
|
|
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
|
|
|
|
*/
|
2019-06-25 13:46:39 +02:00
|
|
|
Collapsible.prototype.expand = function($collapsible) {
|
2019-06-06 15:30:56 +02:00
|
|
|
$collapsible.removeClass('collapsed');
|
|
|
|
$collapsible.css({display: '', height: ''});
|
|
|
|
};
|
|
|
|
|
2019-06-25 14:36:03 +02:00
|
|
|
Icinga.Behaviors.Collapsible = Collapsible;
|
|
|
|
|
|
|
|
// State-Storage abstraction, not for use externally until we've had time to think this properly through
|
|
|
|
|
|
|
|
var StateStorage = function() {};
|
|
|
|
|
|
|
|
StateStorage.prototype.isExpanded = function(selector) {
|
|
|
|
return this.load().has(selector);
|
|
|
|
};
|
|
|
|
|
|
|
|
StateStorage.prototype.expand = function(selector) {
|
|
|
|
var set = this.load();
|
|
|
|
set.add(selector);
|
|
|
|
this.save(set);
|
|
|
|
};
|
|
|
|
|
|
|
|
StateStorage.prototype.collapse = function(selector) {
|
|
|
|
var set = this.load();
|
|
|
|
set.delete(selector);
|
|
|
|
this.save(set);
|
|
|
|
};
|
|
|
|
|
|
|
|
StateStorage.prototype.load = function () {
|
|
|
|
var set = new Set();
|
|
|
|
|
2019-06-07 10:46:32 +02:00
|
|
|
var expanded = localStorage.getItem('behavior.collapsible.expanded');
|
2019-06-07 09:17:13 +02:00
|
|
|
if (!! expanded) {
|
2019-06-07 11:05:33 +02:00
|
|
|
// .forEach() is used because IE11 doesn't support constructor arguments
|
2019-06-25 13:46:39 +02:00
|
|
|
JSON.parse(expanded).forEach(function(value) {
|
2019-06-25 14:36:03 +02:00
|
|
|
set.add(value);
|
2019-06-07 11:05:33 +02:00
|
|
|
}, this);
|
2019-06-06 15:53:20 +02:00
|
|
|
}
|
2019-06-25 14:36:03 +02:00
|
|
|
|
|
|
|
return set;
|
2019-06-06 15:53:20 +02:00
|
|
|
};
|
|
|
|
|
2019-06-25 14:36:03 +02:00
|
|
|
StateStorage.prototype.save = function(set) {
|
|
|
|
if (set.size > 0) {
|
2019-06-07 11:05:33 +02:00
|
|
|
var expanded = [];
|
|
|
|
// .forEach() is used because IE11 doesn't support .values()
|
2019-06-25 14:36:03 +02:00
|
|
|
set.forEach(function(value) {
|
2019-06-07 11:05:33 +02:00
|
|
|
expanded.push(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem('behavior.collapsible.expanded', JSON.stringify(expanded));
|
2019-06-07 09:17:13 +02:00
|
|
|
} else {
|
2019-06-07 10:46:32 +02:00
|
|
|
localStorage.removeItem('behavior.collapsible.expanded');
|
2019-06-07 09:17:13 +02:00
|
|
|
}
|
2019-06-06 15:53:20 +02:00
|
|
|
};
|
|
|
|
|
2018-11-21 13:59:54 +01:00
|
|
|
})(Icinga, jQuery);
|