aboutsummaryrefslogtreecommitdiff
path: root/src/components/swipe_click/swipe_click.js
blob: 238e6df895f2b20e04cd51008302c76218540f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
import GestureService from '../../services/gesture_service/gesture_service'

/**
 * props:
 *   direction: a vector that indicates the direction of the intended swipe
 *   threshold: the minimum distance in pixels the swipe has moved on `direction'
 *              for swipe-finished() to have a non-zero sign
 *   perpendicularTolerance: see gesture_service
 *
 * Events:
 *   preview-requested(offsets)
 *     Emitted when the pointer has moved.
 *     offsets: the offsets from the start of the swipe to the current cursor position
 *
 *   swipe-canceled()
 *     Emitted when the swipe has been canceled due to a pointercancel event.
 *
 *   swipe-finished(sign: 0|-1|1)
 *     Emitted when the swipe has finished.
 *     sign: if the swipe does not meet the threshold, 0
 *           if the swipe meets the threshold in the positive direction, 1
 *           if the swipe meets the threshold in the negative direction, -1
 *
 *   swipeless-clicked()
 *     Emitted when there is a click without swipe.
 *     This and swipe-finished() cannot be emitted for the same pointerup event.
 */
const SwipeClick = {
  props: {
    direction: {
      type: Array
    },
    threshold: {
      type: Function,
      default: () => 30
    },
    perpendicularTolerance: {
      type: Number,
      default: 1.0
    }
  },
  methods: {
    handlePointerDown (event) {
      this.$gesture.start(event)
    },
    handlePointerMove (event) {
      this.$gesture.move(event)
    },
    handlePointerUp (event) {
      this.$gesture.end(event)
    },
    handlePointerCancel (event) {
      this.$gesture.cancel(event)
    },
    handleNativeClick (event) {
      this.$gesture.click(event)
    },
    preview (offsets) {
      this.$emit('preview-requested', offsets)
    },
    end (sign) {
      this.$emit('swipe-finished', sign)
    },
    click () {
      this.$emit('swipeless-clicked')
    },
    cancel () {
      this.$emit('swipe-canceled')
    }
  },
  created () {
    this.$gesture = new GestureService.SwipeAndClickGesture({
      direction: this.direction,
      threshold: this.threshold,
      perpendicularTolerance: this.perpendicularTolerance,
      swipePreviewCallback: this.preview,
      swipeEndCallback: this.end,
      swipeCancelCallback: this.cancel,
      swipelessClickCallback: this.click
    })
  }
}

export default SwipeClick