X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=app.js;h=40830da1a457f3053b4ff230b54e0814c0add6a2;hb=refs%2Fpull%2F8%2Fhead;hp=ffb50076897bbc6cea23abac1d47d0533e26c7c0;hpb=9ca12a40f404ba15d67068087dc521be7d69eefb;p=github%2Fbastienwirtz%2Fhomer.git diff --git a/app.js b/app.js index ffb5007..40830da 100644 --- a/app.js +++ b/app.js @@ -1,43 +1,100 @@ -var app = new Vue({ +const app = new Vue({ el: '#app', data: { config: null, - filter: '' + offline: false, + filter: '', + vlayout: true, + isDark: null }, - beforeCreate () { + created: function () { let that = this; - return getConfig().then(function (config) { - console.log(config); - const size = 3; - config.services.forEach(function(service) { - service.rows = []; - items = service.items; - while (items.length) { - service.rows.push(items.splice(0, size)); - } + this.isDark = 'overrideDark' in localStorage ? + JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches; - if (service.rows.length) { - let last = service.rows.length-1; - service.rows[last] = service.rows[last].concat(Array(size - service.rows[last].length)); - } - }); + if ('vlayout' in localStorage) { + this.vlayout = JSON.parse(localStorage.vlayout) + } + + this.checkOffline(); + that.getConfig().then(function (config) { that.config = config; }).catch(function () { - console.error('Fail to get config'); + that.offline = true; }); + + document.addEventListener('visibilitychange', function () { + if (document.visibilityState == "visible") { + that.checkOffline(); + } + }, false); + }, + methods: { + checkOffline: function () { + let that = this; + return fetch(window.location.href + "?alive", { + method: 'HEAD', + cache: 'no-store' + }).then(function () { + that.offline = false; + }).catch(function () { + that.offline = true; + }); + }, + getConfig: function (event) { + return fetch('config.yml').then(function (response) { + if (response.status != 200) { + return + } + return response.text().then(function (body) { + return jsyaml.load(body); + }); + }); + }, + toggleTheme: function() { + this.isDark = !this.isDark; + localStorage.overrideDark = this.isDark; + }, + toggleLayout: function() { + this.vlayout = !this.vlayout; + localStorage.vlayout = this.vlayout; + }, } }); +Vue.component('service', { + props: ['item'], + template: `
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+

{{ item.name }}

+

{{ item.subtitle }}

+
+
+
+ #{{ item.tag }} +
+
+
+
` +}); -function getConfig() { - return fetch('config.yml').then(function(response) { - if (response.status !== 200) { - return; - } - - return response.text().then(function(body){ - return jsyaml.load(body); - }); +if ('serviceWorker' in navigator) { + window.addEventListener('load', function () { + navigator.serviceWorker.register('worker.js'); }); -} \ No newline at end of file +}