|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var SwipeActionsManager; |
|
5
|
|
|
var $ = require('jquery'); |
|
6
|
|
|
var _ = require('underscore'); |
|
7
|
|
|
var mediator = require('oroui/js/mediator'); |
|
8
|
|
|
var error = require('oroui/js/error'); |
|
9
|
|
|
|
|
10
|
|
|
var DEFAULT_OPTIONS = { |
|
11
|
|
|
minDistanceXAxis: 30, |
|
12
|
|
|
maxDistanceYAxis: 30, |
|
13
|
|
|
maxAllowedTime: 1000 |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Swipe actions on mobile devices |
|
18
|
|
|
* |
|
19
|
|
|
* @param {String} elementSelector |
|
20
|
|
|
* @param {Object} options |
|
21
|
|
|
* @returns {*} |
|
22
|
|
|
* @constructor |
|
23
|
|
|
*/ |
|
24
|
|
|
SwipeActionsManager = function(elementSelector, options) { |
|
25
|
|
|
if (!elementSelector) { |
|
26
|
|
|
return error.showErrorInConsole('"elementSelector" should be defined'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
this.direction = null; |
|
30
|
|
|
this.touchStartCoords = null; |
|
31
|
|
|
this.touchEndCoords = null; |
|
32
|
|
|
this.elapsedTime = 0; |
|
33
|
|
|
this.startTime = 0; |
|
34
|
|
|
|
|
35
|
|
|
this.$el = $(elementSelector); |
|
36
|
|
|
|
|
37
|
|
|
this.swipeInitialize(options); |
|
|
|
|
|
|
38
|
|
|
}; |
|
39
|
|
|
|
|
40
|
|
|
SwipeActionsManager.prototype = { |
|
41
|
|
|
/** |
|
42
|
|
|
* Initialize, merge options |
|
43
|
|
|
* |
|
44
|
|
|
* @param {Object} options |
|
45
|
|
|
*/ |
|
46
|
|
|
swipeInitialize: function(options) { |
|
47
|
|
|
_.extend(this, _.defaults(_.pick(options, |
|
48
|
|
|
['minDistanceXAxis', 'maxDistanceYAxis', 'maxAllowedTime'] |
|
49
|
|
|
), DEFAULT_OPTIONS)); |
|
50
|
|
|
|
|
51
|
|
|
this._bindEvents(); |
|
52
|
|
|
}, |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Bind touch events |
|
56
|
|
|
* @private |
|
57
|
|
|
*/ |
|
58
|
|
|
_bindEvents: function() { |
|
59
|
|
|
this.$el.on('touchstart', _.bind(this._swipeStart, this)); |
|
60
|
|
|
this.$el.on('touchend', _.bind(this._swipeEnd, this)); |
|
61
|
|
|
}, |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Handler for start touch |
|
65
|
|
|
* |
|
66
|
|
|
* @param {jQuery.Event} event |
|
67
|
|
|
* @private |
|
68
|
|
|
*/ |
|
69
|
|
|
_swipeStart: function(event) { |
|
70
|
|
|
event = ('changedTouches' in event) ? event.changedTouches[0] : event; |
|
71
|
|
|
|
|
72
|
|
|
this.touchStartCoords = { |
|
73
|
|
|
x: event.pageX, |
|
74
|
|
|
y: event.pageY |
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
this.startTime = new Date().getTime(); |
|
78
|
|
|
}, |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Handler for end touch and fire external mediator event |
|
82
|
|
|
* |
|
83
|
|
|
* @param {jQuery.Event} event |
|
84
|
|
|
* @private |
|
85
|
|
|
*/ |
|
86
|
|
|
_swipeEnd: function(event) { |
|
87
|
|
|
event = ('changedTouches' in event) ? event.changedTouches[0] : event; |
|
88
|
|
|
|
|
89
|
|
|
this.touchEndCoords = { |
|
90
|
|
|
x: event.pageX - this.touchStartCoords.x, |
|
91
|
|
|
y: event.pageY - this.touchStartCoords.y |
|
92
|
|
|
}; |
|
93
|
|
|
this.elapsedTime = new Date().getTime() - this.startTime; |
|
94
|
|
|
|
|
95
|
|
|
if (this.elapsedTime <= this.maxAllowedTime) { |
|
96
|
|
|
if ( |
|
97
|
|
|
Math.abs(this.touchEndCoords.x) >= this.minDistanceXAxis && |
|
98
|
|
|
Math.abs(this.touchEndCoords.y) <= this.maxDistanceYAxis |
|
99
|
|
|
) { |
|
100
|
|
|
this.direction = (this.touchEndCoords.x < 0) ? 'left' : 'right'; |
|
101
|
|
|
mediator.trigger('swipe-action-' + this.direction); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
}; |
|
106
|
|
|
|
|
107
|
|
|
return SwipeActionsManager; |
|
108
|
|
|
}); |
|
109
|
|
|
|