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');
|
2014-03-31 18:24:29 +02:00
|
|
|
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 () {
|
|
|
|
|
2014-03-31 18:24:29 +02:00
|
|
|
var icinga = this.icinga;
|
|
|
|
|
2014-03-04 14:05:01 +01:00
|
|
|
// No history API, no action
|
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:24:29 +02:00
|
|
|
icinga.logger.debug('Pushing current state to history');
|
2014-03-04 14:05:01 +01:00
|
|
|
var url = '';
|
2014-03-31 18:24:29 +02:00
|
|
|
var blacklist = ['_render', '_reload'];
|
2014-03-04 14:05:01 +01:00
|
|
|
|
|
|
|
// 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) {
|
2014-06-17 16:21:28 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Did we find any URL? Then push it!
|
|
|
|
if (url !== '') {
|
|
|
|
window.history.pushState({icinga: true}, null, url);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2014-06-05 05:40:43 +02:00
|
|
|
// 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();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-31 18:24:29 +02:00
|
|
|
applyLocationBar: function (onload) {
|
2014-03-10 15:48:40 +01:00
|
|
|
var icinga = this.icinga,
|
|
|
|
main,
|
|
|
|
parts;
|
|
|
|
|
2014-03-31 18:24:29 +02:00
|
|
|
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;
|
2014-03-31 18:24:29 +02:00
|
|
|
if (! onload && $('#col1').data('icingaUrl') !== main) {
|
2014-03-04 14:05:01 +01:00
|
|
|
icinga.loader.loadUrl(
|
|
|
|
main,
|
|
|
|
$('#col1')
|
|
|
|
).historyTriggered = true;
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:06:03 +02:00
|
|
|
if (document.location.hash && document.location.hash.match(/^#!/)) {
|
2014-03-04 14:05:01 +01:00
|
|
|
|
|
|
|
parts = document.location.hash.split(/#!/);
|
|
|
|
|
|
|
|
if ($('#col2').data('icingaUrl') !== main) {
|
|
|
|
icinga.loader.loadUrl(
|
|
|
|
parts[1],
|
|
|
|
$('#col2')
|
|
|
|
).historyTriggered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|