|
| 1 | +<template> |
| 2 | + <div v-if="notifications.length > 0"> |
| 3 | + <transition-group tag="div" class="mb-2" name="fade"> |
| 4 | + <div :class="item.class" class="lg:inline-flex mb-2" role="alert" :key="index" v-for="(item, index) in notifications"> |
| 5 | + <span class="title" v-html="item.title" v-if="item.title.length > 0"></span> |
| 6 | + <span class="message" v-html="item.message"></span> |
| 7 | + </div> |
| 8 | + </transition-group> |
| 9 | + </div> |
| 10 | +</template> |
| 11 | + |
| 12 | +<script> |
| 13 | + export default { |
| 14 | + name: 'flash', |
| 15 | + props: { |
| 16 | + timeout: { |
| 17 | + type: Number, |
| 18 | + default: 0, |
| 19 | + }, |
| 20 | + types: { |
| 21 | + type: Object, |
| 22 | + default: function () { |
| 23 | + return { |
| 24 | + base: 'alert', |
| 25 | + success: 'alert success', |
| 26 | + info: 'alert info', |
| 27 | + warning: 'alert warning', |
| 28 | + error: 'alert error', |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + }, |
| 33 | +
|
| 34 | + data: function () { |
| 35 | + return { |
| 36 | + notifications: [], |
| 37 | + }; |
| 38 | + }, |
| 39 | +
|
| 40 | + /** |
| 41 | + * Listen for flash events. |
| 42 | + */ |
| 43 | + created: function () { |
| 44 | + const self = this; |
| 45 | + window.events.$on('flash', function (data) { |
| 46 | + self.flash(data.message, data.title, data.severity); |
| 47 | + }); |
| 48 | +
|
| 49 | + window.events.$on('clear-flashes', function () { |
| 50 | + self.clear(); |
| 51 | + }); |
| 52 | + }, |
| 53 | +
|
| 54 | + methods: { |
| 55 | + /** |
| 56 | + * Flash a message to the screen when a flash event is emitted over |
| 57 | + * the global event stream. |
| 58 | + * |
| 59 | + * @param {string} message |
| 60 | + * @param {string} title |
| 61 | + * @param {string} severity |
| 62 | + */ |
| 63 | + flash: function (message, title, severity) { |
| 64 | + this.notifications.push({ |
| 65 | + message, severity, title, class: this.types[severity] || this.types.base, |
| 66 | + }); |
| 67 | +
|
| 68 | + if (this.timeout > 0) { |
| 69 | + setTimeout(this.hide, this.timeout); |
| 70 | + } |
| 71 | + }, |
| 72 | +
|
| 73 | + /** |
| 74 | + * Clear all of the flash messages from the screen. |
| 75 | + */ |
| 76 | + clear: function () { |
| 77 | + this.notifications = []; |
| 78 | + window.events.$emit('flashes-cleared'); |
| 79 | + }, |
| 80 | +
|
| 81 | + /** |
| 82 | + * Hide a notification after a given amount of time. |
| 83 | + * |
| 84 | + * @param {int} item |
| 85 | + */ |
| 86 | + hide: function (item = this.notifications[0]) { |
| 87 | + let key = this.notifications.indexOf(item); |
| 88 | + this.notifications.splice(key, 1); |
| 89 | + }, |
| 90 | + } |
| 91 | + }; |
| 92 | +</script> |
0 commit comments