This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
LightningTouch.js
248 lines (213 loc) · 7.97 KB
/
LightningTouch.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
243
244
245
246
247
248
/**
* Lightning Touch makes links responsive without the several
* hundred millisecond delay typical in a hendheld touchscreen browser.
*
* Click events can take 300 ms or so to register on a mobile device because
* the device is waiting to see if it's a double click or a touch-and-drag
* event. Use touchStart etc. to work around this issue, but it's not as
* straightforward as one might hope.
*
* This code started with a portion of fastButtons created and shared by Google
* and used according to terms described in the Creative Commons 3.0 Attribution
* License. fastButtons can be found at:
* http://code.google.com/mobile/articles/fast_buttons.html
*
* @module Lightning Touch
* @author Rich Trott
* @copyright Copyright (c) 2012 UC Regents
* @version 1.0.1
*/
/*global document: false, history: false, location: false, window: false */
(function () {
"use strict";
var link = [],
states = [],
indexToUrl = [],
defaultTargetId,
LightningTouch,
setState,
saveState,
getState,
hideArray,
coordinates,
pop,
preventGhostClick,
clickBust,
init;
coordinates = [];
pop = function () {
coordinates.splice(0, 2);
};
preventGhostClick = function (x, y) {
coordinates.push(x, y);
window.setTimeout(pop, 2500);
};
clickBust = function (event) {
var i, x, y;
for (i = 0; i < coordinates.length; i += 2) {
x = coordinates[i];
y = coordinates[i + 1];
if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) {
event.stopPropagation();
event.preventDefault();
}
}
};
document.addEventListener('click', clickBust, true);
LightningTouch = function (element, handler) {
this.element = element;
this.handler = handler;
element.addEventListener('touchstart', this, false);
element.addEventListener('click', this, false);
};
LightningTouch.prototype.handleEvent = function (event) {
switch (event.type) {
case 'touchstart':
this.onTouchStart(event);
break;
case 'touchmove':
this.onTouchMove(event);
break;
case 'touchend':
this.onClick(event);
break;
case 'click':
this.onClick(event);
break;
}
};
LightningTouch.prototype.onTouchStart = function (event) {
event.stopPropagation();
this.element.addEventListener('touchend', this, false);
document.body.addEventListener('touchmove', this, false);
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
};
LightningTouch.prototype.onTouchMove = function (event) {
if (Math.abs(event.touches[0].clientX - this.startX) > 10 ||
Math.abs(event.touches[0].clientY - this.startY) > 10) {
this.reset();
}
};
LightningTouch.prototype.onClick = function (event) {
event.stopPropagation();
this.reset();
this.handler(event);
if (event.type === 'touchend') {
preventGhostClick(this.startX, this.startY);
}
};
LightningTouch.prototype.reset = function () {
this.element.removeEventListener('touchend', this, false);
document.body.removeEventListener('touchmove', this, false);
};
setState = function (object, url) {
var index = indexToUrl.indexOf(url);
if (index < 0) {
index = indexToUrl.length;
indexToUrl.push(url);
}
states[index] = object;
};
saveState = function (object, title, url) {
url = url || location.pathname + location.hash;
setState(object, url);
history.replaceState(object, title, url);
};
getState = function (id) {
var url = id ? location.pathname + '#/' + id : location.pathname + location.hash,
index = indexToUrl.indexOf(url);
return index < 0 ? undefined : states[index];
};
hideArray = function (hide, newHideId) {
if (hide.indexOf(newHideId) < 0) {
hide.push(newHideId);
}
return hide;
};
init = function () {
var anchors, i, touchHandler, popHandler;
defaultTargetId = document.body.getAttribute('data-default-target-id') || '';
function showContent(show, hide) {
var hideElement, showElement, i;
showElement = document.getElementById(show) || document.getElementById(defaultTargetId);
if (showElement) {
for (i = 0; i < hide.length; i += 1) {
hideElement = document.getElementById(hide[i]);
if (hideElement) {
hideElement.setAttribute("style", "display:none");
}
}
showElement.setAttribute("style", "display:block");
}
}
if ((!location.hash) && (defaultTargetId !== '')) {
location.hash = '#/' + defaultTargetId;
}
showContent(location.hash.substring(2), [defaultTargetId]);
if (!(history instanceof Object && history.replaceState instanceof Function)) {
return;
}
touchHandler = function (event) {
var targetId, target, clickedNode, clickedNodeId, state, hide;
targetId = this.element.getAttribute("data-target-id");
target = document.getElementById(targetId);
if (target !== null) {
event.preventDefault();
clickedNode = document.getElementById(location.hash.substr(2));
clickedNodeId = clickedNode ? clickedNode.getAttribute('id') : defaultTargetId;
showContent(targetId, [clickedNodeId]);
state = getState();
hide = (state && state.hasOwnProperty('hide')) ?
hideArray(state.hide, targetId) :
[targetId];
saveState({
show: clickedNodeId,
hide: hide
}, '');
location.hash = '#/' + targetId;
state = getState();
hide = (state && state.hasOwnProperty('hide')) ?
hideArray(state.hide, clickedNodeId) :
[clickedNodeId];
saveState({
show: targetId,
hide: hide
}, '');
}
};
anchors = document.getElementsByTagName("a");
for (i = 0; i < anchors.length; i += 1) {
if (anchors[i].getAttribute('data-target-id') !== null) {
link.push(new LightningTouch(anchors[i], touchHandler));
}
}
popHandler = function (event) {
var state, previousState, hide;
state = getState();
if (state) {
showContent(state.show, state.hide);
}
if (event.state && event.state.hide) {
//Retrieve adjacent pages and add our "show" value to their hide values
for (i = 0; i < event.state.hide.length; i += 1) {
previousState = getState(event.state.hide[i]);
if (previousState) {
hide = hideArray(previousState.hide, event.state.show);
setState({
show: event.state.hide[i],
hide: hide
}, location.pathname + '#/' + event.state.hide[i]);
}
}
showContent(event.state.show, event.state.hide);
}
};
window.addEventListener("popstate", popHandler, false);
};
if (document.readyState === "complete" || document.readyState === "interactive") {
init();
} else {
document.addEventListener('DOMContentLoaded', init, false);
}
}());