forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n3Modal.vue
executable file
·137 lines (130 loc) · 3.31 KB
/
n3Modal.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
<template>
<div :class="classObj">
<div class="{{prefixCls}}-modal-dialog"
:style="{'width': width}">
<div class="{{prefixCls}}-modal-content">
<slot name="header">
<div class="{{prefixCls}}-modal-header">
<button type="button" class="{{prefixCls}}-close" @click="close"><span>×</span></button>
<h4 class="{{prefixCls}}-modal-title" >{{title}}</h4>
</div>
</slot>
<div class="{{prefixCls}}-modal-body">
<slot name="body"></slot>
</div>
<slot name="footer">
<div class="{{prefixCls}}-modal-footer">
<n3-button @click="close">取消</n3-button>
<n3-button type="primary" @click="confirm">确定</n3-button>
</div>
</slot>
</div>
</div>
</div>
</template>
<script>
import getScrollBarWidth from './utils/getScrollBarWidth'
import EventListener from './utils/EventListener'
import n3Button from './n3Button'
import type from './utils/type'
import element from './utils/element'
export default {
props: {
title: {
type: String,
default: ''
},
show: {
require: true,
type: Boolean,
twoWay: true
},
width: {
type: String,
default: '600px'
},
effect: {
type: String,
default: 'fade'
},
backdrop: {
type: Boolean,
default: true
},
onShow: {
type: Function
},
onHide: {
type: Function
},
onConfirm: {
type: Function
},
prefixCls: {
type: String,
default: 'n3'
}
},
components: {
n3Button
},
computed: {
classObj () {
let {prefixCls, effect} = this
let klass = {}
klass[prefixCls + '-modal'] = true
klass[prefixCls + '-modal-' + effect] = true
return klass
}
},
watch: {
show (val) {
if (val) {
if (type.isFunction(this.onShow)) {
this.onShow()
}
} else {
if (type.isFunction(this.onHide)) {
this.onHide()
}
}
const el = this.$el
const body = document.body
const scrollBarWidth = getScrollBarWidth()
if (val) {
el.querySelector('.' + this.prefixCls + '-modal-content').focus()
el.style.display = 'block'
setTimeout(() => element.addClass(el, this.prefixCls + '-modal-in'), 0)
element.addClass(body, this.prefixCls + '-modal-open')
if (!scrollBarWidth) {
element.addClass(body, this.prefixCls + '-modal-hide-y')
}
if (this.backdrop) {
this._blurModalContentEvent = EventListener.listen(this.$el, 'click', (e) => {
if (e.target === el) this.show = false
})
}
} else {
if (this._blurModalContentEvent) this._blurModalContentEvent.remove()
element.removeClass(el, this.prefixCls + '-modal-in')
setTimeout(() => {
el.style.display = 'none'
element.removeClass(body, this.prefixCls + '-modal-open')
element.removeClass(body, this.prefixCls + '-modal-hide-y')
body.style.paddingRight = '0'
}, 300)
}
}
},
methods: {
close () {
this.show = false
},
confirm () {
if (type.isFunction(this.onConfirm)) {
this.onConfirm()
}
}
}
}
</script>