icingaweb2/public/js/icinga/history.js

198 lines
5.9 KiB
JavaScript
Raw Normal View History

/*! Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
2014-03-04 14:05:01 +01:00
/**
* Icinga.History
*
* This is where we care about the browser History API
*
*/
(function (Icinga, $) {
'use strict';
Icinga.History = function (icinga) {
/**
* YES, we need Icinga
*/
this.icinga = icinga;
/**
* Our base url
*/
this.baseUrl = icinga.config.baseUrl;
/**
* Initial URL at load time
*/
this.initialUrl = location.href;
/**
* Whether the History API is enabled
*/
this.enabled = false;
/**
* Workaround for Chrome onload popstate event
*/
this.pushedSomething = false;
};
Icinga.History.prototype = {
/**
* Icinga will call our initialize() function once it's ready
*/
initialize: function () {
// History API will not be enabled without browser support, no fallback
if ('undefined' !== typeof window.history &&
typeof window.history.pushState === 'function'
) {
this.enabled = true;
this.icinga.logger.debug('History API enabled');
this.applyLocationBar(true);
2014-03-04 14:05:01 +01:00
$(window).on('popstate', { self: this }, this.onHistoryChange);
}
},
/**
* Detect active URLs and push combined URL to history
*
* TODO: How should we handle POST requests? e.g. search VS login
*/
pushCurrentState: function () {
var icinga = this.icinga;
2014-03-04 14:05:01 +01:00
// No history API, no action
if (!this.enabled) {
return;
}
icinga.logger.debug('Pushing current state to history');
2014-03-04 14:05:01 +01:00
var url = '';
// We only store URLs of containers sitting directly under #main:
$('#main > .container').each(function (idx, container) {
var cUrl = $(container).data('icingaUrl');
// TODO: I'd prefer to have the rightmost URL first
if ('undefined' !== typeof cUrl) {
// TODO: solve this on server side cUrl = icinga.utils.removeUrlParams(cUrl, blacklist);
2014-03-04 14:05:01 +01:00
if (url === '') {
url = cUrl;
} else {
url = url + '#!' + cUrl;
}
}
});
// TODO: update navigation
2014-03-04 14:05:01 +01:00
// Did we find any URL? Then push it!
if (url !== '') {
window.history.pushState({icinga: true}, null, this.cleanupUrl(url));
2014-03-04 14:05:01 +01:00
}
},
pushUrl: function (url) {
// No history API, no action
if (!this.enabled) {
return;
}
window.history.pushState({icinga: true}, null, this.cleanupUrl(url));
},
cleanupUrl: function(url) {
url = url.replace(/_render=[a-z0-9]+&/, '').replace(/&_render=[a-z0-9]+/, '').replace(/\?_render=[a-z0-9]+$/, '');
url = url.replace(/_reload=[a-z0-9]+&/, '').replace(/&_reload=[a-z0-9]+/, '').replace(/\?_reload=[a-z0-9]+$/, '');
return url;
},
2014-03-04 14:05:01 +01:00
/**
* Event handler for pop events
*
* TODO: Fix active selection, multiple cols
*/
onHistoryChange: function (event) {
var self = event.data.self,
icinga = self.icinga,
2014-03-10 15:48:40 +01:00
onload;
2014-03-04 14:05:01 +01:00
icinga.logger.debug('Got a history change');
// Chrome workaround:
onload = !self.pushedSomething && location.href === self.initialUrl;
self.pushedSomething = true;
// if (onload) { return; } // Temporarily disabled
2014-03-04 14:05:01 +01:00
// End of Chrome workaround
// We might find browsers showing strange behaviour, this log could help
if (event.originalEvent.state === null) {
icinga.logger.debug('No more history steps available');
} else {
icinga.logger.debug('History state', event.originalEvent.state);
}
2014-03-10 15:48:40 +01:00
self.applyLocationBar();
},
applyLocationBar: function (onload) {
2014-03-10 15:48:40 +01:00
var icinga = this.icinga,
main,
parts;
if (typeof onload === 'undefined') {
onload = false;
}
2014-03-04 14:05:01 +01:00
// TODO: Still hardcoding col1/col2, shall be dynamic soon
main = document.location.pathname + document.location.search;
if (! onload && $('#col1').data('icingaUrl') !== main) {
2014-03-04 14:05:01 +01:00
icinga.loader.loadUrl(
main,
$('#col1')
).historyTriggered = true;
}
if (document.location.hash && document.location.hash.match(/^#!/)) {
2014-03-04 14:05:01 +01:00
parts = document.location.hash.split(/#!/);
if ($('#layout > #login').length) {
// We are on the login page!
$('#login form #redirect').val(
$('#login form #redirect').val() + '#!' + parts[1]
);
} else {
if ($('#col2').data('icingaUrl') !== main) {
icinga.loader.loadUrl(
parts[1],
$('#col2')
).historyTriggered = true;
}
2014-03-04 14:05:01 +01:00
}
// TODO: Replace with dynamic columns
2014-03-08 15:08:03 +01:00
icinga.ui.layout2col();
2014-03-04 14:05:01 +01:00
} else {
// TODO: Replace with dynamic columns
2014-03-08 15:08:03 +01:00
icinga.ui.layout1col();
2014-03-04 14:05:01 +01:00
}
},
/**
* Cleanup
*/
destroy: function () {
$(window).off('popstate', this.onHistoryChange);
this.icinga = null;
}
};
}(Icinga, jQuery));