changeset 16:9455ec115a5c

formatting changes, made postJSON() refer to Api.postJSON.
author Atul Varma <avarma@mozilla.com>
date Thu, 24 Jun 2010 22:53:16 -0700
parents a631c77a3bf5
children 18d28e6a9887
files static-files/index.js
diffstat 1 files changed, 84 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/static-files/index.js	Thu Jun 24 22:46:15 2010 -0700
+++ b/static-files/index.js	Thu Jun 24 22:53:16 2010 -0700
@@ -50,98 +50,101 @@
   }
 };
 
-// This anonymous closure sets up the UI.
+(
+  // This anonymous closure sets up the UI.
+  function(window) {
+    function ensureStateIsValid() {
+      if (!('state' in Config.value))
+        Config.setValue({state: "login"});
+    }
 
-(function(window) {
-function ensureState() {
-  if (!('state' in Config.value))
-    Config.setValue({state: "login"});
-}
-
-function updateUI() {
-  ensureState();
+    function updateUI() {
+      ensureStateIsValid();
 
-  $(".screen").hide();
-  $("#" + Config.value.state).show();
-  switch (Config.value.state) {
-  case "login":
-    break;
-  case "wait-for-verify":
-    break;
-  case "logged-in":
-    $(".login-email").text(Config.value.email);
-    jQuery.getJSON(
-      "/profile",
-      {token: Config.value.token},
-      function(data, textStatus) {
-        console.log(data, textStatus);
-      });
-    break;
-  }
-}
+      $(".screen").hide();
+      $("#" + Config.value.state).show();
+      switch (Config.value.state) {
+      case "login":
+        break;
+      case "wait-for-verify":
+        break;
+      case "logged-in":
+        $(".login-email").text(Config.value.email);
+        jQuery.getJSON(
+          "/profile",
+          {token: Config.value.token},
+          function(data, textStatus) {
+            // TODO: This is only temporary, change it.
+            console.log(data, textStatus);
+          });
+        break;
+      }
+    }
 
-function bindConfigToUI() {
-  var lastChanged = Config.lastChanged;
+    function bindConfigToUI() {
+      var lastChanged = Config.lastChanged;
+
+      function onConfigChanged() {
+        lastChanged = Config.lastChanged;
+        updateUI();
+      };
+
+      Config.observers.push(onConfigChanged);
 
-  function onConfigChanged() {
-    lastChanged = Config.lastChanged;
-    updateUI();
-  };
-
-  Config.observers.push(onConfigChanged);
+      window.setInterval(
+        function() {
+          if (Config.lastChanged > lastChanged)
+            onConfigChanged();
+        },
+        1000
+      );
 
-  window.setInterval(
-    function() {
-      if (Config.lastChanged > lastChanged)
-        onConfigChanged();
-    },
-    1000
-  );
+      updateUI();
+    }
 
-  updateUI();
-}
+    function initUI() {
+      ensureStateIsValid();
+
+      $(".start-over").click(function() { Config.wipe(); });
 
-$(window).ready(
-  function() {
-    ensureState();
-
-    $(".start-over").click(function() { Config.wipe(); });
+      $("#login form").submit(
+        function(event) {
+          event.preventDefault();
+          $("#login .error").hide();
+          Api.postJSON(
+            "/challenge/request",
+            {email: $(this).find("#email").val() },
+            function(success, data) {
+              if (success) {
+                Config.setValue({state: "wait-for-verify"});
+              } else {
+                $("#login .error").slideDown();
+              }
+            });
+        });
 
-    $("#login form").submit(
-      function(event) {
-        event.preventDefault();
-        $("#login .error").hide();
-        postJSON(
-          "/challenge/request",
-          {email: $(this).find("#email").val() },
+      var verify = window.location.hash.match(/#verify=(.+)/);
+      if (verify && Config.value.state != "logged-in") {
+        verify = verify[1];
+        Config.setValue({state: "wait-for-verify"});
+        Api.postJSON(
+          "/challenge/respond",
+          {token: verify},
           function(success, data) {
+            window.location.hash = "";
+            bindConfigToUI();
             if (success) {
-              Config.setValue({state: "wait-for-verify"});
+              Config.setValue({state: "logged-in",
+                               token: data.token,
+                               email: data.email});
             } else {
-              $("#login .error").slideDown();
+              $("#wait-for-verify .error").slideDown();
             }
           });
-      });
+      } else
+        bindConfigToUI();
+    }
 
-    var verify = window.location.hash.match(/#verify=(.+)/);
-    if (verify && Config.value.state != "logged-in") {
-      verify = verify[1];
-      Config.setValue({state: "wait-for-verify"});
-      postJSON(
-        "/challenge/respond",
-        {token: verify},
-        function(success, data) {
-          window.location.hash = "";
-          bindConfigToUI();
-          if (success) {
-            Config.setValue({state: "logged-in",
-                             token: data.token,
-                             email: data.email});
-          } else {
-            $("#wait-for-verify .error").slideDown();
-          }
-        });
-    } else
-      bindConfigToUI();
-  });
-})(window);
+    $(window).ready(initUI);
+  }
+)(window);