sclass HTMLSuggestor {
S makeData = "{}";
S ajaxURL = "";
int delay = 1000;
S installTriggers = "";
S onGotResult = "";
bool verbose;
S html() {
if (empty(ajaxURL)) fail("Need ajaxURL");
ret hscript(replaceDollarVars([[
var sugVerbose = $verbose;
var sugLoading = false, sugTriggerAgain = false;
function sugTrigger() {
//if (sugVerbose) console.log("sugTrigger");
if (sugLoading) { sugTriggerAgain = true; return; }
// get form data as JSON
var data = $makeData;
const url = $calcURL;
if (sugVerbose) {
const urlWithParams = url + "?" + new URLSearchParams(data);
console.log("Loading " + urlWithParams);
}
sugLoading = true;
$.post(url, data,
function(result) {
if (sugVerbose) console.log("Suggestor result: " + result);
const answer = !result ? "" : JSON.parse(result).answer;
$onGotResult
if (answer) {
$("#liveResult .sectionContents").html(answer);
$("#liveResult").show();
} else
$("#liveResult").hide();
//$("#suggestorResult").html(answer ? "Suggestor says: " + answer : "");
}
).always(function() {
if (sugVerbose) console.log("sug loading done");
setTimeout(function() {
sugLoading = false;
if (sugTriggerAgain) { sugTriggerAgain = false; sugTrigger(); }
}, $delay);
});
}
$(document).ready(function() {
$installTriggers
sugTrigger();
});
]], +verbose, +delay, +makeData, calcURL := jsQuote(ajaxURL), +installTriggers, +onGotResult));
}
}