-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvusui-layer.js
358 lines (358 loc) · 9.98 KB
/
vusui-layer.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*!
* @Name:vusui-app-layer v1.0.5 uni-app/小程序弹窗组件
* @Site:http://www.vusui.com | https://vusui.github.io
* @Author:林攀
* @License:MIT
* @开发日期:2019-12-18
*/
import Vuex from 'vuex';
import Util from './common/utils.js';
import './theme/default/vusui-layer.css';
export default {
install(Vue) {
Vue.prototype.$vusuiLayer = new Vuex.Store({
state: {
index: 0,
zIndex: 19950512,
dialog: {
show: false,
hide: false,
type: 'dialog',
zIndex: 0
},
message: {
show: false,
hide: false,
type: 'message',
zIndex: 0
},
loading: {
show: false,
hide: false,
type: 'loading',
zIndex: 0
},
prompt: {
show: false,
hide: false,
type: 'prompt',
zIndex: 0
},
drawer: {
show: false,
hide: false,
type: 'drawer',
zIndex: 0
},
page: {
show: false,
hide: false,
type: 'page',
zIndex: 0
},
photos: {
show: false,
hide: false,
type: 'photos',
zIndex: 0
}
},
mutations: {
// 核心方法
open(state, options) {
state[options.type] = Object.assign(state[options.type], options);
state[options.type].show = true;
state[options.type].zIndex = state.zIndex + state.index++;
// #ifndef H5
state[options.type].tabBar && uni.hideTabBar();
// #endif
},
// 确定按钮事件
yes(state, options) {
Util.closeType(options, this);
if (options.outAnim !== -1) {
setTimeout(() => {
options.yes && options.yes(options);
}, 300);
} else {
setTimeout(() => {
options.yes && options.yes(options);
}, 30);
}
},
// 取消按钮事件
cancel(state, options) {
Util.closeType(options, this);
if (options.outAnim !== -1) {
setTimeout(() => {
options.cancel && options.cancel(options);
}, 300);
} else {
setTimeout(() => {
options.cancel && options.cancel(options);
}, 30);
}
},
// 自定义按钮事件
btn(state, options) {
Util.closeType(options, this);
if (options.outAnim !== -1) {
setTimeout(() => {
options['btn'+options.btnIndex] && options['btn'+options.btnIndex](options);
}, 300);
} else {
setTimeout(() => {
options['btn'+options.btnIndex] && options['btn'+options.btnIndex](options);
}, 30);
}
},
// 关闭层时的退出动画效果
hideAnim(state, options) {
state[options.type].hide = options.hide;
},
// 关闭指定层
close(state, options) {
state[options.type].show = false;
// #ifndef H5
if (options.type == 'message' || options.type == 'loading') {
if (!state.prompt.show && !state.drawer.show && !state.page.show && !state.photos.show) {
state[options.type].tabBar && uni.showTabBar();
}
} else if (options.type == 'dialog') {
if (!state.prompt.show && !state.drawer.show && !state.page.show && !state.photos.show) {
state[options.type].tabBar && uni.showTabBar();
}
} else if (options.type == 'prompt') {
if (!state.drawer.show && !state.page.show && !state.photos.show) {
state[options.type].tabBar && uni.showTabBar();
}
} else {
state[options.type].tabBar && uni.showTabBar();
}
// #endif
}
}
})
Vue.prototype.vusui = {
open(options) {
Vue.prototype.$vusuiLayer.commit('open', options);
},
// 提示框
alert(content, options, yes) {
let type = typeof options === 'function';
if (type) yes = options;
return this.open(Object.assign({
type: 'dialog',
title: '提示', //标题
content: content, //['内容', 'font-size:26px']
contentAlign: '', //内容对齐位置 left,right,center
shadeClose: false, //点击遮罩关闭
button: '确定', //按钮 ['btn1','btn2',...]
buttonAlign: 'center', //按钮对齐位置left,center,right
anim: 0, //0-6
outAnim: 0, //0-6
icon: -1, //['0','font-size:18px;'] 0-5
time: 0, //0为不自动关闭,单位毫秒
shade: 0.3, //遮罩透明度 0-1
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
yes: yes, //确定回调
cancel: null, //取消回调
btn1: null,
btn2: null,
btn3: null,
}, type ? {} : options));
},
// 询问框
confirm(content, options, yes, cancel) {
let type = typeof options === 'function';
if (type) {
cancel = yes;
yes = options;
}
return this.open(Object.assign({
type: 'dialog',
title: false, //标题
content: content, //['内容', 'font-size:26px']
contentAlign: '', //内容对齐位置
shadeClose: false, //点击遮罩关闭
button: ['确定', '取消'], //按钮 ['btn1','btn2',...]
buttonAlign: 'center', //按钮对齐位置left,center,right
anim: 0, //0-6
outAnim: 0, //0-6
icon: 4, //['0','font-size:18px;'] 0-5
time: 0, //0为不自动关闭,单位毫秒
shade: 0.3, //遮罩透明度 0-1
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
yes: yes, //确定回调
cancel: cancel, //取消回调
btn1: null,
btn2: null,
btn3: null,
}, type ? {} : options));
},
// 信息提示
msg(content, options, cancel) {
let type = typeof options === 'function';
if (type) cancel = options;
return this.open(Object.assign({
type: 'message',
title: false,
content: content,
contentAlign: '',
shade: 0.01,
shadeClose: false,
button: false,
buttonAlign: '',
closeBtn: false,
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
anim: 0,
outAnim: 0,
icon: -1,
time: 1500,
cancel: cancel
}, type ? {} : options));
},
// 加载层
load(icon, options) {
return this.open(Object.assign({
type: 'loading',
title: false,
content: '',
contentAlign: '',
shade: 0.01,
shadeClose: false,
button: false,
buttonAlign: '',
closeBtn: false,
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
anim: 0,
outAnim: 0,
icon: icon || 0,
time: 0
}, options));
},
// 输入层
prompt(options, yes) {
options = options || {};
if (typeof options === 'function') yes = options;
return this.open(Object.assign({
type: 'prompt',
formType: 1,
value: '', //输入值
content: '', //描述内容 ['描述内容','font-size:20px;']
height: '80px', //输入框高度
regular: '', //正则
placeholder: '请输入',
length: [0, 255], //最小长度和最大长度,0为不限制
count: false, //是否显示计算输入长度
title: false,
content: '', //描述内容 ['描述内容','font-size:20px;']
shade: 0.3,
shadeClose: false,
button: ['确定', '取消'],
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
anim: 0,
outAnim: 0,
cancel: null,
yes: (opt) => {
let reg = opt && opt.regular && new RegExp(opt.regular);
if (!opt.value) {
this.msg('不能为空');
} else if ((opt.value.length < opt.length[0]) || (opt.length[1] > 0 && (opt.value.length > opt.length[1])) || (reg && !reg.test(opt.value))) {
this.msg('填写不正确');
} else {
yes && yes(opt.value);
Util.close(opt, Vue.prototype.$vusuiLayer);
}
}
}, options));
},
// 抽屉层
drawer(position, options) {
return this.open(Object.assign({
type: 'drawer',
content: '',
shade: 0.3,
shadeClose: true,
button: false,
buttonAlign: '',
anim: 4,
outAnim: 4,
time: 0,
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
yes: null,
cancel: null,
btn1: null,
btn2: null,
btn3: null,
position: position || 'right',
}, function() {
options = options || {};
if (position) {
if (position == 'left') {
options.anim = 2;
options.outAnim = 2;
} else if (position == 'top') {
options.anim = 3;
options.outAnim = 3;
} else if (position == 'bottom') {
options.anim = 5;
options.outAnim = 5;
}
}
return options;
}()));
},
// 页面层
page(options) {
return this.open(Object.assign({
type: 'page',
title: false, //标题
content: '', //['内容', 'font-size:26px']
shadeClose: false, //点击遮罩关闭
button: '', //按钮 ['btn1','btn2',...]
buttonAlign: '', //按钮对齐位置left,center,right
closeBtn: true, //关闭按钮
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
anim: 0, //0-6
outAnim: 0, //0-6
shade: 0.3, //遮罩透明度 0-1
yes: null, //确定回调
cancel: null, //取消回调
btn1: null,
btn2: null,
btn3: null,
}, options));
},
// 相集层
photos(options) {
return this.open(Object.assign({
type: 'photos',
title: false, //是否显示标题
content: '',
number: false, //是否显示页码
shade: 0.8,
shadeClose: true,
closeBtn: true,
tabBar: true, //弹出层时是否隐藏原生tabBar导航栏
anim: 0,
outAnim: 0,
time: 0
}, options));
},
// 关闭指定层
close(type) {
const store = Vue.prototype.$vusuiLayer;
Util.close(store.state[type], store);
},
// 关闭所有层
closeAll() {
const store = Vue.prototype.$vusuiLayer;
const types = ['dialog','message','loading','prompt','drawer','page','photos'];
types.map(item => {
Util.close(store.state[item], store);
});
}
}
}
}