-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
106 lines (84 loc) · 3.03 KB
/
scripts.js
File metadata and controls
106 lines (84 loc) · 3.03 KB
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
var lastLogTimestamp;
var toTest = { // https://developer.mozilla.org/en-US/docs/Web/Events
'mouse': { // https://www.w3.org/TR/DOM-Level-3-Events/#events-mouse-types
id: 'mouse-events',
label: 'Mouse Events',
events: ['click', 'dblclick', 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover', 'mouseup']
},
'touch': { // https://www.w3.org/TR/touch-events/#list-of-touchevent-types
id: 'touch-events',
label: 'Touch Events',
events: ['touchstart', 'touchend', 'touchmove', 'touchcancel']
},
'pointer': { // https://www.w3.org/TR/pointerevents/#pointer-event-types
id: 'pointer-events',
label: 'Pointer Events',
events: ['pointerover', 'pointerenter', 'pointerdown', 'pointermove', 'pointerup', 'pointercancel', 'pointerout', 'pointerleave', 'gotpointercapture', 'lostpointercapture']
}
}
function buildCheckboxListItem(item) {
var checkbox, label, li;
var listenerActive = false;
var listenerTarget = document.getElementById('test-container');
var ID = item.id;
var LABEL_TEXT = item.label;
var VALUE = item.id;
li = document.createElement('li')
checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
checkbox.setAttribute('id', ID);
checkbox.setAttribute('value', VALUE);
checkbox.onchange = function() {
listenerActive
? item.events.forEach(function(event) { removeEventListenerFromTarget(listenerTarget, event); })
: item.events.forEach(function(event) { addEventListenerToTarget(listenerTarget, event); });
listenerActive = !listenerActive;
}
li.appendChild(checkbox)
label = document.createElement('label');
label.setAttribute('for', ID);
label.innerHTML = LABEL_TEXT;
li.appendChild(label)
return li;
}
function log(event) {
var container, li, timestamp, timeDiff;
var logMessage = '';
timestamp = Date.now();
timeDiff = lastLogTimestamp ? timestamp - lastLogTimestamp : null;
lastLogTimestamp = timestamp;
container = document.getElementById('log-list')
logMessage += event.type;
if (typeof(timeDiff) !== 'undefined') {
logMessage += '(' + timeDiff + 'ms)'
}
li = document.createElement('li');
li.innerHTML = logMessage;
container.appendChild(li);
}
function addEventListenerToTarget(target, event) {
target.addEventListener(event, log);
}
function removeEventListenerFromTarget(target, event) {
target.removeEventListener(event, log);
}
function addSidebarOptions() {
var container, checkboxes;
container = document.getElementById('options-checkboxes');
checkboxes = document.createElement('ul')
container.appendChild(checkboxes)
Object.keys(toTest).forEach(function(key) {
var li = buildCheckboxListItem(toTest[key]);
checkboxes.appendChild(li);
});
}
function clearResults() {
var container = document.getElementById('log-list');
container.innerHTML = '';
}
function addButtonFunctionality() {
var clearButton = document.getElementById('clear-log-list');
clearButton.onclick = clearResults;
}
addSidebarOptions();
addButtonFunctionality();