diff --git a/browserid/static/js/browserid.js b/browserid/static/js/browserid.js index d833af3613eb847d85293005be5dca4a1d622a6d..944ef1b58749b6b0a7c6fab61669ad0af0e7c2bd 100644 --- a/browserid/static/js/browserid.js +++ b/browserid/static/js/browserid.js @@ -1,4 +1,4 @@ -/*globals BrowserIDNetwork: true, BrowserIDIdentities: true, _: true, confirm: true, getEmails: true, display_saved_ids: true, displayEmails: true, removeEmail: true*/ +/*globals BrowserIDNetwork: true, BrowserIDIdentities: true, _: true, confirm: true, getEmails: true, display_saved_ids: true, removeEmail: true*/ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -37,82 +37,75 @@ "use strict"; $(function() { - BrowserIDNetwork.checkAuth(function(authenticated) { + BrowserIDIdentities.checkAuthenticationAndSync(function onSuccess(authenticated) { if (authenticated) { $("body").addClass("authenticated"); - if ($('#emailList').length) { - display_saved_ids(); - } } + }, function onComplete(authenticated) { + if (authenticated && $('#emailList').length) { + display_saved_ids(); + } }); }); function display_saved_ids() { - var emails = {}; - BrowserIDIdentities.syncIdentities(function() { - emails = getEmails(); - displayEmails(); + $('#cancellink').click(function() { + if (confirm('Are you sure you want to cancel your account?')) { + BrowserIDIdentities.cancelUser(function() { + document.location="/"; + }); + } }); + $("#emailList").empty(); + var emails = BrowserIDIdentities.getStoredIdentities(); + _(emails).each(function(data, e) { + var block = $("<div>").addClass("emailblock"); + var label = $("<div>").addClass("email").text(e); + var meta = $("<div>").addClass("meta"); - function displayEmails() { - $('#cancellink').click(function() { - if (confirm('Are you sure you want to cancel your account?')) { - BrowserIDIdentities.cancelUser(function() { - document.location="/"; - }); - } - }); - - $("#emailList").empty(); - _(emails).each(function(data, e) { - var block = $("<div>").addClass("emailblock"); - var label = $("<div>").addClass("email").text(e); - var meta = $("<div>").addClass("meta"); - - var pub = $("<div class='keyblock'>").hide(); - - var keyText = data.pub.value; - pub.text(keyText); + var pub = $("<div class='keyblock'>").hide(); + + var keyText = data.pub.value; + pub.text(keyText); - var linkblock = $("<div>"); - var puba = $("<a>").text("[show public key]"); - // var priva = $("<a>").text("[show private key]"); - puba.click(function() {pub.show();}); - // priva.click(function() {priv.show()}); - linkblock.append(puba); - // linkblock.append(" / "); - // linkblock.append(priva); - - var deauth = $("<button>").text("Forget this Email"); - meta.append(deauth); - deauth.click(function(data) { - // If it is a primary, we do not have to go back to the server. - // XXX put this into the BrowserIDIdentities abstraction - if (data.isPrimary) { - removeEmail(e); - display_saved_ids(); - } - else { - // remove email from server - BrowserIDNetwork.removeEmail(e, display_saved_ids); - } - }.bind(null, data)); - - var d = new Date(data.created); - var datestamp = $("<div class='date'>").text("Signed in at " + d.toLocaleString()); + var linkblock = $("<div>"); + var puba = $("<a>").text("[show public key]"); + // var priva = $("<a>").text("[show private key]"); + puba.click(function() {pub.show();}); + // priva.click(function() {priv.show()}); + linkblock.append(puba); + // linkblock.append(" / "); + // linkblock.append(priva); + + var deauth = $("<button>").text("Forget this Email"); + meta.append(deauth); + deauth.click(function(data) { + // If it is a primary, we do not have to go back to the server. + // XXX put this into the BrowserIDIdentities abstraction + if (data.isPrimary) { + BrowserIDStorage.removeEmail(e); + display_saved_ids(); + } + else { + // remove email from server + BrowserIDIdentities.removeIdentity(e, display_saved_ids); + } + }.bind(null, data)); + + var d = new Date(data.created); + var datestamp = $("<div class='date'>").text("Signed in at " + d.toLocaleString()); - meta.append(datestamp); - meta.append(linkblock); - - block.append(label); - block.append(meta); - // block.append(priv); - block.append(pub); - - $("#emailList").append(block); - }); - } + meta.append(datestamp); + meta.append(linkblock); + + block.append(label); + block.append(meta); + // block.append(priv); + block.append(pub); + + $("#emailList").append(block); + }); } }());