Jannis Moßhammer 04f7149cfe Allow container-based url in history api
When a url is now loaded for an non-main container, the url for the
container is appended to the GET part of the URL

refs #4303
2013-06-25 11:18:27 +02:00

140 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*global Icinga:false define:false require:false base_url:false console:false */
(function() {
"use strict";
var asyncMgrInstance = null;
define(['icinga/container','logging','jquery'],function(containerMgr,log,$) {
var pending = {
};
var getCurrentGETParameters = function() {
var currentGET = window.location.search.substring(1).split("&");
var params = {};
if(currentGET.length > 0) {
$.each(currentGET, function(idx, elem) {
var keyVal = elem.split("=");
params[encodeURIComponent(keyVal[0])] = encodeURIComponent(keyVal[1]);
});
}
return params;
}
;
var pushGet = function(param, value, url) {
url = url || (window.location.origin+window.location.pathname);
var params = getCurrentGETParameters();
params[encodeURIComponent(param)] = encodeURIComponent(value);
var search = "?";
for (var name in params) {
if(search != "?")
search += "&";
search += name+"="+params[name];
}
return url+search+"#"+window.location.hash;
};
var getDOMForDestination = function(destination) {
var target = destination;
if(typeof destination === "string") {
target = containerMgr.getContainer(destination)[0];
} else if(typeof destination.context !== "undefined") {
target = destination[0];
}
return target;
};
var handleResponse = function(html) {
if(this.destination) {
containerMgr.updateContainer(this.destination,html,this);
} else {
containerMgr.createPopupContainer(html,this);
}
};
var handleFailure = function(result,error) {
if(error === "abort") {
return;
}
log.error("Error loading resource",error,arguments);
if(this.destination) {
containerMgr.updateContainer(this.destination,result.responseText,this);
}
};
var isParent = function(dom,parentToCheck) {
while(dom.parentNode) {
dom = dom.parentNode;
if(dom === parentToCheck) {
return true;
}
}
return false;
};
var CallInterface = function() {
this.clearPendingRequestsFor = function(destination) {
if(!$.isArray(pending)) {
pending = [];
return;
}
var resultset = [];
for(var x=0;x<pending.length;x++) {
var container = pending[x].DOM;
if(isParent(container,getDOMForDestination(destination))) {
pending[x].request.abort();
} else {
resultset.push(pending[x]);
}
}
pending = resultset;
};
this.createRequest = function(url,data) {
var req = $.ajax({
type : data ? 'POST' : 'GET',
url : url,
data : data,
headers: { 'X-Icinga-Accept': 'text/html' }
});
req.url = url;
req.done(handleResponse.bind(req));
req.fail(handleFailure.bind(req));
return req;
};
this.loadToTarget = function(destination,url,data) {
if(destination) {
this.clearPendingRequestsFor(destination);
}
var req = this.createRequest(url,data);
if(destination) {
pending.push({
request: req,
DOM: getDOMForDestination(destination)
});
req.destination = destination;
}
if (destination == "icinga-main") {
History.pushState(data, document.title, url);
} else {
url = pushGet("c["+destination+"]", url);
History.pushState(data, document.title, url);
}
console.log("New url: ", url);
return req;
};
this.loadCSS = function(name) {
};
};
return new CallInterface();
});
})();