pvbemu/app/toolkit/TextBox.js

146 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-04-15 01:51:09 +00:00
import { Component } from /**/"./Component.js";
let Toolkit;
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
///////////////////////////////////////////////////////////////////////////////
// TextBox //
///////////////////////////////////////////////////////////////////////////////
// Text entry field
class TextBox extends Component {
static Component = Component;
///////////////////////// Initialization Methods //////////////////////////
constructor(gui, options) {
super(gui, options, {
className: "tk tk-textbox",
tagName : "input"
});
2021-09-02 00:16:22 +00:00
this.element.type = "text";
2022-04-15 01:51:09 +00:00
this.setAttribute("spellcheck", "false");
// Configure instance fields
this.isEnabled = null;
this.maxLength = null;
this.pattern = null;
// Configure component
options = options || {};
this.setEnabled(!("enabled" in options) || options.enabled);
if ("maxLength" in options)
this.setMaxLength(options.maxLength);
if ("pattern" in options)
this.setPattern(options.pattern);
this.setText (options.text);
// Configure event handlers
this.addEventListener("blur" , e=>this.commit ( ));
this.addEventListener("pointerdown", e=>e.stopPropagation( ));
this.addEventListener("keydown" , e=>this.onKeyDown (e));
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
///////////////////////////// Event Handlers //////////////////////////////
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Key press
onKeyDown(e) {
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Processing by key
switch (e.key) {
case "ArrowLeft":
case "ArrowRight":
e.stopPropagation();
return;
case "Enter":
this.commit();
break;
default: return;
}
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Configure event
e.stopPropagation();
e.preventDefault();
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
///////////////////////////// Public Methods //////////////////////////////
// Programmatically commit the text box
commit() {
this.event("action");
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
// Retrieve the control's value
2021-09-02 00:16:22 +00:00
getText() {
return this.element.value;
}
2022-04-15 01:51:09 +00:00
// Specify whether the button can be activated
2021-09-02 00:16:22 +00:00
setEnabled(enabled) {
2022-04-15 01:51:09 +00:00
this.isEnabled = enabled = !!enabled;
this.setAttribute("disabled", enabled ? null : "true");
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
// Specify the maximum length of the text
setMaxLength(length) {
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Remove limitation
if (length === null) {
this.maxLength = null;
this.setAttribute("maxlength", null);
return;
}
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Error checking
if (typeof length != "number" || isNaN(length))
return;
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Range checking
length = Math.floor(length);
if (length < 0)
return;
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Configure component
this.maxLength = length;
this.setAttribute("maxlength", length);
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
// Specify a regex pattern for valid text characters
setPattern(pattern) {
/*
Disabled because user agents may not prevent invalid input
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Error checking
if (pattern && typeof pattern != "string")
2021-09-02 00:16:22 +00:00
return;
2022-04-15 01:51:09 +00:00
// Configure component
this.pattern = pattern = pattern || null;
this.setAttribute("pattern", pattern);
*/
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
// Specify the widget's display text
setText(text = "") {
this.element.value = text.toString();
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
///////////////////////////// Package Methods /////////////////////////////
2021-09-02 00:16:22 +00:00
2022-04-15 01:51:09 +00:00
// Update the global Toolkit object
static setToolkit(toolkit) {
Toolkit = toolkit;
2021-09-02 00:16:22 +00:00
}
2022-04-15 01:51:09 +00:00
}
export { TextBox };