-
Notifications
You must be signed in to change notification settings - Fork 29
/
vModal.vue
176 lines (174 loc) · 4.07 KB
/
vModal.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<template>
<div role="dialog"
v-bind:class="{
'modal':true,
'fade':effect === 'fade',
'zoom':effect === 'zoom'
}"
>
<div v-bind:class="{'modal-dialog':true,'modal-lg':large,'modal-sm':small}" role="document"
v-bind:style="{width: optionalWidth}">
<div class="modal-content">
<slot name="modal-header">
<div class="modal-header">
<button type="button" class="close" @click="close"><span>×</span></button>
<h4 class="modal-title">
<slot name="title">
<i class="iconfont"></i> {{ title }}
</slot>
</h4>
</div>
</slot>
<slot name="modal-body">
<div class="modal-body"></div>
</slot>
<slot name="modal-footer">
<div class="modal-footer">
<button type="button" class="btn btn-default" @click="close">{{ cancelText }}</button>
<button type="button" class="btn btn-primary" @click="callback">{{ okText }}</button>
</div>
</slot>
</div>
</div>
</div>
</template>
<script>
import {coerce, getScrollBarWidth} from 'jspath/common/utils.js'
import $ from 'jspath/common/nodeList'
export default {
props: {
name: {
type: String,
default: 'vModal_' + Math.floor(Math.random() * 1000 + 1)
},
okText: {
type: String,
default: 'Save changes'
},
cancelText: {
type: String,
default: 'Close'
},
title: {
type: String,
default: '温馨提示!'
},
show: {
required: true,
type: Boolean,
coerce: coerce.boolean
},
width: {
default: null
},
effect: {
type: String,
default: null
},
backdrop: {
type: Boolean,
coerce: coerce.boolean,
default: true
},
large: {
type: Boolean,
coerce: coerce.boolean,
default: false
},
small: {
type: Boolean,
coerce: coerce.boolean,
default: false
}
},
computed: {
optionalWidth () {
if (this.width === null) {
return null
} else if (Number.isInteger(this.width)) {
return this.width + 'px'
}
return this.width
}
},
watch: {
show (val) {
if (val) {
this.isShow(true)
} else {
this.close()
}
}
},
methods: {
isShow (val) {
const el = this.$el
const body = document.body
if (val) {
const scrollBarWidth = getScrollBarWidth()
$(el).find('.modal-content').focus()
el.style.display = 'block'
setTimeout(() => $(el).addClass('in'), 0)
$(body).addClass('modal-open')
if (scrollBarWidth !== 0) {
body.style.paddingRight = scrollBarWidth + 'px'
}
if (this.backdrop) {
$(el).on('click', e => {
if (e.target === el) this.show = false
})
}
} else {
body.style.paddingRight = null
$(body).removeClass('modal-open')
$(el).removeClass('in').on('transitionend', () => {
$(el).off('click transitionend')
el.style.display = 'none'
})
}
},
close () {
this.$emit('backUpState', {show: false, name: this.name})
this.isShow(false)
},
callback () {
this.$emit('backUpState', {show: false, theResult: true, name: this.name})
this.isShow(false)
}
}
}
</script>
<style>
.modal {
transition: all 0.3s ease;
}
.modal.in {
background-color: rgba(0,0,0,0.5);
}
.modal.zoom .modal-dialog {
-webkit-transform: scale(0.1);
-moz-transform: scale(0.1);
-ms-transform: scale(0.1);
transform: scale(0.1);
top: 300;
opacity: 0;
-webkit-transition: all 1.3s;
-moz-transition: all 1.3s;
transition: all 1.3s;
}
.modal.zoom.in .modal-dialog {
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
-webkit-transform: translate3d(0, 300px, 0);
transform: translate3d(0, 300px, 0);
opacity: 1;
}
.modal-title {
line-height: normal;
}
.modal-body {
padding:30px;
}
</style>