¶Ô±ÈÐÂÎļþ |
| | |
| | | const MIN_DISTANCE = 10; |
| | | export default { |
| | | showWatch(newVal, oldVal, ownerInstance, instance, self) { |
| | | var state = self.state |
| | | var $el = ownerInstance.$el || ownerInstance.$vm && ownerInstance.$vm.$el |
| | | if (!$el) return |
| | | this.getDom(instance, ownerInstance, self) |
| | | if (newVal && newVal !== 'none') { |
| | | this.openState(newVal, instance, ownerInstance, self) |
| | | return |
| | | } |
| | | |
| | | if (state.left) { |
| | | this.openState('none', instance, ownerInstance, self) |
| | | } |
| | | this.resetTouchStatus(instance, self) |
| | | }, |
| | | |
| | | /** |
| | | * å¼å§è§¦æ¸æä½ |
| | | * @param {Object} e |
| | | * @param {Object} ins |
| | | */ |
| | | touchstart(e, ownerInstance, self) { |
| | | let instance = e.instance; |
| | | let disabled = instance.getDataset().disabled |
| | | let state = self.state; |
| | | this.getDom(instance, ownerInstance, self) |
| | | // fix by mehaotian, TODO å
¼å®¹ app-vue è·ådataset为å符串 , h5 è·å 为 undefined çé®é¢,å¾
æ¡æ¶ä¿®å¤ |
| | | disabled = this.getDisabledType(disabled) |
| | | if (disabled) return |
| | | // å¼å§è§¦æ¸æ¶ç§»é¤å¨ç»ç±» |
| | | instance.requestAnimationFrame(function() { |
| | | instance.removeClass('ani'); |
| | | ownerInstance.callMethod('closeSwipe'); |
| | | }) |
| | | |
| | | // è®°å½ä¸æ¬¡çä½ç½® |
| | | state.x = state.left || 0 |
| | | // è®¡ç®æ»å¨å¼å§ä½ç½® |
| | | this.stopTouchStart(e, ownerInstance, self) |
| | | }, |
| | | |
| | | /** |
| | | * å¼å§æ»å¨æä½ |
| | | * @param {Object} e |
| | | * @param {Object} ownerInstance |
| | | */ |
| | | touchmove(e, ownerInstance, self) { |
| | | let instance = e.instance; |
| | | // å é¤ä¹åå·²ç»é£ä¸å°å®ä¾äº |
| | | if (!instance) return; |
| | | let disabled = instance.getDataset().disabled |
| | | let state = self.state |
| | | // fix by mehaotian, TODO å
¼å®¹ app-vue è·ådataset为å符串 , h5 è·å 为 undefined çé®é¢,å¾
æ¡æ¶ä¿®å¤ |
| | | disabled = this.getDisabledType(disabled) |
| | | if (disabled) return |
| | | // æ¯å¦å¯ä»¥æ»å¨é¡µé¢ |
| | | this.stopTouchMove(e, self); |
| | | if (state.direction !== 'horizontal') { |
| | | return; |
| | | } |
| | | if (e.preventDefault) { |
| | | // 黿¢é¡µé¢æ»å¨ |
| | | e.preventDefault() |
| | | } |
| | | let x = state.x + state.deltaX |
| | | this.move(x, instance, ownerInstance, self) |
| | | }, |
| | | |
| | | /** |
| | | * ç»æè§¦æ¸æä½ |
| | | * @param {Object} e |
| | | * @param {Object} ownerInstance |
| | | */ |
| | | touchend(e, ownerInstance, self) { |
| | | let instance = e.instance; |
| | | let disabled = instance.getDataset().disabled |
| | | let state = self.state |
| | | // fix by mehaotian, TODO å
¼å®¹ app-vue è·ådataset为å符串 , h5 è·å 为 undefined çé®é¢,å¾
æ¡æ¶ä¿®å¤ |
| | | disabled = this.getDisabledType(disabled) |
| | | |
| | | if (disabled) return |
| | | // æ»å¨è¿ç¨ä¸è§¦æ¸ç»æ,éè¿éå¼å¤ææ¯å¼å¯è¿æ¯å
³é |
| | | // fixed by mehaotian 宿¶å¨è§£å³ç¹å»æé®ï¼touchend è§¦åæ¯ click äºä»¶æ¶æºæ©çé®é¢ ï¼ä¸»è¦æ¯ ios13 |
| | | this.moveDirection(state.left, instance, ownerInstance, self) |
| | | |
| | | }, |
| | | |
| | | /** |
| | | * 设置移å¨è·ç¦» |
| | | * @param {Object} value |
| | | * @param {Object} instance |
| | | * @param {Object} ownerInstance |
| | | */ |
| | | move(value, instance, ownerInstance, self) { |
| | | value = value || 0 |
| | | let state = self.state |
| | | let leftWidth = state.leftWidth |
| | | let rightWidth = state.rightWidth |
| | | // è·å坿»å¨èå´ |
| | | state.left = this.range(value, -rightWidth, leftWidth); |
| | | instance.requestAnimationFrame(function() { |
| | | instance.setStyle({ |
| | | transform: 'translateX(' + state.left + 'px)', |
| | | '-webkit-transform': 'translateX(' + state.left + 'px)' |
| | | }) |
| | | }) |
| | | |
| | | }, |
| | | |
| | | /** |
| | | * è·åå
ç´ ä¿¡æ¯ |
| | | * @param {Object} instance |
| | | * @param {Object} ownerInstance |
| | | */ |
| | | getDom(instance, ownerInstance, self) { |
| | | var state = self.state |
| | | var $el = ownerInstance.$el || ownerInstance.$vm && ownerInstance.$vm.$el |
| | | var leftDom = $el.querySelector('.button-group--left') |
| | | var rightDom = $el.querySelector('.button-group--right') |
| | | |
| | | state.leftWidth = leftDom.offsetWidth || 0 |
| | | state.rightWidth = rightDom.offsetWidth || 0 |
| | | state.threshold = instance.getDataset().threshold |
| | | }, |
| | | |
| | | getDisabledType(value) { |
| | | return (typeof(value) === 'string' ? JSON.parse(value) : value) || false; |
| | | }, |
| | | |
| | | /** |
| | | * è·åèå´ |
| | | * @param {Object} num |
| | | * @param {Object} min |
| | | * @param {Object} max |
| | | */ |
| | | range(num, min, max) { |
| | | return Math.min(Math.max(num, min), max); |
| | | }, |
| | | |
| | | |
| | | /** |
| | | * ç§»å¨æ¹å夿 |
| | | * @param {Object} left |
| | | * @param {Object} value |
| | | * @param {Object} ownerInstance |
| | | * @param {Object} ins |
| | | */ |
| | | moveDirection(left, ins, ownerInstance, self) { |
| | | var state = self.state |
| | | var threshold = state.threshold |
| | | var position = state.position |
| | | var isopen = state.isopen || 'none' |
| | | var leftWidth = state.leftWidth |
| | | var rightWidth = state.rightWidth |
| | | if (state.deltaX === 0) { |
| | | this.openState('none', ins, ownerInstance, self) |
| | | return |
| | | } |
| | | if ((isopen === 'none' && rightWidth > 0 && -left > threshold) || (isopen !== 'none' && rightWidth > 0 && |
| | | rightWidth + |
| | | left < threshold)) { |
| | | // right |
| | | this.openState('right', ins, ownerInstance, self) |
| | | } else if ((isopen === 'none' && leftWidth > 0 && left > threshold) || (isopen !== 'none' && leftWidth > 0 && |
| | | leftWidth - left < threshold)) { |
| | | // left |
| | | this.openState('left', ins, ownerInstance, self) |
| | | } else { |
| | | // default |
| | | this.openState('none', ins, ownerInstance, self) |
| | | } |
| | | }, |
| | | |
| | | |
| | | /** |
| | | * å¼å¯ç¶æ |
| | | * @param {Boolean} type |
| | | * @param {Object} ins |
| | | * @param {Object} ownerInstance |
| | | */ |
| | | openState(type, ins, ownerInstance, self) { |
| | | let state = self.state |
| | | let leftWidth = state.leftWidth |
| | | let rightWidth = state.rightWidth |
| | | let left = '' |
| | | state.isopen = state.isopen ? state.isopen : 'none' |
| | | switch (type) { |
| | | case "left": |
| | | left = leftWidth |
| | | break |
| | | case "right": |
| | | left = -rightWidth |
| | | break |
| | | default: |
| | | left = 0 |
| | | } |
| | | |
| | | // && !state.throttle |
| | | |
| | | if (state.isopen !== type) { |
| | | state.throttle = true |
| | | ownerInstance.callMethod('change', { |
| | | open: type |
| | | }) |
| | | |
| | | } |
| | | |
| | | state.isopen = type |
| | | // æ·»å å¨ç»ç±» |
| | | ins.requestAnimationFrame(() => { |
| | | ins.addClass('ani'); |
| | | this.move(left, ins, ownerInstance, self) |
| | | }) |
| | | }, |
| | | |
| | | |
| | | getDirection(x, y) { |
| | | if (x > y && x > MIN_DISTANCE) { |
| | | return 'horizontal'; |
| | | } |
| | | if (y > x && y > MIN_DISTANCE) { |
| | | return 'vertical'; |
| | | } |
| | | return ''; |
| | | }, |
| | | |
| | | /** |
| | | * éç½®æ»å¨ç¶æ |
| | | * @param {Object} event |
| | | */ |
| | | resetTouchStatus(instance, self) { |
| | | let state = self.state; |
| | | state.direction = ''; |
| | | state.deltaX = 0; |
| | | state.deltaY = 0; |
| | | state.offsetX = 0; |
| | | state.offsetY = 0; |
| | | }, |
| | | |
| | | /** |
| | | * 设置æ»å¨å¼å§ä½ç½® |
| | | * @param {Object} event |
| | | */ |
| | | stopTouchStart(event, ownerInstance, self) { |
| | | let instance = event.instance; |
| | | let state = self.state |
| | | this.resetTouchStatus(instance, self); |
| | | var touch = event.touches[0]; |
| | | state.startX = touch.clientX; |
| | | state.startY = touch.clientY; |
| | | }, |
| | | |
| | | /** |
| | | * æ»å¨ä¸ï¼æ¯å¦ç¦æ¢æå¼ |
| | | * @param {Object} event |
| | | */ |
| | | stopTouchMove(event, self) { |
| | | let instance = event.instance; |
| | | let state = self.state; |
| | | let touch = event.touches[0]; |
| | | |
| | | state.deltaX = touch.clientX - state.startX; |
| | | state.deltaY = touch.clientY - state.startY; |
| | | state.offsetY = Math.abs(state.deltaY); |
| | | state.offsetX = Math.abs(state.deltaX); |
| | | state.direction = state.direction || this.getDirection(state.offsetX, state.offsetY); |
| | | } |
| | | } |