aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/apps/SystemNotificationManager.vue
blob: 00b4f95ea372484344ab31de8c62a625e8362266 (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
74
75
76
77
78
79
<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"
            :placement="placement"
            @destroyMe="destroyNotification(notification)"
        ></system-notification>
    </transition-group>
</template>

<script>
import SystemNotification from '@/vue/components/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: {
        addNotification(notification) {
            this.allNotifications.push({
                key: this.counter++,
                ...notification,
            });
        },
        destroyNotification(notification) {
            this.allNotifications = this.allNotifications.filter((n) => n !== notification);
        },
    },
    created() {
        if (Array.isArray(this.notifications)) {
            this.notifications.map(this.addNotification);
        } else {
            Object.values(this.notifications).map(this.addNotification);
        }
    },
    mounted() {
        this.globalOn('push-system-notification', this.addNotification);

        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>