Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
smtdfc committed Aug 1, 2023
1 parent a2692a4 commit 88c2625
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions src/Component/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export class TurtleComponent extends HTMLElement {
onRerender() {}
onRender() {}
render() {}

connectedCallback() {

this.onCreate()
Expand All @@ -109,6 +108,38 @@ export class TurtleComponent extends HTMLElement {
onRemove() {}
}

export class TurtleStaticComponent extends HTMLElement{
constructor(){
super()
this.componentId = generateKey()
window.TURTLE_COMPONENTS[this.componentId] = this
this.data = {}
this.freeze = false
this.isTurtleComponent = true
this.props = window.TURTLE_COMPONENTS_PROPS[this.getAttribute("props")]
delete window.TURTLE_COMPONENTS_PROPS[this.getAttribute("props")]
this.removeAttribute("props")
}
async requestRender(){
this.beforeRender()
this.dom = document.createElement("template")
this.dom.innerHTML = await this.render()
this.contents = this.dom.content
this.after(this.contents)
this.remove()
this.onRender()
}

connectedCallback() {
this.onCreate()
this.requestRender()
}
render(){}
onCreate(){}
beforeRender(){}
onRender(){}
}

export function createComponent(name, options) {

const $Component = class extends TurtleComponent {
Expand Down Expand Up @@ -141,11 +172,34 @@ export function createComponent(name, options) {
return (options.onRemove ?? new Function()).bind(this)(...args)
}
}
try {
window.customElements.define(name, $Component)

} catch (e) {
throw `Cannot create component : ${name}`
}
}
export function createStaticComponent(name, options) {

const $Component = class extends TurtleStaticComponent {
render() {
return (options.render ?? new Function()).bind(this)()
}
beforeRender() {
return (options.beforeRender ?? new Function()).bind(this)()
}
onRender() {
return (options.onRender ?? new Function()).bind(this)()
}

onCreate() {
return (options.onCreate ?? new Function()).bind(this)()
}

}

try {
window.customElements.define(name, $Component)

} catch (e) {
throw `Cannot create component : ${name}`
}
Expand Down

0 comments on commit 88c2625

Please sign in to comment.