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/DoricContext.ts

75 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-12-19 13:34:56 +08:00
import { jsObtainContext, jsCallEntityMethod } from 'doric/src/runtime/sandbox'
2019-12-19 13:07:33 +08:00
import { Panel } from 'doric'
2019-12-19 11:52:15 +08:00
import { DoricPlugin } from "./DoricPlugin"
2019-12-26 17:33:22 +08:00
import { createContext, destroyContext } from "./DoricDriver"
2019-12-20 17:28:24 +08:00
import { DoricStackNode } from './shader/DoricStackNode'
2019-12-26 19:01:28 +08:00
import { DoricViewNode } from './shader/DoricViewNode'
2019-12-19 11:52:15 +08:00
const doricContexts: Map<string, DoricContext> = new Map
2019-12-19 13:07:33 +08:00
let __contextId__ = 0
function getContextId() {
return `context_${__contextId__++}`
}
2019-12-19 11:52:15 +08:00
export function getDoricContext(contextId: string) {
return doricContexts.get(contextId)
}
export class DoricContext {
2019-12-19 13:07:33 +08:00
contextId = getContextId()
2019-12-19 11:52:15 +08:00
pluginInstances: Map<string, DoricPlugin> = new Map
2019-12-20 17:28:24 +08:00
rootNode: DoricStackNode
2020-01-09 19:31:50 +08:00
headNodes: Map<string, Map<string, DoricViewNode>> = new Map
2019-12-26 19:01:28 +08:00
2019-12-19 13:07:33 +08:00
constructor(content: string) {
createContext(this.contextId, content)
doricContexts.set(this.contextId, this)
2019-12-20 17:28:24 +08:00
this.rootNode = new DoricStackNode(this)
2019-12-19 11:52:15 +08:00
}
2019-12-19 13:07:33 +08:00
get panel() {
2019-12-19 13:34:56 +08:00
return jsObtainContext(this.contextId)?.entity as Panel
}
invokeEntityMethod(method: string, ...otherArgs: any) {
const argumentsList: any = [this.contextId]
for (let i = 0; i < arguments.length; i++) {
argumentsList.push(arguments[i])
}
2019-12-28 14:25:03 +08:00
return Reflect.apply(jsCallEntityMethod, this.panel, argumentsList)
2019-12-19 13:07:33 +08:00
}
2021-04-13 16:22:15 +08:00
init(data?: string) {
this.invokeEntityMethod("__init__", data)
2020-03-30 15:18:35 +08:00
}
2021-04-13 16:22:15 +08:00
onCreate() {
this.invokeEntityMethod("__onCreate__")
}
onDestroy() {
this.invokeEntityMethod("__onDestroy__")
}
onShow() {
this.invokeEntityMethod("__onShow__")
}
onHidden() {
this.invokeEntityMethod("__onHidden__")
}
2020-03-30 15:18:35 +08:00
build(frame: {
2019-12-19 13:34:56 +08:00
width: number,
height: number,
2020-03-30 15:18:35 +08:00
}) {
this.invokeEntityMethod("__build__", frame)
2019-12-19 13:07:33 +08:00
}
2019-12-26 17:33:22 +08:00
teardown() {
2019-12-26 19:01:28 +08:00
for (let plugin of this.pluginInstances.values()) {
plugin.onTearDown()
}
2019-12-26 17:33:22 +08:00
destroyContext(this.contextId)
}
2019-12-19 11:52:15 +08:00
}