summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/NotificationsAlerts.vue
blob: e1aafe3ef8f5d5642c1e5ab99e03ea9ca81c9f48 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<i18n>
{
  "en": {
    "noNotifications": "No notifications.",
    "new": "New",
    "old": "Old",
    "notifications": "Notifications"
  },
  "de": {
    "noNotifications": "Keine Benachrichtigungen.",
    "new": "Neu",
    "old": "Alt",
    "notifications": "Benachrichtigungen"
  }
}
</i18n>

<template>
  <div>
    <v-menu v-model="menu" offset-y left :close-on-content-click="false" :nudge-bottom="10" content-class="notifications" attach>
      <template #activator="{ on }">
        <v-btn icon v-on="on" class="tutorial-element label-top-left element-icon" style="--label-number: '4'">
          <v-badge :value="!menu && newAlertCount" color="red darken-3" bottom>
                <span slot="badge">{{newAlertCount}}</span>
                <v-icon>notifications</v-icon>
          </v-badge>
        </v-btn>
      </template>

      <v-card>
        <v-card-text>
          <div style="max-height: 70vh; overflow: auto">
            <div v-if="alerts.length === 0" class="text-center" style="width: 100%">{{ $t('noNotifications') }}</div>
            <div v-else class="flex-between-center">
              <v-subheader v-if="newAlertCount > 0">{{ $t('new') }}</v-subheader>
              <v-subheader v-else>{{ $t('notifications') }}</v-subheader>
              <v-btn @click="clearAll" icon small style="margin-right: 16px"><v-icon>clear_all</v-icon></v-btn>
            </div>
            <template v-for="(a, index) in alerts">
              <v-alert
                :key="'alert' + index"
                transition="scale-transition"
                :value="a.show"
                :type="a.type"
                :color="a.color"
                :icon="a.icon"
                class="alert"
              >
                <div class="flex-between-center">
                  <div>{{ a.text }}</div>
                  <v-btn @click="closeAlert(a)" small :ripple="false" style="margin: 0; margin-top: -4px" icon><v-icon>close</v-icon></v-btn>
                </div>
              </v-alert>
              <v-subheader :key="'subheader' + index" v-if="newAlertCount < alerts.length && index == newAlertCount - 1">{{ $t('old') }}</v-subheader>
            </template>
          </div>
        </v-card-text>
      </v-card>
    </v-menu>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'NotificationsAlerts',
  data () {
    return {
      menu: false
    }
  },
  computed: {
    ...mapState('notifications', ['newAlertCount', 'alerts']),
    alertsShown () {
      return this.alerts.some(a => a.show)
    }
  },
  watch: {
    menu (value) {
      if (!value) {
        this.$socket.emit('notifications resetNewAlertCount')
        this.resetNewAlertCount()
      }
    }
  },
  methods: {
    ...mapMutations('notifications', ['resetNewAlertCount', 'removeAlert']),
    closeAlert (a, emit = true) {
      if (emit) this.$socket.emit('notifications closeAlert', a)
      this.removeAlert(a.id)
      if (!this.alertsShown) this.menu = false
    },
    clearAll (emit = true) {
      if (emit) this.$socket.emit('notifications clearAll')
      var timeout = this.alerts.length * 50
      setTimeout(this.closeMenu, timeout)
      var removeAlert = this.removeAlert
      this.alerts.forEach(a => {
        timeout -= 50
        setTimeout(function () {
          removeAlert(a.id)
        }, timeout)
      })
    },
    closeMenu () {
      this.menu = false
    }
  },
  created () {
    this.$socket.on('notifications clearAll', () => {
      this.clearAll(false)
    })
    this.$socket.on('notifications newAlert', data => {
      this.$alert(data, false)
    })
    this.$socket.on('notifications closeAlert', id => {
      this.closeAlert(id, false)
    })
    this.$socket.on('notifications resetNewAlertCount', () => {
      this.resetNewAlertCount()
    })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

@media (max-width: 960px) {
  .notifications {
    right: 12px;
    max-width: 100%;
    left: 12px !important;
  }
}

@media (min-width: 961px) {
  .notifications {
    width: 600px;
  }
}

.flex-between-center {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

</style>