]> git.immae.eu Git - github/bastienwirtz/homer.git/blobdiff - app.js
Merge pull request #8 from fbartels/subdir
[github/bastienwirtz/homer.git] / app.js
diff --git a/app.js b/app.js
index 26979755f3acb2bb43eac226bf92207f271c9181..40830da1a457f3053b4ff230b54e0814c0add6a2 100644 (file)
--- a/app.js
+++ b/app.js
-var app = new Vue({
+const app = new Vue({
     el: '#app',
     data: {
         config: null,
-        filter: ''
+        offline: false,
+        filter: '',
+        vlayout: true,
+        isDark: null
     },
-    beforeCreate () {
-        var that = this;
-        return getConfig().then(function (config) {
-            // Splice services list into groups of 3 for flex column display
-            var size = 3;
-            config.services.forEach(function(service) {
-                service.rows = [];
-                items = service.items;
-                while (items.length) {
-                    service.rows.push(items.splice(0, size));
-                }
+    created: function () {
+        let that = this;
 
-                if (service.rows.length) {
-                    var last = service.rows.length-1;
-                    service.rows[last] = service.rows[last].concat(Array(size - service.rows[last].length));
-                }
-            });
+        this.isDark = 'overrideDark' in localStorage ?
+            JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches;
+
+        if ('vlayout' in localStorage) {
+            this.vlayout = JSON.parse(localStorage.vlayout)
+        }
+
+        this.checkOffline();
+        that.getConfig().then(function (config) {
             that.config = config;
+        }).catch(function () {
+            that.offline = true;
         });
-    }
-});
 
-
-function getConfig() {
-    return new Promise(function (resolve, reject) {
-        var xhr = new XMLHttpRequest();
-        xhr.open('GET', 'config.yml');
-        xhr.onload = function () {
-            if (this.status >= 200 && this.status < 300) {
-                try {
-                    var data = jsyaml.load(xhr.response);
-                    resolve(data);
-                } catch (e) {
-                    console.error('fail to parse config file');
-                    reject();
+        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
                 }
-            } else {
-                reject({
-                    status: this.status,
-                    statusText: xhr.statusText
+                return response.text().then(function (body) {
+                    return jsyaml.load(body);
                 });
-            }
-        };
-        xhr.onerror = function () {
-            reject({
-                status: this.status,
-                statusText: xhr.statusText
             });
-        };
-        xhr.send();
+        },
+        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: `<div>
+    <div class="card">
+        <a :href="item.url">
+            <div class="card-content">
+                <div class="media">
+                    <div v-if="item.logo" class="media-left">
+                        <figure class="image is-48x48">
+                            <img :src="item.logo" />
+                        </figure>
+                    </div>
+                    <div v-if="item.icon" class="media-left">
+                        <figure class="image is-48x48">
+                            <i style="font-size: 35px" :class="item.icon"></i>
+                        </figure>
+                    </div>
+                    <div class="media-content">
+                        <p class="title is-4">{{ item.name }}</p>
+                        <p class="subtitle is-6">{{ item.subtitle }}</p>
+                    </div>
+                </div>
+                <div class="tag" :class="item.tagstyle" v-if="item.tag">
+                    <strong class="tag-text">#{{ item.tag }}</strong>
+                </div>
+            </div>
+        </a>
+    </div></div>`
+});
+
+if ('serviceWorker' in navigator) {
+    window.addEventListener('load', function () {
+        navigator.serviceWorker.register('worker.js');
     });
-}
\ No newline at end of file
+}