From 70a86d091497df79aa2e39db57fb56f2d7f99790 Mon Sep 17 00:00:00 2001
From: Lars Berning <151194+laberning@users.noreply.github.com>
Date: Sat, 27 Nov 2021 17:17:58 +0100
Subject: [PATCH] initial attempt to refactor frontend with lit
---
app/client/.eslintrc.json | 21 ++
app/client/components/App.js | 44 ++++
app/client/components/AppElement.js | 16 ++
app/client/components/DashboardActions.js | 59 +++++
app/client/components/DashboardMetric.js | 55 +++++
app/client/components/PerformanceDashboard.js | 40 +++
app/client/index.html | 130 +---------
app/client/index.js | 5 +-
app/client/lib/icons.js | 26 ++
app/client/{app.js => lib/network.js} | 71 ++----
app/client/style.css | 20 +-
package-lock.json | 232 ++++++++++++++++++
package.json | 6 +-
snowpack.config.js | 15 +-
14 files changed, 544 insertions(+), 196 deletions(-)
create mode 100644 app/client/.eslintrc.json
create mode 100644 app/client/components/App.js
create mode 100644 app/client/components/AppElement.js
create mode 100644 app/client/components/DashboardActions.js
create mode 100644 app/client/components/DashboardMetric.js
create mode 100644 app/client/components/PerformanceDashboard.js
create mode 100644 app/client/lib/icons.js
rename app/client/{app.js => lib/network.js} (65%)
diff --git a/app/client/.eslintrc.json b/app/client/.eslintrc.json
new file mode 100644
index 0000000..7235261
--- /dev/null
+++ b/app/client/.eslintrc.json
@@ -0,0 +1,21 @@
+{
+ "env": {
+ "browser": true,
+ "node": false,
+ "es2021": true
+ },
+ "extends": [
+ "standard",
+ "plugin:wc/recommended",
+ "plugin:lit/recommended"
+ ],
+ "parser": "@babel/eslint-parser",
+ "parserOptions": {
+ "ecmaVersion": 12,
+ "sourceType": "module"
+ },
+ "ignorePatterns": ["**/*.min.js"],
+ "rules": {
+ "camelcase": 0
+ }
+}
diff --git a/app/client/components/App.js b/app/client/components/App.js
new file mode 100644
index 0000000..1d68a21
--- /dev/null
+++ b/app/client/components/App.js
@@ -0,0 +1,44 @@
+'use strict'
+/*
+ Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
+
+ Main Component of the Open Rowing Monitor App
+*/
+
+import { AppElement, html, css } from './AppElement'
+import { customElement } from 'lit/decorators.js'
+import { createApp } from '../lib/network.js'
+import './PerformanceDashboard'
+
+@customElement('web-app')
+export class App extends AppElement {
+ static get styles () {
+ return css`
+ `
+ }
+
+ constructor () {
+ super()
+ this.app = createApp()
+ window.app = this.app
+ this.app.setMetricsCallback(metrics => this.metricsUpdated(metrics))
+ }
+
+ static properties = {
+ metrics: { state: true }
+ };
+
+ render () {
+ return html`
+
+ `
+ }
+
+ metricsUpdated (metrics) {
+ this.metrics = Object.assign({}, metrics)
+ }
+
+ createRenderRoot () {
+ return this
+ }
+}
diff --git a/app/client/components/AppElement.js b/app/client/components/AppElement.js
new file mode 100644
index 0000000..c09c638
--- /dev/null
+++ b/app/client/components/AppElement.js
@@ -0,0 +1,16 @@
+'use strict'
+/*
+ Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
+
+ Base Component for all other App Components
+*/
+
+import { LitElement } from 'lit'
+export * from 'lit'
+
+export class AppElement extends LitElement {
+ // todo: should use shadow root once the global style file is dissolved into the components
+ createRenderRoot () {
+ return this
+ }
+}
diff --git a/app/client/components/DashboardActions.js b/app/client/components/DashboardActions.js
new file mode 100644
index 0000000..be1cfc3
--- /dev/null
+++ b/app/client/components/DashboardActions.js
@@ -0,0 +1,59 @@
+'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'
+import { customElement, property } from 'lit/decorators.js'
+import { icon_undo, icon_expand, icon_compress, icon_poweroff, icon_bluetooth } from '../lib/icons'
+
+@customElement('dashboard-actions')
+export class DashboardActions extends AppElement {
+ static get styles () {
+ return css`
+ `
+ }
+
+ @property({ type: String })
+ peripheralMode = ''
+
+ render () {
+ return html`
+
+
+
+
+
+
${this.peripheralMode}
+ `
+ }
+
+ toggleFullscreen () {
+ const fullscreenElement = document.getElementsByTagName('web-app')[0]
+ if (!document.fullscreenElement) {
+ fullscreenElement.requestFullscreen({ navigationUI: 'hide' })
+ } else {
+ if (document.exitFullscreen) {
+ document.exitFullscreen()
+ }
+ }
+ }
+
+ close () {
+ window.close()
+ }
+
+ // todo: should use events instead of communicating via a global app object
+ reset () {
+ window.app.reset()
+ }
+
+ switchPeripheralMode () {
+ window.app.switchPeripheralMode()
+ }
+}
diff --git a/app/client/components/DashboardMetric.js b/app/client/components/DashboardMetric.js
new file mode 100644
index 0000000..b735b82
--- /dev/null
+++ b/app/client/components/DashboardMetric.js
@@ -0,0 +1,55 @@
+'use strict'
+/*
+ Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
+
+ Component that renders a metric of the dashboard
+*/
+
+import { AppElement, html, svg, css } from './AppElement'
+import { customElement, property } from 'lit/decorators.js'
+
+@customElement('dashboard-metric')
+export class DashboardMetric extends AppElement {
+ static get styles () {
+ return css`
+ `
+ }
+
+ @property({ type: Object })
+ icon
+
+ @property({ type: String })
+ unit = ''
+
+ @property({ type: String })
+ value = ''
+
+ @property({ type: String })
+ batteryLevel = ''
+
+ render () {
+ return html`
+ ${this.icon}
+
+ ${this.value}
+ ${this.unit}
+
+ ${this.batteryLevel
+ ? html`${this.batteryIcon}
`
+ : ''}
+ `
+ }
+
+ get batteryIcon () {
+ // 416 is the max width value of the battery bar in the SVG graphic
+ const batteryWidth = this.batteryLevel * 416 / 100
+
+ return svg`
+
+
+ `
+ }
+}
diff --git a/app/client/components/PerformanceDashboard.js b/app/client/components/PerformanceDashboard.js
new file mode 100644
index 0000000..0e82bae
--- /dev/null
+++ b/app/client/components/PerformanceDashboard.js
@@ -0,0 +1,40 @@
+'use strict'
+/*
+ Open Rowing Monitor, https://github.com/laberning/openrowingmonitor
+
+ Component that renders the dashboard
+*/
+
+import { AppElement, html, css } from './AppElement'
+import { customElement, property } from 'lit/decorators.js'
+import './DashboardMetric'
+import './DashboardActions'
+import { icon_route, icon_stopwatch, icon_bolt, icon_paddle, icon_heartbeat, icon_fire, icon_clock } from '../lib/icons'
+
+@customElement('performance-dashboard')
+export class PerformanceDashboard extends AppElement {
+ static get styles () {
+ return css`
+ `
+ }
+
+ @property({ type: Object })
+ metrics
+
+ render () {
+ return html`
+
+
+
+
+ ${this.metrics?.heartrate?.value
+ ? html``
+ : html``}
+
+
+
+ `
+ }
+}
diff --git a/app/client/index.html b/app/client/index.html
index 3700845..be8347d 100644
--- a/app/client/index.html
+++ b/app/client/index.html
@@ -15,132 +15,8 @@
Open Rowing Monitor
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+