Component({
  data: {
    leftPos: 0,
    swiping: false,
    holdSwipe: true
  },
  props: {
    className: '',
    right: [],
    restore: false,
    index: null
  },
  didMount: function didMount() {
    this.btnWidth = 0;
    this.setBtnWidth();
  },
  didUpdate: function didUpdate(_prevProps, prevData) {
    var restore = this.props.restore;
    var holdSwipe = this.data.holdSwipe;

    if (restore === true || prevData.holdSwipe === true && holdSwipe === false) {
      this.setData({
        leftPos: 0,
        swiping: false
      });
    }

    this.setBtnWidth();
  },

  methods: {
    setBtnWidth: function setBtnWidth() {
      var _this = this;

      my.createSelectorQuery().select('.am-swipe-right-' + this.$id).boundingClientRect().exec(function (ret) {
        _this.btnWidth = ret[0].width;
      });
    },
    onSwipeTap: function onSwipeTap() {
      if (!this.data.swiping) {
        this.setData({
          leftPos: 0,
          swiping: false
        });
      }
    },
    onSwipeStart: function onSwipeStart(e) {
      this.touchObject = {
        startX: e.touches[0].pageX,
        startY: e.touches[0].pageY
      };
      var _props = this.props,
          index = _props.index,
          onSwipeStart = _props.onSwipeStart;

      if (onSwipeStart) {
        onSwipeStart({ index: index });
      }
    },
    onSwipeMove: function onSwipeMove(e) {
      var touchObject = this.touchObject;

      var touchePoint = e.touches[0];
      var leftPos = this.data.leftPos;


      touchObject.endX = touchePoint.pageX;

      // 首次触发时，计算滑动角度
      if (touchObject.direction === undefined) {
        var direction = 0;

        var xDist = touchObject.startX - touchePoint.pageX || 0;
        var yDist = touchObject.startY - touchePoint.pageY || 0;

        var r = Math.atan2(yDist, xDist);
        var swipeAngle = Math.round(r * 180 / Math.PI);

        if (swipeAngle < 0) {
          swipeAngle = 360 - Math.abs(swipeAngle);
        }
        if (swipeAngle <= 45 && swipeAngle >= 0) {
          direction = 1;
        }
        if (swipeAngle <= 360 && swipeAngle >= 315) {
          direction = 1;
        }
        if (swipeAngle >= 135 && swipeAngle <= 225) {
          direction = -1;
        }

        touchObject.direction = direction;
      }

      // 通过角度判断是左右方向
      if (touchObject.direction !== 0) {
        var newLeftPos = leftPos;
        // 滑动距离
        var distance = touchObject.endX - touchObject.startX;
        // 左划
        if (distance < 0) {
          newLeftPos = Math.max(distance, -this.btnWidth);
          // 右划
        } else {
          newLeftPos = 0;
        }
        if (Math.abs(distance) > 10) {
          this.setData({
            leftPos: newLeftPos,
            swiping: distance < 0
          });
        }
      }
    },
    onSwipeEnd: function onSwipeEnd(e) {
      var touchObject = this.touchObject;

      if (touchObject.direction !== 0) {
        var touchePoint = e.changedTouches[0];

        touchObject.endX = touchePoint.pageX;

        var leftPos = this.data.leftPos;

        var distance = touchObject.endX - touchObject.startX;
        var newLeftPos = leftPos;
        if (distance < 0) {
          if (Math.abs(distance + leftPos) > this.btnWidth * 0.7) {
            newLeftPos = -this.btnWidth;
          } else {
            newLeftPos = 0;
          }
        }
        this.setData({
          leftPos: newLeftPos,
          swiping: false
        });
      }
    },
    done: function done() {
      var _this2 = this;

      this.setData({
        holdSwipe: false
      }, function () {
        _this2.setData({
          holdSwipe: true
        });
      });
    },
    onItemClick: function onItemClick(e) {
      var _this3 = this;

      var onRightItemClick = this.props.onRightItemClick;
      var holdSwipe = this.data.holdSwipe;

      if (onRightItemClick) {
        var index = e.target.dataset.index;

        onRightItemClick({
          index: index,
          extra: this.props.extra,
          detail: this.props.right[index],
          done: this.done.bind(this)
        });
      }

      if (!this.data.swiping && holdSwipe === false) {
        setTimeout(function () {
          _this3.setData({
            leftPos: 0,
            swiping: false
          });
        }, 300);
      }
    }
  }
});