2013-06-14 13:51:44 +02:00
|
|
|
|
/*global Icinga:false define:false require:false base_url:false console:false */
|
|
|
|
|
(function() {
|
|
|
|
|
"use strict";
|
|
|
|
|
var asyncMgrInstance = null;
|
|
|
|
|
|
2013-06-17 17:13:30 +02:00
|
|
|
|
define(['icinga/container','logging','jquery'],function(containerMgr,log,$) {
|
2013-06-14 13:51:44 +02:00
|
|
|
|
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var headerListeners = {};
|
2013-06-14 13:51:44 +02:00
|
|
|
|
|
|
|
|
|
var pending = {
|
|
|
|
|
|
|
|
|
|
};
|
2013-06-19 13:17:43 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
|
var getDOMForDestination = function(destination) {
|
|
|
|
|
var target = destination;
|
2013-06-20 14:06:02 +02:00
|
|
|
|
if (typeof destination === "string") {
|
2013-06-14 13:51:44 +02:00
|
|
|
|
target = containerMgr.getContainer(destination)[0];
|
|
|
|
|
} else if(typeof destination.context !== "undefined") {
|
|
|
|
|
target = destination[0];
|
|
|
|
|
}
|
|
|
|
|
return target;
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var applyHeaderListeners = function(headers) {
|
|
|
|
|
for (var header in headerListeners) {
|
|
|
|
|
if (headers.getResponseHeader(header) === null) {
|
|
|
|
|
// see if the browser/server converts headers to lowercase
|
|
|
|
|
if (headers.getResponseHeader(header.toLowerCase()) === null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
header = header.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
var value = headers.getResponseHeader(header);
|
|
|
|
|
var listeners = headerListeners[header];
|
|
|
|
|
for (var i=0;i<listeners.length;i++) {
|
|
|
|
|
listeners[i].fn.apply(listeners[i].scope, [value, header, headers]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-06-14 13:51:44 +02:00
|
|
|
|
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var handleResponse = function(html, status, response) {
|
|
|
|
|
applyHeaderListeners(response);
|
2013-06-14 13:51:44 +02:00
|
|
|
|
if(this.destination) {
|
|
|
|
|
containerMgr.updateContainer(this.destination,html,this);
|
|
|
|
|
} else {
|
2013-06-21 15:33:06 +02:00
|
|
|
|
// tbd
|
|
|
|
|
// containerMgr.createPopupContainer(html,this);
|
2013-06-14 13:51:44 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
|
2013-06-21 15:33:06 +02:00
|
|
|
|
this.__internalXHRImplementation = $.ajax;
|
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
|
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) {
|
2013-06-21 15:33:06 +02:00
|
|
|
|
var req = this.__internalXHRImplementation({
|
2013-06-14 13:51:44 +02:00
|
|
|
|
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) {
|
2013-06-20 14:02:58 +02:00
|
|
|
|
log.debug("Laoding to container", destination, url);
|
2013-06-14 13:51:44 +02:00
|
|
|
|
this.clearPendingRequestsFor(destination);
|
|
|
|
|
}
|
|
|
|
|
var req = this.createRequest(url,data);
|
|
|
|
|
if(destination) {
|
|
|
|
|
pending.push({
|
|
|
|
|
request: req,
|
|
|
|
|
DOM: getDOMForDestination(destination)
|
|
|
|
|
});
|
|
|
|
|
req.destination = destination;
|
|
|
|
|
}
|
2013-06-17 17:13:30 +02:00
|
|
|
|
if (destination == "icinga-main") {
|
2013-07-02 11:39:20 +02:00
|
|
|
|
history.pushState(data, document.title, url);
|
2013-06-17 17:13:30 +02:00
|
|
|
|
} else {
|
2013-06-19 13:17:43 +02:00
|
|
|
|
url = pushGet("c["+destination+"]", url);
|
2013-07-02 11:39:20 +02:00
|
|
|
|
history.pushState(data, document.title, url);
|
2013-06-17 17:13:30 +02:00
|
|
|
|
}
|
2013-06-19 13:17:43 +02:00
|
|
|
|
console.log("New url: ", url);
|
2013-06-14 13:51:44 +02:00
|
|
|
|
return req;
|
|
|
|
|
};
|
2013-06-19 13:17:43 +02:00
|
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
|
this.loadCSS = function(name) {
|
|
|
|
|
|
|
|
|
|
};
|
2013-06-21 15:33:06 +02:00
|
|
|
|
|
|
|
|
|
this.registerHeaderListener = function(header, fn, scope) {
|
|
|
|
|
headerListeners[header] = headerListeners[header] || [];
|
|
|
|
|
headerListeners[header].push({fn: fn, scope:scope});
|
|
|
|
|
};
|
2013-06-14 13:51:44 +02:00
|
|
|
|
};
|
|
|
|
|
return new CallInterface();
|
|
|
|
|
});
|
2013-06-17 17:13:30 +02:00
|
|
|
|
})();
|