# HG changeset patch # User Atul Varma # Date 1272156980 25200 # Node ID 7a1162b1b7e765c23ef08b17bb37a3cb69d6a718 # Parent 2a378aa5206318edb1a6ae3ba935bb7400a41048 Added app module diff -r 2a378aa52063 -r 7a1162b1b7e7 js/main.js --- a/js/main.js Sat Apr 24 15:16:02 2010 -0700 +++ b/js/main.js Sat Apr 24 17:56:20 2010 -0700 @@ -2,53 +2,62 @@ function() { const BASE_TITLE = document.title; - function onLoginChange() { - var username = $("#login-username").val(); - var password = $("#login-password").val(); - var title = BASE_TITLE; + var require = Require.build(); + + require("app/login").whenChanged( + function changeTitle(user) { + var title = BASE_TITLE; + + if (user.isLoggedIn) + title = user.username + "'s " + BASE_TITLE; - if (username) { - $("#login").fadeOut(); - title = username + "'s " + BASE_TITLE; + if (document.title != title) { + document.title = title; + $("#header .title").text(title); + } + }); - $(".requires-no-login").hide(); - $(".requires-login").show(); - if (password) { - $(".requires-auth-login").show(); + require("app/login").whenChanged( + function changeUI(user) { + if (user.isLoggedIn) { + $(".requires-no-login").hide(); + $(".requires-login").show(); + if (user.isAuthenticated) { + $(".requires-auth-login").show(); + } else { + $(".requires-auth-login").hide(); + } } else { + $(".requires-no-login").show(); + $(".requires-login").hide(); $(".requires-auth-login").hide(); } - } else { - $("#login").fadeIn(); - $(".requires-no-login").show(); - $(".requires-login").hide(); - $(".requires-auth-login").hide(); - } - - if (document.title != title) { - document.title = title; - $("#header .title").text(title); - } - } + }); $("#header .menu li").click( - function(event) { + function openDialog(event) { var dialog = $("#" + this.title); if (dialog.length == 0) throw new Error("dialog not found: " + this.title); dialog.fadeIn(); }); + $(".dialog").click( + function dismissDialogOnOutsideClick(event) { + if (event.target == this) + $(this).fadeOut(); + }); + $("#login-form").submit( function(event) { event.preventDefault(); - onLoginChange(); + require("app/login").set($("#login-username").val(), + $("#login-password").val()); + $("#login").fadeOut(); }); - $(".dialog").click( - function(event) { - if (event.target == this) - $(this).fadeOut(); - }); - onLoginChange(); + require("app/login").set($("#login-username").val(), + $("#login-password").val()); + if (!require("app/login").isLoggedIn()) + $("#login").fadeIn(); }); diff -r 2a378aa52063 -r 7a1162b1b7e7 js/modules/app.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/js/modules/app.js Sat Apr 24 17:56:20 2010 -0700 @@ -0,0 +1,32 @@ +Require.modules["app/login"] = function(exports) { + var callbacks = []; + var username; + var password; + + exports.whenChanged = function whenChanged(cb) { + callbacks.push(cb); + }; + + exports.isLoggedIn = function isLoggedIn() { + return (username != ""); + }; + + exports.set = function set(newUsername, newPassword) { + if (newUsername == username && newPassword == password) + return; + + username = newUsername; + password = newPassword; + + var isLoggedIn = (username != ""); + var isAuthenticated = (username != "" && password != ""); + + callbacks.forEach( + function(cb) { + cb({username: username, + password: password, + isLoggedIn: isLoggedIn, + isAuthenticated: isAuthenticated}); + }); + }; +}; diff -r 2a378aa52063 -r 7a1162b1b7e7 main.html --- a/main.html Sat Apr 24 15:16:02 2010 -0700 +++ b/main.html Sat Apr 24 17:56:20 2010 -0700 @@ -47,5 +47,5 @@ - +