Mercurial Hosting > chat
view src/site.js @ 20:dade6a560494
add dialogs
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Nov 2024 21:37:58 -0700 |
parents | 2c63b10781e1 |
children | 7b339b1ccd11 |
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(); }