Commits

Luke Plant  committed d845012

Implemented 'logout' button and corresponding bar.

  • Participants
  • Parent commits a6cc3df

Comments (0)

Files changed (3)

File lukeplant_me_uk/bibleverses/templates/base.html

 <div id="footer">
  Site by <a href="http://lukeplant.me.uk">Luke Plant</a>
 </div>
-<div id="userbar" class="hidden offscreen">
+
+<div id="userbar1" class="hidden offscreen">
   <input name="register1" id="register1" type="submit" value="Register" />
   <input name="login1" id="login1" type="submit" value="Login" />
 </div>
+
+<div id="userbar2" class="hidden offscreen">
+  <span id="username"></span>
+  <input name="logout" id="logout" type="submit" value="Logout" />
+</div>
+
 <div id="registerblock" class="hidden offscreen">
 <h2>Register</h2>
 <p>Enter your email address and choose a password.  This is used to

File lukeplant_me_uk/bibleverses/web/css/style.css

     padding-top: 0.5em;
     padding-bottom: 0.5em;
 }
+
+#username {
+    font-family: verdana, sans-serif;
+    color: white;
+    font-size: x-small;
+    font-style: italic;
+    margin-top: -2px;
+}

File lukeplant_me_uk/bibleverses/web/js/bibleverses.js

 var rootUrl = "/bibleverses/";
 var cgiRoot = rootUrl + "cgi-bin/main.cgi/";
 
+function readCookie(name) {
+        var nameEQ = name + "=";
+        var ca = document.cookie.split(';');
+        for(var i=0;i < ca.length;i++) {
+                var c = ca[i];
+                while (c.charAt(0)==' ') c = c.substring(1,c.length);
+                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
+        }
+        return null;
+}
+
+function eraseCookie(name) {
+    setCookie(name, "", new Date(2000,1,1,0,0,0,0))
+}
+
 function encodeObj(obj) {
     var ks = keys(obj);
     var val = "";
 function doPostRequest(method, data) {
     var encoded = encodeObj(data);
     logDebug("Data to send: " + encoded);
-    d = doXHR(cgiRoot + method + "/",
+    var d = doXHR(cgiRoot + method + "/",
 	  {
 	      method: 'POST',
 	      sendContent: encoded,
     d.setFullYear(d.getFullYear()+100);
     setCookie("email", email, d);
     setCookie("password", password, d);
+    showCorrectUserBar();
 }
 
 function registerResult(req) {
 	showElement($('regclosediv'));
 	setLoginCookie($('id_reg_email').value, $('id_reg_password').value);
     } else {
-	fail = $('regresultfail');
-	logDebug(repr(fail));
+	var fail = $('regresultfail');
 	if (json.validation) {
 	    logDebug("Validation: " + repr(json.validation));
 	    replaceChildNodes(fail, json.validation[1]);
 	showElement($('loginclosediv'));
 	setLoginCookie($('id_login_email').value, $('id_login_password').value);
     } else {
-	fail = $('loginresultfail');
-	logDebug(repr(fail));
+	var fail = $('loginresultfail');
 	if (json.validation) {
 	    logDebug("Validation: " + repr(json.validation));
 	    replaceChildNodes(fail, json.validation[1]);
     hideElement($('loginblock'));
 }
 
-function addUserbar() {
-    var userbar = $('userbar');
+function onLogout(ev) {
+    eraseCookie('email');
+    addUserBar1();
+}
+
+function showUserBar(bar) {
     // easiest way to get layout correct is to absolutely 
-    // position this element
+    // position these elements
     var navbar_pos = getElementPosition($('navbarcont'));
     var navbar_dim = getElementDimensions($('navbarcont'));
-    showElement(userbar);
-    var userbar_dim = getElementDimensions(userbar);
-    var userbar_pos = 
-	{x: navbar_pos.x + navbar_dim.w - userbar_dim.w - 1,
-	 y: navbar_pos.y + 1};
-    logDebug("x: " + userbar_pos.x);
-    setElementPosition(userbar, userbar_pos);
+    showElement(bar);
+    var bar_dim = getElementDimensions(bar);
+    var bar_pos = 
+	{x: navbar_pos.x + navbar_dim.w - bar_dim.w - 4,
+	 y: navbar_pos.y + 5};
+    logDebug("x: " + bar_pos.x);
+    setElementPosition(bar, bar_pos);
+}
 
+function addUserBar1() {
+    hideElement($('userbar2'));
+    showUserBar($('userbar1'));
+}
+
+function addUserBar2(email) {
+    replaceChildNodes($('username'), email);
+    hideElement($('userbar1'));
+    showUserBar($('userbar2'));    
+}
+
+function showCorrectUserBar() {
+    email = get_email();
+    if (email != null && email != '') {
+	addUserBar2(email);
+    } else {
+	addUserBar1();
+    }
+}
+function get_email() {
+    var email = readCookie('email');
+    if (email == null) {
+	return null;
+    } else {
+	return decodeURIComponent(email);
+    }
 }
 
 connect(window, 'onload',
         function(ev) {
-            addUserbar();
+	    showCorrectUserBar();
 	    connect($('register1'), 'onclick', onRegister1);
 	    connect($('register2'), 'onclick', onRegister2);
 	    connect($('cancelreg'), 'onclick', onCancelReg);
 	    connect($('login2'), 'onclick', onLogin2);
 	    connect($('cancellogin'), 'onclick', onCancelLogin);
 	    connect($('closelogin'), 'onclick', onCloseLogin);
+	    connect($('logout'), 'onclick', onLogout);
         }
-       );
-
+       );