js: Apply render hooks in a defined order

Currently, form first and detach second.
Any other last. (e.g. grafana's iframe)
This commit is contained in:
Johannes Meyer 2024-02-06 10:49:31 +01:00
parent acf8795d70
commit c79b513066
4 changed files with 31 additions and 13 deletions

View File

@ -64,6 +64,11 @@
*/ */
this.behaviors = {}; this.behaviors = {};
/**
* Site behaviors which hook into the rendering process
*/
this.renderHooks = [];
/** /**
* Loaded modules * Loaded modules
*/ */
@ -94,9 +99,20 @@
this.loader = new Icinga.Loader(this); this.loader = new Icinga.Loader(this);
this.events = new Icinga.Events(this); this.events = new Icinga.Events(this);
this.history = new Icinga.History(this); this.history = new Icinga.History(this);
var _this = this;
$.each(Icinga.Behaviors, function(name, Behavior) { // Initialize all available behaviors
_this.behaviors[name.toLowerCase()] = new Behavior(_this); for (const name in Icinga.Behaviors) {
const behavior = new Icinga.Behaviors[name](this);
this.behaviors[name.toLowerCase()] = behavior;
if (behavior.renderHook) {
this.renderHooks.push(behavior);
}
}
// Sort render hooks by priority
this.renderHooks.sort(function (a, b) {
// Treats all hooks without a priority as "greater", meaning they will be applied last
return (a.priority || 999) - (b.priority || 999);
}); });
this.timezone.initialize(); this.timezone.initialize();

View File

@ -11,6 +11,8 @@
function Detach(icinga) { function Detach(icinga) {
Icinga.EventListener.call(this, icinga); Icinga.EventListener.call(this, icinga);
this.priority = 2;
} }
Detach.prototype = new Icinga.EventListener(); Detach.prototype = new Icinga.EventListener();

View File

@ -13,6 +13,8 @@
Icinga.EventListener.call(this, icinga); Icinga.EventListener.call(this, icinga);
this.on('rendered', '.container', this.onRendered, this); this.on('rendered', '.container', this.onRendered, this);
this.priority = 1;
// store the modification state of all input fields // store the modification state of all input fields
this.inputs = new WeakMap(); this.inputs = new WeakMap();
}; };

View File

@ -1267,17 +1267,15 @@
$container.trigger('beforerender', [content, action, autorefresh, scripted, autoSubmit]); $container.trigger('beforerender', [content, action, autorefresh, scripted, autoSubmit]);
var discard = false; let discard = false;
$.each(_this.icinga.behaviors, function(name, behavior) { for (const hook of _this.icinga.renderHooks) {
if (behavior.renderHook) { const changed = hook.renderHook(content, $container, action, autorefresh, autoSubmit);
var changed = behavior.renderHook(content, $container, action, autorefresh, autoSubmit); if (changed === null) {
if (changed === null) { discard = true;
discard = true; } else {
} else { content = changed;
content = changed;
}
} }
}); }
$('.container', $container).each(function() { $('.container', $container).each(function() {
_this.stopPendingRequestsFor($(this)); _this.stopPendingRequestsFor($(this));