Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

submit login/signup/settings forms with enter key

  • Loading branch information...
commit dc33fd9bbb25f27ee85c70a02cfcc75485a32f33 1 parent 4447f32
@beerriot beerriot authored
View
124 apps/wriaki/priv/www/js/wriaki.js
@@ -62,13 +62,72 @@ function loginSignupSuccess() {
}
}
+function submitLogin() {
+ var username = $('input[name=login_username]').val();
+ var password = $('input[name=login_password]').val();
+
+ $.ajax({
+ url:'/user/'+username,
+ type:'POST',
+ data:{'password':password},
+ success:loginSignupSuccess,
+ error:function(req) {
+ $('#loginerror').text('incorrect username/password combination');
+ }
+ });
+}
+
+function submitSettings() {
+ var data = {};
+
+ var p = $('input[name=password]').val();
+ if (p) data.password = p;
+
+ var e = $('input[name=email]').val();
+ if (e) data.email = e;
+
+ var b = $('input[name=bio]').val();
+ if (b) data.bio = b;
+
+ var u = $('input[name=username]');
+ if (u.length) {
+ data.username = u.val();
+ if (!data.username) {
+ alert("Please choose a username.");
+ return;
+ }
+ if (!data.password) {
+ alert("Please choose a password.");
+ return;
+ }
+ }
+
+ req = {
+ url: data.username ? '/user/'+data.username : window.location.href,
+ type: 'PUT',
+ data: data,
+ success: loginSignupSuccess,
+ error: function(req) {
+ if (req.status == 409)
+ $('#settingserror').text('the requested username is taken');
+ else
+ $('#settingserror').text('an unknown error occured: '+req.responseText);
+ }
+ };
+ $.ajax(req);
+}
+
+function onEnter(handler) {
+ return function(e) {
+ if (e.keyCode == 10 || e.keyCode == 13)
+ handler();
+ };
+}
+
$(function() {
/* Header search buttons */
$('#searchbutton').click(navToSearch);
- $('#searchtext').keyup(function(e) {
- if (e.keyCode == 10 || e.keyCode == 13)
- navToSearch();
- });
+ $('#searchtext').keyup(onEnter(navToSearch));
/* Article editor buttons */
$('#editcancel').click(function() {
@@ -90,60 +149,11 @@ $(function() {
});
/* User settings buttons */
- $('#settingsave').click(function() {
- var data = {};
-
- var p = $('input[name=password]').val();
- if (p) data.password = p;
-
- var e = $('input[name=email]').val();
- if (e) data.email = e;
-
- var b = $('input[name=bio]').val();
- if (b) data.bio = b;
-
- var u = $('input[name=username]');
- if (u.length) {
- data.username = u.val();
- if (!data.username) {
- alert("Please choose a username.");
- return;
- }
- if (!data.password) {
- alert("Please choose a password.");
- return;
- }
- }
+ $('#settingsave').click(submitSettings);
+ $('.settingfield').keyup(onEnter(submitSettings));
- req = {
- url: data.username ? '/user/'+data.username : window.location.href,
- type: 'PUT',
- data: data,
- success: loginSignupSuccess,
- error: function(req) {
- if (req.status == 409)
- $('#settingserror').text('the requested username is taken');
- else
- $('#settingserror').text('an unknown error occured: '+req.responseText);
- }
- };
- $.ajax(req);
- });
-
- $('#loginbutton').click(function() {
- var username = $('input[name=login_username]').val();
- var password = $('input[name=login_password]').val();
-
- $.ajax({
- url:'/user/'+username,
- type:'POST',
- data:{'password':password},
- success:loginSignupSuccess,
- error:function(req) {
- $('#loginerror').text('incorrect username/password combination');
- }
- });
- });
+ $('#loginbutton').click(submitLogin);
+ $('.loginfield').keyup(onEnter(submitLogin));
$('#logoutbutton').click(function() {
View
8 apps/wriaki/templates/account_detail_form.dtl
@@ -24,14 +24,14 @@
<dl class="form">
{% if not user %}
<dt>Username</dt>
- <dd class="required"><input type="text" name="username" value="{{ username|escape }}" /></dd>
+ <dd class="required"><input type="text" name="username" value="{{ username|escape }}" class="settingfield" /></dd>
{% endif %}
<dt>{% if user %}New {% endif %}Password</dt>
- <dd{% if not user %} class="required"{% endif %}><input type="password" name="password" /></dd>
+ <dd{% if not user %} class="required"{% endif %}><input type="password" name="password" class="settingfield" /></dd>
<dt>Email</dt>
- <dd><input type="text" name="email"{% if user %}{% if user.email %} value="{{ user.email|escape }}"{% endif %}{% endif %} /></dd>
+ <dd><input type="text" name="email"{% if user %}{% if user.email %} value="{{ user.email|escape }}"{% endif %}{% endif %} class="settingfield" /></dd>
<dt>Bio</dt>
- <dd><input type="text" name="bio"{% if user %}{% if user.bio %} value="{{ user.bio|escape }}"{% endif %}{% endif %} /></dd>
+ <dd><input type="text" name="bio"{% if user %}{% if user.bio %} value="{{ user.bio|escape }}"{% endif %}{% endif %} class="settingfield" /></dd>
</dl>
{% if not user %}<input type="hidden" name="signup" value="true" />{% endif %}
<button id="settingsave" type="button">{% if user %}save settings{% else %}sign up{% endif %}</button>
View
4 apps/wriaki/templates/login_form.dtl
@@ -25,9 +25,9 @@
<div id="loginerror" class="warning"></div>
<dl class="form">
<dt>Username</dt>
- <dd><input name="login_username" type="text" /></dd>
+ <dd><input name="login_username" type="text" class="loginfield" /></dd>
<dt>Password</dt>
- <dd><input name="login_password" type="password" /></dd>
+ <dd><input name="login_password" type="password" class="loginfield" /></dd>
</dl>
<button id="loginbutton" type="button">login</button>
<hr />
Please sign in to comment.
Something went wrong with that request. Please try again.