Newer
Older
/*globals BrowserID: true, _: true, confirm: true, format: true, gettext: true, EJS: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Shane Tomlinson
committed
Shane Tomlinson
committed
"use strict";
Shane Tomlinson
committed
network = bid.Network,
dom = bid.DOM,
Shane Tomlinson
committed
storage = bid.Storage,
helpers = bid.Helpers,
cancelEvent = pageHelpers.cancelEvent,
complete = helpers.complete,
Shane Tomlinson
committed
tooltip = bid.Tooltip,
authLevel;
function syncAndDisplayEmails(oncomplete) {
displayStoredEmails.call(self, oncomplete);
}, pageHelpers.getFailure(errors.syncEmails, oncomplete));
Shane Tomlinson
committed
}
function displayStoredEmails(oncomplete) {
var emails = user.getSortedEmailKeypairs();
if (_.isEmpty(emails)) {
dom.show("#content");
dom.hide("#vAlign");
renderEmails.call(this, emails);
Shane Tomlinson
committed
function removeEmail(email, oncomplete) {
user.syncEmails(function() {
var emails = user.getStoredEmailKeypairs();
if (!emails[email]) {
displayStoredEmails.call(self, oncomplete);
Shane Tomlinson
committed
}
else if (_.size(emails) > 1) {
if (confirmAction(format(gettext("Remove %(email) from your Persona account?"),
Lloyd Hilaiel
committed
{ email: email }))) {
user.removeEmail(email, function() {
displayStoredEmails.call(self, oncomplete);
}, pageHelpers.getFailure(errors.removeEmail, oncomplete));
}
else {
}
else {
if (confirmAction(gettext("Removing the last address will cancel your Persona account.\nAre you sure you want to continue?"))) {
user.cancelUser(function() {
doc.location="/";
}, pageHelpers.getFailure(errors.cancelUser, oncomplete));
}
else {
Shane Tomlinson
committed
}
}, pageHelpers.getFailure(errors.syncEmails, oncomplete));
Shane Tomlinson
committed
}
function renderEmails(emails) {
var self=this,
list = dom.getElements("#emailList");
dom.setInner(list, "");
Shane Tomlinson
committed
// Set up to use mustache style templating, the normal Django style blows
Shane Tomlinson
committed
// up the node templates
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
var template = dom.getInner("#templateUser");
Shane Tomlinson
committed
_(emails).each(function(item) {
var e = item.address,
identity = _.template(template, { email: e });
Shane Tomlinson
committed
var idEl = dom.appendTo(identity, list),
deleteButton = dom.getDescendentElements(".delete", idEl);
self.click(deleteButton, removeEmail.curry(e));
Shane Tomlinson
committed
});
}
function cancelAccount(oncomplete) {
if (confirmAction(gettext("Are you sure you want to cancel your Persona account?"))) {
user.cancelUser(function() {
doc.location="/";
}, pageHelpers.getFailure(errors.cancelUser, oncomplete));
Shane Tomlinson
committed
function startEdit(event) {
event.preventDefault();
dom.addClass(dom.closest("section", event.target), "edit");
Shane Tomlinson
committed
function cancelEdit(event) {
event.preventDefault();
dom.removeClass(dom.closest("section", event.target), "edit");
function submit(oncomplete) {
var oldPassword = dom.getInner("#old_password"),
newPassword = dom.getInner("#new_password");
Shane Tomlinson
committed
function changePassword() {
user.changePassword(oldPassword, newPassword, function(status) {
if(status) {
dom.removeClass("#edit_password", "edit");
dom.setInner("#old_password", "");
dom.setInner("#new_password", "");
Shane Tomlinson
committed
}
else {
tooltip.showTooltip("#tooltipInvalidPassword");
}
complete(oncomplete, status);
Shane Tomlinson
committed
}, pageHelpers.getFailure(errors.updatePassword, oncomplete));
}
if(!oldPassword) {
tooltip.showTooltip("#tooltipOldRequired");
complete(oncomplete, false);
else if(oldPassword.length < bid.PASSWORD_MIN_LENGTH || bid.PASSWORD_MAX_LENGTH < oldPassword.length) {
// If the old password is out of range, we know it is invalid. Show the
// tooltip. See issue #2121
// - https://github.com/mozilla/browserid/issues/2121
tooltip.showTooltip("#tooltipInvalidPassword");
complete(oncomplete, false);
else if(!newPassword) {
tooltip.showTooltip("#tooltipNewRequired");
complete(oncomplete, false);
else if(newPassword === oldPassword) {
tooltip.showTooltip("#tooltipPasswordsSame");
complete(oncomplete, false);
else if(newPassword.length < bid.PASSWORD_MIN_LENGTH || bid.PASSWORD_MAX_LENGTH < newPassword.length) {
Shane Tomlinson
committed
tooltip.showTooltip("#tooltipPasswordLength");
complete(oncomplete, false);
Shane Tomlinson
committed
else if(authLevel !== "password") {
var email = getSecondary();
// go striaght to the network level instead of user level so that if
// the user gets the password wrong, we don't clear their info.
network.authenticate(email, oldPassword, function(status) {
Shane Tomlinson
committed
authLevel = "password";
changePassword();
}
else {
tooltip.showTooltip("#tooltipInvalidPassword");
complete(oncomplete, false);
Shane Tomlinson
committed
}, pageHelpers.getFailure(errors.authenticate, oncomplete));
}
else {
changePassword();
Shane Tomlinson
committed
Shane Tomlinson
committed
function displayHelpTextToNewUser() {
var newUser = !storage.manage_page.get("has_visited_manage_page");
dom[newUser ? "addClass" : "removeClass"]("body", "newuser");
storage.manage_page.set("has_visited_manage_page", true);
}
Shane Tomlinson
committed
function displayChangePassword(oncomplete) {
Shane Tomlinson
committed
var canSetPassword = !!getSecondary();
dom[canSetPassword ? "addClass" : "removeClass"]("body", "canSetPassword");
Shane Tomlinson
committed
}
function getSecondary() {
var emails = storage.getEmails();
for(var key in emails) {
if(emails[key].type === "secondary") {
return key;
}
}
Shane Tomlinson
committed
}
var Module = bid.Modules.PageModule.extend({
start: function(options) {
options = options || {};
if (options.document) doc = options.document;
if (options.confirm) confirmAction = options.confirm;
var self=this,
oncomplete = options.ready,
manage = dom.getInner("#templateManage") });
dom.insertAfter(manage, "#hAlign");
self.click("#cancelAccount", cancelAccount);
Shane Tomlinson
committed
self.bind("button.edit", "click", startEdit);
self.bind("button.done", "click", cancelEdit);
Shane Tomlinson
committed
user.checkAuthentication(function(auth_level) {
authLevel = auth_level;
syncAndDisplayEmails.call(self, function() {
displayHelpTextToNewUser();
displayChangePassword(oncomplete);
});
}, pageHelpers.getFailure(errors.checkAuthentication, oncomplete));
Module.sc.start.call(self, options);
},
submit: submit
// BEGIN TESTING API
,
cancelAccount: cancelAccount,
removeEmail: removeEmail,