2015-10-28 22:55:04 +01:00
|
|
|
|
|
|
|
(function(Icinga) {
|
|
|
|
|
|
|
|
var Director = function(module) {
|
|
|
|
this.module = module;
|
|
|
|
|
|
|
|
this.initialize();
|
|
|
|
|
|
|
|
this.openedFieldsets = {};
|
|
|
|
|
|
|
|
this.module.icinga.logger.debug('Director module loaded');
|
|
|
|
};
|
|
|
|
|
|
|
|
Director.prototype = {
|
|
|
|
|
|
|
|
initialize: function()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Tell Icinga about our event handlers
|
|
|
|
*/
|
2015-12-17 21:15:12 +01:00
|
|
|
this.module.on('rendered', this.rendered);
|
2015-10-28 22:55:04 +01:00
|
|
|
this.module.on('click', 'fieldset > legend', this.toggleFieldset);
|
2016-03-05 10:54:48 +01:00
|
|
|
this.module.on('focus', 'form input', this.formElementFocus);
|
|
|
|
this.module.on('focus', 'form select', this.formElementFocus);
|
2015-10-28 22:55:04 +01:00
|
|
|
this.module.icinga.logger.debug('Director module initialized');
|
|
|
|
},
|
|
|
|
|
2016-03-05 10:54:48 +01:00
|
|
|
formElementFocus: function(ev)
|
|
|
|
{
|
|
|
|
var $input = $(ev.currentTarget);
|
|
|
|
var $dd = $input.closest('dd');
|
2016-03-05 13:49:08 +01:00
|
|
|
if ($dd.attr('id') && $dd.attr('id').match(/button/)) {
|
2016-03-05 10:54:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var $li = $input.closest('li');
|
|
|
|
var $dt = $dd.prev();
|
|
|
|
var $form = $dt.closest('form');
|
|
|
|
$form.find('dt').removeClass('active');
|
|
|
|
$form.find('dd').removeClass('active');
|
|
|
|
$form.find('li').removeClass('active');
|
|
|
|
$li.addClass('active');
|
|
|
|
$dt.addClass('active');
|
|
|
|
$dd.addClass('active');
|
|
|
|
},
|
|
|
|
|
2015-10-28 22:55:04 +01:00
|
|
|
toggleFieldset: function (ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
var $fieldset = $(ev.currentTarget).closest('fieldset');
|
|
|
|
$fieldset.toggleClass('collapsed');
|
2015-10-29 17:20:28 +01:00
|
|
|
this.fixFieldsetInfo($fieldset);
|
2015-10-28 22:55:04 +01:00
|
|
|
this.openedFieldsets[$fieldset.attr('id')] = ! $fieldset.hasClass('collapsed');
|
|
|
|
},
|
|
|
|
|
|
|
|
rendered: function(ev) {
|
|
|
|
var $container = $(ev.currentTarget);
|
|
|
|
var self = this;
|
2015-12-17 21:15:12 +01:00
|
|
|
$container.find('form').each(self.restoreFieldsets.bind(self));
|
2016-03-07 14:52:43 +01:00
|
|
|
|
|
|
|
var $objectType = $container.find('form').find('select[name=object_type]');
|
|
|
|
if ($objectType.length) {
|
|
|
|
if ($objectType[0].value === '') {
|
|
|
|
$objectType.focus();
|
|
|
|
}
|
|
|
|
}
|
2015-10-28 22:55:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
restoreFieldsets: function(idx, form) {
|
|
|
|
var $form = $(form);
|
|
|
|
var formId = $form.attr('id');
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$('fieldset', $form).each(function(idx, fieldset) {
|
|
|
|
var $fieldset = $(fieldset);
|
2015-12-17 21:15:12 +01:00
|
|
|
if ($fieldset.find('.required').length == 0 && (! self.fieldsetWasOpened($fieldset))) {
|
2015-10-28 22:55:04 +01:00
|
|
|
$fieldset.addClass('collapsed');
|
2015-10-29 17:20:28 +01:00
|
|
|
self.fixFieldsetInfo($fieldset);
|
2015-10-28 22:55:04 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
fieldsetWasOpened: function($fieldset) {
|
|
|
|
var id = $fieldset.attr('id');
|
|
|
|
if (typeof this.openedFieldsets[id] === 'undefined') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.openedFieldsets[id];
|
2015-10-29 17:20:28 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
fixFieldsetInfo: function($fieldset) {
|
|
|
|
if ($fieldset.hasClass('collapsed')) {
|
2015-12-17 21:15:12 +01:00
|
|
|
if ($fieldset.find('legend span.element-count').length === 0) {
|
|
|
|
var cnt = $fieldset.find('dt').length;
|
|
|
|
$fieldset.find('legend').append($('<span class="element-count"> (' + cnt + ')</span>'));
|
|
|
|
}
|
2015-10-29 17:20:28 +01:00
|
|
|
} else {
|
2015-12-17 21:15:12 +01:00
|
|
|
$fieldset.find('legend span.element-count').remove();
|
2015-10-29 17:20:28 +01:00
|
|
|
}
|
2015-10-28 22:55:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Icinga.availableModules.director = Director;
|
|
|
|
|
|
|
|
}(Icinga));
|
|
|
|
|