view src/site.js @ 14:80105b716a62

start bot
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 Oct 2023 23:56:48 -0600
parents 3e2cb946d511
children
line wrap: on
line source

'use strict';

function ajax(url,postData,context) {
	let request = new XMLHttpRequest();
	let method = postData ? 'POST' : 'GET';
	request.open( method, url );
	if( postData )
		request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	request.onload = function() {
		if( request.status !== 200 ) {
			console.log( 'ajax failed: ' + request.status );
			if( request.responseText )
				document.write('<pre>'+request.responseText+'</pre>');
			return;
		}
		try {
			eval( request.responseText );
		} catch(e) {
			console.log( request.responseText );
			window.err = '\najax-url = ' + url;
			throw e;
		}
	};
	request.send(postData);
}

window.onerror = function(msg, url, line, col, error) {
	if (!url)
		return;
	let err = msg;
	err += '\nurl = ' + url;
	if (url != window.location)
		err += '\npage = ' + window.location;
	err += '\nline = '+line;
	if (col)
		err += '\ncolumn = ' + col;
	if (error && error.stack)
		err += '\nstack = ' + error.stack;
	if( window.err ) {
		err += window.err;
		window.err = null;
	}
	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
};


function ajaxForm(url,form) {
	let post = '';
	for( let i=0; i<form.length; i++ ) {
		let input = form[i];
		let name = input.name;
		if( name === '' )
			continue;
		let type = input.type;
		if( (type==='radio' || type==='checkbox') && !input.checked )
			continue;
		post += name + '=' + encodeURIComponent(input.value) + '&';
	}
	ajax(url,post,{form:form});
}

function clearErrors(form) {
	let divs = form.querySelectorAll('div[error]');
	for( let i=0; i<divs.length; i++ ) {
		divs[i].textContent = '';
	}
}

function showError(form,field,message) {
	clearErrors(form);
	let err = form.querySelector('[error="'+field+'"]');
	err.textContent = message;
	err.scrollIntoViewIfNeeded(false);
	err.setAttribute('flash','');
	setTimeout(function(){err.removeAttribute('flash')},2000);
	let input = form.querySelector('input[name="'+field+'"]');
	if(input) input.focus();
}


let currentPulldown = null;
let newPulldown = null;

function clickMenu(clicked,display) {
	//console.log("clickMenu");
	let pulldown = clicked.parentNode.querySelector('[pulldown_menu]');
	if( pulldown !== currentPulldown ) {
		pulldown.style.display = display || "flex";
		newPulldown = pulldown;
		window.onclick = function() {
			if( currentPulldown ) {
				currentPulldown.style.display = "none";
				if( !newPulldown )
					window.onclick = null;
			}
			currentPulldown = newPulldown;
			newPulldown = null;
		};
		pulldown.scrollIntoViewIfNeeded(false);
	}
}

function logout() {
	document.cookie = 'user=; Max-Age=0; path=/;';
	location = '/';
}