summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/NotificationsAlerts.vue
blob: 058cc4fcc60bf7f87894d4a24cc40dbeace56309 (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
<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">
      <v-btn icon slot="activator">
        <v-badge :value="newAlertCount" color="red darken-3" bottom>
              <span slot="badge">{{newAlertCount}}</span>
              <v-icon>notifications</v-icon>
        </v-badge>
      </v-btn>

      <v-card>
        <v-card-text>
          <div style="max-height: 70vh; overflow: auto">
            <div v-if="alerts.length === 0" class="text-xs-center" style="width: 100%">{{ $t('noNotifications') }}</div>
            <div v-else class="flex-between-center">
              <v-subheader v-if="newCount > 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"
              >
                <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="newCount < alerts.length && index == newCount - 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,
      newCount: 0
    }
  },
  computed: {
    ...mapState('notifications', ['newAlertCount', 'alerts']),
    alertsShown () {
      return this.alerts.some(a => a.show)
    }
  },
  watch: {
    menu (value) {
      if (value) {
        this.newCount = this.newAlertCount
        this.resetNewAlertCount()
      }
    }
  },
  methods: {
    ...mapMutations('notifications', ['resetNewAlertCount', 'removeAlert']),
    closeAlert (a) {
      this.removeAlert(a)
      if (!this.alertsShown) this.menu = false
    },
    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)
        }, timeout)
      })
    },
    closeMenu () {
      this.menu = false
    }
  }
}
</script>

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

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

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

  }
}

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

</style>