From eeb96ee489f9ac13e4c866df325d85e51ef2bd6c Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Tue, 1 Nov 2011 12:05:35 +0000 Subject: [PATCH] Remember the user's email address throughout all of the various front end pages. * Makes it easier for the user whenever they go from sign up to sign in to forgot password and back. * Stop remembering the username when the user successfully completes an action * Put some code into /js/pageHelpers.js to keep /js/browserid.js clean. * Start tests for pageHelpers.js. close #476 --- .../qunit/{ => js}/browserid_unit_test.js | 4 +- .../test/qunit/js/page_helpers_unit_test.js | 81 ++++++++++++++++ resources/static/dialog/test/qunit/qunit.js | 3 +- resources/static/js/browserid.js | 22 +---- resources/static/js/page_helpers.js | 93 +++++++++++++++++++ resources/static/js/pages/forgot.js | 8 +- resources/static/js/pages/signin.js | 15 +-- resources/static/js/pages/signup.js | 6 +- resources/views/layout.ejs | 2 + scripts/compress.sh | 2 +- 10 files changed, 200 insertions(+), 36 deletions(-) rename resources/static/dialog/test/qunit/{ => js}/browserid_unit_test.js (93%) create mode 100644 resources/static/dialog/test/qunit/js/page_helpers_unit_test.js create mode 100644 resources/static/js/page_helpers.js diff --git a/resources/static/dialog/test/qunit/browserid_unit_test.js b/resources/static/dialog/test/qunit/js/browserid_unit_test.js similarity index 93% rename from resources/static/dialog/test/qunit/browserid_unit_test.js rename to resources/static/dialog/test/qunit/js/browserid_unit_test.js index 5755fabc5..840039df5 100644 --- a/resources/static/dialog/test/qunit/browserid_unit_test.js +++ b/resources/static/dialog/test/qunit/js/browserid_unit_test.js @@ -34,10 +34,10 @@ * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ -steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid", function() { +steal.plugins("jquery", "funcunit/qunit").then("/js/page_helpers", "/js/browserid", function() { "use strict"; - module("browserid-unit"); + module("/js/browserid"); }); diff --git a/resources/static/dialog/test/qunit/js/page_helpers_unit_test.js b/resources/static/dialog/test/qunit/js/page_helpers_unit_test.js new file mode 100644 index 000000000..9f076596e --- /dev/null +++ b/resources/static/dialog/test/qunit/js/page_helpers_unit_test.js @@ -0,0 +1,81 @@ +/*jshint browsers:true, forin: true, laxbreak: true */ +/*global steal: true, test: true, start: true, stop: true, module: true, ok: true, equal: true, BrowserID: true */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla BrowserID. + * + * The Initial Developer of the Original Code is Mozilla. + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +steal.plugins("jquery", "funcunit/qunit").then("/dialog/resources/browserid", function() { + "use strict"; + + var pageHelpers = BrowserID.PageHelpers; + + module("/js/page_helpers"); + + + test("setStoredEmail/getStoredEmail/setupEmail prefills the email address", function() { + $("#email").val(""); + + pageHelpers.setStoredEmail("testuser@testuser.com"); + pageHelpers.setupEmail(); + + equal($("#email").val(), "testuser@testuser.com", "email was set on setupEmail"); + equal(pageHelpers.getStoredEmail(), "testuser@testuser.com", "getStoredEmail works correctly"); + }); + + test("a key press in the email address field saves it", function() { + $("#email").val(""); + + pageHelpers.setStoredEmail("testuser@testuser.co"); + pageHelpers.setupEmail(); + + // The fake jQuery event does not actually cause the letter to be added, we + // have to do that manually. + $("#email").val("testuser@testuser.com"); + + var e = jQuery.Event("keyup"); + e.which = 77; //choose the one you want + e.keyCode = 77; + $("#email").trigger(e); + + equal(pageHelpers.getStoredEmail(), "testuser@testuser.com", "hitting a key updates the stored email"); + }); + + test("clearStoredEmail clears the email address from storage", function() { + pageHelpers.clearStoredEmail(); + + equal(pageHelpers.getStoredEmail(), "", "clearStoredEmail clears stored email"); + }); + +}); + + diff --git a/resources/static/dialog/test/qunit/qunit.js b/resources/static/dialog/test/qunit/qunit.js index 55f9a46cf..093568db7 100644 --- a/resources/static/dialog/test/qunit/qunit.js +++ b/resources/static/dialog/test/qunit/qunit.js @@ -17,7 +17,8 @@ steal("/dialog/resources/browserid.js", 'pickemail.ejs', 'offline.ejs', 'error.ejs') - .then("browserid_unit_test") + .then("js/browserid_unit_test") + .then("js/page_helpers_unit_test") .then("include_unit_test") .then("relay/relay_unit_test") .then("pages/add_email_address_test") diff --git a/resources/static/js/browserid.js b/resources/static/js/browserid.js index d40061658..217e7cd03 100644 --- a/resources/static/js/browserid.js +++ b/resources/static/js/browserid.js @@ -34,8 +34,6 @@ * * ***** END LICENSE BLOCK ***** */ -window.BrowserID = window.BrowserID || {}; - $(function() { "use strict"; @@ -43,21 +41,11 @@ $(function() { * For the main page */ - function getParameterByName( name ) { - name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); - var regexS = "[\\?&]"+name+"=([^&#]*)"; - var regex = new RegExp( regexS ); - var results = regex.exec( window.location.href ); - if( results == null ) - return ""; - else - return decodeURIComponent(results[1].replace(/\+/g, " ")); - } - - var token = getParameterByName("token"), - path = document.location.pathname, - bid = BrowserID, - user = bid.User; + var bid = BrowserID, + pageHelpers = bid.PageHelpers, + user = bid.User, + token = pageHelpers.getParameterByName("token"), + path = document.location.pathname; if (!path || path === "/") { bid.index(); diff --git a/resources/static/js/page_helpers.js b/resources/static/js/page_helpers.js new file mode 100644 index 000000000..1e0bb3238 --- /dev/null +++ b/resources/static/js/page_helpers.js @@ -0,0 +1,93 @@ +/*globals BrowserID: true, _: true */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla BrowserID. + * + * The Initial Developer of the Original Code is Mozilla. + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +BrowserID.PageHelpers = (function() { + "use strict"; + + var win = window, + locStorage = win.localStorage, + bid = BrowserID; + + function setStoredEmail(email) { + locStorage.signInEmail = email; + } + + function onEmailKeyUp(event) { + var email = $("#email").val(); + setStoredEmail(email); + } + + function prefillEmail() { + // If the user tried to sign in on the sign up page with an existing email, + // place that email in the email field, then focus the password. + var el = $("#email"), + email = locStorage.signInEmail; + + if (email) { + el.val(email); + if ($("#password").length) $("#password").focus(); + } + + el.keyup(onEmailKeyUp); + } + + function clearStoredEmail() { + locStorage.removeItem("signInEmail"); + } + + function getStoredEmail() { + return locStorage.signInEmail || ""; + } + + function getParameterByName( name ) { + name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); + var regexS = "[\\?&]"+name+"=([^&#]*)"; + var regex = new RegExp( regexS ); + var results = regex.exec( win.location.href ); + if( results === null ) + return ""; + else + return decodeURIComponent(results[1].replace(/\+/g, " ")); + } + + return { + setupEmail: prefillEmail, + setStoredEmail: setStoredEmail, + clearStoredEmail: clearStoredEmail, + getStoredEmail: getStoredEmail, + getParameterByName: getParameterByName + }; +}()); diff --git a/resources/static/js/pages/forgot.js b/resources/static/js/pages/forgot.js index 1ab1bb6f0..b329a1da7 100644 --- a/resources/static/js/pages/forgot.js +++ b/resources/static/js/pages/forgot.js @@ -37,9 +37,14 @@ BrowserID.forgot = (function() { "use strict"; + var bid = BrowserID, + pageHelpers = bid.PageHelpers; + return function() { $("form input[autofocus]").focus(); + pageHelpers.setupEmail(); + $("#signUpForm").bind("submit", function(event) { event.preventDefault(); $(".notifications .notification").hide(); @@ -53,7 +58,8 @@ BrowserID.forgot = (function() { return false; } - BrowserID.User.createUser(email, function onSuccess(keypair) { + pageHelpers.clearStoredEmail(); + bid.User.createUser(email, function onSuccess(keypair) { $('#sent_to_email').html(email); $('#forminputs').fadeOut(); $(".notifications .notification.emailsent").fadeIn(); diff --git a/resources/static/js/pages/signin.js b/resources/static/js/pages/signin.js index 1df6ec7b8..69590b274 100644 --- a/resources/static/js/pages/signin.js +++ b/resources/static/js/pages/signin.js @@ -39,23 +39,13 @@ var bid = BrowserID, user = bid.User, + pageHelpers = bid.PageHelpers, validation = bid.Validation; - function prefillEmail() { - // If the user tried to sign in on the sign up page with an existing email, - // place that email in the email field, then focus the password. - var email = window.localStorage.signInEmail; - if (email) { - $("#email").val(email); - window.localStorage.removeItem('signInEmail'); - $("#password").focus(); - } - } - bid.signIn = function () { $("form input[autofocus]").focus(); - prefillEmail(); + pageHelpers.setupEmail(); $("#signUpForm").bind("submit", function(event) { event.preventDefault(); @@ -68,6 +58,7 @@ if (valid) { user.authenticate(email, password, function onSuccess(authenticated) { if (authenticated) { + pageHelpers.clearStoredEmail(); document.location = "/"; } else { diff --git a/resources/static/js/pages/signup.js b/resources/static/js/pages/signup.js index c60e3f01e..429fbccc6 100644 --- a/resources/static/js/pages/signup.js +++ b/resources/static/js/pages/signup.js @@ -39,10 +39,10 @@ var bid = BrowserID, user = bid.User, + pageHelpers = bid.PageHelpers, ANIMATION_SPEED = 250; bid.signUp = function () { - function replaceWithMessage(selector) { $('.forminputs').fadeOut(ANIMATION_SPEED, function() { $(selector).fadeIn(ANIMATION_SPEED); @@ -61,6 +61,8 @@ $(function () { $("form input[autofocus]").focus(); + pageHelpers.setupEmail(); + $("#email").bind("keyup", function(event) { if (event.which !== 13) { $(".notification").fadeOut(ANIMATION_SPEED); @@ -79,6 +81,7 @@ user.isEmailRegistered(email, function(registered) { if (!registered) { + pageHelpers.clearStoredEmail(); user.createUser(email, function onSuccess(keypair) { $('#sentToEmail').html(email); replaceWithMessage(".emailsent"); @@ -87,7 +90,6 @@ else { $('#registeredEmail').html(email); showNotice(".alreadyRegistered"); - window.localStorage.signInEmail = email; } }, onFailure); }); diff --git a/resources/views/layout.ejs b/resources/views/layout.ejs index 0a5c452ca..af3a352a3 100644 --- a/resources/views/layout.ejs +++ b/resources/views/layout.ejs @@ -19,6 +19,8 @@ <script src="/js/json2.js" type="text/javascript"></script> <script src="/dialog/resources/underscore-min.js" type="text/javascript"></script> <script src="/dialog/resources/browserid-extensions.js" type="text/javascript"></script> + <script src="/dialog/resources/browserid.js" type="text/javascript"></script> + <script src="/js/page_helpers.js" type="text/javascript"></script> <script src="/js/browserid.js" type="text/javascript"></script> <script src="/js/pages/index.js" type="text/javascript"></script> <script src="/dialog/resources/storage.js" type="text/javascript"></script> diff --git a/scripts/compress.sh b/scripts/compress.sh index 4ef16ff38..6233646e5 100755 --- a/scripts/compress.sh +++ b/scripts/compress.sh @@ -50,7 +50,7 @@ echo '' cd ../js # re-minimize everything together -cat jquery-1.6.2.min.js json2.js browserid.js ../dialog/resources/underscore-min.js ../dialog/resources/browserid-extensions.js ../dialog/resources/storage.js ../dialog/resources/network.js ../dialog/resources/user.js ../dialog/resources/tooltip.js ../dialog/resources/validation.js pages/index.js pages/add_email_address.js pages/verify_email_address.js pages/manage_account.js pages/signin.js pages/signup.js pages/forgot.js > lib.js +cat jquery-1.6.2.min.js json2.js ../dialog/resources/browserid.js page_helpers.js browserid.js ../dialog/resources/underscore-min.js ../dialog/resources/browserid-extensions.js ../dialog/resources/storage.js ../dialog/resources/network.js ../dialog/resources/user.js ../dialog/resources/tooltip.js ../dialog/resources/validation.js pages/index.js pages/add_email_address.js pages/verify_email_address.js pages/manage_account.js pages/signin.js pages/signup.js pages/forgot.js > lib.js $UGLIFY < lib.js > lib.min.js cd ../css -- GitLab