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-28 15:52:41 +02:00
|
|
|
this.on('layout-change', this.onLayoutChange, this);
|
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 08:11:19 +02:00
|
|
|
this.defaultVisibleRows = 2;
|
2019-06-07 08:13:36 +02:00
|
|
|
this.defaultVisibleHeight = 36;
|
2019-07-02 09:48:23 +02:00
|
|
|
|
|
|
|
$(window).on('StorageAwareSetAdd', { self: this }, this.onExternalExpansion);
|
|
|
|
$(window).on('StorageAwareSetDelete', { self: this }, this.onExternalCollapse);
|
|
|
|
this.state = new Icinga.Storage.StorageAwareSet.withStorage(
|
|
|
|
new Icinga.BehaviorStorage('collapsible'),
|
|
|
|
'expanded'
|
|
|
|
);
|
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);
|
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-07-02 09:48:23 +02:00
|
|
|
if (! _this.state.has(_this.icinga.utils.getCSSPath($collapsible))) {
|
2019-06-28 15:52:41 +02:00
|
|
|
_this.collapse($collapsible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates all collapsibles.
|
|
|
|
*
|
|
|
|
* @param event Event The `layout-change` event triggered by window resizing or column changes
|
|
|
|
*/
|
|
|
|
Collapsible.prototype.onLayoutChange = function(event) {
|
|
|
|
var _this = event.data.self;
|
|
|
|
|
|
|
|
$('.collapsible').each(function() {
|
|
|
|
var $collapsible = $(this);
|
|
|
|
var collapsiblePath = _this.icinga.utils.getCSSPath($collapsible);
|
|
|
|
|
|
|
|
if ($collapsible.is('.can-collapse')) {
|
|
|
|
if (! _this.canCollapse($collapsible)) {
|
|
|
|
$collapsible.next('.collapsible-control').remove();
|
|
|
|
$collapsible.removeClass('can-collapse');
|
|
|
|
_this.expand($collapsible);
|
|
|
|
}
|
|
|
|
} else if (_this.canCollapse($collapsible)) {
|
|
|
|
// It's expanded but shouldn't
|
|
|
|
$collapsible.after($('#collapsible-control-ghost').clone().removeAttr('id'));
|
|
|
|
$collapsible.addClass('can-collapse');
|
|
|
|
|
2019-07-02 09:48:23 +02:00
|
|
|
if (! _this.state.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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-02 09:48:23 +02:00
|
|
|
/**
|
|
|
|
* A collapsible got expanded in another window, try to apply this here as well
|
|
|
|
*
|
|
|
|
* @param {Event} event
|
|
|
|
* @param {string} collapsiblePath
|
|
|
|
*/
|
|
|
|
Collapsible.prototype.onExternalExpansion = function(event, collapsiblePath) {
|
|
|
|
var _this = event.data.self;
|
|
|
|
var $collapsible = $(collapsiblePath);
|
|
|
|
|
|
|
|
if ($collapsible.length) {
|
|
|
|
_this.expand($collapsible);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collapsible got collapsed in another window, try to apply this here as well
|
|
|
|
*
|
|
|
|
* @param {Event} event
|
|
|
|
* @param {string} collapsiblePath
|
|
|
|
*/
|
|
|
|
Collapsible.prototype.onExternalCollapse = function(event, collapsiblePath) {
|
|
|
|
var _this = event.data.self;
|
|
|
|
var $collapsible = $(collapsiblePath);
|
|
|
|
|
|
|
|
if ($collapsible.length) {
|
|
|
|
_this.collapse($collapsible);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-07-02 09:48:23 +02:00
|
|
|
if (_this.state.has(collapsiblePath)) {
|
|
|
|
_this.state.delete(collapsiblePath);
|
2019-06-06 15:30:56 +02:00
|
|
|
_this.collapse($collapsible);
|
2019-06-07 09:17:13 +02:00
|
|
|
} else {
|
2019-07-02 09:48:23 +02:00
|
|
|
_this.state.add(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 15:51:53 +02:00
|
|
|
var actualHeight = $collapsible[0].scrollHeight,
|
2019-06-28 08:29:01 +02:00
|
|
|
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;
|
|
|
|
|
2018-11-21 13:59:54 +01:00
|
|
|
})(Icinga, jQuery);
|