Add user friendly names (right now just usernames)
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / static / mctx.gui.js
index 089e93c..8c6da7a 100644 (file)
@@ -3,12 +3,20 @@
  */
 
 mctx = {};
-mctx.location = location.protocol + "//" + location.host + "/";
-mctx.api = mctx.location + "/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.has_control = false;
+//mctx.debug = true;
 
-mctx.return_codes = {
+mctx.statusCodes = {
+  STATUS_OK : 1
+}
+
+mctx.statusCodesDescription = {
   "1" : "Ok",
   "-1" : "General error",
   "-2" : "Unauthorized",
@@ -52,24 +60,39 @@ 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({
+  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) {
-      //Note: this only clears the nameless cookie
-      document.cookie = ""; 
-      window.location = mctx.location + "login.html";
+      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 (!isLoginPage) {
-      window.location = mctx.location + "login.html";
-    } else {
+    if (mctx.debug) {
       debugLog("Failed to ident server. Is API running?")
+    } else if (!isLoginPage) {
+      window.location = mctx.location + "login.html";
     }
   });
 }
@@ -196,11 +219,17 @@ $.fn.login = function () {
     url : mctx.api + "bind",
     data : {user: username, pass : password}
   }).done(function (data) {
-    //todo: error check
-    mctx.has_control = true;
-    out.attr("class", "pass");
-    out.text("Login ok!");
-    setTimeout(redirect, 1000);
+    if (data.status < 0) {
+      mctx.has_control = false;
+      out.attr("class", "fail");
+      out.text("Login failed: " + data.description);
+    } else {
+      //todo: error check
+      mctx.has_control = true;
+      out.attr("class", "pass");
+      out.text("Login ok!");
+      setTimeout(redirect, 800);      
+    }
   }).fail(function (jqXHR) {
     mctx.has_control = false;
     out.attr("class", "fail");

UCC git Repository :: git.ucc.asn.au