changeset 631:f36d1a168be7

For issue 27, disable login dialog button during POST. This seems to prevent multiple logins most of the time. You can still bang on the enter key and sometimes get more through.
author Brian Neal <bgneal@gmail.com>
date Wed, 14 Nov 2012 20:57:05 -0600
parents 63603e931503
children a5b8f25e1752
files accounts/static/js/ajax_login.js
diffstat 1 files changed, 54 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/accounts/static/js/ajax_login.js	Wed Nov 14 17:47:01 2012 -0600
+++ b/accounts/static/js/ajax_login.js	Wed Nov 14 20:57:05 2012 -0600
@@ -7,55 +7,69 @@
       height: 375,
       width: 380,
       modal: true,
-      buttons: {
-         "Login": function() {
-            loginError.text('').hide();
-            $.ajax({
-               url: '/accounts/login/ajax/',
-               type: 'POST',
-               data: {
-                  username: userBox.val(),
-                  password: passBox.val(),
-                  csrfmiddlewaretoken: csrf_token
-               },
-               dataType: 'json',
-               success: function(data, textStatus) {
-                  if (data.success) {
-                     loginDialog.dialog("close");
-                     if (window.location.pathname == "/accounts/logout/") {
-                        window.location.replace("/");
+      buttons: [
+         {
+            id: "login-button",
+            text: "Login",
+            click: function() {
+               loginError.text('').hide();
+               $("#login-button").button("disable");
+               $.ajax({
+                  url: '/accounts/login/ajax/',
+                  type: 'POST',
+                  data: {
+                     username: userBox.val(),
+                     password: passBox.val(),
+                     csrfmiddlewaretoken: csrf_token
+                  },
+                  dataType: 'json',
+                  success: function(data, textStatus) {
+                     $("#login-button").button("enable");
+                     if (data.success) {
+                        loginDialog.dialog("close");
+                        if (window.location.pathname == "/accounts/logout/") {
+                           window.location.replace("/");
+                        }
+                        else {
+                           $('#header-nav').html(data.navbar_html);
+                        }
                      }
                      else {
-                        $('#header-nav').html(data.navbar_html);
+                        loginError.text(data.error).show();
+                        userBox.val('');
+                        passBox.val('');
+                        userBox.focus();
+                     }
+                  },
+                  error: function (xhr, textStatus, ex) {
+                     $("#login-button").button("enable");
+                     if (xhr.status == 403) {
+                        loginDialog.dialog("close");
+                        alert("Oops, we are detecting some strange behavior and are blocking this action. If you feel this is an error, please feel free to contact us. Thank you.");
+                        window.location.href = "/";
+                     }
+                     else {
+                        loginError.text('Oops, an error occurred. If this problem persists, please contact us.').show();
                      }
                   }
-                  else {
-                     loginError.text(data.error).show();
-                     userBox.val('');
-                     passBox.val('');
-                     userBox.focus();
-                  }
-               },
-               error: function (xhr, textStatus, ex) {
-                  if (xhr.status == 403) {
-                     loginDialog.dialog("close");
-                     alert("Oops, we are detecting some strange behavior and are blocking this action. If you feel this is an error, please feel free to contact us. Thank you.");
-                     window.location.href = "/";
-                  }
-                  else {
-                     loginError.text('Oops, an error occurred. If this problem persists, please contact us.').show();
-                  }
-               }
-            });
+               });
+            }
          },
-         "Cancel": function() {
-            loginDialog.dialog("close");
+         {
+            id: "cancel-button",
+            text: "Cancel",
+            click: function() {
+               loginDialog.dialog("close");
+            }
          }
-      },
+      ],
       focus: function() {
          $(':input', this).keyup(function(event) {
             if (event.keyCode == 13) {
-                $('.ui-dialog-buttonpane button:first').click();
+                var loginButton = $("#login-button");
+                if (!loginButton.button("option", "disabled")) {
+                   loginButton.click();
+                }
             }
          });
       }