X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2FMCTXWeb%2Fpublic_html%2Fstatic%2Fmctx.gui.js;h=8c6da7ad5887b5f577ed4266191825c74f5af7f2;hb=826791abc3a3bb383c2908d7d39618b99ad7665c;hp=247484d77f4087421ebd0c5e36b627627a12fd36;hpb=21a6918d547282354ac7e0bf1700c1149083582c;p=matches%2FMCTX3420.git diff --git a/testing/MCTXWeb/public_html/static/mctx.gui.js b/testing/MCTXWeb/public_html/static/mctx.gui.js index 247484d..8c6da7a 100644 --- a/testing/MCTXWeb/public_html/static/mctx.gui.js +++ b/testing/MCTXWeb/public_html/static/mctx.gui.js @@ -3,12 +3,20 @@ */ mctx = {}; -mctx.api = location.protocol + "//" + location.host + "/api/"; +//Don't use this in the final version +mctx.location = window.location.pathname; +mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/"; +//mctx.location = location.protocol + "//" + location.host + "/"; +mctx.api = location.protocol + "//" + location.host + "/" + "api/"; mctx.expected_api_version = 0; -mctx.key = undefined; mctx.has_control = false; +//mctx.debug = true; -mctx.return_codes = { +mctx.statusCodes = { + STATUS_OK : 1 +} + +mctx.statusCodesDescription = { "1" : "Ok", "-1" : "General error", "-2" : "Unauthorized", @@ -37,6 +45,14 @@ mctx.strain_gauges = {}; mctx.strain_gauges.ids = [0, 1, 2, 3]; mctx.strain_gauges.time_limit = 20; +function debugLog (msg) { + if (typeof console === "undefined" || typeof console.log === "undefined") { + alert(msg); + } else { + console.log(msg); + } +} + /** * Writes the current date to wherever it's called. */ @@ -44,6 +60,43 @@ function getDate(){ document.write((new Date()).toDateString()); } +/** + * Should be run before the load of any GUI page. + * To hook events to be called after this function runs, + * use the 'always' method, e.g runBeforeLoad().always(function() {my stuff}); + * @param {type} isLoginPage + * @returns The return value of calling $.ajax + */ +function runBeforeLoad(isLoginPage) { + return $.ajax({ + url : mctx.api + "identify" + }).done(function (data) { + if (data.logged_in && isLoginPage) { + if (mctx.debug) { + debugLog("Redirect disabled!"); + } else { + window.location = mctx.location; + } + } else if (!data.logged_in && !isLoginPage) { + if (mctx.debug) { + debugLog("Redirect disabled!"); + } else { + //Note: this only clears the nameless cookie + document.cookie = ""; + window.location = mctx.location + "login.html"; + } + } else { + mctx.friendlyName = data.friendly_name; + } + }).fail(function (jqHXR) { + if (mctx.debug) { + debugLog("Failed to ident server. Is API running?") + } else if (!isLoginPage) { + window.location = mctx.location + "login.html"; + } + }); +} + /** * Populates a submenu of the navigation bar * @param {string} header The header @@ -154,33 +207,46 @@ $.fn.setStrainGraphs = function () { $.fn.login = function () { var username = this.find("input[name='username']").val(); var password = this.find("input[name='pass']").val(); - var force = this.find("input[name='force']").is(":checked"); - var url = mctx.api + "control"; - - var authFunc = function(xhr) { - xhr.setRequestHeader("Authorization", - "Basic " + base64.encode(username + ":" + password)); + var out = this.find("#result"); + var redirect = function () { + window.location.href = mctx.location; }; - + + out.removeAttr("class"); + out.text("Logging in..."); + $.ajax({ - url : url, - data : {action : "lock", force : (force ? true : undefined)}, - beforeSend : authFunc + url : mctx.api + "bind", + data : {user: username, pass : password} }).done(function (data) { - mctx.key = data.key; if (data.status < 0) { - alert("no - " + data.description); + mctx.has_control = false; + out.attr("class", "fail"); + out.text("Login failed: " + data.description); } else { + //todo: error check mctx.has_control = true; - alert("yes - " + mctx.key); + out.attr("class", "pass"); + out.text("Login ok!"); + setTimeout(redirect, 800); } }).fail(function (jqXHR) { - mctx.key = undefined; mctx.has_control = false; - alert("no"); + out.attr("class", "fail"); + out.text("Login request failed - connection issues.") }); }; +$.fn.logout = function () { + $.ajax({ + url : mctx.api + "unbind" + }).always(function () { + //Note: this only clears the nameless cookie + document.cookie = ""; + window.location = mctx.location + "login.html"; + }); +} + $.fn.setErrorLog = function () { var url = mctx.api + "errorlog"; var outdiv = this;