Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
m1k1o
GitHub Repository: m1k1o/neko
Path: blob/master/client/src/plugins/swal.ts
1301 views
1
import Vue from 'vue'
2
3
import { SweetAlertOptions } from 'sweetalert2'
4
import Swal from 'sweetalert2/dist/sweetalert2.js'
5
6
type VueSwalInstance = typeof Swal.fire
7
8
declare module 'vue/types/vue' {
9
interface Vue {
10
$swal: VueSwalInstance
11
}
12
13
interface VueConstructor {
14
swal: VueSwalInstance
15
}
16
}
17
18
interface VueSweetalert2Options extends SweetAlertOptions {
19
// includeCss?: boolean;
20
}
21
22
class VueSweetalert2 {
23
static install(vue: Vue | any, options?: VueSweetalert2Options): void {
24
const swalFunction = (...args: [SweetAlertOptions]) => {
25
if (options) {
26
const mixed = Swal.mixin(options)
27
28
return mixed.fire(...args)
29
}
30
31
return Swal.fire(...args)
32
}
33
34
let methodName: string | number | symbol
35
36
for (methodName in Swal) {
37
// @ts-ignore
38
if (Object.prototype.hasOwnProperty.call(Swal, methodName) && typeof Swal[methodName] === 'function') {
39
// @ts-ignore
40
swalFunction[methodName] = ((method) => {
41
return (...args: any[]) => {
42
// @ts-ignore
43
return Swal[method](...args)
44
}
45
})(methodName)
46
}
47
}
48
49
vue['swal'] = swalFunction
50
51
// add the instance method
52
if (!vue.prototype.hasOwnProperty('$swal')) {
53
vue.prototype.$swal = swalFunction
54
}
55
}
56
}
57
58
export default VueSweetalert2
59
60