summaryrefslogblamecommitdiffstats
path: root/webapp/src/components/NotificationsAlerts.vue
blob: 3f4f6d7df167f0b046637586ebcd5700a3103a59 (plain) (tree)



































































































































                                                                                                                                             
<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: scroll">
            <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>