gaoluyang
3 天以前 92230c9a97dc9ce9df3313d11d26999c04bb6b26
src/uni_modules/uni-swipe-action/components/uni-swipe-action-item/wx.wxs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,341 @@
var MIN_DISTANCE = 10;
/**
 * åˆ¤æ–­å½“前是否为H5、app-vue
 */
var IS_HTML5 = false
if (typeof window === 'object') IS_HTML5 = true
/**
 * ç›‘听页面内值的变化,主要用于动态开关swipe-action
 * @param {Object} newValue
 * @param {Object} oldValue
 * @param {Object} ownerInstance
 * @param {Object} instance
 */
function showWatch(newVal, oldVal, ownerInstance, instance) {
   var state = instance.getState()
   getDom(instance, ownerInstance)
   if (newVal && newVal !== 'none') {
      openState(newVal, instance, ownerInstance)
      return
   }
   if (state.left) {
      openState('none', instance, ownerInstance)
   }
   resetTouchStatus(instance)
}
/**
 * å¼€å§‹è§¦æ‘¸æ“ä½œ
 * @param {Object} e
 * @param {Object} ins
 */
function touchstart(e, ownerInstance) {
   var instance = e.instance;
   var disabled = instance.getDataset().disabled
   var state = instance.getState();
   getDom(instance, ownerInstance)
   // fix by mehaotian, TODO å…¼å®¹ app-vue èŽ·å–dataset为字符串 , h5 èŽ·å– ä¸º undefined çš„问题,待框架修复
   disabled = (typeof(disabled) === 'string' ? JSON.parse(disabled) : disabled) || false;
   if (disabled) return
   // å¼€å§‹è§¦æ‘¸æ—¶ç§»é™¤åŠ¨ç”»ç±»
   instance.requestAnimationFrame(function() {
      instance.removeClass('ani');
      ownerInstance.callMethod('closeSwipe');
   })
   // è®°å½•上次的位置
   state.x = state.left || 0
   // è®¡ç®—滑动开始位置
   stopTouchStart(e, ownerInstance)
}
/**
 * å¼€å§‹æ»‘动操作
 * @param {Object} e
 * @param {Object} ownerInstance
 */
function touchmove(e, ownerInstance) {
   var instance = e.instance;
   var disabled = instance.getDataset().disabled
   var state = instance.getState()
   // fix by mehaotian, TODO å…¼å®¹ app-vue èŽ·å–dataset为字符串 , h5 èŽ·å– ä¸º undefined çš„问题,待框架修复
   disabled = (typeof(disabled) === 'string' ? JSON.parse(disabled) : disabled) || false;
   if (disabled) return
   // æ˜¯å¦å¯ä»¥æ»‘动页面
   stopTouchMove(e);
   if (state.direction !== 'horizontal') {
      return;
   }
   if (e.preventDefault) {
      // é˜»æ­¢é¡µé¢æ»šåЍ
      e.preventDefault()
   }
   move(state.x + state.deltaX, instance, ownerInstance)
}
/**
 * ç»“束触摸操作
 * @param {Object} e
 * @param {Object} ownerInstance
 */
function touchend(e, ownerInstance) {
   var instance = e.instance;
   var disabled = instance.getDataset().disabled
   var state = instance.getState()
   // fix by mehaotian, TODO å…¼å®¹ app-vue èŽ·å–dataset为字符串 , h5 èŽ·å– ä¸º undefined çš„问题,待框架修复
   disabled = (typeof(disabled) === 'string' ? JSON.parse(disabled) : disabled) || false;
   if (disabled) return
   // æ»‘动过程中触摸结束,通过阙值判断是开启还是关闭
   // fixed by mehaotian å®šæ—¶å™¨è§£å†³ç‚¹å‡»æŒ‰é’®ï¼Œtouchend è§¦å‘比 click äº‹ä»¶æ—¶æœºæ—©çš„问题 ï¼Œä¸»è¦æ˜¯ ios13
   moveDirection(state.left, instance, ownerInstance)
}
/**
 * è®¾ç½®ç§»åŠ¨è·ç¦»
 * @param {Object} value
 * @param {Object} instance
 * @param {Object} ownerInstance
 */
function move(value, instance, ownerInstance) {
   value = value || 0
   var state = instance.getState()
   var leftWidth = state.leftWidth
   var rightWidth = state.rightWidth
   // èŽ·å–å¯æ»‘åŠ¨èŒƒå›´
   state.left = 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
 */
function getDom(instance, ownerInstance) {
   var state = instance.getState()
   var leftDom = ownerInstance.selectComponent('.button-group--left')
   var rightDom = ownerInstance.selectComponent('.button-group--right')
   var leftStyles = {
      width: 0
   }
   var rightStyles = {
      width: 0
   }
   leftStyles = leftDom.getBoundingClientRect()
   rightStyles = rightDom.getBoundingClientRect()
   state.leftWidth = leftStyles.width || 0
   state.rightWidth = rightStyles.width || 0
   state.threshold = instance.getDataset().threshold
}
/**
 * èŽ·å–èŒƒå›´
 * @param {Object} num
 * @param {Object} min
 * @param {Object} max
 */
function range(num, min, max) {
   return Math.min(Math.max(num, min), max);
}
/**
 * ç§»åŠ¨æ–¹å‘åˆ¤æ–­
 * @param {Object} left
 * @param {Object} value
 * @param {Object} ownerInstance
 * @param {Object} ins
 */
function moveDirection(left, ins, ownerInstance) {
   var state = ins.getState()
   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) {
      openState('none', ins, ownerInstance)
      return
   }
   if ((isopen === 'none' && rightWidth > 0 && -left > threshold) || (isopen !== 'none' && rightWidth > 0 &&
         rightWidth +
         left < threshold)) {
      // right
      openState('right', ins, ownerInstance)
   } else if ((isopen === 'none' && leftWidth > 0 && left > threshold) || (isopen !== 'none' && leftWidth > 0 &&
         leftWidth - left < threshold)) {
      // left
      openState('left', ins, ownerInstance)
   } else {
      // default
      openState('none', ins, ownerInstance)
   }
}
/**
 * å¼€å¯çŠ¶æ€
 * @param {Boolean} type
 * @param {Object} ins
 * @param {Object} ownerInstance
 */
function openState(type, ins, ownerInstance) {
   var state = ins.getState()
   var leftWidth = state.leftWidth
   var rightWidth = state.rightWidth
   var 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(function() {
      ins.addClass('ani');
      move(left, ins, ownerInstance)
   })
   // è®¾ç½®æœ€ç»ˆç§»åŠ¨ä½ç½®,理论上只要进入到这个函数,肯定是要打开的
}
function getDirection(x, y) {
   if (x > y && x > MIN_DISTANCE) {
      return 'horizontal';
   }
   if (y > x && y > MIN_DISTANCE) {
      return 'vertical';
   }
   return '';
}
/**
 * é‡ç½®æ»‘动状态
 * @param {Object} event
 */
function resetTouchStatus(instance) {
   var state = instance.getState();
   state.direction = '';
   state.deltaX = 0;
   state.deltaY = 0;
   state.offsetX = 0;
   state.offsetY = 0;
}
/**
 * è®¾ç½®æ»‘动开始位置
 * @param {Object} event
 */
function stopTouchStart(event) {
   var instance = event.instance;
   var state = instance.getState();
   resetTouchStatus(instance);
   var touch = event.touches[0];
   if (IS_HTML5 && isPC()) {
      touch = event;
   }
   state.startX = touch.clientX;
   state.startY = touch.clientY;
}
/**
 * æ»‘动中,是否禁止打开
 * @param {Object} event
 */
function stopTouchMove(event) {
   var instance = event.instance;
   var state = instance.getState();
   var touch = event.touches[0];
   if (IS_HTML5 && isPC()) {
      touch = event;
   }
   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 || getDirection(state.offsetX, state.offsetY);
}
function isPC() {
   var userAgentInfo = navigator.userAgent;
   var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
   var flag = true;
   for (var v = 0; v < Agents.length - 1; v++) {
      if (userAgentInfo.indexOf(Agents[v]) > 0) {
         flag = false;
         break;
      }
   }
   return flag;
}
var movable = false
function mousedown(e, ins) {
   if (!IS_HTML5) return
   if (!isPC()) return
   touchstart(e, ins)
   movable = true
}
function mousemove(e, ins) {
   if (!IS_HTML5) return
   if (!isPC()) return
   if (!movable) return
   touchmove(e, ins)
}
function mouseup(e, ins) {
   if (!IS_HTML5) return
   if (!isPC()) return
   touchend(e, ins)
   movable = false
}
function mouseleave(e, ins) {
   if (!IS_HTML5) return
   if (!isPC()) return
   movable = false
}
module.exports = {
   showWatch: showWatch,
   touchstart: touchstart,
   touchmove: touchmove,
   touchend: touchend,
   mousedown: mousedown,
   mousemove: mousemove,
   mouseup: mouseup,
   mouseleave: mouseleave
}