-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (181 loc) · 4.01 KB
/
index.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* ElFormPopover 错误提示插件
* author saojun
* date 2021-11-18
* more detail : https://github.com/saojun1024/elformpopover
*
**/
var ElFormPopover = (function(){
let io = null
const ElFormPopover = {
template:`
<el-popover
title=""
trigger="manual"
:popper-class="'pop-tips'+' pop-tips--'+ theme"
:placement="placement"
:width="width"
:content="errorMsg"
v-model="visible"
@show="showPopover">
<template slot="reference">
<slot></slot>
</template>
</el-popover>
`,
inject:['elFormItem'],
data(){
return {
once:false,
inputEl:null,
visible:false,
errorMsg:'',
ioInstance:null
}
},
watch:{
'elFormItem.validateMessage':{
handler(newVal){
this.visible = newVal ? true : false
if(this.refer){
this.hiddenPopOutside()
}
this.errorMsg = newVal
this.once = this.errorMsg ? false : true
}
}
},
props:{
// hidden-after不为0时重新显示错误信息的触发方式
trigger:{
type:String,
default:''
},
// 出现位置 支持 top bottom right
placement:{
type:String,
default:'top'
},
// 宽度 数值或者设置成auto等
width:{
type:Number|String,
default:'auto'
},
// 主题 danger 以及light
theme:{
type:String,
default:'danger'
},
// 多少秒后隐藏
hiddenAfter:{
type:Number,
default:0
},
// 自动隐藏,当设置成hiddenAfter以及trigger时,以后事件触发后会不会再次自动隐藏
autoHidden:{
type:Boolean,
default:false
},
// refer 参考元素
refer:{
type:String,
default:""
},
// 开启交叉观察器
openIo:{
type:Boolean,
default:false
}
},
mounted(){
this.inputEl = this.$children[0].$children[0]
if(this.refer){
this.hiddenPopOutside()
}
if(!this.hiddenAfter){
if(this.openIo){
io = new IntersectionObserver((entries)=>{
entries.forEach((item)=>{
if(item.intersectionRatio <= 0 && this.errorMsg){
this.visible = false
}
if(item.intersectionRatio > 0 && this.errorMsg){
this.visible = true
}
})
});
io.observe(this.inputEl.$el)
}
}else{
this.inputOnFocus()
}
},
beforeDestroy(){
this.inputEl.$off(this.trigger)
if(io){
io.unobserve(this.inputEl.$el);
}
},
methods:{
// 设置了过多少毫秒后需要监听focus事件来重新显示错误信息
inputOnFocus(){
// if(this.hiddenAfter){
// this.inputEl = this.$children[0].$children[0]
// this.inputEl.$el.addEventListener(this.trigger,()=>{
// if(this.errorMsg){
// this.visible = true
// if(this.hiddenAfter && this.hiddenAfter>0){
// setTimeout(()=>{
// this.visible = false
// },this.hiddenAfter)
// }
// }
// })
// }
if(this.trigger){
this.inputEl.$el.addEventListener(this.trigger,()=>{
if(this.errorMsg){
this.visible = true
if(this.hiddenAfter && this.hiddenAfter>0){
setTimeout(()=>{
this.visible = false
},this.hiddenAfter)
}
}
})
}
},
// 设置了参考元素,则隐藏在视野之外的气泡框
hiddenPopOutside(){
const referEl = document.querySelector(this.refer).getBoundingClientRect()
const slotEl = this.$slots.default[0].elm.getBoundingClientRect()
const isVisible = (referEl.top<=slotEl.top) && (slotEl.top <= referEl.bottom)
if(!isVisible){
this.visible = false
}
},
showPopover(){
if(this.hiddenAfter){
if(this.autoHidden){
setTimeout(()=>{
this.visible = false
},this.hiddenAfter)
} else {
if(this.once === false){
setTimeout(()=>{
this.visible = false
this.once = true
},this.hiddenAfter)
}
}
}
}
}
}
return {
version:'1.0.0',
install:function(_Vue){
_Vue.component("ElFormPopover",ElFormPopover)
}
}
})()