-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathVtToastContainer.vue
156 lines (147 loc) · 4.4 KB
/
VtToastContainer.vue
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
<div>
<div v-for="pos in positions" :key="pos">
<VtTransition
:transition="defaults.transition"
:transition-duration="defaults.transitionDuration"
:class="getClasses(pos)"
>
<Toast
v-for="toast in getPositionToasts(pos)"
:key="toast.id"
v-bind="toast"
/>
</VtTransition>
</div>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import { EVENTS, POSITION, VT_NAMESPACE } from "../ts/constants";
import PROPS, { PluginOptionsType } from "../ts/propValidators";
import {
PluginOptions,
ToastID,
ToastOptionsAndContent,
ToastOptionsAndRequiredContent,
} from "../types";
import { removeElement, isUndefined, isFunction } from "../ts/utils";
import Toast from "./VtToast.vue";
import VtTransition from "./VtTransition.vue";
export default Vue.extend({
components: { Toast, VtTransition },
props: Object.assign({}, PROPS.CORE_TOAST, PROPS.CONTAINER, PROPS.TRANSITION),
data() {
const data: {
count: number;
positions: POSITION[];
toasts: {
[toastId: number]: ToastOptionsAndRequiredContent;
[toastId: string]: ToastOptionsAndRequiredContent;
};
defaults: PluginOptionsType;
} = {
count: 0,
positions: Object.values(POSITION),
toasts: {},
defaults: {} as PluginOptionsType,
};
return data;
},
computed: {
toastArray(): ToastOptionsAndRequiredContent[] {
return Object.values(this.toasts);
},
filteredToasts(): ToastOptionsAndRequiredContent[] {
return this.defaults.filterToasts(this.toastArray);
},
},
beforeMount() {
this.setup(this.container);
const events = this.eventBus;
events.$on(EVENTS.ADD, this.addToast);
events.$on(EVENTS.CLEAR, this.clearToasts);
events.$on(EVENTS.DISMISS, this.dismissToast);
events.$on(EVENTS.UPDATE, this.updateToast);
events.$on(EVENTS.UPDATE_DEFAULTS, this.updateDefaults);
this.defaults = this.$props as PluginOptionsType;
},
methods: {
async setup(container: PluginOptionsType["container"]) {
if (isFunction(container)) {
container = await container();
}
removeElement(this.$el);
container.appendChild(this.$el);
},
setToast(props: ToastOptionsAndRequiredContent) {
if (!isUndefined(props.id)) {
this.$set(this.toasts, props.id, props);
}
},
addToast(params: ToastOptionsAndRequiredContent) {
const props = Object.assign(
{},
this.defaults,
params.type &&
this.defaults.toastDefaults &&
this.defaults.toastDefaults[params.type],
params
);
const toast = this.defaults.filterBeforeCreate(props, this.toastArray);
toast && this.setToast(toast);
},
dismissToast(id: ToastID) {
const toast = this.toasts[id];
if (!isUndefined(toast) && !isUndefined(toast.onClose)) {
toast.onClose();
}
this.$delete(this.toasts, id);
},
clearToasts() {
Object.keys(this.toasts).forEach((id: ToastID) => {
this.dismissToast(id);
});
},
getPositionToasts(position: POSITION) {
const toasts = this.filteredToasts
.filter((toast) => toast.position === position)
.slice(0, this.defaults.maxToasts);
return this.defaults.newestOnTop ? toasts.reverse() : toasts;
},
updateDefaults(update: PluginOptions) {
// Update container if changed
if (!isUndefined(update.container)) {
this.setup(update.container);
}
this.defaults = Object.assign({}, this.defaults, update);
},
updateToast({
id,
options,
create,
}: {
id: ToastID;
options: ToastOptionsAndContent;
create: boolean;
}) {
if (this.toasts[id]) {
// If a timeout is defined, and is equal to the one before, change it
// a little so the progressBar is reset
if (options.timeout && options.timeout === this.toasts[id].timeout) {
options.timeout++;
}
this.setToast(Object.assign({}, this.toasts[id], options));
} else if (create) {
this.addToast(
Object.assign({}, { id }, options as ToastOptionsAndRequiredContent)
);
}
},
getClasses(position: POSITION) {
const classes = [`${VT_NAMESPACE}__container`, position];
return classes.concat(this.defaults.containerClassName);
},
},
});
</script>