¶Ô±ÈÐÂÎļþ |
| | |
| | | // nvueæä½domçåºï¼ç¨äºè·ådomçå°ºå¯¸ä¿¡æ¯ |
| | | const dom = uni.requireNativePlugin('dom'); |
| | | const bindingX = uni.requireNativePlugin('bindingx'); |
| | | const animation = uni.requireNativePlugin('animation'); |
| | | |
| | | export default { |
| | | data() { |
| | | return { |
| | | // æææé®çæ»å®½åº¦ |
| | | buttonsWidth: 0, |
| | | // æ¯å¦æ£å¨ç§»å¨ä¸ |
| | | moving: false |
| | | } |
| | | }, |
| | | computed: { |
| | | // è·åè¿æ¸¡æ¶é´ |
| | | getDuratin() { |
| | | let duration = String(this.duration) |
| | | // 妿ms为åä½ï¼è¿åmsçæ°å¼é¨å |
| | | if (duration.indexOf('ms') >= 0) return parseInt(duration) |
| | | // 妿s为åä½ï¼ä¸ºäºå¾å°msçæ°å¼ï¼éè¦ä¹ä»¥1000 |
| | | if (duration.indexOf('s') >= 0) return parseInt(duration) * 1000 |
| | | // 妿å¼ä¼ äºæ°å¼ï¼ä¸å°äº30ï¼è®¤ä¸ºæ¯såä½ |
| | | duration = Number(duration) |
| | | return duration < 30 ? duration * 1000 : duration |
| | | } |
| | | }, |
| | | watch: { |
| | | show(n) { |
| | | if(n) { |
| | | this.moveCellByAnimation('open') |
| | | } else { |
| | | this.moveCellByAnimation('close') |
| | | } |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.initialize() |
| | | }, |
| | | methods: { |
| | | initialize() { |
| | | this.queryRect() |
| | | }, |
| | | // å
³éåå
æ ¼ï¼ç¨äºæå¼ä¸ä¸ªï¼èªå¨å
³éå
¶ä»åå
æ ¼çåºæ¯ |
| | | closeHandler() { |
| | | if(this.status === 'open') { |
| | | // 妿卿å¼ç¶æä¸ï¼è¿è¡ç¹å»çè¯ï¼ç´æ¥å
³éåå
æ ¼ |
| | | return this.moveCellByAnimation('close') && this.unbindBindingX() |
| | | } |
| | | }, |
| | | // ç¹å»åå
æ ¼ |
| | | clickHandler() { |
| | | // 妿å¨ç§»å¨ä¸è¢«ç¹å»ï¼è¿è¡å¿½ç¥ |
| | | if(this.moving) return |
| | | // å°è¯å
³éå
¶ä»æå¼çåå
æ ¼ |
| | | this.parent && this.parent.closeOther(this) |
| | | if(this.status === 'open') { |
| | | // 妿卿å¼ç¶æä¸ï¼è¿è¡ç¹å»çè¯ï¼ç´æ¥å
³éåå
æ ¼ |
| | | return this.moveCellByAnimation('close') && this.unbindBindingX() |
| | | } |
| | | }, |
| | | // æ»å¨åå
æ ¼ |
| | | onTouchstart(e) { |
| | | // 妿å½åæ£å¨ç§»å¨ä¸ï¼æè
disabledç¶æï¼åè¿å |
| | | if(this.moving || this.disabled) { |
| | | return this.unbindBindingX() |
| | | } |
| | | if(this.status === 'open') { |
| | | // 妿卿å¼ç¶æä¸ï¼è¿è¡ç¹å»çè¯ï¼ç´æ¥å
³éåå
æ ¼ |
| | | return this.moveCellByAnimation('close') && this.unbindBindingX() |
| | | } |
| | | // ç¹æ®æ
åµä¸ï¼eå¯è½ä¸ä¸ºä¸ä¸ªå¯¹è±¡ |
| | | e?.stopPropagation && e.stopPropagation() |
| | | e?.preventDefault && e.preventDefault() |
| | | this.moving = true |
| | | // è·åå
ç´ ref |
| | | const content = this.getContentRef() |
| | | let expression = `min(max(${-this.buttonsWidth}, x), 0)` |
| | | // å°è¯å
³éå
¶ä»æå¼çåå
æ ¼ |
| | | this.parent && this.parent.closeOther(this) |
| | | |
| | | // é¿é为äºKPIè弿ºçBindingX |
| | | this.panEvent = bindingX.bind({ |
| | | anchor: content, |
| | | eventType: 'pan', |
| | | props: [{ |
| | | element: content, |
| | | // ç»å®width屿§ï¼è®¾ç½®å
¶å®½åº¦å¼ |
| | | property: 'transform.translateX', |
| | | expression |
| | | }] |
| | | }, (res) => { |
| | | this.moving = false |
| | | if (res.state === 'end' || res.state === 'exit') { |
| | | const deltaX = res.deltaX |
| | | if(deltaX <= -this.buttonsWidth || deltaX >= 0) { |
| | | // å¦æè§¦æ¸æ»å¨çè¿ç¨ä¸ï¼å¤§äºåå
æ ¼çæ»å®½åº¦ï¼æè
大äº0ï¼æå³çå·²ç»å¨è¿æ»å¨è¾¾å°äºæå¼æè
å
³éçç¶æ |
| | | // è¿éç´æ¥è¿è¡ç¶æçæ è®° |
| | | this.$nextTick(() => { |
| | | this.status = deltaX <= -this.buttonsWidth ? 'open' : 'close' |
| | | }) |
| | | } else if(Math.abs(deltaX) > uni.$u.getPx(this.threshold)) { |
| | | // å¨ç§»å¨å¤§äºéå¼ãå¹¶ä¸å°äºæ»æé®å®½åº¦æ¶ï¼è¿è¡èªå¨æå¼æè
å
³é |
| | | // ç§»å¨è·ç¦»å¤§äº0æ¶ï¼æå³çéè¦å
³éç¶æ |
| | | if(Math.abs(deltaX) < this.buttonsWidth) { |
| | | this.moveCellByAnimation(deltaX > 0 ? 'close' : 'open') |
| | | } |
| | | } else { |
| | | // å¨å°äºé弿¶ï¼è¿è¡å
³éæä½(妿卿å¼ç¶æä¸ï¼å°ä¸ä¼æ§è¡bindingX) |
| | | this.moveCellByAnimation('close') |
| | | } |
| | | } |
| | | }) |
| | | }, |
| | | // éæ¾bindingX |
| | | unbindBindingX() { |
| | | // éæ¾ä¸ä¸æ¬¡çèµæº |
| | | if (this?.panEvent?.token != 0) { |
| | | bindingX.unbind({ |
| | | token: this.panEvent?.token, |
| | | // pan为æå¿äºä»¶ |
| | | eventType: 'pan' |
| | | }) |
| | | } |
| | | }, |
| | | // æ¥è¯¢æé®èç¹ä¿¡æ¯ |
| | | queryRect() { |
| | | // åéæææé®æ°ç»ï¼éè¿getRectByDomè¿åä¸ä¸ªpromise |
| | | const promiseAll = this.options.map((item, index) => { |
| | | return this.getRectByDom(this.$refs[`u-swipe-action-item__right__button-${index}`][0]) |
| | | }) |
| | | // éè¿promise.allæ¹æ³ï¼è®©æææé®çæ¥è¯¢ç»æè¿åä¸ä¸ªæ°ç»çå½¢å¼ |
| | | Promise.all(promiseAll).then(sizes => { |
| | | this.buttons = sizes |
| | | // è®¡ç®æææé®æ»å®½åº¦ |
| | | this.buttonsWidth = sizes.reduce((sum, cur) => sum + cur.width, 0) |
| | | }) |
| | | }, |
| | | // éè¿nvueçdom模åï¼æ¥è¯¢èç¹ä¿¡æ¯ |
| | | getRectByDom(ref) { |
| | | return new Promise(resolve => { |
| | | dom.getComponentRect(ref, res => { |
| | | resolve(res.size) |
| | | }) |
| | | }) |
| | | }, |
| | | // ç§»å¨åå
æ ¼å°å·¦è¾¹æè
å³è¾¹å°½å¤´ |
| | | moveCellByAnimation(status = 'open') { |
| | | if(this.moving) return |
| | | // æ è¯å½åç¶æ |
| | | this.moveing = true |
| | | const content = this.getContentRef() |
| | | const x = status === 'open' ? -this.buttonsWidth : 0 |
| | | animation.transition(content, { |
| | | styles: { |
| | | transform: `translateX(${x}px)`, |
| | | }, |
| | | duration: uni.$u.getDuration(this.duration, false), |
| | | timingFunction: 'ease-in-out' |
| | | }, () => { |
| | | this.moving = false |
| | | this.status = status |
| | | this.unbindBindingX() |
| | | }) |
| | | }, |
| | | // è·åå
ç´ ref |
| | | getContentRef() { |
| | | return this.$refs['u-swipe-action-item__content'].ref |
| | | }, |
| | | beforeDestroy() { |
| | | this.unbindBindingX() |
| | | } |
| | | } |
| | | } |