UNPKG

3.23 kBJavaScriptView Raw
1import "core-js/modules/es6.array.find-index";
2import "core-js/modules/web.dom.iterable";
3import "core-js/modules/es6.array.for-each";
4import "core-js/modules/es6.object.assign";
5import { getDirection } from '../utils';
6var swipeDefaults = {
7 axis: 'all',
8 time: 300,
9 speed: 200,
10 offset: 100,
11 preventDefault: true
12};
13
14var Swipe = function () {
15 function Swipe(node, options) {
16 this.node = typeof node === 'string' ? document.querySelector(node) : node;
17 this.options = Object.assign({}, swipeDefaults, options);
18 this.moveEvents = [];
19 this.listeners = [];
20
21 this._init();
22 }
23
24 var _proto = Swipe.prototype;
25
26 _proto._init = function _init() {
27 var _this = this;
28
29 this.node.addEventListener('touchstart', function (e) {
30 if (_this.options.preventDefault) {
31 e.preventDefault();
32 }
33
34 _this.sTime = e.timeStamp;
35 _this.sTouch = _this.eTouch = e.targetTouches[0];
36 }, false);
37 this.node.addEventListener('touchmove', function (e) {
38 var point = e.targetTouches[0];
39
40 if (_this.moveEvents.length > 0) {
41 _this.moveEvents.forEach(function (fn) {
42 return fn(point.pageX - _this.sTouch.pageX, point.pageY - _this.sTouch.pageY);
43 });
44 }
45
46 if (e.timeStamp - _this.sTime > _this.options.time) {
47 _this.sTime = e.timeStamp;
48 _this.eTouch = point;
49 }
50 }, false);
51 this.node.addEventListener('touchend', function (e) {
52 var point = e.changedTouches[0];
53 var time = e.timeStamp - _this.sTime;
54 var offsetX = point.pageX - _this.eTouch.pageX;
55 var offsetY = point.pageY - _this.eTouch.pageY;
56 var offsetXAll = point.pageX - _this.sTouch.pageX;
57 var offsetYAll = point.pageY - _this.sTouch.pageY;
58 var offset, offsetAll, direction;
59
60 if (_this.options.axis === 'x') {
61 offset = offsetX;
62 offsetAll = offsetXAll;
63 direction = getDirection(offsetXAll, 0);
64 } else if (_this.options.axis === 'y') {
65 offset = offsetY;
66 offsetAll = offsetYAll;
67 direction = getDirection(0, offsetYAll);
68 } else {
69 if (Math.abs(offsetXAll) > Math.abs(offsetYAll)) {
70 offset = offsetX;
71 offsetAll = offsetXAll;
72 } else {
73 offset = offsetY;
74 offsetAll = offsetYAll;
75 }
76
77 direction = getDirection(offsetXAll, offsetYAll);
78 }
79
80 if (Math.abs(offsetAll) >= _this.options.offset || Math.abs(offset) / time * 1000 >= _this.options.speed) {
81 _this.dispatchEvent(direction);
82 }
83 }, false);
84 };
85
86 _proto.onMove = function onMove(fn) {
87 this.moveEvents.push(fn);
88 };
89
90 _proto.addEvent = function addEvent(fn) {
91 this.listeners.push(fn);
92 };
93
94 _proto.removeEvent = function removeEvent(fn) {
95 if (this.listeners.length > 0) {
96 var index = this.listeners.findIndex(function (item) {
97 return item === fn;
98 });
99
100 if (index !== -1) {
101 this.listeners.splice(index, 1);
102 }
103 }
104 };
105
106 _proto.dispatchEvent = function dispatchEvent(info) {
107 if (this.listeners.length > 0) {
108 this.listeners.forEach(function (fn) {
109 return fn(info);
110 });
111 }
112 };
113
114 return Swipe;
115}();
116
117export { Swipe as default };
\No newline at end of file