X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2FMCTXWeb%2Fpublic_html%2Fstatic%2Fmctx.gui.js;h=3df2bac10e52374bdd6baae629a8a5922a057898;hb=1538b01df7b16dc1223c1459620dac4b22c916a4;hp=26f6f3c1db418f2844d6cee587d640dff09cd512;hpb=f7835777d9a2e85ab310919a150a200ea55c7222;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 26f6f3c..3df2bac 100644 --- a/testing/MCTXWeb/public_html/static/mctx.gui.js +++ b/testing/MCTXWeb/public_html/static/mctx.gui.js @@ -1,6 +1,12 @@ /** - * MCTX3420 2013 GUI stuff. - */ +* MCTX3420 2013 GUI stuff. +* Coding style: +* - Always end statements with semicolons +* - Egyptian brackets are highly recommended (*cough*). +* - Don't use synchronous stuff - hook events into callbacks +* - $.fn functions should return either themselves or some useful object +* to allow for chaining of method calls +*/ mctx = {}; //Don't use this in the final version @@ -10,253 +16,331 @@ mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/" mctx.api = location.protocol + "//" + location.host + "/" + "api/"; mctx.expected_api_version = 0; mctx.has_control = false; -//mctx.debug = true; +mctx.debug = true; -mctx.statusCodes = { - STATUS_OK : 1 -} +mctx.menu = [ + {'text' : 'Home', href : mctx.location + 'index.html'}, + {'text' : 'Experiment control', href : mctx.location + 'control.html'}, + {'text' : 'Pin debugging', href : mctx.location + 'pintest.html'}, + {'text' : 'Help', href : mctx.location + 'help.html'} +]; + +mctx.status = { + OK : 1, + ERROR : -1, + UNAUTHORIZED : -2, + NOTRUNNING : -3, + ALREADYEXISTS : -4 +}; mctx.statusCodesDescription = { - "1" : "Ok", - "-1" : "General error", - "-2" : "Unauthorized", - "-3" : "Not running", - "-4" : "Already exists" + "1" : "Ok", + "-1" : "General error", + "-2" : "Unauthorized", + "-3" : "Not running", + "-4" : "Already exists" }; mctx.sensors = { - 0 : {name : "Strain gauge 1"}, - 1 : {name : "Strain gauge 2"}, - 2 : {name : "Strain gauge 3"}, - 3 : {name : "Strain gauge 4"}, - 4 : {name : "Pressure sensor 1"}, - 5 : {name : "Pressure sensor 2"}, - 6 : {name : "Pressure sensor 3"} + 0 : {name : "Strain gauge 1"}, + 1 : {name : "Strain gauge 2"}, + 2 : {name : "Strain gauge 3"}, + 3 : {name : "Strain gauge 4"}, + 4 : {name : "Pressure sensor 1"}, + 5 : {name : "Pressure sensor 2"}, + 6 : {name : "Pressure sensor 3"} }; mctx.actuators = { - 0 : {name : "Solenoid 1"}, - 1 : {name : "Solenoid 2"}, - 2 : {name : "Solenoid 3"}, - 3 : {name : "Pressure regulator"} + 0 : {name : "Solenoid 1"}, + 1 : {name : "Solenoid 2"}, + 2 : {name : "Solenoid 3"}, + 3 : {name : "Pressure regulator"} }; +mctx.actuator = {}; +mctx.actuator.pressure_regulator = 0; + 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); - } +/** +* Logs a message if mctx.debug is enabled. This function takes +* a variable number of arguments and passes them +* to alert or console.log (based on browser support). +* @returns {undefined} +*/ +function debugLog () { + if (mctx.debug) { + if (typeof console === "undefined" || typeof console.log === "undefined") { + for (var i = 0; i < arguments.length; i++) { + alert(arguments[i]); + } + } else { + try { + console.log.apply(this, arguments); + } catch (e) { + //Chromie + for (var i = 0; i < arguments.length; i++) { + console.log(arguments[i]); + } + } + } + } } /** - * Writes the current date to wherever it's called. - */ -function getDate(){ - document.write((new Date()).toDateString()); +* Writes the current date to wherever it's called. +*/ +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) { - $.ajax({ - url : mctx.api + "identify" - }).done(function (data) { - if (mctx.debug) { - debugLog("Redirect disabled!"); - } else if (data.logged_in && isLoginPage) { - window.location = mctx.location; - } else if (!data.logged_in && !isLoginPage) { - //Note: this only clears the nameless cookie - document.cookie = ""; - window.location = mctx.location + "login.html"; - } else { - mctx.friendlyName = data.friendly_name; - $("#content").css("display", "block"); - } - }).fail(function (jqHXR) { - if (!isLoginPage) { - window.location = mctx.location + "login.html"; - } else { - debugLog("Failed to ident server. Is API running?") - } - }); + 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"; + } + }).always(function () { + + }); } /** - * Populates a submenu of the navigation bar - * @param {string} header The header - * @param {object} items An object representing the submenu items - * @param {function} translator A function that translates an object item - * into a text and href. - * @returns {$.fn} Itself + * Populates the navigation menu. */ -$.fn.populateSubmenu = function(header, items, translator) { - var submenuHeader = $("
  • ").append($("", {text : header, href : "#"})); - var submenu = $("