|
1 | | -export const flash = { |
2 | | - methods: { |
3 | | - /** |
4 | | - * Flash a message to the event stream in the browser. |
5 | | - */ |
6 | | - flash: function (message: string, title: string, severity: string = 'info'): void { |
7 | | - severity = severity || 'info'; |
8 | | - if (['danger', 'fatal', 'error'].includes(severity)) { |
9 | | - severity = 'error'; |
10 | | - } |
11 | | - |
12 | | - // @ts-ignore |
13 | | - window.events.$emit('flash', { message, title, severity }); |
14 | | - }, |
15 | | - |
16 | | - /** |
17 | | - * Clear all of the flash messages from the screen. |
18 | | - */ |
19 | | - clearFlashes: function (): void { |
20 | | - // @ts-ignore |
21 | | - window.events.$emit('clear-flashes'); |
22 | | - }, |
23 | | - |
24 | | - /** |
25 | | - * Helper function to flash a normal success message to the user. |
26 | | - */ |
27 | | - success: function (message: string): void { |
28 | | - this.flash(message, 'Success', 'success'); |
29 | | - }, |
30 | | - |
31 | | - /** |
32 | | - * Helper function to flash a normal info message to the user. |
33 | | - */ |
34 | | - info: function (message: string): void { |
35 | | - this.flash(message, 'Info', 'info'); |
36 | | - }, |
37 | | - |
38 | | - /** |
39 | | - * Helper function to flash a normal warning message to the user. |
40 | | - */ |
41 | | - warning: function (message: string): void { |
42 | | - this.flash(message, 'Warning', 'warning'); |
43 | | - }, |
44 | | - |
45 | | - /** |
46 | | - * Helper function to flash a normal error message to the user. |
47 | | - */ |
48 | | - error: function (message: string): void { |
49 | | - this.flash(message, 'Error', 'danger'); |
50 | | - }, |
| 1 | +import {ComponentOptions} from "vue"; |
| 2 | +import {Vue} from "vue/types/vue"; |
| 3 | + |
| 4 | +export interface FlashInterface { |
| 5 | + flash(message: string, title: string, severity: string): void; |
| 6 | + |
| 7 | + clear(): void, |
| 8 | + |
| 9 | + success(message: string): void, |
| 10 | + |
| 11 | + info(message: string): void, |
| 12 | + |
| 13 | + warning(message: string): void, |
| 14 | + |
| 15 | + error(message: string): void, |
| 16 | +} |
| 17 | + |
| 18 | +class Flash implements FlashInterface { |
| 19 | + flash(message: string, title: string, severity: string = 'info'): void { |
| 20 | + severity = severity || 'info'; |
| 21 | + if (['danger', 'fatal', 'error'].includes(severity)) { |
| 22 | + severity = 'error'; |
| 23 | + } |
| 24 | + |
| 25 | + // @ts-ignore |
| 26 | + window.events.$emit('flash', {message, title, severity}); |
| 27 | + } |
| 28 | + |
| 29 | + clear(): void { |
| 30 | + // @ts-ignore |
| 31 | + window.events.$emit('clear-flashes'); |
51 | 32 | } |
| 33 | + |
| 34 | + success(message: string): void { |
| 35 | + this.flash(message, 'Success', 'success'); |
| 36 | + } |
| 37 | + |
| 38 | + info(message: string): void { |
| 39 | + this.flash(message, 'Info', 'info'); |
| 40 | + } |
| 41 | + |
| 42 | + warning(message: string): void { |
| 43 | + this.flash(message, 'Warning', 'warning'); |
| 44 | + } |
| 45 | + |
| 46 | + error(message: string): void { |
| 47 | + this.flash(message, 'Error', 'error'); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export const FlashMixin: ComponentOptions<Vue> = { |
| 52 | + methods: { |
| 53 | + '$flash': function () { |
| 54 | + return new Flash(); |
| 55 | + } |
| 56 | + }, |
52 | 57 | }; |
0 commit comments