annotate src/site.js @ 13:af2b95854072

add server records
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Oct 2023 21:32:42 -0600
parents 3e2cb946d511
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 'use strict';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 function ajax(url,postData,context) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let request = new XMLHttpRequest();
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 let method = postData ? 'POST' : 'GET';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 request.open( method, url );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 if( postData )
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 request.onload = function() {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 if( request.status !== 200 ) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 console.log( 'ajax failed: ' + request.status );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 if( request.responseText )
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 document.write('<pre>'+request.responseText+'</pre>');
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 try {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 eval( request.responseText );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 } catch(e) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 console.log( request.responseText );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 window.err = '\najax-url = ' + url;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 throw e;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 };
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 request.send(postData);
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 window.onerror = function(msg, url, line, col, error) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if (!url)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 let err = msg;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 err += '\nurl = ' + url;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if (url != window.location)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 err += '\npage = ' + window.location;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 err += '\nline = '+line;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 if (col)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 err += '\ncolumn = ' + col;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if (error && error.stack)
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 err += '\nstack = ' + error.stack;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 if( window.err ) {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 err += window.err;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 window.err = null;
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 }
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 };
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
46
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
47 function ajaxForm(url,form) {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
48 let post = '';
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
49 for( let i=0; i<form.length; i++ ) {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
50 let input = form[i];
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
51 let name = input.name;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
52 if( name === '' )
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
53 continue;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
54 let type = input.type;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
55 if( (type==='radio' || type==='checkbox') && !input.checked )
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
56 continue;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
57 post += name + '=' + encodeURIComponent(input.value) + '&';
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
58 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
59 ajax(url,post,{form:form});
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
60 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
61
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
62 function clearErrors(form) {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
63 let divs = form.querySelectorAll('div[error]');
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
64 for( let i=0; i<divs.length; i++ ) {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
65 divs[i].textContent = '';
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
66 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
67 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
68
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
69 function showError(form,field,message) {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
70 clearErrors(form);
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
71 let err = form.querySelector('[error="'+field+'"]');
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
72 err.textContent = message;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
73 err.scrollIntoViewIfNeeded(false);
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
74 err.setAttribute('flash','');
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
75 setTimeout(function(){err.removeAttribute('flash')},2000);
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
76 let input = form.querySelector('input[name="'+field+'"]');
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
77 if(input) input.focus();
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
78 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
79
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
80
9
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
81 let currentPulldown = null;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
82 let newPulldown = null;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
83
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
84 function clickMenu(clicked,display) {
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
85 //console.log("clickMenu");
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
86 let pulldown = clicked.parentNode.querySelector('[pulldown_menu]');
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
87 if( pulldown !== currentPulldown ) {
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 9
diff changeset
88 pulldown.style.display = display || "flex";
9
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
89 newPulldown = pulldown;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
90 window.onclick = function() {
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
91 if( currentPulldown ) {
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
92 currentPulldown.style.display = "none";
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
93 if( !newPulldown )
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
94 window.onclick = null;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
95 }
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
96 currentPulldown = newPulldown;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
97 newPulldown = null;
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
98 };
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
99 pulldown.scrollIntoViewIfNeeded(false);
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
100 }
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
101 }
88f425d261b3 add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
102
4
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 function logout() {
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 document.cookie = 'user=; Max-Age=0; path=/;';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 location = '/';
8896ffd7b152 start login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }