-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathinterface.ts
111 lines (105 loc) · 2.83 KB
/
interface.ts
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
109
110
111
import _Vue from "vue";
import ToastContainer from "../components/VtToastContainer.vue";
import {
ToastContent,
ToastOptions,
ToastID,
PluginOptions,
ToastOptionsAndRequiredContent
} from "../types";
import events from "./events";
import { TYPE, EVENTS } from "./constants";
import { getId, isUndefined } from "./utils";
const ToastInterface = (Vue: typeof _Vue, globalOptions?: PluginOptions) => {
const containerComponent = new (Vue.extend(ToastContainer))({
el: document.createElement("div"),
propsData: globalOptions
});
const onMounted = globalOptions?.onMounted;
if (!isUndefined(onMounted)) {
onMounted(containerComponent);
}
/**
* Display a toast
*/
const toast = (content: ToastContent, options?: ToastOptions): ToastID => {
const props: ToastOptionsAndRequiredContent & {
id: ToastID;
} = Object.assign({}, { id: getId(), type: TYPE.DEFAULT }, options, {
content
});
events.$emit(EVENTS.ADD, props);
return props.id;
};
/**
* Clear all toasts
*/
toast.clear = () => events.$emit(EVENTS.CLEAR);
/**
* Update Plugin Defaults
*/
toast.updateDefaults = (update: PluginOptions) => {
events.$emit(EVENTS.UPDATE_DEFAULTS, update);
};
/**
* Dismiss toast specified by an id
*/
toast.dismiss = (id: ToastID) => {
events.$emit(EVENTS.DISMISS, id);
};
/**
* Update Toast
*/
function updateToast(
id: ToastID,
{ content, options }: { content?: ToastContent; options?: ToastOptions },
create?: false
): void;
function updateToast(
id: ToastID,
{ content, options }: { content: ToastContent; options?: ToastOptions },
create?: true
): void;
function updateToast(
id: ToastID,
{ content, options }: { content?: ToastContent; options?: ToastOptions },
create = false
): void {
events.$emit(EVENTS.UPDATE, {
id,
options: Object.assign({}, options, { content }),
create
});
}
toast.update = updateToast;
/**
* Display a success toast
*/
toast.success = (
content: ToastContent,
options?: ToastOptions & { type?: TYPE.SUCCESS }
) => toast(content, Object.assign({}, options, { type: TYPE.SUCCESS }));
/**
* Display an info toast
*/
toast.info = (
content: ToastContent,
options?: ToastOptions & { type?: TYPE.INFO }
) => toast(content, Object.assign({}, options, { type: TYPE.INFO }));
/**
* Display an error toast
*/
toast.error = (
content: ToastContent,
options?: ToastOptions & { type?: TYPE.ERROR }
) => toast(content, Object.assign({}, options, { type: TYPE.ERROR }));
/**
* Display a warning toast
*/
toast.warning = (
content: ToastContent,
options?: ToastOptions & { type?: TYPE.WARNING }
) => toast(content, Object.assign({}, options, { type: TYPE.WARNING }));
return toast;
};
export default ToastInterface;