view src/site.js @ 93:d0566cc4a2ac default tip

uploa fix
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Apr 2025 19:47:48 -0600
parents 7b339b1ccd11
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 ) {
			let err = 'ajax failed: ' + request.status;
			if( request.responseText ) {
				err += '\n' + request.responseText.trim();
				document.write('<pre>'+request.responseText+'</pre>');
			}
			err += '\nurl = ' + url;
			err += '\npage = ' + window.location;
			ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
			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 ) {
		if( error.stack )
			err += '\nstack = ' + error.stack;
		if( error.cause )
			err += '\ncause= ' + error.cause;
		if( error.fileName )
			err += '\nfileName= ' + error.fileName;
	}
	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 logout() {
	document.cookie = 'user=; Max-Age=0; path=/;';
	document.cookie = 'password=; Max-Age=0; path=/;';
	location = '/';
}

function openModal(dialog) {
	//dialog.close();
	dialog.showModal();
	//dialog.scrollTop = 0;
}

function getEnclosingDialog(el) {
	while( el.nodeName !== 'DIALOG' )
		el = el.parentNode;
	return el;
}

function closeModal(el) {
	getEnclosingDialog(el).close();
}

let times = [
	{
		time: 1000*60*60*24,
		unit: 'day'
	},
	{
		time: 1000*60*60,
		unit: 'hour'
	},
	{
		time: 1000*60,
		unit: 'minute'
	},
	{
		time: 1000,
		unit: 'second'
	}
];

function ago(time) {
	for( let t of times ) {
		let n = Math.floor(time / t.time);
		if( n > 0 ) {
			let s = `${n} ${t.unit}`;
			if( n > 1 )
				s = s + 's';
			return s + ' ago';
		}
	}
	return 'just now';
}