summaryrefslogtreecommitdiff
path: root/src/InstallNotification.vue
blob: a60ec9c9864ee8cc59be3178277a9b31dc7f2d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<template>
  <div class="notification is-primary" v-show="deferredPrompt != undefined">
    <button class="delete" @click="deferredPrompt = undefined"></button>
    <div class="has-text-centered">Add to your homescreen for fast and offline access?</div>
    <div class="has-text-centered install-button">
      <button class="button is-medium" @click="deferredPrompt.prompt()">
        <span class="mdi mdi-download"></span>
        <span>&nbsp;Install</span>
      </button>
    </div>
  </div>
</template>

<style scoped lang="scss">
.install-button {
  margin-top: 1em;
  margin-bottom: 0.5em;
}
</style>

<script>
import Vue from 'vue';

export default Vue.component('install-notification', {
  data: function() {
    return {
      deferredPrompt: undefined,
    };
  },
  methods: {
    promptInstall: function(p) {
      this.$ga.event('PWA', 'available');
      p.preventDefault();
      this.deferredPrompt = p;
      this.deferredPrompt.userChoice.then((choiceResult) => {
        this.$ga.event('PWA', 'popup', choiceResult.outcome);
        this.deferredPrompt = null;
      });
    },
    installed: function(e) {
      this.$ga.event('PWA', 'installed');
    },
  },
  created: function() {
    window.addEventListener('beforeinstallprompt', this.promptInstall);
    window.addEventListener('appinstalled', this.installed);
  },
  destroyed: function() {
    window.removeEventListener('beforeinstallprompt', this.promptInstall);
    window.removeEventListener('appinstalled', this.installed);
  },
});
</script>