This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-web/src/navigate/NavigationElement.ts
2019-12-31 18:39:38 +08:00

36 lines
989 B
TypeScript

import { DoricElement } from "../DoricElement"
export class NavigationElement extends HTMLElement {
elementStack: DoricElement[] = []
get currentNode() {
for (let i = 0; i < this.childNodes.length; i++) {
if (this.childNodes[i] instanceof DoricElement) {
return this.childNodes[i] as DoricElement
}
}
return undefined
}
push(element: DoricElement) {
const currentNode = this.currentNode
if (currentNode) {
this.elementStack.push(currentNode)
this.replaceChild(element, currentNode)
} else {
this.appendChild(element)
}
}
pop() {
const lastElement = this.elementStack.pop()
const currentNode = this.currentNode
if (lastElement && currentNode) {
this.replaceChild(lastElement, currentNode)
currentNode.onDestroy()
} else {
window.history.back()
}
}
}