diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2019-02-18 00:23:20 -0800 |
---|---|---|
committer | Bastien Wirtz <bastien.wirtz@gmail.com> | 2019-02-18 00:23:20 -0800 |
commit | 9baec9aec294656fb632123906b4ac6a712267ba (patch) | |
tree | 0dff55723b3503bfc81f17bfe1d10a8f4298d56f /app.js | |
parent | e41196e76e6b184fc918fb44adc9af0ff3fdae30 (diff) | |
download | homer-9baec9aec294656fb632123906b4ac6a712267ba.tar.gz homer-9baec9aec294656fb632123906b4ac6a712267ba.tar.zst homer-9baec9aec294656fb632123906b4ac6a712267ba.zip |
Add offline cache + improve layout
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 67 |
1 files changed, 39 insertions, 28 deletions
@@ -1,42 +1,53 @@ | |||
1 | var app = new Vue({ | 1 | const app = new Vue({ |
2 | el: '#app', | 2 | el: '#app', |
3 | data: { | 3 | data: { |
4 | config: null, | 4 | config: null, |
5 | filter: '' | 5 | offline: false, |
6 | filter: '', | ||
6 | }, | 7 | }, |
7 | beforeCreate() { | 8 | created: function () { |
8 | let that = this; | 9 | let that = this; |
9 | 10 | ||
10 | return getConfig().then(function (config) { | 11 | this.checkOffline(); |
11 | const size = 3; | 12 | that.getConfig().then(function (config) { |
12 | config.services.forEach(function (service) { | ||
13 | service.rows = []; | ||
14 | items = service.items; | ||
15 | while (items.length) { | ||
16 | service.rows.push(items.splice(0, size)); | ||
17 | } | ||
18 | |||
19 | if (service.rows.length) { | ||
20 | let last = service.rows.length - 1; | ||
21 | service.rows[last] = service.rows[last].concat(Array(size - service.rows[last].length)); | ||
22 | } | ||
23 | }); | ||
24 | that.config = config; | 13 | that.config = config; |
25 | }).catch(function () { | 14 | }).catch(function () { |
26 | console.error('Fail to get config'); | 15 | that.offline = true; |
27 | }); | 16 | }); |
17 | |||
18 | document.addEventListener('visibilitychange', function () { | ||
19 | if (document.visibilityState == "visible") { | ||
20 | that.checkOffline(); | ||
21 | } | ||
22 | }, false); | ||
23 | }, | ||
24 | methods: { | ||
25 | checkOffline: function () { | ||
26 | let that = this; | ||
27 | return fetch(window.location.href + "?alive", { | ||
28 | method: 'HEAD', | ||
29 | cache: 'no-store' | ||
30 | }).then(function () { | ||
31 | that.offline = false; | ||
32 | }).catch(function () { | ||
33 | that.offline = true; | ||
34 | }); | ||
35 | }, | ||
36 | getConfig: function (event) { | ||
37 | return fetch('config.yml').then(function (response) { | ||
38 | if (response.status != 200) { | ||
39 | return | ||
40 | } | ||
41 | return response.text().then(function (body) { | ||
42 | return jsyaml.load(body); | ||
43 | }); | ||
44 | }); | ||
45 | }, | ||
28 | } | 46 | } |
29 | }); | 47 | }); |
30 | 48 | ||
31 | 49 | if ('serviceWorker' in navigator) { | |
32 | function getConfig() { | 50 | window.addEventListener('load', function () { |
33 | return fetch('config.yml').then(function (response) { | 51 | navigator.serviceWorker.register('/worker.js'); |
34 | if (response.status !== 200) { | ||
35 | return; | ||
36 | } | ||
37 | |||
38 | return response.text().then(function (body) { | ||
39 | return jsyaml.load(body); | ||
40 | }); | ||
41 | }); | 52 | }); |
42 | } | 53 | } |