@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
188 lines • 5.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
class TkBaseWidget extends mercury_event_emitter_1.AbstractEventEmitter {
type = 'abstract';
parent;
term;
id;
children = [];
shouldLockWidthWithParent = false;
shouldLockHeightWithParent = false;
shouldLockRightWithParent = false;
shouldLockBottomWithParent = false;
padding = {};
frameLockDeltas = {
leftDelta: 0,
widthDelta: 0,
topDelta: 0,
heightDelta: 0,
rightDelta: 0,
bottomDelta: 0,
};
constructor(options) {
super(options.eventContract ?? { eventSignatures: {} });
this.parent = options.parent ?? null;
this.term = options.term;
this.id = options.id ?? null;
this.shouldLockHeightWithParent =
options.shouldLockHeightWithParent ?? false;
this.shouldLockWidthWithParent =
options.shouldLockWidthWithParent ?? false;
this.shouldLockRightWithParent =
options.shouldLockRightWithParent ?? false;
this.shouldLockBottomWithParent =
options.shouldLockBottomWithParent ?? false;
this.padding = {
left: 0,
top: 0,
right: 0,
bottom: 0,
...options.padding,
};
if (this.parent) {
this.parent.addChild(this);
}
}
getChildren() {
return this.children;
}
addChild(child) {
this.children.push(child);
}
getId() {
return this.id;
}
getParent() {
return this.parent;
}
getFrame() {
const element = this.getTermKitElement();
if (element) {
return {
left: element.outputX,
top: element.outputY,
width: element.outputWidth,
height: element.outputHeight,
};
}
throw new Error(element
? `${this.type} does not implement getFrame()`
: `${this.type} does not implement getTermKitElement()`);
}
setFrame(frame) {
const element = this.getTermKitElement();
if (element) {
if (element.resize) {
element.resize({
x: frame.left,
y: frame.top,
width: frame.width,
height: frame.height,
});
}
else {
element.outputX = frame.left ?? element.outputX;
element.outputY = frame.top ?? element.outputY;
element.outputWidth = frame.width ?? element.outputWidth;
element.outputHeight = frame.height ?? element.outputHeight;
}
this.sizeLockedChildren();
element.draw();
return;
}
throw new Error(`${this.type} does not implement getTermKitElement()`);
}
sizeLockedChildren() {
const newFrame = this.getFrame();
for (const child of this.children) {
child.handleParentResize(newFrame);
}
}
getChildById(id) {
for (const child of this.getChildren()) {
if (child.getId() === id) {
return child;
}
}
return null;
}
removeChild(child) {
this.children = this.children.filter((c) => c !== child);
}
handleParentResize(parentFrame) {
const updatedFrame = this.getFrame();
let shouldSetFrame = false;
if (this.shouldLockHeightWithParent) {
shouldSetFrame = true;
updatedFrame.height =
parentFrame.height - this.frameLockDeltas.heightDelta;
}
if (this.shouldLockWidthWithParent) {
shouldSetFrame = true;
updatedFrame.width =
parentFrame.width - this.frameLockDeltas.widthDelta;
}
if (this.shouldLockRightWithParent) {
shouldSetFrame = true;
updatedFrame.left =
parentFrame.width -
this.frameLockDeltas.rightDelta -
updatedFrame.width;
}
if (this.shouldLockBottomWithParent) {
shouldSetFrame = true;
updatedFrame.top =
parentFrame.height -
updatedFrame.height -
this.frameLockDeltas.bottomDelta;
}
if (shouldSetFrame) {
this.setFrame(updatedFrame);
}
}
async destroy() {
this.getTermKitElement()?.destroy();
this.getParent()?.removeChild(this);
}
getTermKitElement() {
return null;
}
calculateSizeLockDeltas() {
const frame = this.getFrame();
const parentFrame = this.getParent()?.getFrame();
if (!parentFrame) {
return;
}
let leftDelta = 0;
let widthDelta = 0;
let topDelta = 0;
let heightDelta = 0;
let rightDelta = 0;
let bottomDelta = 0;
if (this.shouldLockWidthWithParent) {
leftDelta = frame.left;
widthDelta = parentFrame.width - frame.width;
}
if (this.shouldLockHeightWithParent) {
topDelta = frame.top;
heightDelta = parentFrame.height - frame.height;
}
if (this.shouldLockRightWithParent) {
rightDelta = frame.left + frame.width - parentFrame.width;
}
if (this.shouldLockBottomWithParent) {
bottomDelta = parentFrame.height - (frame.top + frame.height);
}
this.frameLockDeltas = {
leftDelta,
widthDelta,
topDelta,
heightDelta,
rightDelta,
bottomDelta,
};
}
}
exports.default = TkBaseWidget;
//# sourceMappingURL=TkBaseWidget.js.map