-
Notifications
You must be signed in to change notification settings - Fork 0
/
holo-touch.js
62 lines (52 loc) · 2.07 KB
/
holo-touch.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
window.addEventListener("load", function(e) {
// Do not run the script on Chrome 32 or higher.
/*if(/Chrome\/[3-9][2-9]/.test(navigator.userAgent)) {
return;
}*/
// Create an array of all input elements.
var inputElems = Array.prototype.slice.call(document.getElementsByTagName("button")).concat(
Array.prototype.slice.call(document.getElementsByTagName("select"))).concat(
Array.prototype.slice.call(document.getElementsByTagName("input")));
// If they can be found, identify all elements with role="button".
if(document.querySelectorAll) {
inputElems = inputElems.concat(
Array.prototype.slice.call(document.querySelectorAll("*[role=\"button\"]")));
}
var elemTypes = ["button", "select"];
var inputTypes = ["button", "checkbox", "radio", "range", "reset", "submit"];
for(var i = 0; i < inputElems.length; i++) {
// If the element is a supported form element,
if(elemTypes.indexOf(inputElems[i].tagName.toLowerCase()) !== -1 ||
// Or the element is a supported <input> type,
inputTypes.indexOf(inputElems[i].type.toLowerCase()) !== -1 ||
// Or the element has role="button",
(inputElems[i].getAttribute("role") && inputElems[i].getAttribute("role").toLowerCase() === "button")) {
// Add the touch event listeners.
inputElems[i].addEventListener("touchstart", makeActive, false);
inputElems[i].addEventListener("touchend", makeInactive, false);
inputElems[i].addEventListener("touchcancel", makeInactive, false);
}
}
/**
* Adds the “active” class to the touched element
* @param {TouchEvent} e
*/
function makeActive(e) {
if(!!e.srcElement.classList) { // use classList API if available
e.srcElement.classList.add("active");
} else {
e.srcElement.className += " active";
}
}
/**
* Removes the “active” class from the touched element
* @param {TouchEvent} e
*/
function makeInactive(e) {
if(!!e.srcElement.classList) { // use classList API if available
e.srcElement.classList.remove("active");
} else {
e.srcElement.className = e.srcElement.className.replace(/\s*active/g, "");
}
}
}, false);