diff options
Diffstat (limited to 'js/core/touch.js')
-rwxr-xr-x | js/core/touch.js | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/js/core/touch.js b/js/core/touch.js deleted file mode 100755 index 43577d8..0000000 --- a/js/core/touch.js +++ /dev/null | |||
@@ -1,175 +0,0 @@ | |||
1 | /*! UIkit 2.26.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */ | ||
2 | // Based on Zeptos touch.js | ||
3 | // https://raw.github.com/madrobby/zepto/master/src/touch.js | ||
4 | // Zepto.js may be freely distributed under the MIT license. | ||
5 | |||
6 | ;(function($){ | ||
7 | |||
8 | if ($.fn.swipeLeft) { | ||
9 | return; | ||
10 | } | ||
11 | |||
12 | |||
13 | var touch = {}, touchTimeout, tapTimeout, swipeTimeout, longTapTimeout, longTapDelay = 750, gesture; | ||
14 | |||
15 | function swipeDirection(x1, x2, y1, y2) { | ||
16 | return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down'); | ||
17 | } | ||
18 | |||
19 | function longTap() { | ||
20 | longTapTimeout = null; | ||
21 | if (touch.last) { | ||
22 | if ( touch.el !== undefined ) touch.el.trigger('longTap'); | ||
23 | touch = {}; | ||
24 | } | ||
25 | } | ||
26 | |||
27 | function cancelLongTap() { | ||
28 | if (longTapTimeout) clearTimeout(longTapTimeout); | ||
29 | longTapTimeout = null; | ||
30 | } | ||
31 | |||
32 | function cancelAll() { | ||
33 | if (touchTimeout) clearTimeout(touchTimeout); | ||
34 | if (tapTimeout) clearTimeout(tapTimeout); | ||
35 | if (swipeTimeout) clearTimeout(swipeTimeout); | ||
36 | if (longTapTimeout) clearTimeout(longTapTimeout); | ||
37 | touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null; | ||
38 | touch = {}; | ||
39 | } | ||
40 | |||
41 | function isPrimaryTouch(event){ | ||
42 | return event.pointerType == event.MSPOINTER_TYPE_TOUCH && event.isPrimary; | ||
43 | } | ||
44 | |||
45 | $(function(){ | ||
46 | var now, delta, deltaX = 0, deltaY = 0, firstTouch; | ||
47 | |||
48 | if ('MSGesture' in window) { | ||
49 | gesture = new MSGesture(); | ||
50 | gesture.target = document.body; | ||
51 | } | ||
52 | |||
53 | $(document) | ||
54 | .on('MSGestureEnd gestureend', function(e){ | ||
55 | |||
56 | var swipeDirectionFromVelocity = e.originalEvent.velocityX > 1 ? 'Right' : e.originalEvent.velocityX < -1 ? 'Left' : e.originalEvent.velocityY > 1 ? 'Down' : e.originalEvent.velocityY < -1 ? 'Up' : null; | ||
57 | |||
58 | if (swipeDirectionFromVelocity && touch.el !== undefined) { | ||
59 | touch.el.trigger('swipe'); | ||
60 | touch.el.trigger('swipe'+ swipeDirectionFromVelocity); | ||
61 | } | ||
62 | }) | ||
63 | // MSPointerDown: for IE10 | ||
64 | // pointerdown: for IE11 | ||
65 | .on('touchstart MSPointerDown pointerdown', function(e){ | ||
66 | |||
67 | if(e.type == 'MSPointerDown' && !isPrimaryTouch(e.originalEvent)) return; | ||
68 | |||
69 | firstTouch = (e.type == 'MSPointerDown' || e.type == 'pointerdown') ? e : e.originalEvent.touches[0]; | ||
70 | |||
71 | now = Date.now(); | ||
72 | delta = now - (touch.last || now); | ||
73 | touch.el = $('tagName' in firstTouch.target ? firstTouch.target : firstTouch.target.parentNode); | ||
74 | |||
75 | if(touchTimeout) clearTimeout(touchTimeout); | ||
76 | |||
77 | touch.x1 = firstTouch.pageX; | ||
78 | touch.y1 = firstTouch.pageY; | ||
79 | |||
80 | if (delta > 0 && delta <= 250) touch.isDoubleTap = true; | ||
81 | |||
82 | touch.last = now; | ||
83 | longTapTimeout = setTimeout(longTap, longTapDelay); | ||
84 | |||
85 | // adds the current touch contact for IE gesture recognition | ||
86 | if (gesture && ( e.type == 'MSPointerDown' || e.type == 'pointerdown' || e.type == 'touchstart' ) ) { | ||
87 | gesture.addPointer(e.originalEvent.pointerId); | ||
88 | } | ||
89 | |||
90 | }) | ||
91 | // MSPointerMove: for IE10 | ||
92 | // pointermove: for IE11 | ||
93 | .on('touchmove MSPointerMove pointermove', function(e){ | ||
94 | |||
95 | if (e.type == 'MSPointerMove' && !isPrimaryTouch(e.originalEvent)) return; | ||
96 | |||
97 | firstTouch = (e.type == 'MSPointerMove' || e.type == 'pointermove') ? e : e.originalEvent.touches[0]; | ||
98 | |||
99 | cancelLongTap(); | ||
100 | touch.x2 = firstTouch.pageX; | ||
101 | touch.y2 = firstTouch.pageY; | ||
102 | |||
103 | deltaX += Math.abs(touch.x1 - touch.x2); | ||
104 | deltaY += Math.abs(touch.y1 - touch.y2); | ||
105 | }) | ||
106 | // MSPointerUp: for IE10 | ||
107 | // pointerup: for IE11 | ||
108 | .on('touchend MSPointerUp pointerup', function(e){ | ||
109 | |||
110 | if (e.type == 'MSPointerUp' && !isPrimaryTouch(e.originalEvent)) return; | ||
111 | |||
112 | cancelLongTap(); | ||
113 | |||
114 | // swipe | ||
115 | if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) || (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)){ | ||
116 | |||
117 | swipeTimeout = setTimeout(function() { | ||
118 | if ( touch.el !== undefined ) { | ||
119 | touch.el.trigger('swipe'); | ||
120 | touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2))); | ||
121 | } | ||
122 | touch = {}; | ||
123 | }, 0); | ||
124 | |||
125 | // normal tap | ||
126 | } else if ('last' in touch) { | ||
127 | |||
128 | // don't fire tap when delta position changed by more than 30 pixels, | ||
129 | // for instance when moving to a point and back to origin | ||
130 | if (isNaN(deltaX) || (deltaX < 30 && deltaY < 30)) { | ||
131 | // delay by one tick so we can cancel the 'tap' event if 'scroll' fires | ||
132 | // ('tap' fires before 'scroll') | ||
133 | tapTimeout = setTimeout(function() { | ||
134 | |||
135 | // trigger universal 'tap' with the option to cancelTouch() | ||
136 | // (cancelTouch cancels processing of single vs double taps for faster 'tap' response) | ||
137 | var event = $.Event('tap'); | ||
138 | event.cancelTouch = cancelAll; | ||
139 | if ( touch.el !== undefined ) touch.el.trigger(event); | ||
140 | |||
141 | // trigger double tap immediately | ||
142 | if (touch.isDoubleTap) { | ||
143 | if ( touch.el !== undefined ) touch.el.trigger('doubleTap'); | ||
144 | touch = {}; | ||
145 | } | ||
146 | |||
147 | // trigger single tap after 250ms of inactivity | ||
148 | else { | ||
149 | touchTimeout = setTimeout(function(){ | ||
150 | touchTimeout = null; | ||
151 | if ( touch.el !== undefined ) touch.el.trigger('singleTap'); | ||
152 | touch = {}; | ||
153 | }, 250); | ||
154 | } | ||
155 | }, 0); | ||
156 | } else { | ||
157 | touch = {}; | ||
158 | } | ||
159 | deltaX = deltaY = 0; | ||
160 | } | ||
161 | }) | ||
162 | // when the browser window loses focus, | ||
163 | // for example when a modal dialog is shown, | ||
164 | // cancel all ongoing events | ||
165 | .on('touchcancel MSPointerCancel', cancelAll); | ||
166 | |||
167 | // scrolling the window indicates intention of the user | ||
168 | // to scroll, not tap or swipe, so cancel all ongoing events | ||
169 | $(window).on('scroll', cancelAll); | ||
170 | }); | ||
171 | |||
172 | ['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){ | ||
173 | $.fn[eventName] = function(callback){ return $(this).on(eventName, callback); }; | ||
174 | }); | ||
175 | })(jQuery); | ||