org.magicwerk.brownies.html.console.WebConsole.js Maven / Gradle / Ivy
The newest version!
// Typedefs
//
$(document).ready(() => {
log("ready");
$('body').keydown(function (e) {
onKey(e);
});
});
handleMessage = function (obj) {
var type = obj.type;
if (type === 'showMenu') {
showMenu(obj);
}
else if (type === 'askText') {
askText(obj);
}
else {
invalidMessage(obj);
}
};
// Dialogs
let dialogIndex = 0;
let dialogAlignVertically = true;
let dialogIfDoneDisable = true;
let dialogIfDoneHide = false;
let dialogMenu = null;
let dialogMenuKeys = null; // array of entered keys
let dialogText = null;
function getNewDialogIndex() {
dialogIndex++;
return dialogIndex;
}
function getDialogId(dialog) {
return "dialog_" + dialog;
}
function askText(obj) {
log("askText " + prompt);
let title = obj.title;
let message = obj.message;
let defaultText = (obj.defaultText) ? obj.defaultText : "";
let index = getNewDialogIndex();
let html = "";
if (title) {
html += "" + escapeHtml(title) + "";
}
if (message) {
html += "" + escapeHtml(message) + "";
}
html += "";
html += "";
html += " ";
html += " ";
html += "";
html += "";
append(html);
dialogText = obj;
}
function append(html) {
addHtml(html, 'body', 'append');
scrollWindow(true);
}
function askTextButtonClicked(dialog, button) {
log("askTextButtonClicked " + dialog + ", " + button);
onDialogButtonClicked(dialog, button);
// Send result back
let dialogId = "#" + getDialogId(dialog);
let result = { text: null };
if (button === 0) {
result.text = $(dialogId).find(':text').val();
}
else {
$(dialogId).find(':text').val("");
}
send(result);
dialogText = null;
}
function showMenu(obj) {
log("showMenu " + obj);
let title = require(obj.title);
let message = require(obj.message);
let options = require(obj.options);
let useEnterKey = require(obj.useEnterKey);
let index = getNewDialogIndex();
let html = "";
if (title) {
html += "" + escapeHtml(title) + "";
}
if (message) {
html += "" + escapeHtml(message) + "";
}
for (let i = 0; i < options.length; i++) {
let option = options[i];
let text = option.text;
let key = option.keyCodes.join("");
let desc = text;
if (key) {
desc += " (" + key + ")";
}
let onclick = "showMenuButtonClicked(" + index + ", " + i + ")";
let button = " ";
if (dialogAlignVertically) {
button = "" + button + "";
}
html += button;
}
html += "";
append(html);
dialogMenu = obj;
dialogMenuKeys = [];
}
function showMenuButtonClicked(dialog, button) {
log("showMenuButtonClicked " + dialog + ", " + button);
onDialogButtonClicked(dialog, button);
// Send result back
let result = { result: null };
result.result = button;
send(result);
dialogMenu = null;
dialogMenuKeys = null;
}
// Javascript Key Event
// event.key / event.keyCode
// 'a', 'KeyA'
// 'A', 'KeyA'
// '1', 'Digit1'
const KEYCODE_ENTER = 'Enter';
const KEYCODE_ESCAPE = 'Escape';
const KEYCODE_BACKSPACE = 'Backspace';
const KEYCODE_ANY = 'Any';
function onKey(event) {
log("onKey key=" + event.key + " code=" + event.code);
let code = event.code;
if (dialogText) {
if (code === KEYCODE_ENTER) {
askTextButtonClicked(dialogIndex, 0);
}
else if (code === KEYCODE_ESCAPE) {
askTextButtonClicked(dialogIndex, 1);
}
}
else if (dialogMenu) {
let handle = false;
if (code === KEYCODE_BACKSPACE) {
dialogMenuKeys.pop();
}
else if (code === KEYCODE_ENTER) {
if (dialogMenu.useEnterKey) {
handle = true;
}
else {
dialogMenuKeys.push(code);
}
}
else if (code === KEYCODE_ESCAPE) {
dialogMenuKeys.push(code);
handle = true;
}
else {
dialogMenuKeys.push(event.key.toUpperCase());
if (!dialogMenu.useEnterKey) {
handle = true;
}
}
if (handle) {
let opts = dialogMenu.options;
for (let i = 0; i < opts.length; i++) {
let opt = opts[i];
if (matchOptionKeys(opt)) {
showMenuButtonClicked(dialogIndex, i);
return;
}
}
dialogMenuKeys = [];
}
}
}
function matchOptionKeys(opt) {
if (opt.keyCodes[0] === KEYCODE_ANY) {
return true;
}
let arr1 = opt.keyCodes;
let arr2 = dialogMenuKeys;
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
function onDialogButtonClicked(dialog, button) {
let dialogId = "#" + getDialogId(dialog);
if (dialogIfDoneDisable) {
// Disable all buttons in this dialog
$(dialogId).find(':button').prop('disabled', true);
$(dialogId).find(':button:eq(' + button + ')').addClass('selected');
}
if (dialogIfDoneHide) {
// Hide dialog
$(dialogId).hide();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy