mpalipay.js 3.9 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
export default {
	data() {
		return {
			x: 0,
			transition: false,
			width: 0,
			viewWidth: 0,
			swipeShow: 0
		}
	},
	watch: {
		show(newVal) {
			if (this.autoClose) return
study夏羽's avatar
study夏羽 已提交
14
			if (newVal && newVal !== 'none') {
study夏羽's avatar
study夏羽 已提交
15 16 17 18 19 20 21 22
				this.transition = true
				this.open(newVal)
			} else {
				this.close()
			}
		}
	},
	created() {
study夏羽's avatar
study夏羽 已提交
23
		this.swipeaction = this.getSwipeAction()
study夏羽's avatar
study夏羽 已提交
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
		if (this.swipeaction.children !== undefined) {
			this.swipeaction.children.push(this)
		}
	},
	mounted() {
		this.isopen = false
		setTimeout(() => {
			this.getQuerySelect()
		}, 50)
	},
	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
				})
			}
		},
		/**
		 * 移动触发
		 * @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) {},
		touchend(e) {
			// 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)
		},

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

		/**
		 * 执行结束动画
		 * @param {Object} type
		 */
		animation(type) {
			this.$nextTick(() => {
				if (type === 'left') {
					this.x = 0
				} else {
					this.x = -this.rightWidth - this.leftWidth
				}
study夏羽's avatar
study夏羽 已提交
153 154

				if (this.isopen !== type) {
study夏羽's avatar
study夏羽 已提交
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
					this.$emit('change', type)
				}
				this.isopen = type
			})

		},
		getSlide(x) {},
		getQuerySelect() {
			const query = uni.createSelectorQuery().in(this);
			query.selectAll('.movable-view--hock').boundingClientRect(data => {
				this.leftWidth = data[1].width
				this.rightWidth = data[2].width
				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
				}
				this.moveX = this.x
				this.$nextTick(() => {
					this.swipeShow = 1
				})

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

				if (this.autoClose) return
				if (this.show !== 'none') {
					this.transition = true
					this.open(this.shows)
				}
			}).exec();

		}
	}
}