Blame view

node_modules/uview-ui/components/u-popup/u-popup.vue 12.4 KB
c7add6cf   “wangming”   初始版本开发完毕
1
  <template>

25852764   unknown   s
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  	<view v-if="visibleSync" :style="[customStyle, {

  		zIndex: uZindex - 1

  	}]" class="u-drawer" hover-stop-propagation>

  		<u-mask :duration="duration" :custom-style="maskCustomStyle" :maskClickAble="maskCloseAble" :z-index="uZindex - 2" :show="showDrawer && mask" @click="maskClick"></u-mask>

  		<view

  			class="u-drawer-content"

  			@tap="modeCenterClose(mode)"

  			:class="[

  				safeAreaInsetBottom ? 'safe-area-inset-bottom' : '',

  				'u-drawer-' + mode,

  				showDrawer ? 'u-drawer-content-visible' : '',

  				zoom && mode == 'center' ? 'u-animation-zoom' : ''

  			]"

  			@touchmove.stop.prevent

  			@tap.stop.prevent

  			:style="[style]"

c7add6cf   “wangming”   初始版本开发完毕
18
  		>

25852764   unknown   s
19
20
21
  			<view class="u-mode-center-box" @tap.stop.prevent @touchmove.stop.prevent v-if="mode == 'center'" :style="[centerStyle]">

  				<u-icon

  					@click="close"

c7add6cf   “wangming”   初始版本开发完毕
22
  					v-if="closeable"

25852764   unknown   s
23
24
25
26
27
28
29
30
31
  					class="u-close"

  					:class="['u-close--' + closeIconPos]"

  					:name="closeIcon"

  					:color="closeIconColor"

  					:size="closeIconSize"

  				></u-icon>

  				<scroll-view class="u-drawer__scroll-view" scroll-y="true">

  					<slot />

  				</scroll-view>

c7add6cf   “wangming”   初始版本开发完毕
32
  			</view>

25852764   unknown   s
33
34
35
36
37
38
39
40
41
42
43
44
  			<scroll-view class="u-drawer__scroll-view" scroll-y="true" v-else>

  				<slot />

  			</scroll-view>

  			<view @tap="close" class="u-close" :class="['u-close--' + closeIconPos]">

  				<u-icon

  					v-if="mode != 'center' && closeable"

  					:name="closeIcon"

  					:color="closeIconColor"

  					:size="closeIconSize"

  				></u-icon>

  			</view>

  		</view>

c7add6cf   “wangming”   初始版本开发完毕
45
46
47
48
  	</view>

  </template>

  

  <script>

25852764   unknown   s
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
  /**

   * popup 弹窗

   * @description 弹出层容器,用于展示弹窗、信息提示等内容,支持上、下、左、右和中部弹出。组件只提供容器,内部内容由用户自定义

   * @tutorial https://www.uviewui.com/components/popup.html

   * @property {String} mode 弹出方向(默认left

   * @property {Boolean} mask 是否显示遮罩(默认true

   * @property {Stringr | Number} length mode=left | 见官网说明(默认auto

   * @property {Boolean} zoom 是否开启缩放动画,只在modecenter时有效(默认true

   * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false

   * @property {Boolean} mask-close-able 点击遮罩是否可以关闭弹出层(默认true

   * @property {Object} custom-style 用户自定义样式

   * @property {Stringr | Number} negative-top 中部弹出时,往上偏移的值

   * @property {Numberr | String} border-radius 弹窗圆角值(默认0

   * @property {Numberr | String} z-index 弹出内容的z-index值(默认1075

   * @property {Boolean} closeable 是否显示关闭图标(默认false

   * @property {String} close-icon 关闭图标的名称,只能uView的内置图标

   * @property {String} close-icon-pos 自定义关闭图标位置(默认top-right

   * @property {String} close-icon-color 关闭图标的颜色(默认#909399

   * @property {Number | String} close-icon-size 关闭图标的大小,单位rpx(默认30

   * @event {Function} open 弹出层打开

   * @event {Function} close 弹出层收起

   * @example <u-popup v-model="show"><view>出淤泥而不染,濯清涟而不妖</view></u-popup>

   */

  export default {

  	name: 'u-popup',

  	props: {

  		/**

  		 * 显示状态

  		 */

  		show: {

  			type: Boolean,

  			default: false

  		},

  		/**

  		 * 弹出方向,left|right|top|bottom|center

  		 */

  		mode: {

  			type: String,

  			default: 'left'

  		},

  		/**

  		 * 是否显示遮罩

  		 */

  		mask: {

  			type: Boolean,

  			default: true

  		},

  		// 抽屉的宽度(mode=left|right),或者高度(mode=top|bottom),单位rpx,或者"auto"

  		// 或者百分比"50%",表示由内容撑开高度或者宽度

  		length: {

  			type: [Number, String],

  			default: 'auto'

  		},

  		// 是否开启缩放动画,只在mode=center时有效

  		zoom: {

  			type: Boolean,

  			default: true

  		},

  		// 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距

  		safeAreaInsetBottom: {

  			type: Boolean,

  			default: false

  		},

  		// 是否可以通过点击遮罩进行关闭

  		maskCloseAble: {

  			type: Boolean,

  			default: true

  		},

  		// 用户自定义样式

  		customStyle: {

  			type: Object,

  			default() {

  				return {};

c7add6cf   “wangming”   初始版本开发完毕
122
123
  			}

  		},

25852764   unknown   s
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
  		value: {

  			type: Boolean,

  			default: false

  		},

  		// 此为内部参数,不在文档对外使用,为了解决Picker和keyboard等融合了弹窗的组件

  		// 对v-model双向绑定多层调用造成报错不能修改props值的问题

  		popup: {

  			type: Boolean,

  			default: true

  		},

  		// 显示显示弹窗的圆角,单位rpx

  		borderRadius: {

  			type: [Number, String],

  			default: 0

  		},

  		zIndex: {

  			type: [Number, String],

  			default: ''

  		},

  		// 是否显示关闭图标

  		closeable: {

  			type: Boolean,

  			default: false

  		},

  		// 关闭图标的名称,只能uView的内置图标

  		closeIcon: {

  			type: String,

  			default: 'close'

  		},

  		// 自定义关闭图标位置,top-left为左上角,top-right为右上角,bottom-left为左下角,bottom-right为右下角

  		closeIconPos: {

  			type: String,

  			default: 'top-right'

  		},

  		// 关闭图标的颜色

  		closeIconColor: {

  			type: String,

  			default: '#909399'

  		},

  		// 关闭图标的大小,单位rpx

  		closeIconSize: {

  			type: [String, Number],

  			default: '30'

  		},

  		// 宽度,只对左,右,中部弹出时起作用,单位rpx,或者"auto"

  		// 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数

  		width: {

  			type: String,

  			default: ''

  		},

  		// 高度,只对上,下,中部弹出时起作用,单位rpx,或者"auto"

  		// 或者百分比"50%",表示由内容撑开高度或者宽度,优先级高于length参数

  		height: {

  			type: String,

  			default: ''

  		},

  		// 给一个负的margin-top,往上偏移,避免和键盘重合的情况,仅在mode=center时有效

  		negativeTop: {

  			type: [String, Number],

  			default: 0

  		},

  		// 遮罩的样式,一般用于修改遮罩的透明度

  		maskCustomStyle: {

  			type: Object,

  			default() {

  				return {}

c7add6cf   “wangming”   初始版本开发完毕
190
191
  			}

  		},

25852764   unknown   s
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
  		// 遮罩打开或收起的动画过渡时间,单位ms

  		duration: {

  			type: [String, Number],

  			default: 250

  		}

  	},

  	data() {

  		return {

  			visibleSync: false,

  			showDrawer: false,

  			timer: null,

  			closeFromInner: false, // value的值改变,是发生在内部还是外部

  		};

  	},

  	computed: {

  		// 根据mode的位置,设定其弹窗的宽度(mode = left|right),或者高度(mode = top|bottom)

  		style() {

  			let style = {};

  			// 如果是左边或者上边弹出时,需要给translate设置为负值,用于隐藏

  			if (this.mode == 'left' || this.mode == 'right') {

  				style = {

  					width: this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length),

  					height: '100%',

  					transform: `translate3D(${this.mode == 'left' ? '-100%' : '100%'},0px,0px)`

  				};

  			} else if (this.mode == 'top' || this.mode == 'bottom') {

  				style = {

  					width: '100%',

  					height: this.height ? this.getUnitValue(this.height) : this.getUnitValue(this.length),

  					transform: `translate3D(0px,${this.mode == 'top' ? '-100%' : '100%'},0px)`

  				};

  			}

  			style.zIndex = this.uZindex;

  			// 如果用户设置了borderRadius值,添加弹窗的圆角

  			if (this.borderRadius) {

  				switch (this.mode) {

  					case 'left':

  						style.borderRadius = `0 ${this.borderRadius}rpx ${this.borderRadius}rpx 0`;

  						break;

  					case 'top':

  						style.borderRadius = `0 0 ${this.borderRadius}rpx ${this.borderRadius}rpx`;

  						break;

  					case 'right':

  						style.borderRadius = `${this.borderRadius}rpx 0 0 ${this.borderRadius}rpx`;

  						break;

  					case 'bottom':

  						style.borderRadius = `${this.borderRadius}rpx ${this.borderRadius}rpx 0 0`;

  						break;

  					default:

c7add6cf   “wangming”   初始版本开发完毕
241
  				}

25852764   unknown   s
242
243
244
245
246
  				// 不加可能圆角无效

  				style.overflow = 'hidden';

  			}

  			if(this.duration) style.transition = `all ${this.duration / 1000}s linear`;

  			return style;

c7add6cf   “wangming”   初始版本开发完毕
247
  		},

25852764   unknown   s
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
  		// 中部弹窗的特有样式

  		centerStyle() {

  			let style = {};

  			style.width = this.width ? this.getUnitValue(this.width) : this.getUnitValue(this.length);

  			// 中部弹出的模式,如果没有设置高度,就用auto值,由内容撑开高度

  			style.height = this.height ? this.getUnitValue(this.height) : 'auto';

  			style.zIndex = this.uZindex;

  			style.marginTop = `-${this.$u.addUnit(this.negativeTop)}`;

  			if (this.borderRadius) {

  				style.borderRadius = `${this.borderRadius}rpx`;

  				// 不加可能圆角无效

  				style.overflow = 'hidden';

  			}

  			return style;

  		},

  		// 计算整理后的z-index值

  		uZindex() {

  			return this.zIndex ? this.zIndex : this.$u.zIndex.popup;

  		}

  	},

  	watch: {

  		value(val) {

  			if (val) {

  				this.open();

  			} else if(!this.closeFromInner) {

  				this.close();

  			}

  			this.closeFromInner = false;

  		}

  	},

  	mounted() {

  		// 组件渲染完成时,检查value是否为true,如果是,弹出popup

  		this.value && this.open();

  	},

      methods: {

  		// 判断传入的值,是否带有单位,如果没有,就默认用rpx单位

  		getUnitValue(val) {

  			if(/(%|px|rpx|auto)$/.test(val)) return val;

  			else return val + 'rpx'

  		},

  		// 遮罩被点击

  		maskClick() {

  			this.close();

  		},

  		close() {

  			// 标记关闭是内部发生的,否则修改了value值,导致watch中对value检测,导致再执行一遍close

  			// 造成@close事件触发两次

  			this.closeFromInner = true;

  			this.change('showDrawer', 'visibleSync', false);

  		},

  		// 中部弹出时,需要.u-drawer-content将居中内容,此元素会铺满屏幕,点击需要关闭弹窗

  		// 让其只在mode=center时起作用

  		modeCenterClose(mode) {

  			if (mode != 'center' || !this.maskCloseAble) return;

  			this.close();

  		},

  		open() {

  			this.change('visibleSync', 'showDrawer', true);

  		},

  		// 此处的原理是,关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件

  		// 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用

  		change(param1, param2, status) {

  			// 如果this.popup为false,意味着为picker,actionsheet等组件调用了popup组件

  			if (this.popup == true) {

  				this.$emit('input', status);

  			}

  			this[param1] = status;

  			if(status) {

  				// #ifdef H5 || MP

  				this.timer = setTimeout(() => {

  					this[param2] = status;

  					this.$emit(status ? 'open' : 'close');

  				}, 50);

  				// #endif

  				// #ifndef H5 || MP

  				this.$nextTick(() => {

  					this[param2] = status;

  					this.$emit(status ? 'open' : 'close');

  				})

  				// #endif

  			} else {

  				this.timer = setTimeout(() => {

  					this[param2] = status;

  					this.$emit(status ? 'open' : 'close');

  				}, this.duration);

c7add6cf   “wangming”   初始版本开发完毕
333
  			}

c7add6cf   “wangming”   初始版本开发完毕
334
335
  		}

  	}

25852764   unknown   s
336
  };

c7add6cf   “wangming”   初始版本开发完毕
337
338
  </script>

  

25852764   unknown   s
339
340
  <style scoped lang="scss">

  @import "../../libs/css/style.components.scss";

c7add6cf   “wangming”   初始版本开发完毕
341
  

25852764   unknown   s
342
343
344
345
346
347
348
349
350
351
352
  .u-drawer {

  	/* #ifndef APP-NVUE */

  	display: block;

  	/* #endif */

  	position: fixed;

  	top: 0;

  	left: 0;

  	right: 0;

  	bottom: 0;

  	overflow: hidden;

  }

c7add6cf   “wangming”   初始版本开发完毕
353
  

25852764   unknown   s
354
355
356
357
358
359
360
361
  .u-drawer-content {

  	/* #ifndef APP-NVUE */

  	display: block;

  	/* #endif */

  	position: absolute;

  	z-index: 1003;

  	transition: all 0.25s linear;

  }

c7add6cf   “wangming”   初始版本开发完毕
362
  

25852764   unknown   s
363
364
365
366
  .u-drawer__scroll-view {

  	width: 100%;

  	height: 100%;

  }

c7add6cf   “wangming”   初始版本开发完毕
367
  

25852764   unknown   s
368
369
370
371
372
373
  .u-drawer-left {

  	top: 0;

  	bottom: 0;

  	left: 0;

  	background-color: #ffffff;

  }

c7add6cf   “wangming”   初始版本开发完毕
374
  

25852764   unknown   s
375
376
377
378
379
380
  .u-drawer-right {

  	right: 0;

  	top: 0;

  	bottom: 0;

  	background-color: #ffffff;

  }

c7add6cf   “wangming”   初始版本开发完毕
381
  

25852764   unknown   s
382
383
384
385
386
387
  .u-drawer-top {

  	top: 0;

  	left: 0;

  	right: 0;

  	background-color: #ffffff;

  }

c7add6cf   “wangming”   初始版本开发完毕
388
  

25852764   unknown   s
389
390
391
392
393
394
  .u-drawer-bottom {

  	bottom: 0;

  	left: 0;

  	right: 0;

  	background-color: #ffffff;

  }

c7add6cf   “wangming”   初始版本开发完毕
395
  

25852764   unknown   s
396
397
398
399
400
401
402
403
404
405
406
407
  .u-drawer-center {

  	@include vue-flex;

  	flex-direction: column;

  	bottom: 0;

  	left: 0;

  	right: 0;

  	top: 0;

  	justify-content: center;

  	align-items: center;

  	opacity: 0;

  	z-index: 99999;

  }

c7add6cf   “wangming”   初始版本开发完毕
408
  

25852764   unknown   s
409
410
411
412
413
414
415
416
417
  .u-mode-center-box {

  	min-width: 100rpx;

  	min-height: 100rpx;

  	/* #ifndef APP-NVUE */

  	display: block;

  	/* #endif */

  	position: relative;

  	background-color: #ffffff;

  }

c7add6cf   “wangming”   初始版本开发完毕
418
  

25852764   unknown   s
419
420
421
422
  .u-drawer-content-visible.u-drawer-center {

  	transform: scale(1);

  	opacity: 1;

  }

c7add6cf   “wangming”   初始版本开发完毕
423
  

25852764   unknown   s
424
425
426
  .u-animation-zoom {

  	transform: scale(1.15);

  }

c7add6cf   “wangming”   初始版本开发完毕
427
  

25852764   unknown   s
428
429
430
  .u-drawer-content-visible {

  	transform: translate3D(0px, 0px, 0px) !important;

  }

c7add6cf   “wangming”   初始版本开发完毕
431
  

25852764   unknown   s
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  .u-close {

  	position: absolute;

  	z-index: 3;

  }

  

  .u-close--top-left {

  	top: 30rpx;

  	left: 30rpx;

  }

  

  .u-close--top-right {

  	top: 30rpx;

  	right: 30rpx;

  }

  

  .u-close--bottom-left {

  	bottom: 30rpx;

  	left: 30rpx;

  }

  

  .u-close--bottom-right {

  	right: 30rpx;

  	bottom: 30rpx;

  }

c7add6cf   “wangming”   初始版本开发完毕
456
  </style>