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