From 7fd9dc6f10a30748cf63caf61ee71f602407d6ac Mon Sep 17 00:00:00 2001 From: Bastien Wirtz Date: Tue, 17 Dec 2019 14:10:04 -0800 Subject: Optionaly load message from an endpoint. --- app.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index def0be7..93902d8 100644 --- a/app.js +++ b/app.js @@ -7,9 +7,9 @@ const app = new Vue({ vlayout: true, isDark: null }, - created: function () { + created: async function () { let that = this; - + this.isDark = 'overrideDark' in localStorage ? JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches; @@ -18,11 +18,23 @@ const app = new Vue({ } this.checkOffline(); - that.getConfig().then(function (config) { - that.config = config; - }).catch(function () { - that.offline = true; - }); + try { + this.config = await this.getConfig(); + } catch (error) { + this.offline = true; + } + + // Look for a new message if an endpoint is provided. + if (this.config.message.url) { + this.getMessage(this.config.message.url).then(function(message){ + // keep the original config value if no value is provided by the endpoint + for (const prop of ['title','style','content']) { + if (prop in message && message[prop] !== null) { + that.config.message[prop] = message[prop]; + } + } + }); + } document.addEventListener('visibilitychange', function () { if (document.visibilityState == "visible") { @@ -52,6 +64,14 @@ const app = new Vue({ }); }); }, + getMessage: function (url) { + return fetch(url).then(function (response) { + if (response.status != 200) { + return; + } + return response.json(); + }); + }, toggleTheme: function() { this.isDark = !this.isDark; localStorage.overrideDark = this.isDark; -- cgit v1.2.3