88 lines
3.3 KiB
JavaScript
88 lines
3.3 KiB
JavaScript
function Helpers() {
|
|
var self = this;
|
|
/**
|
|
* Create a request URL - references apiEndpoints object to construct url with args, and optional callback url.
|
|
* @param {string} routePath
|
|
* @param {Array<Object<string>>} params - Key, Value object detailing the param name (key) and value (value).
|
|
* @param {boolean} requiresCallback - True - add callback function for JSONP/CORS.
|
|
* @param {boolean} isAbsolutePath - True, create a relative URL (without root).
|
|
* @returns {string} the url generated
|
|
* @example
|
|
* createRequestUrl("/api/endpoint", [{key:"param", value:"value"}], true, false);
|
|
* returns: "http://192.168.2.2/api/endpoint?param=value&callback=?"
|
|
*/
|
|
self.createRequestUrl = function (routePath, params, requiresCallback, isAbsoluteUrl) {
|
|
var appender = "?";
|
|
var url = "";
|
|
if (isAbsoluteUrl) {
|
|
url = self.apiEndpoints.root;
|
|
}
|
|
url = url + routePath;
|
|
if (params !== undefined
|
|
&& params !== null) {
|
|
if (params.length > 0) {
|
|
for (var i = 0; i < params.length; i++) {
|
|
url += appender + params[i].key + "=" + params[i].value;
|
|
appender = "&";
|
|
}
|
|
}
|
|
}
|
|
if (requiresCallback) {
|
|
url += appender + "callback=?";
|
|
}
|
|
return url;
|
|
};
|
|
self.goToMenuOption = function(page) {
|
|
location.hash = page;
|
|
console.log("goToMenuOption: " + page);
|
|
};
|
|
self.processRequestFailure = function(xmlHttpRequest, textStatus, errorThrown) {
|
|
if (xmlHttpRequest.readyState === 4) {
|
|
return {
|
|
errorCode: xmlHttpRequest.status,
|
|
errorMessage: xmlHttpRequest.statusText,
|
|
errorSource: ""
|
|
};
|
|
}
|
|
else if (xmlHttpRequest.readyState === 0) {
|
|
return {
|
|
errorCode: xmlHttpRequest.status,
|
|
errorMessage: "Network Error - Is the server available?",
|
|
errorSource: ""
|
|
};
|
|
}
|
|
else {
|
|
return {
|
|
errorCode: xmlHttpRequest.status,
|
|
errorMessage: "Unknown Error",
|
|
errorSource: ""
|
|
};
|
|
}
|
|
}
|
|
/**
|
|
* Function to redirect to a page in the sammy.js eco system.
|
|
* Relies on "pagedestination" tag in the html. This is a button click handler.
|
|
* @param {Object<unknown>} data - dunno?
|
|
* @param {Object<buttonhandle>} event - handle to the button that was clicked.
|
|
* @returns {nothing} - redirects to the url referenced by the pageDestination tag.
|
|
*/
|
|
self.getPageDestination = function(data, event) {
|
|
var target = null;
|
|
if (event.target) target = event.target;
|
|
else if (event.srcElement) target = event.srcElement;
|
|
var destination = "";
|
|
if (target != null) {
|
|
for (var i = 0; i < target.attributes.length; i++) {
|
|
if (target.attributes[i].nodeName === "pagedestination") {
|
|
destination = target.attributes[i].value;
|
|
break;
|
|
}
|
|
}
|
|
if (destination !== "") {
|
|
return destination;
|
|
}
|
|
} else {
|
|
console.log("target is null, going nowhere");
|
|
}
|
|
};
|
|
}; |