Skip to content

Commit

Permalink
js: Add new special redirect target __CLOSE__
Browse files Browse the repository at this point in the history
This has the same effect as a normal redirect. The benefit of it however
is that the server doesn't need to know what's being shown in the left
column. It just instructs the client to close the right and refresh the
left column. But still produces a new history state, it's a forward
navigation nonetheless.
  • Loading branch information
nilmerg committed Mar 30, 2021
1 parent d064258 commit e65ec19
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion public/js/icinga/loader.js
Expand Up @@ -497,7 +497,6 @@

var originUrl = req.$target.data('icingaUrl');

// We may just close the right column, refresh the left one in this case
$(window).on('popstate.__back__', { self: this }, function (event) {
var _this = event.data.self;
var $refreshTarget = $('#col2');
Expand All @@ -521,12 +520,18 @@
$refreshTarget = $('#col1');
}

// loadUrl won't run this request, as due to the history handling it's already running.
// The important bit though is that it returns this (already running) request. This way
// it's possible to set `scripted = true` on the request. (`addToHistory = true` should
// already be the case, though it's added again just in case it's not already `true`..)
var req = _this.loadUrl(refreshUrl, $refreshTarget);
req.addToHistory = false;
req.scripted = true;

setTimeout(function () {
// TODO: Find a better solution than a hardcoded one
// This is still the *cheat* to get live results
// (in case there's a delay and a change is not instantly effective)
var req = _this.loadUrl(refreshUrl, $refreshTarget);
req.addToHistory = false;
req.scripted = true;
Expand All @@ -538,6 +543,35 @@
// Navigate back, no redirect desired
window.history.back();

return true;
} else if (redirect.match(/__CLOSE__/)) {
if (req.$redirectTarget.is('#col1')) {
icinga.logger.warn('Cannot close #col1');
return false;
}

// Close right column as requested
icinga.ui.layout1col();

// Refresh left column and produce a new history state for it
var $col1 = $('#col1');
var col1Url = icinga.history.getCol1State();
var refresh = this.loadUrl(col1Url, $col1);
refresh.scripted = true;

var _this = this;
setTimeout(function () {
// TODO: Find a better solution than a hardcoded one
// This is still the *cheat* to get live results
// (in case there's a delay and a change is not instantly effective)
var secondRefresh = _this.loadUrl(col1Url, $col1);
if (secondRefresh !== refresh) {
// Only change these properties if it's not still the first refresh
secondRefresh.addToHistory = false;
secondRefresh.scripted = true;
}
}, 1000);

return true;
}

Expand Down

0 comments on commit e65ec19

Please sign in to comment.