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)
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
if (this.src && this.context === undefined) {
|
2020-01-03 16:35:04 +08:00
|
|
|
if (this.src.startsWith("http")) {
|
|
|
|
axios.get<string>(this.src).then(result => {
|
|
|
|
this.load(result.data)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.load(this.src)
|
|
|
|
}
|
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() {
|
|
|
|
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)
|
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
|
2019-12-19 13:34:56 +08:00
|
|
|
this.context.init({
|
2019-12-19 20:44:14 +08:00
|
|
|
width: divElement.offsetWidth,
|
|
|
|
height: divElement.offsetHeight,
|
2019-12-19 13:34:56 +08:00
|
|
|
})
|
2019-12-19 10:42:57 +08:00
|
|
|
}
|
|
|
|
}
|