'use strict' /* Open Rowing Monitor, https://github.com/laberning/openrowingmonitor Component that renders the action buttons of the dashboard */ import { AppElement, html, css } from './AppElement.js' import { customElement, property, queryAll } from 'lit/decorators.js' import { icon_settings } from '../lib/icons.js' import './AppDialog.js' @customElement('settings-dialog') export class DashboardActions extends AppElement { static styles = css` .metric-selector-feedback{ font-size: 0.4em; padding-top: 8px; } .metric-selector-feedback>div { display: grid; grid-template-columns: repeat(4,1fr); grid-template-rows: repeat(2, max-content); gap: 8px; } .settings-dialog>div.metric-selector{ display: grid; grid-template-columns: repeat(4,max-content); grid-template-rows: repeat(3, max-content); gap: 8px; } .settings-dialog>div>label{ font-size: 0.6em; } input[type="checkbox"]{ cursor: pointer; align-self: center; width: 1.5em; height: 1.5em; } .icon { height: 1.6em; } legend{ text-align: center; } table { min-height: 70px; margin-top: 8px; width: 100%; } table, th, td { font-size: 0.8em; border: 1px solid white; border-collapse: collapse; } tr { height: 50%; } th, td { padding: 8px; text-align: center; background-color: var(--theme-widget-color); } ` @property({ type: Object }) config @queryAll('input') inputs static get properties () { return { selectedMetrics: { type: Array }, sumSelectedSlots: { type: Number }, isValid: { type: Boolean } } } constructor () { super() this.selectedMetrics = [] this.sumSelectedSlots = 0 this.isValid = false } @property({ type: Object }) icon render () { return html` ${icon_settings}
Settings

Select metrics to be shown:

Slots remaining: ${8 - this.sumSelectedSlots} ${this.renderSelectedMetrics()}
` } firstUpdated () { this.selectedMetrics = this.config.dashboardMetrics this.sumSelectedSlots = this.selectedMetrics.length if (this.sumSelectedSlots === 8) { this.isValid = true } else { this.isValid = false } [...this.inputs].forEach(input => { input.checked = this.selectedMetrics.find(metric => metric === input.name) !== undefined }) } renderSelectedMetrics () { const selectedMetrics = [html`${[0, 1, 2, 3].map(index => html`${this.selectedMetrics[index]}`)}`] selectedMetrics.push(html`${[4, 5, 6, 7].map(index => html`${this.selectedMetrics[index]}`)}`) return selectedMetrics } toggleCheck (e) { if ((e.target.checked && this.selectedMetrics.length < 4 && e.target.size > 1 && this.selectedMetrics.length + e.target.size > 4) || (e.target.checked && this.sumSelectedSlots + 1 > 8)) { this.isValid = this.isFormValid() e.target.checked = false return } if (e.target.checked) { for (let index = 0; index < e.target.size; index++) { this.selectedMetrics = [...this.selectedMetrics, e.target.name] } } else { for (let index = 0; index < e.target.size; index++) { this.selectedMetrics.splice(this.selectedMetrics.findIndex(metric => metric === e.target.name), 1) } } this.sumSelectedSlots = this.selectedMetrics.length if (this.isFormValid()) { this.isValid = true } else { this.isValid = false } } isFormValid () { return this.sumSelectedSlots === 8 && this.selectedMetrics[3] !== this.selectedMetrics[4] } close (event) { this.dispatchEvent(new CustomEvent('close')) if (event.detail === 'confirm') { this.dispatchEvent(new CustomEvent('changeGuiSetting', { detail: { ...this.appState, config: { ...this.appState.config, guiConfigs: { dashboardMetrics: this.selectedMetrics } } }, bubbles: true, composed: true })) } } }