homer/app.js

175 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-02-18 09:23:20 +01:00
const app = new Vue({
2018-06-14 07:14:05 +02:00
el: '#app',
data: {
config: null,
2019-02-18 09:23:20 +01:00
offline: false,
filter: '',
2019-10-02 06:35:51 +02:00
vlayout: true,
isDark: null,
showMenu: false
2018-06-14 07:14:05 +02:00
},
created: async function () {
2018-06-17 00:48:28 +02:00
let that = this;
this.isDark = 'overrideDark' in localStorage ?
2019-10-08 07:32:30 +02:00
JSON.parse(localStorage.overrideDark) : matchMedia("(prefers-color-scheme: dark)").matches;
2018-06-17 00:48:28 +02:00
2019-10-08 07:32:30 +02:00
if ('vlayout' in localStorage) {
this.vlayout = JSON.parse(localStorage.vlayout)
}
2019-02-18 09:23:20 +01:00
this.checkOffline();
try {
this.config = await this.getConfig();
document.title = this.config.title + ' | Homer';
} catch (error) {
this.offline = true;
}
// Look for a new message if an endpoint is provided.
2020-01-15 01:37:04 +01:00
if (this.config.message && 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];
}
}
});
}
2019-02-18 09:23:20 +01:00
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);
});
});
},
getMessage: function (url) {
return fetch(url).then(function (response) {
if (response.status != 200) {
return;
}
return response.json();
});
},
2019-10-02 06:35:51 +02:00
toggleTheme: function() {
2019-10-08 07:32:30 +02:00
this.isDark = !this.isDark;
localStorage.overrideDark = this.isDark;
},
toggleLayout: function() {
this.vlayout = !this.vlayout;
localStorage.vlayout = this.vlayout;
},
toggleMenu: function() {
this.showMenu = !this.showMenu;
},
matchesFilter: function(item) {
return (item.name.toLowerCase().includes(this.filter.toLowerCase())
|| (item.tag && item.tag.toLowerCase().includes(this.filter.toLowerCase())))
},
firstMatchingService: function() {
for (group of this.config.services) {
for (item of group.items) {
if (this.matchesFilter(item)) {
return item;
}
}
}
return null;
},
navigateToFirstService: function(target) {
service = this.firstMatchingService();
if (service) {
window.open(service.url, target || service.target || '_self');
}
}
},
mounted() {
function isSmallScreen() {
return window.matchMedia('screen and (max-width: 1023px)').matches;
}
this._keyListener = function(e) {
if (e.key === '/') {
if (isSmallScreen()) {
this.showMenu = true;
}
Vue.nextTick(() => {
this.$refs.search.focus();
});
e.preventDefault();
}
if (e.key === 'Escape') {
this.filter = '';
this.$refs.search.blur();
if (isSmallScreen()) {
this.showMenu = false;
}
}
}
document.addEventListener('keydown', this._keyListener.bind(this));
},
beforeDestroy() {
document.removeEventListener('keydown', this._keyListener);
2018-06-14 07:14:05 +02:00
}
});
2019-02-20 07:20:46 +01:00
Vue.component('service', {
props: ['item'],
template: `<div>
<div class="card">
2020-01-13 07:04:26 +01:00
<a :href="item.url" :target="item.target">
2019-02-20 07:20:46 +01:00
<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>
2019-10-23 23:29:55 +02:00
<div class="tag" :class="item.tagstyle" v-if="item.tag">
<strong class="tag-text">#{{ item.tag }}</strong>
</div>
2019-02-20 07:20:46 +01:00
</div>
</a>
</div></div>`
});
2019-02-18 09:23:20 +01:00
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('worker.js');
2018-06-14 07:14:05 +02:00
});
2018-11-13 06:14:38 +01:00
}