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 = {};
/**
* Site behaviors which hook into the rendering process
*/
this.renderHooks = [];
/**
* Loaded modules
*/
@ -94,9 +99,20 @@
this.loader = new Icinga.Loader(this);
this.events = new Icinga.Events(this);
this.history = new Icinga.History(this);
var _this = this;
$.each(Icinga.Behaviors, function(name, Behavior) {
_this.behaviors[name.toLowerCase()] = new Behavior(_this);
// Initialize all available behaviors
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();

View File

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

View File

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

View File

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