-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.html
153 lines (134 loc) · 4.77 KB
/
example.html
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
<!DOCTYPE html>
<html>
<head>
<title>Example App</title>
<link rel="stylesheet" href="https://unpkg.com/bridge-1100/dist/index.css">
<link rel="stylesheet" href="https://unpkg.com/bridge-1100/dist/font.css">
<style>
.screen:not([hidden]) {
display: flex;
flex-direction: column;
height: 100%;
}
.screen>*:not(footer) {
height: 100%;
}
.screen ul {
list-style: none;
padding: 0;
margin: 0;
}
.screen li.active {
background-color: var(--c-black-alpha);
color: var(--c-trans);
}
</style>
</head>
<body>
<div class="screen" id="intro">
<span>This is an example app to demo Bridge 1100's use. Select an option on the next page</span>
<footer>Next</footer>
</div>
<div class="screen" id="events" hidden>
<ul>
<li class="active">Keypad/numpad</li>
<li>Vibration (mobile)</li>
</ul>
<footer>Select</footer>
</div>
<div class="screen" id="event-keypad" hidden>
<div>Key pressed: <span id="key">--</span></div>
<div>(Hold any key to return)</div>
</div>
<div class="screen" id="event-haptics" hidden>
<ul>
<li class="active">Light</li>
<li>Medium</li>
<li>Heavy</li>
</ul>
<footer>Vibrate</footer>
</div>
<script src="https://unpkg.com/bridge-1100/dist/index.umd.js"></script>
<script>
var screens = document.querySelectorAll(".screen");
var screenMap = {
intro: document.querySelector("#intro"),
events: document.querySelector("#events"),
'event-keypad': document.querySelector("#event-keypad"),
'event-haptics': document.querySelector("#event-haptics"),
}
/** @type {keyof screenMap} */
var activeScreen = 'intro';
var activeSelection = 0;
var shakeIntensity = ['LIGHT', 'MEDIUM', 'HEAVY'];
/** @param {keyof screenMap} screenId */
function toggleScreen(screenId) {
screens.forEach(screen => screen.hidden = true);
screenMap[screenId].hidden = false;
activeScreen = screenId;
toggleSelection(0);
}
function toggleSelection(selection) {
var list = screenMap[activeScreen].querySelector('ul');
if (!list) return;
var items = list.querySelectorAll('li');
items.forEach(li => li.classList.remove('active'));
items[selection].classList.add('active');
activeSelection = selection;
}
function handleKeyPress(key) {
if (activeScreen === 'intro' && key === 'ok') {
toggleScreen('events');
return; // prevent fallthrough
}
if (activeScreen === 'events') {
if (key === 'up') {
toggleSelection(activeSelection === 0 ? 1 : 0);
}
if (key === 'down') {
toggleSelection(activeSelection === 1 ? 0 : 1);
}
if (key === 'ok') {
if (activeSelection === 0) {
toggleScreen('event-keypad');
}
if (activeSelection === 1) {
toggleScreen('event-haptics');
}
}
if (key === 'clear') {
toggleScreen('intro');
}
return; // prevent fallthrough
}
if (activeScreen === 'event-keypad') {
document.querySelector("#key").textContent = key;
return; // prevent fallthrough
}
if (activeScreen === 'event-haptics') {
if (key === 'up') {
toggleSelection(activeSelection === 0 ? 2 : activeSelection - 1);
}
if (key === 'down') {
toggleSelection(activeSelection === 2 ? 0 : activeSelection + 1);
}
if (key === 'ok') {
window.bridge.send(window.parent, { event: "shake", data: shakeIntensity[activeSelection] });
}
if (key === 'clear') {
toggleScreen('events');
}
}
}
function handleKeyHold(key) {
if (activeScreen === 'event-keypad') {
toggleScreen('events');
}
}
window.bridge.on("keypress", handleKeyPress);
window.bridge.on("numpress", handleKeyPress);
window.bridge.on('keyhold', handleKeyHold);
window.bridge.on('numhold', handleKeyHold);
</script>
</body>
</html>