-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
355 lines (314 loc) · 7.91 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* $Id$ */
/*
* Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
(function(root) {
'use strict';
/*
* Send a GET query to the give url.
* Invokes "setup" before running anything, "error" upon network
* error (with the HTTP error code and response text), and
* success with the response text on 200.
*/
function sendQuery(url, setup, error, success)
{
var xmh = new XMLHttpRequest();
var v;
if (null !== setup)
setup();
xmh.onreadystatechange = function() {
v = xmh.responseText;
if (xmh.readyState === 4 &&
xmh.status === 200) {
if (null !== success)
success(v);
} else if (xmh.readyState === 4) {
if (null !== error)
error(xmh.status, v);
}
};
xmh.open('GET', url, true);
xmh.send(null);
}
/*
* Send a POST query to the give url with the form.
* Invokes "setup" before running anything (given the form),
* "error" upon network error (with the form, HTTP error code,
* and response text), and success with the form and response
* text on 200.
*/
function sendForm(form, setup, error, success)
{
var xmh = new XMLHttpRequest();
var v;
if (null !== setup)
setup(form);
xmh.onreadystatechange=function() {
v = xmh.responseText;
if (xmh.readyState === 4 &&
xmh.status === 200) {
if (null !== success)
success(form, v);
} else if (xmh.readyState === 4) {
if (null !== error)
error(form, xmh.status, v);
}
};
xmh.open(form.method, form.action, true);
xmh.send(new FormData(form));
return(false);
}
/*
* Shows (removes the "hide" class) a particular element.
* Accepts "root", which is an element or the identifier string
* of one.
*/
function show(root)
{
var e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
if (e.classList.contains('hide'))
e.classList.remove('hide');
}
/*
* Hide all classes named "name" under a particular element.
* Accepts "root", which is an element or the identifier string
* of one, under which (inclusive) all elements will be queried.
* See show().
*/
function showcl(root, name)
{
var list, i, sz, e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
list = e.getElementsByClassName(name);
for (i = 0, sz = list.length; i < sz; i++)
show(list[i]);
}
/*
* Simple lookup function for element by name.
* Emits a console warning on error.
*/
function find(root)
{
var e;
if (null === (e = document.getElementById(root)))
console.log('cannot find: ' + root);
return(e);
}
/*
* Replace all text within an element.
* Accepts "root", which is an element or the identifier string
* of one.
*/
function repl(root, text)
{
var e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
while (e.firstChild)
e.removeChild(e.firstChild);
e.appendChild(document.createTextNode(text));
}
/*
* Replace all text within elements of class "name" under a
* particular element.
* Accepts "root", which is an element or the identifier string
* of one, under which (inclusive) all elements will be queried.
* See repl().
*/
function replcl(root, name, text)
{
var list, i, sz, e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
list = e.getElementsByClassName(name);
for (i = 0, sz = list.length; i < sz; i++)
repl(list[i], text);
}
/*
* Hide (adds the "hide" class) a particular element.
* Accepts "root", which is an element or the identifier string
* of one.
*/
function hide(root)
{
var e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
if ( ! e.classList.contains('hide'))
e.classList.add('hide');
}
/*
* Show all classes named "name" under a particular element.
* Accepts "root", which is an element or the identifier string
* of one, under which (inclusive) all elements will be queried.
* See hide().
*/
function hidecl(root, name)
{
var list, i, sz, e;
e = typeof root === 'string' ?
document.getElementById(root) : root;
if (null === e)
return;
list = e.getElementsByClassName(name);
for (i = 0, sz = list.length; i < sz; i++)
hide(list[i]);
}
/*
* Generic setup for a JSON form.
* Hides the submit button (class "submit"), an error messages
* (class "error"), and shows the pending button (class
* "pending").
*/
function formSetup(e)
{
showcl(e, 'pending');
hidecl(e, 'submit');
hidecl(e, 'error');
}
/*
* Generic error for a JSON form.
* Shows the submit button (class "submit"), an particular error
* message (class "error" and the error code; or if greater than
* error 500, just 5xx), and hides the pending button (class
* "pending").
*/
function formError(e, code)
{
hidecl(e, 'pending');
showcl(e, 'submit');
if (code < 500)
showcl(e, 'error' + code);
else
showcl(e, 'error5xx');
}
/*
* Generic success (doing nothing).
* Shows the submit button (class "submit") and hides the
* pending button (class "pending").
*/
function formSuccess(e, resp)
{
hidecl(e, 'pending');
showcl(e, 'submit');
}
/*
* Success that reloads page.
* Invokes formSuccess().
*/
function formSuccessReload(e, resp)
{
formSuccess(e, resp);
document.location.reload();
}
/*
* Generic form that does nothing except submit the form
* contents, do the usual button swapping, then reload on
* success.
* This function is asynchronous.
*/
function formReloadProc()
{
return(sendForm(this, formSetup,
formError, formSuccessReload));
}
/*
* Simply parse a JSON message from the "resp" string.
* Returns null on failure, the object on success.
*/
function formParse(resp)
{
var res;
try {
res = JSON.parse(resp);
} catch (error) {
console.log('JSON parse fail: ' + resp);
return(null);
}
return(res);
}
/*
* Prepare for receiving the index page.
*/
function indexSetup()
{
show('loading');
hide('loaded');
hide('loggedin');
hide('login');
}
/*
* The index.json page was not retrieved.
*/
function indexError(code, resp)
{
hide('loading');
show('loaded');
hide('loggedin');
show('login');
}
/*
* The index.json file was retrieved.
* Format the page appropriately.
*/
function indexSuccess(resp)
{
var res, e;
if (null === (res = formParse(resp))) {
indexError(500, null);
return;
}
hide('loading');
show('loaded');
show('loggedin');
hide('login');
replcl(document, 'user-email', res.user.email);
}
/*
* Configure the index page.
*/
function loadIndex()
{
/* Reset all existing forms. */
hidecl(document, 'error');
hidecl(document, 'pending');
showcl(document, 'submit');
/* Set all "submit" handlers. */
find('modpassform').onsubmit = formReloadProc;
find('modemailform').onsubmit = formReloadProc;
find('loginform').onsubmit = formReloadProc;
find('logoutform').onsubmit = formReloadProc;
/* Actually try to load the user page. */
sendQuery('@CGIURI@/index.json',
indexSetup, indexError, indexSuccess);
}
root.loadIndex = loadIndex;
})(this);
document.addEventListener('DOMContentLoaded', function() {
loadIndex();
});