51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import Conquer from '@burguillosinfo/conquer'
|
|
export default abstract class ConquerInterface {
|
|
private alreadyGenerated = false
|
|
private nodes: HTMLElement[]
|
|
private callbacks: Record<string, Array<() => void>> = {}
|
|
public getNodes(): HTMLElement[] {
|
|
if (!this.alreadyGenerated) {
|
|
this.nodes = this.generateNodes()
|
|
this.alreadyGenerated = true
|
|
}
|
|
return this.nodes
|
|
}
|
|
protected abstract generateNodes(): HTMLElement[]
|
|
|
|
public run(): void {
|
|
return
|
|
}
|
|
public prune(): void {
|
|
return
|
|
}
|
|
|
|
protected getNodeFromTemplateId(id: string): HTMLElement {
|
|
const template = document.getElementById(id)
|
|
if (template === null) {
|
|
Conquer.fail(`Unable to find template id ${id}.`)
|
|
}
|
|
const finalNode = template.cloneNode(true)
|
|
if (!(finalNode instanceof HTMLElement)) {
|
|
Conquer.fail('The node is not an Element.')
|
|
}
|
|
return finalNode
|
|
}
|
|
|
|
public on(eventName: string, callback: () => void): void {
|
|
if (this.callbacks[eventName] === undefined) {
|
|
this.callbacks[eventName] = []
|
|
}
|
|
this.callbacks[eventName].push(callback)
|
|
}
|
|
|
|
private runCallbacks(eventName: string) {
|
|
const callbacks = this.callbacks[eventName];
|
|
if (callbacks === undefined) {
|
|
return
|
|
}
|
|
for (const callback of callbacks) {
|
|
callback()
|
|
}
|
|
}
|
|
}
|