import { CustomHTMLElement } from './lib/custom_html_element.mjs'
export class SmonDashboard extends CustomHTMLElement {
static {
this.tmpl = document.createElement('template')
this.tmpl.innerHTML = `
`
}
constructor() {
super(true)
const script = this.querySelector('script')
this.data = JSON.parse(script.textContent)
this.render()
}
render() {
const widgets = []
for (const wd of this.data.Widgets) {
const widget = SmonWidget.create(wd.Type, wd)
widgets.push(widget)
}
this.elGrid.replaceChildren(...widgets)
}
}
class SmonWidget extends CustomHTMLElement {
static create(t, data) {
switch (t) {
case 0: return new SmonWidgetLabel(data)
case 1: return new SmonWidgetOnOff(data)
}
}
constructor(data) {
super(true)
this.data = data
}
render() {
this.style.gridColumn = this.data.X
this.style.gridRow = this.data.Y
}
}
class SmonWidgetLabel extends SmonWidget {
static {
this.tmpl = document.createElement('template')
this.tmpl.innerHTML = `
`
}
constructor(data) {
super(data)
this.render()
}
render() {
super.render()
this.elLabel.innerText = this.data.Attributes?.Label || '[set Label]'
}
}
class SmonWidgetOnOff extends SmonWidget {
static {
this.tmpl = document.createElement('template')
this.tmpl.innerHTML = `
`
}
constructor(data) {
super(data)
this.render()
}
render() {
super.render()
let img
switch (this.data.Attributes.Status) {
case 'ON':
img = 'widget_light_green.svg'
break
case 'OFF':
img = 'widget_light_red.svg'
break
default:
img = 'widget_light_off.svg'
}
this.elStatus.setAttribute('src', `/images/${_VERSION}/${img}`)
this.elLabel.innerText = this.data.Attributes.Label
}
}
customElements.define("smon-widget", SmonWidget)
customElements.define("smon-widget-label", SmonWidgetLabel)
customElements.define("smon-widget-onoff", SmonWidgetOnOff)
customElements.define("smon-dashboard", SmonDashboard)