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-09 08:41:34 +01:00
|
|
|
this.module.on('click', 'input.related-action', this.extensibleSetAction);
|
2016-03-05 10:54:48 +01:00
|
|
|
this.module.on('focus', 'form input', this.formElementFocus);
|
2016-03-14 13:09:18 +01:00
|
|
|
this.module.on('focus', 'form textarea', this.formElementFocus);
|
2016-03-05 10:54:48 +01:00
|
|
|
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-09 08:41:34 +01:00
|
|
|
extensibleSetAction: function(ev)
|
|
|
|
{
|
|
|
|
var el = ev.currentTarget;
|
|
|
|
|
|
|
|
if (el.name.match(/__MOVE_UP$/)) {
|
|
|
|
var $li = $(el).closest('li');
|
|
|
|
var $prev = $li.prev()
|
2016-03-22 01:42:09 +01:00
|
|
|
if ($li.find('input[type=text].autosubmit')) {
|
|
|
|
if (iid = $prev.find('input[type=text]').attr('id')) {
|
|
|
|
$li.closest('.container').data('activeExtensibleEntry', iid);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 08:41:34 +01:00
|
|
|
if ($prev.length) {
|
|
|
|
$prev.before($li.detach());
|
|
|
|
this.fixRelatedActions($li.closest('ul'));
|
|
|
|
}
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
return false;
|
|
|
|
} else if (el.name.match(/__MOVE_DOWN$/)) {
|
|
|
|
var $li = $(el).closest('li');
|
|
|
|
var $next = $li.next()
|
2016-03-22 01:42:09 +01:00
|
|
|
if ($li.find('input[type=text].autosubmit')) {
|
|
|
|
if (iid = $next.find('input[type=text]').attr('id')) {
|
|
|
|
$li.closest('.container').data('activeExtensibleEntry', iid);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 08:41:34 +01:00
|
|
|
if ($next.length && ! $next.find('.extend-set').length) {
|
|
|
|
$next.after($li.detach());
|
|
|
|
this.fixRelatedActions($li.closest('ul'));
|
|
|
|
}
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
return false;
|
|
|
|
} else if (el.name.match(/__MOVE_REMOVE$/)) {
|
|
|
|
// TODO: skipping for now, wasn't able to prevent web2 form
|
|
|
|
// submission once removed
|
|
|
|
return;
|
|
|
|
|
|
|
|
var $li = $(el).closest('li').remove();
|
|
|
|
this.fixRelatedActions($li.closest('ul'));
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fixRelatedActions: function($ul)
|
|
|
|
{
|
|
|
|
var $uls = $ul.find('li');
|
2016-03-10 20:58:15 +01:00
|
|
|
var last = $uls.length - 1;
|
|
|
|
if ($ul.find('.extend-set').length) {
|
|
|
|
last--;
|
|
|
|
}
|
|
|
|
|
2016-03-09 08:41:34 +01:00
|
|
|
$uls.each(function (idx, li) {
|
|
|
|
var $li = $(li);
|
|
|
|
if (idx === 0) {
|
|
|
|
$li.find('.action-move-up').attr('disabled', 'disabled');
|
|
|
|
if (last === 0) {
|
|
|
|
$li.find('.action-move-down').attr('disabled', 'disabled');
|
|
|
|
} else {
|
|
|
|
$li.find('.action-move-down').removeAttr('disabled');
|
|
|
|
}
|
|
|
|
} else if (idx === last) {
|
|
|
|
$li.find('.action-move-up').removeAttr('disabled');
|
|
|
|
$li.find('.action-move-down').attr('disabled', 'disabled');
|
|
|
|
} else {
|
|
|
|
$li.find('.action-move-up').removeAttr('disabled');
|
|
|
|
$li.find('.action-move-down').removeAttr('disabled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-03-05 10:54:48 +01:00
|
|
|
formElementFocus: function(ev)
|
|
|
|
{
|
|
|
|
var $input = $(ev.currentTarget);
|
|
|
|
var $dd = $input.closest('dd');
|
2016-03-18 11:59:55 +01:00
|
|
|
$dd.find('p.description').show();
|
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');
|
2016-03-17 02:21:50 +01:00
|
|
|
$form.find('dt, dd, li').removeClass('active');
|
2016-03-05 10:54:48 +01:00
|
|
|
$li.addClass('active');
|
|
|
|
$dt.addClass('active');
|
|
|
|
$dd.addClass('active');
|
2016-03-18 11:59:55 +01:00
|
|
|
$dd.find('p.description.fading-out')
|
|
|
|
.stop(true)
|
|
|
|
.removeClass('fading-out')
|
|
|
|
.fadeIn('fast');
|
|
|
|
|
|
|
|
$form.find('dd').not($dd)
|
|
|
|
.find('p.description')
|
|
|
|
.not('.fading-out')
|
|
|
|
.addClass('fading-out')
|
|
|
|
.delay(2000)
|
|
|
|
.fadeOut('slow', function() {
|
|
|
|
$(this).removeClass('fading-out').hide()
|
|
|
|
});
|
2016-03-05 10:54:48 +01:00
|
|
|
},
|
|
|
|
|
2016-03-17 02:21:50 +01:00
|
|
|
highlightFormErrors: function($container)
|
|
|
|
{
|
|
|
|
$container.find('dd ul.errors').each(function(idx, ul) {
|
|
|
|
var $ul = $(ul);
|
|
|
|
var $dd = $ul.closest('dd');
|
|
|
|
var $dt = $dd.prev();
|
|
|
|
|
|
|
|
$dt.addClass('errors');
|
|
|
|
$dd.addClass('errors');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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');
|
|
|
|
},
|
|
|
|
|
2016-03-18 11:59:55 +01:00
|
|
|
hideInactiveFormDescriptions: function($container) {
|
|
|
|
$container.find('dd').not('.active').find('p.description').hide();
|
|
|
|
},
|
|
|
|
|
2015-10-28 22:55:04 +01:00
|
|
|
rendered: function(ev) {
|
|
|
|
var $container = $(ev.currentTarget);
|
2016-03-17 02:21:50 +01:00
|
|
|
this.restoreContainerFieldsets($container);
|
|
|
|
this.backupAllExtensibleSetDefaultValues($container);
|
|
|
|
this.putFocusOnFirstObjectTypeElement($container);
|
|
|
|
this.highlightFormErrors($container);
|
2016-03-18 11:59:55 +01:00
|
|
|
this.hideInactiveFormDescriptions($container);
|
2016-03-22 01:42:09 +01:00
|
|
|
if (iid = $container.data('activeExtensibleEntry')) {
|
|
|
|
$('#' + iid).focus();
|
|
|
|
$container.removeData('activeExtensibleEntry');
|
|
|
|
}
|
2016-03-17 02:21:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
restoreContainerFieldsets: function($container)
|
|
|
|
{
|
2015-10-28 22:55:04 +01:00
|
|
|
var self = this;
|
2015-12-17 21:15:12 +01:00
|
|
|
$container.find('form').each(self.restoreFieldsets.bind(self));
|
2016-03-17 02:21:50 +01:00
|
|
|
},
|
2016-03-07 14:52:43 +01:00
|
|
|
|
2016-03-17 02:21:50 +01:00
|
|
|
putFocusOnFirstObjectTypeElement: function($container)
|
|
|
|
{
|
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
|
|
|
},
|
|
|
|
|
2016-03-17 02:21:50 +01:00
|
|
|
backupAllExtensibleSetDefaultValues: function($container) {
|
|
|
|
var self = this;
|
|
|
|
$container.find('.extensible-set').each(function (idx, eSet) {
|
|
|
|
$(eSet).find('input[type=text]').each(self.backupDefaultValue);
|
|
|
|
$(eSet).find('select').each(self.backupDefaultValue);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
backupDefaultValue: function(idx, el) {
|
|
|
|
$(el).data('originalvalue', el.value);
|
|
|
|
},
|
|
|
|
|
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) {
|
2016-03-24 11:44:24 +01:00
|
|
|
var cnt = $fieldset.find('dt, li').length;
|
2015-12-17 21:15:12 +01:00
|
|
|
$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));
|
|
|
|
|