Merge pull request #4055 from Icinga/feature/js-history-replace-state

JS: Introduce Icinga.History.replaceCurrentState()
This commit is contained in:
Johannes Meyer 2020-01-16 08:54:49 +01:00 committed by GitHub
commit 8d4e0126ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,18 +52,17 @@
}, },
/** /**
* Detect active URLs and push combined URL to history * Get the current state (url and title) as object
* *
* TODO: How should we handle POST requests? e.g. search VS login * @returns {object}
*/ */
pushCurrentState: function () { getCurrentState: function () {
// No history API, no action
if (! this.enabled) { if (! this.enabled) {
return; return null;
} }
var url = ''; var title = null;
var title = ''; var url = null;
// We only store URLs of containers sitting directly under #main: // We only store URLs of containers sitting directly under #main:
$('#main > .container').each(function (idx, container) { $('#main > .container').each(function (idx, container) {
@ -74,7 +73,7 @@
// TODO: I'd prefer to have the rightmost URL first // TODO: I'd prefer to have the rightmost URL first
if ('undefined' !== typeof cUrl) { if ('undefined' !== typeof cUrl) {
// TODO: solve this on server side cUrl = icinga.utils.removeUrlParams(cUrl, blacklist); // TODO: solve this on server side cUrl = icinga.utils.removeUrlParams(cUrl, blacklist);
if (url === '') { if (! url) {
url = cUrl; url = cUrl;
} else { } else {
url = url + '#!' + cUrl; url = url + '#!' + cUrl;
@ -86,13 +85,52 @@
} }
}); });
// Did we find any URL? Then push it! return {
if (url !== '') { title: title,
this.icinga.logger.debug('Pushing current state to history'); url: url,
this.push(url); };
},
/**
* Detect active URLs and push combined URL to history
*
* TODO: How should we handle POST requests? e.g. search VS login
*/
pushCurrentState: function () {
// No history API, no action
if (! this.enabled) {
return;
} }
if (title !== '') {
this.icinga.ui.setTitle(title); var state = this.getCurrentState();
// Did we find any URL? Then push it!
if (state.url) {
this.icinga.logger.debug('Pushing current state to history');
this.push(state.url);
}
if (state.title) {
this.icinga.ui.setTitle(state.title);
}
},
/**
* Replace the current history entry with the current state
*/
replaceCurrentState: function () {
if (! this.enabled) {
return;
}
var state = this.getCurrentState();
if (state.url) {
this.icinga.logger.debug('Replacing current history state');
window.history.replaceState(
this.getBehaviorState(),
null,
state.url
);
} }
}, },