aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/SystemNotificationManager.vue
blob: 0acef248002e9506d38e4f335e303d84b56b2216 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<template>
    <transition-group name="system-notification-slide"
                      :class="'system-notifications ' + (placement === 'topcenter' ? 'top-center' : 'bottom-right')"
                      tag="div"
                      role="alert"
                      appear
    >
        <system-notification v-for="notification in allNotifications"
                             :key="`message-${notification.key}`"
                             :notification="notification"
                             @destroyMe="destroyNotification(notification)"
        ></system-notification>
    </transition-group>
</template>

<script>
import SystemNotification from './SystemNotification.vue';

export default {
    name: 'SystemNotificationManager',
    components: { SystemNotification },
    props: {
        appendAllTo: String,
        notifications: {
            type: [Array, Object],
            default: () => []
        },
        placement: {
            type: String,
            default: 'topcenter',
            validator: value => {
                return ['topcenter', 'bottomright'].includes(value);
            }
        }
    },
    data() {
        return {
            allNotifications: [],
            counter: 0,
            stoppedNotifications: false
        }
    },
    methods: {
        destroyNotification(notification) {
            this.allNotifications = this.allNotifications.filter(n => n !== notification);
        }
    },
    created() {
        if (Array.isArray(this.notifications)) {
            this.allNotifications = [...this.notifications];
        } else {
            this.allNotifications = Object.values(this.notifications);
        }
    },
    mounted() {
        this.globalOn('push-system-notification', notification => {
            this.allNotifications.push({
                key: this.counter++,
                ...notification
            });
        });

        window.addEventListener('keydown', evt => {
            if (evt.altKey && evt.ctrlKey && evt.code === 'KeyT') {
                this.stoppedNotifications = !this.stoppedNotifications;

                const event = this.stoppedNotifications ? 'disrupt-system-notifications' : 'resume-system-notifications';
                this.globalEmit(event);
            }
        });
    }
}
</script>