ascii-ui
Version:
Graphic terminal emulator for HTML canvas elements
73 lines • 1.93 kB
JavaScript
import { deepAssignAndDiff } from './util/deepAssignAndDiff';
export class Widget {
constructor(terminal, options, parent) {
this.options = {};
this.terminal = terminal;
this.parent = parent;
this.setOptions(Object.assign({}, Widget.defaultOptions, options));
}
destruct() {
}
getParent() {
return this.parent;
}
setOptions(options) {
const changes = deepAssignAndDiff(this.options, options);
if (!this.options.focusable && this.focused) {
this.blur();
}
this.allocated = this.options.col >= 0
&& this.options.line >= 0
&& this.options.width >= 0
&& this.options.height >= 0;
this.updateOptions(changes);
}
getSize() {
return {
columns: this.options.width,
rows: this.options.height,
};
}
getPosition() {
return {
col: this.options.col,
line: this.options.line,
};
}
isAt(column, line) {
const options = this.options;
return column >= options.col
&& column < options.col + options.width
&& line >= options.line
&& line < options.line + options.height;
}
isFocusable() {
return this.options.focusable;
}
focus() {
if (this.options.focusable) {
const wasFocused = this.focused;
this.focused = true;
if (!wasFocused) {
this.render();
return true;
}
}
return false;
}
blur() {
const wasFocused = this.focused;
this.focused = false;
if (wasFocused) {
this.render();
}
return wasFocused;
}
isFocused() {
return this.focused;
}
}
Widget.defaultOptions = {
focusable: true,
};
//# sourceMappingURL=Widget.js.map