Peter Harpending 1aed42598a reorg
2025-10-27 09:30:29 -07:00

79 lines
2.6 KiB
JavaScript

/**
* Home page ts/js
*
* @module
*/
import * as libfewd from './libfewd.js';
//------------------------------------------------------------------
// page element stuff
//------------------------------------------------------------------
main();
function main() {
let ielt = document.getElementById('wfc-input');
let oelt = document.getElementById('wfc-output');
let cb_resize = document.getElementById('auto-resize-output');
let cb_scroll = document.getElementById('auto-scroll');
let MAX_OELT_HEIGHT = 300;
ielt.addEventListener('keydown', function (e) {
on_input_key(e, ielt, oelt, cb_resize, cb_scroll, MAX_OELT_HEIGHT);
});
}
// when user hits any key
async function on_input_key(evt, ielt, oelt, cb_resize, cb_scroll, max_height) {
if (evt.key === 'Enter') {
// don't do default thing
evt.preventDefault();
// grab contents
let contents = ielt.value;
let trimmed = contents.trim();
let nonempty = trimmed.length > 0;
// if contents are nonempty
if (nonempty) {
// clear input
ielt.value = '';
// put in output
oelt.value += '> ' + trimmed + '\n';
oelt.hidden = false;
// query backend for result
let result = await fetch_wfcin(trimmed);
if (result.ok)
oelt.value += result.result;
else
oelt.value += result.error;
oelt.value += '\n';
// auto-resize
libfewd.auto_resize(cb_resize, oelt, max_height);
libfewd.auto_scroll_to_bottom(cb_scroll, oelt);
}
}
}
function assert(condition, fail_msg) {
if (!condition)
throw new Error(fail_msg);
}
async function fetch_wfcin(user_line) {
let req_body_obj = { wfcin: user_line };
let req_body_str = JSON.stringify(req_body_obj);
let req_options = { method: 'POST',
headers: { 'content-type': 'application/json' },
body: req_body_str };
// default result = somehow neither branch of code below was run(?)
// putting this here so ts doesn't chimp out
let result = { ok: false,
error: 'IT DO BE LIKE THAT MISTA STANCIL' };
try {
let response = await fetch('/wfcin', req_options);
if (response.ok)
result = await response.json();
else {
console.log('bad http response:', response);
result = { ok: false, error: 'BAD HTTP RESPONSE' };
}
}
catch (x) {
console.log('network error:', x);
result = { ok: false, error: 'NETWORK ERROR' };
}
return result;
}
//# sourceMappingURL=wfc.js.map