vasille
Version:
The same framework which is designed to build bulletproof frontends (core library).
51 lines (50 loc) • 1.28 kB
JavaScript
import { safe } from "../functional/safety.js";
import { Fragment } from "../node/node.js";
/**
* Repeat node repeats its children
* @class RepeatNode
* @extends Fragment
*/
export class RepeatNode extends Fragment {
/**
* Children node hash
* @type {Map}
*/
nodes = new Map();
slot;
constructor(input, runner) {
super(runner);
this.slot = input.slot && safe(input.slot);
}
createChild(id, item, before) {
const node = new Fragment(this.runner);
node.parent = this;
this.destroyChild(id, item);
if (before) {
this.children.add(node);
before.insertBefore(node);
}
else {
const lastChild = this.lastChild;
if (lastChild) {
lastChild.insertAfter(node);
}
this.children.add(node);
}
this.lastChild = node;
this.slot?.(node, item, id);
this.nodes.set(id, node);
}
destroyChild(id, item) {
const child = this.nodes.get(id);
if (child) {
child.remove();
child.destroy();
this.nodes.delete(id);
this.children.delete(child);
}
}
destroy() {
this.nodes.clear();
}
}