burguillos.info/js-src/conquer/interface-manager.ts

62 lines
2.3 KiB
TypeScript

import Conquer from '@burguillosinfo/conquer'
import ConquerInterface from '@burguillosinfo/conquer/interface'
export default class ConquerInterfaceManager {
private interfaces: ConquerInterface[] = []
public push(conquerInterface: ConquerInterface) {
const nodesForInterface = conquerInterface.getNodes()
for (const nodeInInterface of nodesForInterface) {
nodeInInterface.id = ""
document.body.appendChild(nodeInInterface)
}
this.interfaces.push(conquerInterface)
conquerInterface.run()
let startInterface = this.interfaces.length - 2;
if (startInterface < 0) {
startInterface = 0
}
this.recalculateAllZIndexes(startInterface)
}
public remove(conquerInterface: ConquerInterface) {
for (let i = this.interfaces.length - 1; i >= 0; i--) {
if (conquerInterface !== this.interfaces[i]) {
continue
}
this.interfaces.splice(i, 1)
for (const nodeToDelete of conquerInterface.getNodes()) {
document.body.removeChild(nodeToDelete)
}
conquerInterface.prune()
this.recalculateAllZIndexes()
}
}
private recalculateAllZIndexes(start = 0) : void {
let currentZindex = 5;
if (start < 0) {
Conquer.fail('ConquerInterfaceManager.recalculateAllZIndexes must not be passed negative values.')
}
if (start > 0) {
const lastInterface = this.interfaces[start-1];
if (lastInterface === undefined) {
Conquer.fail('Last interface should not be null, dying...')
}
const lastInterfaceNodes = lastInterface.getNodes()
const lastInterfaceLastNode = lastInterfaceNodes[lastInterfaceNodes.length-1]
if (lastInterfaceLastNode === undefined) {
Conquer.fail('Last interface last node should not be null, dying...')
}
currentZindex = parseInt(lastInterfaceLastNode.style.zIndex)
}
for (let i = start; i < this.interfaces.length; i++) {
const conquerInterface = this.interfaces[i]
for (const node of conquerInterface.getNodes()) {
node.style.zIndex = currentZindex + ''
currentZindex++
}
}
}
}