-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
127 lines (109 loc) · 4.14 KB
/
client.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
{% load staticfiles %}
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="{% static 'css/client.css' %}">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<!-- This is all the page independent functions. The ones which directly interact with the page are inlined below. -->
<script type="text/javascript" src="{% static 'js/client.js' %}"></script>
</head>
<center>
<div id="login-page" class="box">
<div id="login-message" class="message"></div>
<center>
<form>
Username: <input id="login-username" type="text" name="username"><br>
Password: <input id="login-password" type="password" name="pwd"><br>
<input id="login-button" type="submit" value="Login">
<input id="add-user-button" type="submit" value="Add User">
</form>
</center>
</div>
<div id="welcome-page" class="box">
<div id="welcome-message" class="message"></div>
<center>
<form>
<input type="submit" id="logout-button" value="Logout">
</form>
</center>
</div>
</center>
<script type="text/javascript">
<!--
/* Put the page into a blank state until everything finishes loading
This is not the ideal thing to do, but better than nothing. */
$('#login-page').hide()
$('#welcome-page').hide()
$('#login-username').val("")
$('#login-password').val("")
$('#login-message').html("Please enter your credentials below")
$('#welcome-message').html("You should never see this text");
/* setup the page so that only one of the forms is shown */
$(document).ready(function() {
show_login_page();
});
/* Note: These two functions are deliberately written to ignore the starting
state. This makes them slightly slower, but has the side effect of
restoring any invariant that gets accidentally broken.*/
function show_login_page(message) {
if(! message) message = "Please enter your credentials below";
$('#welcome-page').hide()
$('#login-username').val("")
$('#login-password').val("")
$('#login-message').html(message)
$('#login-page').show()
}
function show_welcome_page(user, count) {
$('#login-page').hide();
$('#welcome-page').show();
$('#welcome-message').html("Welcome "+user+"<br>You have logged in "+count+" times.");
}
function handle_login_response(data, user) {
if( data.errCode > 0 ) {
c = data.count;
show_welcome_page(user, c);
} else {
if( debug_flag ) {
if( data.errCode != ERR_BAD_CREDENTIALS ) {
alert( 'Illegal error code encounted for this state');
}
}
show_login_page( get_message_for_errcode(data.errCode) );
}
}
function handle_add_user_response(data, user) {
if( data.errCode > 0 ) {
c = data.count;
show_welcome_page(user, c);
} else {
if( debug_flag ) {
if( data.errCode != ERR_BAD_USERNAME && data.errCode != ERR_USER_EXISTS ) {
alert( 'Illegal error code encounted for this state');
}
}
show_login_page( get_message_for_errcode(data.errCode) );
}
}
$('#login-button').click(function() {
username = $('#login-username').val()
password = $('#login-password').val()
json_request("/users/login", { user: username, password: password }, function(data) { return handle_login_response(data, username); }, function(err) { alert('error occurred on request'); });
return false;
});
$('#add-user-button').click(function() {
username = $('#login-username').val()
password = $('#login-password').val()
json_request("/users/add", { user: username, password: password }, function(data) { return handle_add_user_response(data, username); }, function(err) {alert('error occurred on request'); });
return false;
});
$('#logout-button').click(function() {
show_login_page();
return false;
});
-->
</script>
<body>
</body>
</html>