From 79ed9f62a9bb88b576e30bc975a382a7dc88c77e Mon Sep 17 00:00:00 2001
From: Shane Tomlinson <stomlinson@mozilla.com>
Date: Wed, 10 Aug 2011 14:29:36 -0700
Subject: [PATCH] Starting a pick email controller.

---
 .../controllers/chooseemail_controller.js     | 35 ++++++++
 .../dialog/controllers/dialog_controller.js   | 83 ++++++++++---------
 browserid/static/dialog/dialog.js             |  3 +-
 .../static/dialog/views/bottom-pickemail.ejs  |  2 +-
 browserid/static/dialog/views/signin.ejs      | 12 ++-
 5 files changed, 89 insertions(+), 46 deletions(-)
 create mode 100644 browserid/static/dialog/controllers/chooseemail_controller.js

diff --git a/browserid/static/dialog/controllers/chooseemail_controller.js b/browserid/static/dialog/controllers/chooseemail_controller.js
new file mode 100644
index 000000000..cb883f58d
--- /dev/null
+++ b/browserid/static/dialog/controllers/chooseemail_controller.js
@@ -0,0 +1,35 @@
+(function() {
+  "use strict";
+
+  PageController.extend("Chooseemail", {}, {
+    init: function(options) {
+      this._super({
+        bodyTemplate: "signin.ejs",
+        bodyVars: {
+          sitename: BrowserIDNetwork.origin,
+          identities: getEmails()
+        },
+        footerTemplate: "bottom-pickemail.ejs",
+        footerVars: {}
+      });
+      // select the first option
+      this.find('input:first').attr('checked', true);
+    },
+
+    submit: function() {
+      var email = $("#identities input:checked").val();
+      this.close("chooseemail:complete", {
+        email: email
+      });
+    },
+
+    "#addemail click": function(event) {
+      this.close("chooseemail:addemail");
+    },
+
+    "#notme click": function(event) {
+      this.close("chooseemail:notme");
+    }
+  });
+
+}());
diff --git a/browserid/static/dialog/controllers/dialog_controller.js b/browserid/static/dialog/controllers/dialog_controller.js
index da7f322d5..504b397eb 100644
--- a/browserid/static/dialog/controllers/dialog_controller.js
+++ b/browserid/static/dialog/controllers/dialog_controller.js
@@ -24,27 +24,6 @@ PageController.extend("Dialog", {}, {
       this.doAuthenticate();
     },
 
-    "#pickemail click": function(event) {
-      var email = $("#identities input:checked").val();
-
-      // yay!  now we need to produce an assertion.
-      var storedID = getEmails()[email];
-
-      var privkey = storedID.priv;
-      var issuer = storedID.issuer;
-      var audience = BrowserIDNetwork.origin;
-      var assertion = CryptoStubs.createAssertion(audience, email, privkey, issuer);
-      // Clear onerror before the call to onsuccess - the code to onsuccess 
-      // calls window.close, which would trigger the onerror callback if we 
-      // tried this afterwards.
-      this.onerror = null;
-      this.onsuccess(assertion);
-    },
-
-    "#addemail click": function(event) {
-      this.doNewEmail();
-    },
-
     "#addemail_button click": function(event) {
       // add the actual email
       // now we need to actually try to stage the creation of this account.
@@ -65,11 +44,6 @@ PageController.extend("Dialog", {}, {
         });
     },
 
-    "#notme click": function(event) {
-      clearEmails();
-      BrowserIDNetwork.logout(this.doAuthenticate.bind(this));
-    },
-      
     "#cancel click": function(event) {
       this.onerror("canceled");
     },
@@ -97,7 +71,7 @@ PageController.extend("Dialog", {}, {
     stateMachine: function() {
       var self=this, hub = OpenAjax.hub, el = this.element;
 
-      hub.subscribe("createaccount:created", function(info) {
+      hub.subscribe("createaccount:created", function(msg, info) {
         self.doConfirmEmail(info.email, info.keypair);
       });
 
@@ -114,17 +88,25 @@ PageController.extend("Dialog", {}, {
       });
 
       hub.subscribe("checkregistration:confirmed", function() {
-        // this is a secondary registration from browserid.org, persist
-        // email, keypair, and that fact
-        self.persistAddressAndKeyPair(self.confirmEmail, 
-          self.confirmKeypair, "browserid.org:443");
-        self.syncidentities();
+        self.doRegistrationConfirmed();
       });
 
       hub.subscribe("checkregistration:complete", function() {
         self.doSignIn();
       });
 
+      hub.subscribe("chooseemail:complete", function(msg, info) {
+        self.doEmailSelected(info.email);
+      });
+
+      hub.subscribe("chooseemail:addemail", function() {
+        self.doNewEmail();
+      });
+
+      hub.subscribe("chooseemail:notme", function() {
+        self.doNotMe();
+      });
+
       hub.subscribe("cancel", function() {
         // cancel
         if(self.onerror) {
@@ -153,11 +135,7 @@ PageController.extend("Dialog", {}, {
     },
       
     doSignIn: function() {
-      this.renderTemplates("signin.ejs", {sitename: BrowserIDNetwork.origin, identities: getEmails()},
-                           "bottom-pickemail.ejs", {});
-
-      // select the first option
-      this.find('input:first').attr('checked', true);
+      this.element.chooseemail();
     },
 
     doAuthenticate: function() {
@@ -189,6 +167,37 @@ PageController.extend("Dialog", {}, {
       this.element.checkregistration({email: email});
     },
 
+    doRegistrationConfirmed: function() {
+        var self = this;
+        // this is a secondary registration from browserid.org, persist
+        // email, keypair, and that fact
+        self.persistAddressAndKeyPair(self.confirmEmail, 
+          self.confirmKeypair, "browserid.org:443");
+        self.syncidentities();
+
+    },
+
+    doEmailSelected: function(email) {
+      var self=this,
+          // yay!  now we need to produce an assertion.
+          storedID = getEmails()[email],
+          privkey = storedID.priv,
+          issuer = storedID.issuer,
+          audience = BrowserIDNetwork.origin,
+          assertion = CryptoStubs.createAssertion(audience, email, privkey, issuer);
+
+      // Clear onerror before the call to onsuccess - the code to onsuccess 
+      // calls window.close, which would trigger the onerror callback if we 
+      // tried this afterwards.
+      self.onerror = null;
+      self.onsuccess(assertion);
+    },
+
+    doNotMe: function() {
+      clearEmails();
+      BrowserIDNetwork.logout(this.doAuthenticate.bind(this));
+    },
+
     persistAddressAndKeyPair: function(email, keypair, issuer) {
       var new_email_obj= {
         created: new Date(),
diff --git a/browserid/static/dialog/dialog.js b/browserid/static/dialog/dialog.js
index ac61d495a..8c3c19ce2 100644
--- a/browserid/static/dialog/dialog.js
+++ b/browserid/static/dialog/dialog.js
@@ -25,7 +25,8 @@ steal.plugins(
                'authenticate',
                'createaccount',
                'checkregistration',
-               'forgotpassword')					// loads files in controllers folder
+               'forgotpassword',
+               'chooseemail')					// loads files in controllers folder
 
 	.views('authenticate.ejs',
            'addemail.ejs',
diff --git a/browserid/static/dialog/views/bottom-pickemail.ejs b/browserid/static/dialog/views/bottom-pickemail.ejs
index debe989ff..b1cf8b513 100644
--- a/browserid/static/dialog/views/bottom-pickemail.ejs
+++ b/browserid/static/dialog/views/bottom-pickemail.ejs
@@ -1,2 +1,2 @@
-  <button id="pickemail" class="righty action submit">Sign In</button>
+  <input type="submit" id="pickemail" class="righty action submit" value="Sign In" />
   <button id="cancel" class="righty">Cancel</button>
diff --git a/browserid/static/dialog/views/signin.ejs b/browserid/static/dialog/views/signin.ejs
index 0fda9e7e1..25519d6ec 100644
--- a/browserid/static/dialog/views/signin.ejs
+++ b/browserid/static/dialog/views/signin.ejs
@@ -1,12 +1,10 @@
   <div class="content">
     <p class="prompt">What email address would you like to use to sign into <span class="sitename bad"><%= sitename %></span>?</p>
-    <form id="identities" name="identities">
-      <ul>
-        <% _.each(identities, function(email_obj, email_address) { %>
-          <li><input type="radio" name="identity" value="<%=email_address%>" id="<%=email_address%>" /><label for="<%=email_address%>"><%= email_address %></label></li>
-        <% }); %>
-      </ul>
-    </form>
+    <ul id="identities">
+      <% _.each(identities, function(email_obj, email_address) { %>
+        <li><input type="radio" name="identity" value="<%=email_address%>" id="<%=email_address%>" /><label for="<%=email_address%>"><%= email_address %></label></li>
+      <% }); %>
+    </ul>
   </div>
   <div class="actions">
     <div class="action"><a id="addemail" href="#">Add a new email address</a></div>
-- 
GitLab