forked from tomasz-swirski/iOS9-Safari-Click-Fix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iOS9.js
242 lines (193 loc) · 6.7 KB
/
iOS9.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* jQuery 2.0+ REQUIRED
* ==============================================
* iOS9 'click', 'mousedown' and 'mouseup' fix
* ---------------------------------------------
* Include this script in your poject to fix 'click', 'mousedown' and 'mouseup' event
* handling for $(window), $(document), $('body') and $('html'). By default iOS9 Safari is
* suppressing those events in some situations and without some magic they can't be rely on.
* This fix is blocking native event handlers from firing
* (in some rare cases event will reach it's destination)
* and it handles native event handlers basing on 'touchstart' and 'touchend' event.
* ---------------------------------------------
* Use at your own risk
*/
$(document).ready(function(){
/** Device is not iOS. There's no need to hack the planet. */
if( typeof navigator.userAgent == 'undefined' || ! navigator.userAgent.match(/(iPad|iPhone|iPod)/i) ){
return;
}
var EVENT_NAMESPACE = 'IOS9FIX';
var MAX_DOM_DEPTH = 100;
/**
* Suppress event for $object.
* @param $object
* @param eventType
*/
var blockEventFor = function($object, eventType) {
var eventQueue, eventRepo = new Array();
if($._data($object.get(0),"events") !== undefined){
eventQueue = $._data($object.get(0),"events")[eventType];
}
if(eventQueue !== undefined) {
for(var i = 0; i < eventQueue.length; i++) {
eventRepo.push({
handler: eventQueue[i].handler,
selector: eventQueue[i].selector,
namespace: eventQueue[i].namespace
});
}
$object.off(eventType);
}
$object.on(eventType + '.' + EVENT_NAMESPACE, '*', function(event){
event.stopImmediatePropagation();
});
for(var i = 0; i < eventRepo.length; i++) {
var _eventType = eventRepo[i].namespace
? eventType + '.' + eventRepo[i].namespace
: eventType;
$object.on(_eventType, eventRepo[i].selector, eventRepo[i].handler);
}
};
/**
* EXECUTE MOCKED-EVENT HANDLERS
* @param object $object
* @param string mockedEventType
* @param object originalEvent
*/
var executeMockedEventHandlers = function($object, mockedEventType, originalEvent){
/** Let's say touch is mouse left button (by default touch event has .which === 0) */
originalEvent.which = 1;
var mockedEventQueue, $target = $(originalEvent.target);
if($._data($object.get(0), "events") !== undefined){
mockedEventQueue = $._data($object.get(0), "events")[mockedEventType];
}
/** No event-handlers for event of such type */
if(mockedEventQueue === undefined){
return false;
}
/** Traverse DOM from 'target' to 'base' and execute mockedEventHandlers for all matched elements */
for(var preventEndlessLoop = 0; preventEndlessLoop < MAX_DOM_DEPTH; preventEndlessLoop++){
/** END THE LOOP */
if($target.length == 0){
break;
}
/** EXECUTE MOCKED EVENT HANDLERS */
for(var i = 0; i < mockedEventQueue.length; i++){
// Skip eventHandler used to block originalEvent for mockedEvent
if(mockedEventQueue[i].namespace === EVENT_NAMESPACE){
continue;
}
if(mockedEventQueue[i].selector === undefined){
// Skip $object level eventHandlers until current DOM level is $object level
if( ! $target.is($object[0])) {
continue;
}
} else {
// Skip eventHandlers not meant for current DOM level
if( ! $target.is(mockedEventQueue[i].selector)){
continue;
}
}
// Execute handler for current DOM level
if(mockedEventQueue[i].handler.call($target[0], originalEvent) === false){
originalEvent.stopImmediatePropagation();
}
// Check for stopImmediatePropagation() */
if(originalEvent.isImmediatePropagationStopped()){
break;
}
}
if(originalEvent.isPropagationStopped()){
break;
}
/** Go to parent level */
$target = $target.parent();
}
};
/*****************************
* INITIALIZATION
****************************/
/**
* Go through objects and suppress all selected events.
*/
$.each([$(document), $(window), $('body'), $('html')], function(objectIndex, $object){
$.each(['mousedown', 'click', 'mouseup'], function(eventIndex, eventType){
blockEventFor($object, eventType);
});
});
/**
* MOCK MOUSEDOWN EVENT
*/
/**
* Init MouseDown-Mock for Dom $object
* @param $object
*/
var initMouseDownMock = function($object) {
$object.on('touchstart', function (event) {
executeMockedEventHandlers($object, 'mousedown', event);
});
};
/**
* Init MouseDown-Mock for objects...
*/
$.each([$(document), $(window), $('body'), $('html')], function(objectIndex, $object){
initMouseDownMock($object);
});
/**
* MOCK MOUSEUP EVENT
*/
/**
* Init MouseUp-Mock for Dom $object
* @param $object
*/
var initMouseUpMock = function($object) {
$object.on('touchend', function (event) {
executeMockedEventHandlers($object, 'mouseup', event);
});
};
/**
* Init MouseUp-Mock for objects...
*/
$.each([$(document), $(window), $('body'), $('html')], function(objectIndex, $object){
initMouseUpMock($object);
});
/**
* MOCK CLICK EVENT
*/
/**
* Init Click-Mock for Dom $object
* @param $object
*/
var initClickMock = function($object) {
var clickCancelationTimer, isClick, cursorX, cursorY, target;
$object.on('touchstart', function(event){
isClick = true;
cursorX = event.originalEvent.touches[0].pageX;
cursorY = event.originalEvent.touches[0].pageY;
target = event.target;
/** Click Timeout */
clickCancelationTimer = setTimeout(function(){
isClick = false;
}, 300);
});
/** moved more than 10 px away from starting position */
$object.on('touchmove', function(event){
if(Math.abs(cursorX - event.originalEvent.touches[0].pageX) > 10 || Math.abs(cursorY - event.originalEvent.touches[0].pageY) > 10){
isClick = false;
}
});
$object.on('touchend', function(event){
clearTimeout(clickCancelationTimer);
if(isClick){
executeMockedEventHandlers($object, 'click', event);
}
});
};
/**
* Init Click-Mock for objects...
*/
$.each([$(document), $(window), $('body'), $('html')], function(objectIndex, $object){
initClickMock($object);
});
});