2021-08-30 02:14:06 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Display text component
|
|
|
|
Toolkit.Label = class Label extends Toolkit.Component {
|
|
|
|
|
|
|
|
// Object constructor
|
|
|
|
constructor(application, options) {
|
2021-09-02 00:16:22 +00:00
|
|
|
super(application, options&&options.label ? "label" : "div", options);
|
2021-08-30 02:14:06 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// Configure instance fields
|
2021-09-02 00:16:22 +00:00
|
|
|
this.focusable = "focusable" in options ? !!options.focusable : false;
|
2021-08-30 02:14:06 +00:00
|
|
|
this.localized = "localized" in options ? !!options.localized : false;
|
|
|
|
this.text = options.text || "";
|
|
|
|
|
|
|
|
// Configure element
|
|
|
|
this.element.style.cursor = "default";
|
|
|
|
this.element.style.userSelect = "none";
|
|
|
|
|
|
|
|
// Configure properties
|
2021-09-02 00:16:22 +00:00
|
|
|
this.setFocusable(this.focusable);
|
|
|
|
this.setText (this.text);
|
2021-08-30 02:14:06 +00:00
|
|
|
if (this.localized)
|
|
|
|
this.application.addComponent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////// Public Methods //////////////////////////////
|
|
|
|
|
2021-09-02 00:16:22 +00:00
|
|
|
// Request focus on the appropriate element
|
|
|
|
focus() {
|
|
|
|
if (this.focusable)
|
|
|
|
this.element.focus();
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
// Retrieve the label's display text
|
|
|
|
getText() {
|
|
|
|
return this.text;
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:16:22 +00:00
|
|
|
// Determine whether the component is focusable
|
|
|
|
isFocusable() {
|
|
|
|
return this.focusable;
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
// Specify the label's display text
|
|
|
|
setText(text) {
|
|
|
|
this.text = text || "";
|
|
|
|
this.localize();
|
|
|
|
}
|
|
|
|
|
2021-09-02 00:16:22 +00:00
|
|
|
// Specify whether the component is focusable
|
|
|
|
setFocusable(focusable) {
|
|
|
|
this.focusable = focusable = !!focusable;
|
|
|
|
if (focusable) {
|
|
|
|
this.element.setAttribute("tabindex", "0");
|
|
|
|
this.localized && this.application &&
|
|
|
|
this.application.addComponent(this);
|
|
|
|
} else {
|
|
|
|
this.element.removeAttribute("aria-label");
|
|
|
|
this.element.removeAttribute("tabindex");
|
|
|
|
this.localized && this.application &&
|
|
|
|
this.application.removeComponent(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:14:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////// Package Methods /////////////////////////////
|
|
|
|
|
|
|
|
// Update display text with localized strings
|
|
|
|
localize() {
|
|
|
|
let text = this.text;
|
|
|
|
if (this.localized && this.application)
|
|
|
|
text = this.application.translate(text, this);
|
|
|
|
this.element.innerText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|