96 lines
3.3 KiB
JavaScript
96 lines
3.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Home page ts/js
|
|
*
|
|
* @module
|
|
*/
|
|
//------------------------------------------------------------------
|
|
// 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
|
|
auto_resize_output(cb_resize, oelt, max_height);
|
|
auto_scroll_to_bottom(cb_scroll, oelt);
|
|
}
|
|
}
|
|
}
|
|
function auto_resize_output(checkbox_element, target_element, max_height) {
|
|
// if the user has manually resized their output, we do nothing
|
|
if (checkbox_element.checked) {
|
|
let target_height = target_element.scrollHeight;
|
|
// resize it automagically up to 500px
|
|
if (target_height < max_height)
|
|
target_element.style.height = String(target_height) + 'px';
|
|
else
|
|
target_element.style.height = String(max_height) + 'px';
|
|
}
|
|
}
|
|
function auto_scroll_to_bottom(checkbox_element, target_element) {
|
|
if (checkbox_element.checked) {
|
|
// scroll to bottom
|
|
target_element.scrollTop = target_element.scrollHeight;
|
|
}
|
|
}
|
|
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=index.js.map
|