js: Ensure that rendered events of modules are called on page-load

fixes #9869
This commit is contained in:
Johannes Meyer 2015-08-10 14:46:58 +02:00
parent 6d24e09479
commit 0468bddc83
2 changed files with 89 additions and 31 deletions

View File

@ -112,38 +112,69 @@
/** /**
* Load a given module by name * Load a given module by name
*
* @param {string} name
*
* @return {boolean}
*/ */
loadModule: function (name) { loadModule: function (name) {
if (this.hasModule(name)) { if (this.isLoadedModule(name)) {
this.logger.error('Cannot load module ' + name + ' twice'); this.logger.error('Cannot load module ' + name + ' twice');
return; return false;
} }
this.modules[name] = new Icinga.Module(this, name); if (! this.hasModule(name)) {
this.logger.error('Cannot find module ' + name);
return false;
}
this.modules[name] = new Icinga.Module(
this,
name,
Icinga.availableModules[name]
);
return true;
}, },
/** /**
* Whether a module matching the given name exists * Whether a module matching the given name exists or is loaded
*
* @param {string} name
*
* @return {boolean}
*/ */
hasModule: function (name) { hasModule: function (name) {
return 'undefined' !== typeof this.modules[name] || return this.isLoadedModule(name) ||
'undefined' !== typeof Icinga.availableModules[name]; 'undefined' !== typeof Icinga.availableModules[name];
}, },
/**
* Return whether the given module is loaded
*
* @param {string} name The name of the module
*
* @returns {Boolean}
*/
isLoadedModule: function (name) {
return 'undefined' !== typeof this.modules[name];
},
/** /**
* Get a module by name * Get a module by name
*
* @param {string} name
*
* @return {object}
*/ */
module: function (name) { module: function (name) {
if ('undefined' === typeof this.modules[name]) { if (this.hasModule(name) && !this.isLoadedModule(name)) {
if ('undefined' !== typeof Icinga.availableModules[name]) { this.modules[name] = new Icinga.Module(
this.modules[name] = new Icinga.Module( this,
this, name,
name, Icinga.availableModules[name]
Icinga.availableModules[name] );
);
}
} }
return this.modules[name]; return this.modules[name];

View File

@ -13,6 +13,7 @@
this.icinga = icinga; this.icinga = icinga;
this.searchValue = ''; this.searchValue = '';
this.initializeModules = true;
}; };
Icinga.Events.prototype = { Icinga.Events.prototype = {
@ -35,6 +36,46 @@
var self = event.data.self; var self = event.data.self;
var icinga = self.icinga; var icinga = self.icinga;
if (self.initializeModules) {
var loaded = false;
var moduleName = $target.data('icingaModule');
if (moduleName) {
if (icinga.hasModule(moduleName) && !icinga.isLoadedModule(moduleName)) {
loaded |= icinga.loadModule(moduleName);
}
}
$('.icinga-module', $target).each(function(idx, mod) {
moduleName = $(mod).data('icingaModule');
if (icinga.hasModule(moduleName) && !icinga.isLoadedModule(moduleName)) {
loaded |= icinga.loadModule(moduleName);
}
});
if (loaded) {
// Modules may register their own handler for the 'renderend' event
// so we need to ensure that it is called the first time they are
// initialized
event.stopImmediatePropagation();
self.initializeModules = false;
var $container = $target.closest('.container');
if (! $container.length) {
// The page obviously got loaded for the first time,
// so we'll trigger the event for all containers
$container = $('.container');
}
$container.trigger('rendered');
// But since we're listening on this event by ourself, we'll have
// to abort our own processing as we'll process it twice otherwise
return false;
}
} else {
self.initializeModules = true;
}
$('.dashboard > div', $target).each(function(idx, el) { $('.dashboard > div', $target).each(function(idx, el) {
var $element = $(el); var $element = $(el);
var $url = $element.data('icingaUrl'); var $url = $element.data('icingaUrl');
@ -43,21 +84,6 @@
} }
}); });
var moduleName = $target.data('icingaModule');
if (moduleName) {
if (icinga.hasModule(moduleName)) {
icinga.module(moduleName);
}
}
$('.icinga-module', $target).each(function(idx, mod) {
var $mod = $(mod);
moduleName = $mod.data('icingaModule');
if (icinga.hasModule(moduleName)) {
icinga.module(moduleName);
}
});
var $searchField = $('#menu input.search', $target); var $searchField = $('#menu input.search', $target);
// Remember initial search field value if any // Remember initial search field value if any
if ($searchField.length && $searchField.val().length) { if ($searchField.length && $searchField.val().length) {
@ -75,13 +101,14 @@
* Global default event handlers * Global default event handlers
*/ */
applyGlobalDefaults: function () { applyGlobalDefaults: function () {
// Apply element-specific behavior whenever the layout is rendered
// Note: It is important that this is the first handler for this event!
$(document).on('rendered', { self: this }, this.applyHandlers);
$.each(self.icinga.behaviors, function (name, behavior) { $.each(self.icinga.behaviors, function (name, behavior) {
behavior.bind($(document)); behavior.bind($(document));
}); });
// Apply element-specific behavior whenever the layout is rendered
$(document).on('rendered', { self: this }, this.applyHandlers);
// We catch resize events // We catch resize events
$(window).on('resize', { self: this.icinga.ui }, this.icinga.ui.onWindowResize); $(window).on('resize', { self: this.icinga.ui }, this.icinga.ui.onWindowResize);