diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2019-12-17 14:10:04 -0800 |
---|---|---|
committer | Bastien Wirtz <bastien.wirtz@gmail.com> | 2019-12-17 14:10:04 -0800 |
commit | 7fd9dc6f10a30748cf63caf61ee71f602407d6ac (patch) | |
tree | 33bb97b0645605e3875326292cdc99b654fe5249 | |
parent | 05bdc2680193564dbb8acbd85aa8d79eeec546f5 (diff) | |
download | homer-7fd9dc6f10a30748cf63caf61ee71f602407d6ac.tar.gz homer-7fd9dc6f10a30748cf63caf61ee71f602407d6ac.tar.zst homer-7fd9dc6f10a30748cf63caf61ee71f602407d6ac.zip |
Optionaly load message from an endpoint.
-rw-r--r-- | README.md | 14 | ||||
-rw-r--r-- | app.js | 34 | ||||
-rw-r--r-- | config.yml | 1 |
3 files changed, 42 insertions, 7 deletions
@@ -27,6 +27,7 @@ logo: "assets/homer.png" | |||
27 | 27 | ||
28 | # Optional message | 28 | # Optional message |
29 | message: | 29 | message: |
30 | # url: "https://<my-api-endpoint>" # Can fetch information from an endpoint to override value below. | ||
30 | style: "is-warning" | 31 | style: "is-warning" |
31 | title: "Optional message!" | 32 | title: "Optional message!" |
32 | content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula." | 33 | content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula." |
@@ -85,3 +86,16 @@ services: | |||
85 | url: "#" | 86 | url: "#" |
86 | 87 | ||
87 | ``` | 88 | ``` |
89 | |||
90 | If you choose to fetch message information from an endpoint, the output format should be: | ||
91 | |||
92 | ```json | ||
93 | { | ||
94 | "style": null, | ||
95 | "title": "Lorem ipsum 42", | ||
96 | "content": "LA LA LA Lorem ipsum dolor sit amet, ....." | ||
97 | } | ||
98 | ``` | ||
99 | |||
100 | `null` value or missing keys will be ignored and value from the `config.yml` will be used if available. | ||
101 | Empty values (either in `config.yml` or the endpoint data) will hide the element (ex: set `"title": ""` to hide the title bar) | ||
@@ -7,9 +7,9 @@ const app = new Vue({ | |||
7 | vlayout: true, | 7 | vlayout: true, |
8 | isDark: null | 8 | isDark: null |
9 | }, | 9 | }, |
10 | created: function () { | 10 | created: async function () { |
11 | let that = this; | 11 | let that = this; |
12 | 12 | ||
13 | this.isDark = 'overrideDark' in localStorage ? | 13 | this.isDark = 'overrideDark' in localStorage ? |
14 | JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches; | 14 | JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches; |
15 | 15 | ||
@@ -18,11 +18,23 @@ const app = new Vue({ | |||
18 | } | 18 | } |
19 | 19 | ||
20 | this.checkOffline(); | 20 | this.checkOffline(); |
21 | that.getConfig().then(function (config) { | 21 | try { |
22 | that.config = config; | 22 | this.config = await this.getConfig(); |
23 | }).catch(function () { | 23 | } catch (error) { |
24 | that.offline = true; | 24 | this.offline = true; |
25 | }); | 25 | } |
26 | |||
27 | // Look for a new message if an endpoint is provided. | ||
28 | if (this.config.message.url) { | ||
29 | this.getMessage(this.config.message.url).then(function(message){ | ||
30 | // keep the original config value if no value is provided by the endpoint | ||
31 | for (const prop of ['title','style','content']) { | ||
32 | if (prop in message && message[prop] !== null) { | ||
33 | that.config.message[prop] = message[prop]; | ||
34 | } | ||
35 | } | ||
36 | }); | ||
37 | } | ||
26 | 38 | ||
27 | document.addEventListener('visibilitychange', function () { | 39 | document.addEventListener('visibilitychange', function () { |
28 | if (document.visibilityState == "visible") { | 40 | if (document.visibilityState == "visible") { |
@@ -52,6 +64,14 @@ const app = new Vue({ | |||
52 | }); | 64 | }); |
53 | }); | 65 | }); |
54 | }, | 66 | }, |
67 | getMessage: function (url) { | ||
68 | return fetch(url).then(function (response) { | ||
69 | if (response.status != 200) { | ||
70 | return; | ||
71 | } | ||
72 | return response.json(); | ||
73 | }); | ||
74 | }, | ||
55 | toggleTheme: function() { | 75 | toggleTheme: function() { |
56 | this.isDark = !this.isDark; | 76 | this.isDark = !this.isDark; |
57 | localStorage.overrideDark = this.isDark; | 77 | localStorage.overrideDark = this.isDark; |
@@ -10,6 +10,7 @@ icon: "fas fa-skull-crossbones" | |||
10 | # Optional message | 10 | # Optional message |
11 | # See https://bulma.io/documentation/components/message/#colors for styling options. | 11 | # See https://bulma.io/documentation/components/message/#colors for styling options. |
12 | message: | 12 | message: |
13 | # url: https://.... | ||
13 | style: "is-warning" | 14 | style: "is-warning" |
14 | title: "Optional message!" | 15 | title: "Optional message!" |
15 | content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula." | 16 | content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque risus mi, tempus quis placerat ut, porta nec nulla. Vestibulum rhoncus ac ex sit amet fringilla. Nullam gravida purus diam, et dictum felis venenatis efficitur. Aenean ac eleifend lacus, in mollis lectus. Donec sodales, arcu et sollicitudin porttitor, tortor urna tempor ligula." |