Newer
Older
/* 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/. */
$(function() {
Shane Tomlinson
committed
/**
* For the main page
*/
Shane Tomlinson
committed
var bid = BrowserID,
helpers = bid.Helpers,
Shane Tomlinson
committed
pageHelpers = bid.PageHelpers,
user = bid.User,
xhr = bid.XHR,
network = bid.Network,
Shane Tomlinson
committed
token = pageHelpers.getParameterByName("token"),
Shane Tomlinson
committed
path = document.location.pathname,
moduleManager = bid.module,
modules = bid.Modules,
CookieCheck = modules.CookieCheck,
XHRDisableForm = modules.XHRDisableForm,
Shane Tomlinson
committed
ANIMATION_TIME = 500,
checkCookiePaths = [ "/signin", "/signup", "/forgot", "/add_email_address", "/verify_email_address" ];
Shane Tomlinson
committed
Shane Tomlinson
committed
function shouldCheckCookies(path) {
if (path) {
// IE6 and IE7 will blow up if trying to use indexOf on the array.
for(var i = 0, checkCookiePath; checkCookiePath = checkCookiePaths[i]; ++i) {
if (checkCookiePath === path) return true;
}
}
}
xhr.init({ time_until_delay: 10 * 1000 });
network.init();
Shane Tomlinson
committed
$(".display_always,.display_auth,.display_nonauth").hide();
if ($('#vAlign').length) {
$(window).bind('resize', function() {
var height = $(window).height() - $("header").outerHeight() - $("footer").outerHeight();
$('#vAlign').css({'height' : height });
}).trigger('resize');
}
moduleManager.register("xhr_delay", XHRDelay);
moduleManager.start("xhr_delay");
moduleManager.register("xhr_disable_form", XHRDisableForm);
moduleManager.start("xhr_disable_form");
Shane Tomlinson
committed
if(shouldCheckCookies(path)) {
// do a cookie check on every page except the main page.
moduleManager.register("cookie_check", CookieCheck);
moduleManager.start("cookie_check", { ready: start });
}
else {
// the main page makes it through without checking for cookies.
start(true);
}
Shane Tomlinson
committed
// If cookies are disabled, do not run any of the page specific code and
// instead just show the error message.
if(!status) return;
dom.addClass("body", "ready");
if (!path || path === "/") {
bid.index();
}
else if (path === "/signin") {
var module = bid.signIn.create();
module.start({});
}
else if (path === "/signup") {
var module = bid.signUp.create();
module.start({});
}
else if (path === "/forgot") {
bid.forgot();
}
else if (path === "/add_email_address") {
Shane Tomlinson
committed
var module = bid.verifySecondaryAddress.create();
Shane Tomlinson
committed
token: token,
verifyFunction: "verifyEmail"
Shane Tomlinson
committed
});
}
else if(path === "/verify_email_address") {
Shane Tomlinson
committed
var module = bid.verifySecondaryAddress.create();
module.start({
token: token,
verifyFunction: "verifyUser"
});
else {
// Instead of throwing a hard error here, adding a message to the console
// to let developers know something is up.
helpers.log("unknown path");
}
user.checkAuthentication(function(authenticated) {
if (authenticated) {
displayAuthenticated();
}
else {
displayNonAuthenticated();
}
});
function displayAuthenticated() {
$(".display_always,.display_auth").fadeIn(ANIMATION_TIME);
dom.addClass("body", "authenticated");
if (!path || path === "/") {
bid.manageAccount();
}
$("a.signOut").click(function(event) {
event.preventDefault();
event.stopPropagation();
user.logoutUser(function() {
document.location = "/";
}, pageHelpers.getFailure(bid.Errors.logout));
});
Shane Tomlinson
committed
}
function displayNonAuthenticated() {
$(".display_always").fadeIn(ANIMATION_TIME);
dom.addClass("body", "not_authenticated");
$(".display_nonauth").fadeIn(ANIMATION_TIME);
}
Shane Tomlinson
committed
}