-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.html
386 lines (372 loc) · 10.7 KB
/
sim.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<title>KaiOS Simulator</title>
</head>
<body>
<div class="container">
<div class="panel device">
<div class="header">
<div class="headersection left">
<div class="headeritem mode">🔡</div>
</div>
<div class="headersection right">
<div class="headeritem">📶</div>
<div class="headeritem clock"></div>
</div>
</div>
<div class="glass">
<div class="keyselector">
<div>2</div>
<div class="selected">a</div>
<div>b</div>
<div>c</div>
</div>
</div>
<iframe id="appFrame" src="./" scrolling="no"></iframe>
<div class="brand">SIM</div>
<table class="keypad">
<tr>
<td><div class="button sk largeicon" data-key="SoftLeft">—</div></td>
<td><div class="button sk" data-key="ArrowUp">↑</div></td>
<td><div class="button sk largeicon" data-key="SoftRight">—</div></td>
</tr>
<tr>
<td><div class="button lr" data-key="ArrowLeft">←</div></td>
<td><div class="button sk" data-key="Enter">Enter</div></td>
<td><div class="button lr" data-key="ArrowRight">→</div></td>
</tr>
<tr>
<td><div class="button largeicon call">—</div></td>
<td><div class="button sk" data-key="ArrowDown">↓</div></td>
<td><div class="button largeicon backspace" data-key="Backspace">—</div></td>
</tr>
<tr>
<td><div class="button char" data-key="1">1 <span>.,?</span></div></td>
<td><div class="button char" data-key="2">2 <span>ABC</span></div></td>
<td><div class="button char" data-key="3">3 <span>DEF</span></div></td>
</tr>
<tr>
<td><div class="button char" data-key="4">4 <span>GHI</span></div></td>
<td><div class="button char" data-key="5">5 <span>JKL</span></div></td>
<td><div class="button char" data-key="6">6 <span>MNO</span></div></td>
</tr>
<tr>
<td><div class="button char" data-key="7">7 <span>PQRS</span></div></td>
<td><div class="button char" data-key="8">8 <span>TUV</span></div></td>
<td><div class="button char" data-key="9">9 <span>WXYZ</span></div></td>
</tr>
<tr>
<td><div class="button specialchars largeicon" data-key="*">∗</div></td>
<td><div class="button char" data-key="0">0 _</div></td>
<td><div class="button mode" data-key="#"># <span>⇪</span></div></td>
</tr>
</table>
</div>
<style type="text/css">
.container {
display: flex;
flex-wrap: wrap;
}
.panel {
margin: 20px;
width: 260px;
box-sizing: border-box;
}
.device {
width: 260px;
padding: 20px 10px;
border-radius: 20px;
background-color: grey;
position: relative;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
height: 20px;
background-color: #ccc;
}
.headersection.left {
display: flex;
justify-content: flex-start;
}
.headersection.right {
display: flex;
justify-content: flex-end;
}
.headeritem {
font-size: 14px;
font-family: sans-serif;
align-self: center;
margin: 0;
padding: 4px;
}
.glass {
position: absolute;
top: 40px;
left: 10px;
width: 240px;
height: 290px;
cursor: not-allowed;
}
.keyselector {
display: flex;
position: absolute;
bottom: 0;
left: 0;
height: 30px;
width: 100%;
background-color: #ccc;
}
.keyselector div {
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
}
.keyselector div.selected {
background-color: blue;
color: white;
}
#appFrame {
width: 240px;
height: 290px;
border: 0;
}
.brand {
padding: 10px 10px 6px 38px;
letter-spacing: 20px;
text-align: center;
color: white;
font-weight: bold;
font-family: sans-serif;
}
.keypad {
width: 100%;
}
.button {
color: white;
background-color: black;
font-weight: bold;
text-align: center;
padding: 5px;
border-radius: 20px;
font-size: 12px;
line-height: 12.5px;
font-family: sans-serif;
cursor: pointer;
}
.button.largeicon {
font-size: 26px;
}
.button.call {
color: green;
}
.button.backspace {
color: red;
}
.button span {
font-size: 8px;
}
</style>
<script type="text/javascript">
const keysMap = {
'1': ['.', ',', '?', '!', '1', ';', ':', '/', '@'],
'2': ['a', 'b', 'c', '2'],
'3': ['d', 'e', 'f', '3'],
'4': ['g', 'h', 'i', '4'],
'5': ['j', 'k', 'l', '5'],
'6': ['m', 'n', 'o', '6'],
'7': ['p', 'q', 'r', 's', '7'],
'8': ['t', 'u', 'v', '8'],
'9': ['w', 'x', 'y', 'z', '9'],
'0': [' ', '0']
};
const inputModes = [
{ name: 'lowercase', text: '🔡', fn: function(key, keys) { return keys.map(k => k.toLowerCase()); } },
{ name: 'uppercase', text: '🔠', fn: function(key, keys) { return keys.map(k => k.toUpperCase()); } },
{ name: 'number', text: '🔢', fn: function(key) { return key; } }
];
let currentInputMode = 0;
const inputModeIndicator = document.querySelector('.headeritem.mode');
const nextInputMode = function() {
currentInputMode = (currentInputMode === inputModes.length - 1 ? 0 : currentInputMode + 1);
inputModeIndicator.innerHTML = inputModes[currentInputMode].text;
}
const keysInCurrentMode = function(key) {
return inputModes[currentInputMode].fn(key, keysMap[key]);
}
const appFrame = document.querySelector('#appFrame');
const keySelector = (function($element) {
$element.style.visibility = 'hidden';
let keys = [];
let currentKey = null;
let timeout = null;
const close = function() {
$element.style.visibility = 'hidden';
}
const isClosed = function() {
return $element.style.visibility === 'hidden';
};
const open = function() {
$element.style.visibility = 'visible';
clearTimeout(timeout);
timeout = setTimeout(close, 2000);
};
const render = function() {
$element.innerHTML = '';
keys.forEach(function(k) {
const div = document.createElement('div');
if (k === currentKey) {
div.classList.add('selected')
}
div.appendChild(document.createTextNode(k));
$element.appendChild(div);
})
};
const arraysEqual = function(a, b) {
if (a.length !== b.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
return function(newKeys) {
let replace;
if (isClosed() || !arraysEqual(keys, newKeys)) {
keys = newKeys;
currentKey = newKeys[0];
replace = false;
} else {
let index = keys.indexOf(currentKey) + 1;
if (index >= keys.length) {
index = 0;
}
currentKey = keys[index];
replace = true;
}
render();
open();
return [ currentKey, replace ];
};
})(document.querySelector('.keyselector'));
const dispatchKeyToDoc = function(key) {
const newEvent = new KeyboardEvent('keydown', {
key: key,
composed: true,
bubbles: true,
cancelable: false
});
debugger;
appFrame.contentDocument.dispatchEvent(newEvent);
}
const isInput = function ($element) {
return $element.tagName === 'INPUT' || $element.tagName === 'TEXTAREA';
}
const keyHandlers = {
'.button.sk': function(e) {
dispatchKeyToDoc(e.currentTarget.getAttribute('data-key'));
},
'.button.backspace': function(e) {
const element = appFrame.contentDocument.activeElement;
if (isInput(element)) {
if (element.value !== '' && element.selectionStart > 0) {
const v = element.value
const caret = element.selectionStart - 1
element.value = v.substring(0, caret) + v.substring(caret + 1)
element.setSelectionRange(caret, caret)
element.dispatchEvent(new Event('input'))
}
} else {
dispatchKeyToDoc('Backspace')
}
},
'.button.lr': function(e) {
const element = appFrame.contentDocument.activeElement;
const key = e.currentTarget.getAttribute('data-key');
if (isInput(element)) {
if (key === 'ArrowRight') {
if (element.selectionStart < element.value.length) {
const caret = element.selectionStart + 1
element.setSelectionRange(caret, caret)
}
} else if (key === 'ArrowLeft') {
if (element.selectionStart > 0) {
const caret = element.selectionStart - 1
element.setSelectionRange(caret, caret)
}
}
} else {
dispatchKeyToDoc(key)
}
},
'.button.char': function(e) {
const keys = keysInCurrentMode(e.currentTarget.getAttribute('data-key'));
const element = appFrame.contentDocument.activeElement;
if (isInput(element)) {
const [key, replace] = Array.isArray(keys) ? keySelector(keys) : [keys, false]
let val = element.value
let caret = element.selectionStart
if (replace) {
val = val.substring(0, caret - 1) + key + val.substring(caret);
} else {
caret++;
val = val.substring(0, caret) + key + val.substring(caret);
}
element.value = val;
element.setSelectionRange(caret, caret);
element.dispatchEvent(new Event('input'));
} else {
dispatchKeyToDoc(keys[0]);
}
},
'.button.mode': nextInputMode
};
Object.keys(keyHandlers).forEach(function(selector) {
Array.from(document.querySelectorAll(selector)).forEach(function(b) {
b.addEventListener('click', keyHandlers[selector]);
});
});
// Prevent key and mouse events from stealing focus from the iframe
['keypress', 'keydown', 'keyup', 'mousedown', 'mouseup', 'click'].forEach(function(k) {
window.addEventListener(k, function(e) {
if (!e.target.getAttribute('data-allowlist')) {
e.preventDefault();
}
}, true);
});
// Cancel key and mouse events in app container iframe
['keypress', 'keydown', 'keyup', 'mousedown', 'mouseup', 'click'].forEach(function(k) {
appFrame.contentWindow.addEventListener(k, function(e) {
if (e.cancelable) {
e.preventDefault();
e.stopPropagation();
}
}, true);
});
// Clock
const pad = function(number) {
return (number < 10 ? '0' : '') + number;
}
const clock = document.querySelector('.headeritem.clock');
const updateClock = function() {
const d = new Date();
clock.innerText = d.getHours() + ':' + pad(d.getMinutes());
}
updateClock()
setInterval(updateClock, 60*1000);
</script>
</div>
</body>
</html>