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

59 lines
1.3 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)
}
connectedCallback() {
if (this.src && this.context === undefined) {
2020-01-04 14:43:28 +08:00
axios.get<string>(this.src).then(result => {
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() {
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)
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
})
2019-12-19 10:42:57 +08:00
}
}