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

71 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-12-19 10:42:57 +08:00
import axios from 'axios'
2019-12-19 13:07:33 +08:00
import { DoricContext } from './DoricContext'
2019-12-19 10:42:57 +08:00
export class DoricElement extends HTMLElement {
2019-12-19 13:07:33 +08:00
context?: DoricContext
2019-12-19 10:42:57 +08:00
constructor() {
super()
2019-12-26 17:09:03 +08:00
}
get src() {
return this.getAttribute('src') as string
}
get alias() {
return this.getAttribute('alias') as string
}
set src(v: string) {
this.setAttribute('src', v)
}
set alias(v: string) {
this.setAttribute('alias', v)
}
2021-04-13 16:22:15 +08:00
get initData() {
return this.getAttribute('data') as string
}
set initData(v: string) {
this.setAttribute('data', v)
}
2019-12-26 17:09:03 +08:00
connectedCallback() {
if (this.src && this.context === undefined) {
2021-04-13 16:22:15 +08:00
axios.get(this.src).then(result => {
2020-01-04 14:43:28 +08:00
this.load(result.data)
})
2019-12-26 17:09:03 +08:00
}
}
disconnectedCallback() {
}
adoptedCallback() {
}
attributeChangedCallback() {
2019-12-19 10:42:57 +08:00
}
2019-12-26 17:33:22 +08:00
onDestroy() {
2021-04-13 16:22:15 +08:00
this.context?.onDestroy()
2019-12-26 17:33:22 +08:00
this.context?.teardown()
}
2019-12-19 10:42:57 +08:00
load(content: string) {
2019-12-19 13:07:33 +08:00
this.context = new DoricContext(content)
2021-04-13 16:22:15 +08:00
this.context.init(this.initData)
this.context.onCreate()
2019-12-19 20:44:14 +08:00
const divElement = document.createElement('div')
2019-12-26 17:09:03 +08:00
divElement.style.position = 'relative'
2019-12-19 20:44:14 +08:00
divElement.style.height = '100%'
this.append(divElement)
this.context.rootNode.view = divElement
2020-03-30 15:18:35 +08:00
this.context.build({
2019-12-19 20:44:14 +08:00
width: divElement.offsetWidth,
height: divElement.offsetHeight,
2019-12-19 13:34:56 +08:00
})
2021-04-13 16:22:15 +08:00
this.context.onShow()
2019-12-19 10:42:57 +08:00
}
}