aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/StudipMessageBox.vue
blob: 351b0added1c4aa33cbc2c5971ed07fe98ee164c (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
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
<template>
    <div v-if="!closed" :class="classNames" role="region" :aria-label="label" :aria-describedby="'messagebox-' + id">
        <div class="messagebox-icon"></div>
        <div class="messagebox-content" role="status" :id="'messagebox-' + id">
            <p class="messagebox-message">
                <slot></slot>
            </p>
            <button
                v-if="hasDetails"
                class="messagebox-button messagebox-details-toggle"
                href="#"
                :title="$gettext('Detailanzeige umschalten')"
                @click.prevent.stop="closedDetails = !closedDetails"
            >
                {{ $gettext('Details') }}
            </button>
            <div v-if="showDetails" class="messagebox-details">
                <slot name="details">
                    <ul>
                        <li v-for="(detail, index) in details" v-html="detail" :key="index"></li>
                    </ul>
                </slot>
            </div>
        </div>
        <button
            v-if="!hideClose"
            class="messagebox-button messagebox-close"
            role="button"
            :title="$gettext('Nachrichtenbox schließen')"
            @click.prevent="close()"
        ></button>
    </div>
</template>

<script>
export default {
    name: 'studip-message-box',
    props: {
        type: {
            type: String, // exception, error, success, info, warning
            default: 'info',
            validator(type) {
                return ['exception', 'error', 'warning', 'success', 'info'].indexOf(type) !== -1;
            },
        },
        details: {
            type: Array,
            default: () => [],
        },
        hideDetails: {
            type: Boolean,
            default: false,
        },
        hideClose: {
            type: Boolean,
            default: false,
        },
    },
    computed: {
        classNames() {
            return {
                messagebox: true,
                [`messagebox_${this.type}`]: true,
                details_hidden: !this.showDetails,
            };
        },
        hasDetails() {
            return !!this.$slots.details || this.details.length > 0;
        },
        showDetails() {
            return this.hasDetails && !this.closedDetails;
        },
        label() {
            switch (this.type) {
                case 'exception':
                    return this.$gettext('Systemfehler');
                case 'error':
                    return this.$gettext('Fehler');
                case 'warning':
                    return this.$gettext('Warnung');
                case 'info':
                    return this.$gettext('Hinweis');
                case 'success':
                    return this.$gettext('Erfolg');
            }

            return '';
        },
    },
    methods: {
        close() {
            this.closed = true;

            this.$emit('close');
        },
    },
    data() {
        return {
            closed: false,
            closedDetails: this.hideDetails,
            id: null,
        };
    },
    mounted() {
        this.id = this._uid;
    },
};
</script>