mpalipay.js 4.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7
export default {
	data() {
		return {
			x: 0,
			transition: false,
			width: 0,
			viewWidth: 0,
8
			swipeShow: 0
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10 11 12
		}
	},
	watch: {
		show(newVal) {
13 14
			if (this.autoClose) return
			if (newVal && newVal !== 'none') {
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17 18 19 20 21 22 23 24 25
				this.transition = true
				this.open(newVal)
			} else {
				this.close()
			}
		}
	},
	created() {
		this.swipeaction = this.getSwipeAction()
		if (this.swipeaction.children !== undefined) {
			this.swipeaction.children.push(this)
26
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
27 28 29 30 31 32 33
	},
	mounted() {
		this.isopen = false
		setTimeout(() => {
			this.getQuerySelect()
		}, 50)
	},
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	methods: {
		appTouchStart(e) {
			const {
				clientX
			} = e.changedTouches[0]
			this.clientX = clientX
			this.timestamp = new Date().getTime()
		},
		appTouchEnd(e, index, item, position) {
			const {
				clientX
			} = e.changedTouches[0]
			// fixed by xxxx 模拟点击事件,解决 ios 13 点击区域错位的问题
			let diff = Math.abs(this.clientX - clientX)
			let time = (new Date().getTime()) - this.timestamp
			if (diff < 40 && time < 300) {
				this.$emit('click', {
					content: item,
					index,
					position
				})
			}
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
		/**
		 * 移动触发
		 * @param {Object} e
		 */
		onChange(e) {
			this.moveX = e.detail.x
			this.isclose = false
		},
		touchstart(e) {
			this.transition = false
			this.isclose = true
			this.autoClose && this.swipeaction.closeOther(this)
		},
		touchmove(e) {},
71
		touchend(e) {
DCloud_JSON's avatar
DCloud_JSON 已提交
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
			// 0的位置什么都不执行
			if (this.isclose && this.isopen === 'none') return
			if (this.isclose && this.isopen !== 'none') {
				this.transition = true
				this.close()
			} else {
				this.move(this.moveX + this.leftWidth)
			}
		},

		/**
		 * 移动
		 * @param {Object} moveX
		 */
		move(moveX) {
			// 打开关闭的处理逻辑不太一样
			this.transition = true
			// 未打开状态
			if (!this.isopen || this.isopen === 'none') {
				if (moveX > this.threshold) {
					this.open('left')
				} else if (moveX < -this.threshold) {
					this.open('right')
				} else {
					this.close()
				}
			} else {
				if (moveX < 0 && moveX < this.rightWidth) {
					const rightX = this.rightWidth + moveX
					if (rightX < this.threshold) {
						this.open('right')
					} else {
						this.close()
					}
				} else if (moveX > 0 && moveX < this.leftWidth) {
					const leftX = this.leftWidth - moveX
					if (leftX < this.threshold) {
						this.open('left')
					} else {
						this.close()
					}
				}

			}

		},

		/**
		 * 打开
		 */
		open(type) {
			this.x = this.moveX
			this.animation(type)
		},

		/**
		 * 关闭
		 */
130 131
		close() {
			this.x = this.moveX
DCloud_JSON's avatar
DCloud_JSON 已提交
132 133
			// TODO 解决 x 值不更新的问题,所以会多触发一次 nextTick ,待优化
			this.$nextTick(() => {
134 135 136
				this.x = -this.leftWidth
				if (this.isopen !== 'none') {
					this.$emit('change', 'none')
DCloud_JSON's avatar
DCloud_JSON 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
				}
				this.isopen = 'none'
			})
		},

		/**
		 * 执行结束动画
		 * @param {Object} type
		 */
		animation(type) {
			this.$nextTick(() => {
				if (type === 'left') {
					this.x = 0
				} else {
					this.x = -this.rightWidth - this.leftWidth
152 153 154 155 156
				}

				if (this.isopen !== type) {
					this.$emit('change', type)
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
157 158 159 160 161 162 163 164 165
				this.isopen = type
			})

		},
		getSlide(x) {},
		getQuerySelect() {
			const query = uni.createSelectorQuery().in(this);
			query.selectAll('.movable-view--hock').boundingClientRect(data => {
				this.leftWidth = data[1].width
166
				this.rightWidth = data[2].width
DCloud_JSON's avatar
DCloud_JSON 已提交
167 168 169 170 171 172 173
				this.width = data[0].width
				this.viewWidth = this.width + this.rightWidth + this.leftWidth
				if (this.leftWidth === 0) {
					// TODO 疑似bug ,初始化的时候如果x 是0,会导致移动位置错误,所以让元素超出一点
					this.x = -0.1
				} else {
					this.x = -this.leftWidth
174
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
175 176 177 178 179 180 181 182 183 184
				this.moveX = this.x
				this.$nextTick(() => {
					this.swipeShow = 1
				})

				if (!this.buttonWidth) {
					this.disabledView = true
				}

				if (this.autoClose) return
185
				if (this.show !== 'none') {
DCloud_JSON's avatar
DCloud_JSON 已提交
186 187 188 189 190 191 192
					this.transition = true
					this.open(this.shows)
				}
			}).exec();

		}
	}
雪洛's avatar
雪洛 已提交
193
}