From e0b8975d4a241da1ccc9b42c64cbf23ff7b0cf77 Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Wed, 10 Aug 2011 10:27:16 -0700
Subject: [PATCH] Starting to split off the authentication controller for
 easier reuse

---
 .../controllers/authenticate_controller.js    | 43 ++++++++++
 .../dialog/controllers/dialog_controller.js   | 30 ++-----
 .../dialog/controllers/page_controller.js     | 86 +++++++++++++++++++
 browserid/static/dialog/dialog.js             |  4 +-
 browserid/static/dialog/style.css             |  6 +-
 browserid/static/dialog/views/body.ejs        | 10 ++-
 .../static/dialog/views/bottom-signin.ejs     |  2 +-
 7 files changed, 151 insertions(+), 30 deletions(-)
 create mode 100644 browserid/static/dialog/controllers/authenticate_controller.js
 create mode 100644 browserid/static/dialog/controllers/page_controller.js

diff --git a/browserid/static/dialog/controllers/authenticate_controller.js b/browserid/static/dialog/controllers/authenticate_controller.js
new file mode 100644
index 000000000..7d5eb7f35
--- /dev/null
+++ b/browserid/static/dialog/controllers/authenticate_controller.js
@@ -0,0 +1,43 @@
+(function() {
+  "use strict";
+
+  PageController.extend("Authenticate", {}, {
+      init: function() {
+        this._super({
+          bodyTemplate: "authenticate.ejs",
+          bodyVars: {
+            sitename: BrowserIDNetwork.origin
+          },
+          footerTemplate: "bottom-signin.ejs",
+          footerVars: {}
+        });
+      },
+
+      validate: function() {
+        var email = $("#email_input").val();
+        var pass = $("#password_input").val();
+
+        return true;
+      },
+
+      submit: function() {
+        var email = $("#email_input").val();
+        var pass = $("#password_input").val();
+
+        var self = this;
+        BrowserIDNetwork.authenticate(email, pass, function(authenticated) {
+          if (!authenticated) {
+            self.find("#nosuchaccount").hide().fadeIn(400);
+          } else {
+              self.doWait(BrowserIDWait.authentication);
+              self.publish("authenticate:authenticated");
+          }
+        }, function(resp) {
+          self.runErrorDialog(BrowserIDErrors.authentication);
+          self.publish("cancel");
+        });
+        
+      }
+  });
+
+}());
diff --git a/browserid/static/dialog/controllers/dialog_controller.js b/browserid/static/dialog/controllers/dialog_controller.js
index 6f7b0c6f1..1aed5d65a 100644
--- a/browserid/static/dialog/controllers/dialog_controller.js
+++ b/browserid/static/dialog/controllers/dialog_controller.js
@@ -37,24 +37,6 @@ $.Controller("Dialog", {}, {
     "#suggest_signin click": function(event) {
       this.doAuthenticate();
     },
-      
-    "#signin click": function(event) {
-      var email = $("#email_input").val();
-      var pass = $("#password_input").val();
-
-      var self = this;
-
-      BrowserIDNetwork.authenticate(email, pass, function(authenticated) {
-        if (!authenticated) {
-          self.find("#nosuchaccount").hide().fadeIn(400);
-        } else {
-          self.doWait(BrowserIDWait.authentication);
-          self.syncIdentities();
-        }
-      }, function(resp) {
-        runErrorDialog(BrowserIDErrors.authentication);
-      });
-    },
 
     "#pickemail click": function(event) {
       var email = $("#identities input:checked").val();
@@ -186,9 +168,15 @@ $.Controller("Dialog", {}, {
     },
 
     doAuthenticate: function() {
-      this.renderTemplates("authenticate.ejs", {sitename: BrowserIDNetwork.origin},
-                           "bottom-signin.ejs", {});
-
+      var el = this.element, self=this;
+      el.authenticate();
+      OpenAjax.hub.subscribe("authenticate:authenticated", function() {
+        el.authenticate('destroy');
+        self.syncIdentities();
+      });
+      OpenAjax.hub.subscribe("cancel", function() {
+        el.authenticate('destroy');
+      });
     },
       
     doCreate: function() {
diff --git a/browserid/static/dialog/controllers/page_controller.js b/browserid/static/dialog/controllers/page_controller.js
new file mode 100644
index 000000000..68b7829b5
--- /dev/null
+++ b/browserid/static/dialog/controllers/page_controller.js
@@ -0,0 +1,86 @@
+/*jshint browser:true, jQuery: true, forin: true, laxbreak:true */                                             
+/*global Channel:true, CryptoStubs:true, alert:true, errorOut:true, setupChannel:true, getEmails:true, clearEmails: true, console: true, _: true, pollTimeout: true, addEmail: true, removeEmail:true, BrowserIDNetwork: true, BrowserIDWait:true, BrowserIDErrors: true, runErrorDialog:true */ 
+//
+// a JMVC controller for the browserid dialog
+//
+
+(function() {
+"use strict";
+
+  $.Controller.extend("PageController", {
+    }, {
+    init: function(options) {
+      var bodyTemplate = options.bodyTemplate;
+      var bodyVars = options.bodyVars;
+      var footerTemplate = options.footerTemplate;
+      var footerVars = options.footerVars;
+
+      this.renderTemplates(bodyTemplate, bodyVars, footerTemplate, footerVars);
+      $('form').bind("submit", this.onSubmit.bind(this));
+    },
+
+    renderTemplates: function(body, body_vars, footer, footer_vars) {
+      if (body) {
+        var bodyHtml = $.View("//dialog/views/" + body, body_vars);
+        $('#dialog').html(bodyHtml).hide().fadeIn(300, function() {
+          $('#dialog input').eq(0).focus(); 
+        });
+      }
+
+      if (footer) {
+        var footerHtml = $.View("//dialog/views/" + footer, footer_vars);
+        $('#bottom-bar').html(footerHtml);
+      }
+      setupEnterKey();
+    },
+
+    onSubmit: function(event) {
+      event.stopPropagation();
+      event.preventDefault();
+      if(this.validate()) {
+        this.submit();
+      }
+    },
+
+    validate: function() {
+      return true;
+    },
+
+    submit: function() {
+      this.publish("submit");
+    },
+
+    doWait: function(info) {
+      this.renderTemplates("wait.ejs", {title: info.message, message: info.description});
+    },
+
+    runErrorDialog: function(info) {
+      $(".dialog").hide();
+
+      $("#error_dialog div.title").text(info.message);
+      $("#error_dialog div.content").text(info.description);
+
+      $("#back").hide();
+      $("#cancel").hide();
+      $("#submit").show().unbind('click').click(function() {
+      }).text("Close");
+
+      $("#error_dialog").fadeIn(500);
+    },
+
+    "#cancel click": function() {
+      this.publish("cancel");
+    }
+
+  });
+
+  function setupEnterKey() {
+    $("input").keyup(function(e) {
+      if(e.which == 13) {
+        e.preventDefault();
+        $('.submit').click();
+      }
+    });
+  }
+
+}());
diff --git a/browserid/static/dialog/dialog.js b/browserid/static/dialog/dialog.js
index 71ed5782f..eee2d76fb 100644
--- a/browserid/static/dialog/dialog.js
+++ b/browserid/static/dialog/dialog.js
@@ -20,7 +20,9 @@ steal.plugins(
 
 	.models()						// loads files in models folder 
 
-	.controllers('dialog')					// loads files in controllers folder
+	.controllers('dialog',
+               'page',
+               'authenticate')					// loads files in controllers folder
 
 	.views('authenticate.ejs',
            'addemail.ejs',
diff --git a/browserid/static/dialog/style.css b/browserid/static/dialog/style.css
index 2612391d9..5dcca5f87 100644
--- a/browserid/static/dialog/style.css
+++ b/browserid/static/dialog/style.css
@@ -54,7 +54,7 @@ span.sitename, span.email {
     font-size: .7em;
 }
 
-#bottom-bar button {
+#bottom-bar button, #bottom-bar input[type=submit] {
     height: 25px;
     border: 1px solid rgb(145, 145, 145);
     -moz-border-radius: 4px;
@@ -69,11 +69,11 @@ span.sitename, span.email {
     z-index: 0;
 }
 
-#bottom-bar button.righty {
+#bottom-bar button.righty, #bottom-bar input[type=submit].righty {
     float: right;
 }
 
-#bottom-bar button.action {
+#bottom-bar button.action, #bottom-bar input[type=submit] {
     background-color: rgb(0,128,211);
     color: #fff;
     font-weight: bold;
diff --git a/browserid/static/dialog/views/body.ejs b/browserid/static/dialog/views/body.ejs
index b2952fe21..7b17ff107 100644
--- a/browserid/static/dialog/views/body.ejs
+++ b/browserid/static/dialog/views/body.ejs
@@ -6,11 +6,13 @@
   <div id="labs_logo" class="sprite"></div>
 </header>
 
-<div id="dialog" class="dialog">
-</div>
+<form action=""> <!-- using the form element to hook into with js -->
+  <div id="dialog" class="dialog">
+  </div>
 
-<div id="bottom-bar">
-</div>
+  <div id="bottom-bar">
+  </div>
+</form>
 
 <footer id="terms">
     BrowserID is a <a target="_blank" href="http://mozillalabs.org">Mozilla Labs</a> service.
diff --git a/browserid/static/dialog/views/bottom-signin.ejs b/browserid/static/dialog/views/bottom-signin.ejs
index f069d5033..1c235718f 100644
--- a/browserid/static/dialog/views/bottom-signin.ejs
+++ b/browserid/static/dialog/views/bottom-signin.ejs
@@ -1,2 +1,2 @@
-  <button id="signin" class="righty action submit">Sign In</button>
+  <input type="submit" id="signin" class="righty action submit" name="submit" value="Sign In"/ >
   <button id="cancel" class="righty">Cancel</button>
-- 
GitLab