diff --git a/app/client/components/App.js b/app/client/components/App.js index 1d68a21..0069d64 100644 --- a/app/client/components/App.js +++ b/app/client/components/App.js @@ -6,8 +6,9 @@ */ import { AppElement, html, css } from './AppElement' -import { customElement } from 'lit/decorators.js' -import { createApp } from '../lib/network.js' +import { APP_STATE } from '../store/appState' +import { customElement, state } from 'lit/decorators.js' +import { createApp } from '../lib/network' import './PerformanceDashboard' @customElement('web-app') @@ -17,11 +18,22 @@ export class App extends AppElement { ` } + @state() + globalAppState = APP_STATE + constructor () { super() - this.app = createApp() + this.app = createApp(this.globalAppState) window.app = this.app this.app.setMetricsCallback(metrics => this.metricsUpdated(metrics)) + + // this is how we implement changes to the global state: + // once any child component sends this CustomEvent we update the global state according + // to the changes that were passed to us + this.addEventListener('app-state-changed', (event) => { + const newState = event.detail + this.globalAppState = newState + }) } static properties = { @@ -30,7 +42,10 @@ export class App extends AppElement { render () { return html` - + ` } diff --git a/app/client/components/AppElement.js b/app/client/components/AppElement.js index c09c638..1614aed 100644 --- a/app/client/components/AppElement.js +++ b/app/client/components/AppElement.js @@ -6,10 +6,30 @@ */ import { LitElement } from 'lit' +import { property } from 'lit/decorators.js' +import { APP_STATE } from '../store/appState' export * from 'lit' export class AppElement extends LitElement { - // todo: should use shadow root once the global style file is dissolved into the components + // this is how we implement a global state: a global state object is passed via properties + // to child components + @property({ type: Object }) + appState = APP_STATE + + // ..and state changes are send back to the root component of the app by dispatching + // a CustomEvent + updateState () { + this.dispatchEvent( + new CustomEvent('app-state-changed', { + detail: { ...this.appState }, + bubbles: true, + composed: true + }) + ) + } + + // currently we do not use shadow root since there is still a global style file + // but maybe one day we dissolve it into the components and use shadow dom instead createRenderRoot () { return this } diff --git a/app/client/components/DashboardActions.js b/app/client/components/DashboardActions.js index be1cfc3..d30c8a5 100644 --- a/app/client/components/DashboardActions.js +++ b/app/client/components/DashboardActions.js @@ -28,10 +28,11 @@ export class DashboardActions extends AppElement {
${icon_compress}
- -
${this.peripheralMode}
+ +
${this.appState.peripheralMode}
` } + //
${this.peripheralMode}
toggleFullscreen () { const fullscreenElement = document.getElementsByTagName('web-app')[0] @@ -54,6 +55,9 @@ export class DashboardActions extends AppElement { } switchPeripheralMode () { - window.app.switchPeripheralMode() + // window.app.switchPeripheralMode() + // todo: this is just a test property to see if the concept works... + this.appState.peripheralMode = 'PM5' + this.updateState() } } diff --git a/app/client/components/PerformanceDashboard.js b/app/client/components/PerformanceDashboard.js index dd93cdf..fffb925 100644 --- a/app/client/components/PerformanceDashboard.js +++ b/app/client/components/PerformanceDashboard.js @@ -6,6 +6,7 @@ */ import { AppElement, html, css } from './AppElement' +import { APP_STATE } from '../store/appState' import { customElement, property } from 'lit/decorators.js' import './DashboardMetric' import './DashboardActions' @@ -21,6 +22,9 @@ export class PerformanceDashboard extends AppElement { @property({ type: Object }) metrics + @property({ type: Object }) + appState = APP_STATE + render () { return html` @@ -34,7 +38,7 @@ export class PerformanceDashboard extends AppElement { : html``} - + ` } } diff --git a/app/client/store/appState.js b/app/client/store/appState.js new file mode 100644 index 0000000..525adc2 --- /dev/null +++ b/app/client/store/appState.js @@ -0,0 +1,11 @@ +'use strict' +/* + Open Rowing Monitor, https://github.com/laberning/openrowingmonitor + + Defines the global state of the app +*/ + +export const APP_STATE = { + // todo: this is just a test property to see if the concept works... + peripheralMode: 'FTMSROWER' +}