@react-slate/core
Version:
Write interactive CLI apps with React
71 lines • 2.83 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const Base_1 = __importDefault(require("./Base"));
class ContainerBase extends Base_1.default {
findChild(child) {
return this.children.indexOf(child);
}
updateLayoutNode() {
// Remove all previous children layout nodes.
// After each removal `childCount` will return a new value - if greater than 0,
// the next child to remove will be moved to the top, at index 0.
while (this.layoutNode.childCount) {
this.layoutNode.removeChildAtIndex(0);
}
for (let i = 0; i < this.children.length; i++) {
if (!this.children[i].isAbsolute) {
this.children[i].isAbsolute = this.isAbsolute;
this.children[i].zIndex = this.zIndex;
}
this.layoutNode.addChild(this.children[i].layoutNode);
}
}
prependChild(child, position) {
child.parent = this;
this.eventListener.addChild(child.eventListener);
if (position !== undefined) {
assert_1.default(position >= 0 && position <= this.children.length - 1, `child position out of bounds: ${position}`);
this.children.splice(position, 0, child);
}
else {
this.children.unshift(child);
}
this.updateLayoutNode();
}
appendChild(child, position) {
child.parent = this;
this.eventListener.addChild(child.eventListener);
if (position !== undefined) {
assert_1.default(position >= 0 && position <= this.children.length, `child position out of bounds: ${position}`);
this.children.splice(position + 1, 0, child);
}
else {
this.children.push(child);
}
this.updateLayoutNode();
}
removeChild(child) {
const nodeIndex = this.children.indexOf(child);
assert_1.default(nodeIndex >= 0, 'child not found');
delete child.parent;
delete child.layoutNode;
this.eventListener.removeChild(child.eventListener);
this.children.splice(nodeIndex, 1);
this.layoutNode.removeChildAtIndex(nodeIndex);
}
notifyOnLayoutHook(layout, { offsetX, offsetY }) {
super.notifyOnLayoutHook(layout, { offsetX, offsetY });
for (let i = 0; i < this.children.length; i++) {
this.children[i].notifyOnLayoutHook(layout.child(i), {
offsetX: offsetX + layout.x,
offsetY: offsetY + layout.y,
});
}
}
}
exports.default = ContainerBase;
//# sourceMappingURL=ContainerBase.js.map