annotate src/site.js @ 88:7b339b1ccd11

add tools/users.html
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 13 Mar 2025 16:18:39 -0600
parents dade6a560494
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 'use strict';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 function ajax(url,postData,context) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let request = new XMLHttpRequest();
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 let method = postData ? 'POST' : 'GET';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 request.open( method, url );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 if( postData )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 request.onload = function() {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 if( request.status !== 200 ) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 let err = 'ajax failed: ' + request.status;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 if( request.responseText ) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 err += '\n' + request.responseText.trim();
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 document.write('<pre>'+request.responseText+'</pre>');
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 err += '\nurl = ' + url;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 err += '\npage = ' + window.location;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 try {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 eval( request.responseText );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 } catch(e) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 console.log( request.responseText );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 window.err = '\najax-url = ' + url;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 throw e;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 };
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 request.send(postData);
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 window.onerror = function(msg, url, line, col, error) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 if( !url )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 let err = msg;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 err += '\nurl = ' + url;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if( url != window.location )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 err += '\npage = ' + window.location;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 err += '\nline = '+line;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 if( col )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 err += '\ncolumn = ' + col;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 if( error ) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 if( error.stack )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 err += '\nstack = ' + error.stack;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 if( error.cause )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 err += '\ncause= ' + error.cause;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if( error.fileName )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 err += '\nfileName= ' + error.fileName;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 if( window.err ) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 err += window.err;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 window.err = null;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 };
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function ajaxForm(url,form) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 let post = '';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 for( let i=0; i<form.length; i++ ) {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 let input = form[i];
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 let name = input.name;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 if( name === '' )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 continue;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 let type = input.type;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 if( (type==='radio' || type==='checkbox') && !input.checked )
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 continue;
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 post += name + '=' + encodeURIComponent(input.value) + '&';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 ajax(url,post,{form:form});
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 function logout() {
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 document.cookie = 'user=; Max-Age=0; path=/;';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 document.cookie = 'password=; Max-Age=0; path=/;';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 location = '/';
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
77
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
78 function openModal(dialog) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
79 //dialog.close();
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
80 dialog.showModal();
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
81 //dialog.scrollTop = 0;
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
82 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
83
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
84 function getEnclosingDialog(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
85 while( el.nodeName !== 'DIALOG' )
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
86 el = el.parentNode;
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
87 return el;
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
88 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
89
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
90 function closeModal(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
91 getEnclosingDialog(el).close();
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
92 }
88
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
93
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
94 let times = [
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
95 {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
96 time: 1000*60*60*24,
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
97 unit: 'day'
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
98 },
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
99 {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
100 time: 1000*60*60,
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
101 unit: 'hour'
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
102 },
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
103 {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
104 time: 1000*60,
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
105 unit: 'minute'
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
106 },
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
107 {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
108 time: 1000,
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
109 unit: 'second'
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
110 }
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
111 ];
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
112
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
113 function ago(time) {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
114 for( let t of times ) {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
115 let n = Math.floor(time / t.time);
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
116 if( n > 0 ) {
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
117 let s = `${n} ${t.unit}`;
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
118 if( n > 1 )
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
119 s = s + 's';
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
120 return s + ' ago';
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
121 }
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
122 }
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
123 return 'just now';
7b339b1ccd11 add tools/users.html
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
124 }