Pressing enter/return in the openid url box submits the form
[citadel.git] / webcit / static / authmethods.js
index 3709669d0151e00bbe95789d44942790f5fdb26f..8ad1e29fa14c44cb5d7bc9af8a2e29c4e3d799a3 100644 (file)
@@ -1,11 +1,15 @@
 /*
- * Copyright 2010, the Citadel Team
+ * Copyright 2010-2011, the Citadel Team
  * Licensed under the GPL V3
  *
  * JavaScript functions which handle various authentication methods.
  */
 
 
+
+/****************** COMMON CODE ***********************/
+
+
 /*
  * Are we logged in right now?
  */
@@ -19,7 +23,6 @@ function IsLoggedIn() {
 }
 
 
-
 /*
  * Wrapper script to require logging in before completing an action
  */
@@ -27,18 +30,20 @@ function GetLoggedInFirst(destination_url) {
 
        /* If logged in already, go directly to the destination. */
        if (IsLoggedIn()) {
-               window.location = destination_url;
+               window.location = decodeURIComponent(destination_url);
                return;
        }
 
+       p = 'push?url=' + destination_url;
+       new Ajax.Request(p, { method: 'get' } );
+
        /* If not logged in, go modal and ask the user to log in first. */
-       p = 'do_template?template=get_logged_in?destination_url=' + destination_url;
        new Ajax.Updater(
                'md-content',
-               p,
+               'do_template?template=get_logged_in',
                 {
                         method: 'get',
-                       onSuccess: function(cl_success) {
+                       onSuccess: function() {
                                toggleModal(1);
                        }
                 }
@@ -46,15 +51,40 @@ function GetLoggedInFirst(destination_url) {
 }
 
 
+/*
+ * tab handler for the login box
+ */
+function authtoggle(show_which_div) {
+       $('authbox_userpass').style.display = 'none';
+       $('authbox_newuser').style.display = 'none';
+       $('authbox_openid').style.display = 'none';
+       $(show_which_div).style.display = 'block';
+}
+
+
+/*
+ * Pop out a window for external auth methods
+ * (most of them don't handle inline auth very well)
+ */
+function do_auth_popout(popout_url) {
+       window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
+}
+
+
+
+
+/****************** USERNAME AND PASSWORD ***********************/
+
 /*
  * Attempt login with username/password, called from modal dialog
  */
-function ajax_try_username_and_password(destination_url) {
+function ajax_try_username_and_password() {
+
        $('login_errmsg').innerHTML = "";
         $('ajax_username_password_form').request({
                onSuccess: function(ctdlresult) {
                        if (ctdlresult.responseText.substr(0,1) == '2') {
-                               window.location = destination_url;
+                               window.location = 'pop';
                        }
                        else {
                                $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
@@ -63,11 +93,90 @@ function ajax_try_username_and_password(destination_url) {
        });
 }
 
+
 /*
- * tab handler for the login box
+ * The user pressed a key while in the username or password box.
+ * Is it the enter/return key?  Submit the form.
  */
-function authtoggle(show_which_div) {
-       $('authbox_userpass').style.display = 'none';
-       $('authbox_openid').style.display = 'none';
-       $(show_which_div).style.display = 'block';
+function username_and_password_onkeypress(e) {
+       if (window.event) {             /* IE */
+               keynum = e.keyCode
+       }
+       else if (e.which) {             /* real browsers */
+               keynum = e.which
+       }
+       if (keynum == 13) {             /* enter/return key */
+               ajax_try_username_and_password();
+       }
+}
+
+
+/****************** REGISTER NEW USER ***********************/
+
+/*
+ * Attempt to create a new local username/password, called from modal dialog
+ */
+function ajax_try_newuser() {
+
+       $('login_errmsg').innerHTML = "";
+        $('ajax_newuser_form').request({
+               onSuccess: function(ctdlresult) {
+                       if (ctdlresult.responseText.substr(0,1) == '2') {
+                               window.location = 'pop';
+                       }
+                       else {
+                               $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
+                       }
+               }
+       });
+}
+
+
+/*
+ * The user pressed a key while in the newuser or newpassword box.
+ * Is it the enter/return key?  Submit the form.
+ */
+function newuser_onkeypress(e) {
+       if (window.event) {             /* IE */
+               keynum = e.keyCode;
+       }
+       else if (e.which) {             /* real browsers */
+               keynum = e.which;
+       }
+       if (keynum == 13) {             /* enter/return key */
+               ajax_try_newuser();
+       }
 }
+
+
+
+
+/****************** OPENID ***********************/
+
+/*
+ * Attempt login with OpenID, called from modal dialog
+ */
+function ajax_try_openid() {
+       $('login_errmsg').innerHTML = "";
+       openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
+       do_auth_popout("openid_login?openid_url=" + openid_url);
+}
+
+
+/*
+ * The user pressed a key while in the openid login box.
+ * Is it the enter/return key?  Submit the form.
+ */
+function openid_onkeypress(e) {
+       if (window.event) {             /* IE */
+               keynum = e.keyCode;
+       }
+       else if (e.which) {             /* real browsers */
+               keynum = e.which;
+       }
+       if (keynum == 13) {             /* enter/return key */
+               ajax_try_openid();
+       }
+}
+
+