2019-12-19 10:42:57 +08:00
|
|
|
import axios from 'axios'
|
2021-04-13 16:49:38 +08:00
|
|
|
import { loadDoricJSBundle } from './DoricBundleLoader'
|
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:49:38 +08:00
|
|
|
loadDoricJSBundle(this.src).then(result => {
|
|
|
|
this.load(result)
|
2020-01-04 14:43:28 +08:00
|
|
|
})
|
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)
|
2019-12-19 21:12:41 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|