X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=app.js;h=e79611069f9d779d4a09e969157ec805b0c24628;hb=fcf5f412a55b37033006c0c1866bc4693ca8bd57;hp=93902d803cbef8e1c4e72e9f2997b645198fae55;hpb=7fd9dc6f10a30748cf63caf61ee71f602407d6ac;p=github%2Fbastienwirtz%2Fhomer.git diff --git a/app.js b/app.js index 93902d8..e796110 100644 --- a/app.js +++ b/app.js @@ -5,27 +5,29 @@ const app = new Vue({ offline: false, filter: '', vlayout: true, - isDark: null + isDark: null, + showMenu: false }, created: async function () { let that = this; - this.isDark = 'overrideDark' in localStorage ? + 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(); 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. - if (this.config.message.url) { + 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']) { @@ -78,8 +80,60 @@ const app = new Vue({ }, toggleLayout: function() { this.vlayout = !this.vlayout; - localStorage.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); } }); @@ -87,7 +141,7 @@ Vue.component('service', { props: ['item'], template: `
- +
@@ -115,6 +169,6 @@ Vue.component('service', { if ('serviceWorker' in navigator) { window.addEventListener('load', function () { - navigator.serviceWorker.register('/worker.js'); + navigator.serviceWorker.register('worker.js'); }); }