add alert for asking user for string

This commit is contained in:
Eric House 2021-02-16 14:25:22 -08:00
parent 52773c577f
commit 007e1e4acb
3 changed files with 48 additions and 4 deletions

View file

@ -78,6 +78,13 @@ EM_JS(void, call_dialog, (const char* str, const char** but_strs,
nbDialog(UTF8ToString(str), buttons, proc, closure);
} );
EM_JS(void, call_get_string, (const char* msg, const char* dflt,
AlertProc proc, void* closure), {
let jsMgs = UTF8ToString(msg);
let jsDflt = UTF8ToString(dflt);
nbGetString( jsMgs, jsDflt, proc, closure );
} );
EM_JS(void, call_haveDevID, (void* closure, const char* devid), {
onHaveDevID(closure, UTF8ToString(devid));
});

View file

@ -67,6 +67,11 @@
padding: 10px;
}
textarea.stringedit {
rows: 1;
resize: none;
}
</style>
</head>
<body class="centered">

View file

@ -87,17 +87,23 @@ function mqttSend( topic, ptr ) {
return canSend;
}
function nbDialog(msg, buttons, proc, closure) {
function newDlgWMsg(msg) {
let container = document.getElementById('nbalert');
let dlg = document.createElement('div');
dlg.classList.add('nbalert');
dlg.style.zIndex = 10000;
dlg.style.zIndex = 10000 + container.childElementCount;
container.appendChild( dlg );
let txtDiv = document.createElement('div');
dlg.appendChild( txtDiv );
txtDiv.textContent = msg
dlg.appendChild( txtDiv );
return dlg;
}
function nbDialog(msg, buttons, proc, closure) {
let dlg = newDlgWMsg( msg );
let span = document.createElement('div');
span.classList.add('buttonRow');
@ -107,13 +113,39 @@ function nbDialog(msg, buttons, proc, closure) {
button.onclick = function() {
Module.ccall('onDlgButton', null, ['number', 'number', 'string'],
[proc, closure, buttonTxt]);
container.removeChild(dlg);
dlg.parentNode.removeChild(dlg);
};
span.appendChild( button );
}
dlg.appendChild( span );
}
function nbGetString(msg, dflt, proc, closure) {
let dlg = newDlgWMsg( msg );
let tarea = document.createElement('textarea');
tarea.classList.add('stringedit');
tarea.value = dflt;
dlg.appendChild( tarea );
dismissed = function(str) {
dlg.parentNode.removeChild(dlg);
Module.ccall('onDlgButton', null, ['number', 'number', 'string'],
[proc, closure, str]);
}
let buttons = document.createElement('div');
dlg.appendChild(buttons);
let cancel = document.createElement('button');
cancel.textContent = "Cancel";
buttons.appendChild(cancel);
cancel.onclick = function() { dismissed(null);}
let ok = document.createElement('button');
ok.textContent = 'OK';
buttons.appendChild(ok);
ok.onclick = function() { dismissed(tarea.value);}
}
for ( let one of ['paho-mqtt.js'] ) {
let script = document.createElement('script');
script.src = one