-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.js
351 lines (336 loc) · 10.8 KB
/
keyboard.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
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
/* -*- js-indent-level: 2 -*- */
/* global $:readonly */
/* exported transcriptKeyboard */
// $input is a jQuery DOM element in which to insert text (a <textarea> or
// <input> element). This element will be wrapped in a container element
// ($wrapper), and a virtual keyboard ($keyboard) will added at the end of the
// container.
function transcriptKeyboard($wrapper, $input, $keyboardIcon) {
'use strict'
// Insert text in a textarea.
let input = $input[0] // DOM input element
function insertAtCursor(str) {
let focused = document.activeElement // focused DOM element
let refocused = false
// Focus text input element (if not already focused).
if (focused !== input) {
input.focus()
refocused = true
}
// Edit input field.
if (document.selection) {
// IE support
let sel = document.selection.createRange()
sel.text = str
} else if (input.selectionStart || input.selectionStart === 0) {
// MOZILLA and others
let begPos = input.selectionStart
let endPos = input.selectionEnd
let newPos = begPos + str.length
let value = input.value
// Replace string & move cursor.
input.value = value.slice(0, begPos) + str + value.slice(endPos)
input.setSelectionRange(newPos, newPos)
} else {
// other
input.value += str
}
// Refocus original element.
if (refocused) {
focused.focus()
}
}
function onButton(e) {
let k = e.which
if (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) {
return
}
if (k === 1 || k === 13 || k === 32) { // Click, enter or space
e.preventDefault()
if ($(e.target).is('button')) {
// String inside button (stripping off any + ◌).
let str = $(e.target).html().replace(/( |◌)/g, '')
insertAtCursor(str)
}
}
}
function insertKeyboardInDom($wrapper) {
let keyboardHtml = [[
{ prefix: 'Relation: ' },
'<nobr>',
['◌', 'Brevid'],
['◌', 'Framför'],
['◌', 'Innanför'],
['◌', 'Ovanför'],
['◌', 'Nedanför'],
'</nobr>',
], [
{ prefix: 'Attityd: ' },
'<nobr>',
['', 'Vänsterriktad'],
['', 'Högerriktad'],
'</nobr>',
'<nobr>',
['', 'Framåtriktad'],
['', 'Inåtriktad'],
'</nobr>',
'<nobr>',
['', 'Uppåtriktad'],
['', 'Nedåtriktad'],
'</nobr>',
'<nobr>',
[' ', 'Vänstervänd'],
[' ', 'Högervänd'],
'</nobr>',
'<nobr>',
[' ', 'Framåtvänd'],
[' ', 'Inåtvänd'],
'</nobr>',
'<nobr>',
[' ', 'Uppåtvänd'],
[' ', 'Nedåtvänd'],
'</nobr>',
], [
{ prefix: 'Läge: ' },
'<nobr>',
['', 'Ansikte'],
['', 'Övre ansikte'],
['', 'Undre ansikte'],
'</nobr>',
['', 'Hjässa'],
['', 'Panna'],
['', 'Ögon'],
['', 'Öga'],
['', 'Öra'],
['', 'Vänster öra'],
['', 'Höger öra'],
['', 'Kinder'],
['', 'Vänster kind'],
['', 'Höger kind'],
['', 'Näsa'],
['', 'Mun'],
['', 'Haka'],
['', 'Nacke'],
['', 'Hals'],
['', 'Axlar'],
['', 'Vänster axel'],
['', 'Höger axel'],
['', 'Arm'],
['', 'Överarm'],
['', 'Armvecket'],
['', 'Underarm'],
['', 'Bröst'],
['', 'Vänster bröst'],
['', 'Höger bröst'],
['', 'Mage'],
['', 'Höfter'],
'<nobr>',
['', 'Vänster höft'],
['', 'Höger höft'],
['', 'Ben'],
'</nobr>',
], [
{ prefix: 'Handform: ' },
'<nobr>',
['', ['a-hand', 'A-hand']],
['', ['tumvinkelhand', 'Tumvinkelhand']],
['', ['vinkelhand', 'Vinkelhand']],
'</nobr>',
['', ['tumhand', 'Tumhand']],
['', ['matthand', 'Måtthand'], ['kopa-hand', 'Köpa-handen']],
['', ['rak-matthand', 'Rak måtthand']],
['', ['d-hand', 'D-hand']],
['', ['nyphand-a', 'Nyphand A'], ['nyphand-b', 'Nyphand B']],
['', ['liten-o-hand-a', 'Liten O-hand A'],
['liten-o-hand-b', 'Liten O-hand B']],
['', ['e-hand', 'E-hand']],
['', ['stor-hallhand', 'Stor hållhand']],
['', ['knuten-hand', 'Knuten hand']],
['', ['stor-nyphand', 'Stor nyphand']],
['', ['lillfinger', 'Lillfinger']],
['', ['flyghand', 'Flyghand']],
['', ['flat-hand', 'Flat hand']],
['', ['flat-tumhand', 'Flat tumhand']],
['', ['krokfinger', 'Krokfinger']],
['', ['k-hand', 'K-hand']],
['', ['pekfinger', 'Pekfinger'],
['vinklat-pekfinger', 'Vinklat pekfinger']],
['', ['l-hand', 'L-hand']],
['', ['m-hand', 'M-hand']],
['', ['n-hand', 'N-hand'], ['n-tumhand', 'N-tumhand'],
['vinklad-n-hand', 'Vinklad N-hand'],
['vinklad-n-tumhand', 'Vinklad N-tumhand']],
['', ['o-hand', 'O-hand']],
['', ['hallhand', 'Hållhand']],
['', ['q-hand', 'Q-hand']],
['', ['langfinger', 'Långfinger'],
['vinklat-langfinger', 'Vinklat långfinger']],
['', ['s-hand', 'S-hand'], ['s-tumhand', 'S-tumhand']],
['', ['klohand', 'Klohand'], ['matte-hand', 'Matte-hand']],
['', ['t-hand', 'T-hand']],
['', ['u-hand', 'U-hand']],
['', ['dubbelkroken', 'Dubbelkrok']],
['', ['bojd-tupphand', 'Böjd tupphand']],
['', ['v-hand', 'V-hand']],
['', ['tupphand', 'Tupphand']],
['', ['w-hand', 'W-hand']],
['', ['x-hand', 'X-hand']],
['', ['sprethand', 'Sprethand'],
['vinkad-sprethand', 'Vinklad sprethand']],
'<nobr>',
['', ['stort-langfinger', 'Stort långfinger']],
['', ['runt-langfinger', 'Runt långfinger']],
['', ['4-hand', '4-hand']],
'</nobr>',
], [
{},
'<nobr>',
[' ', 'Medial kontakt (modifierande)'],
['', 'Kontakt'],
['', 'Hålls stilla'],
'</nobr>',
'<nobr>',
['', 'Förs åt vänster'],
['', 'Förs åt höger'],
['', 'Förs i sidled'],
'</nobr>',
'<nobr>',
['', 'Förs framåt'],
['', 'Förs inåt/bakåt'],
['', 'Förs i djupled'],
'</nobr>',
'<nobr>',
['', 'Förs uppåt'],
['', 'Förs nedåt'],
['', 'Förs i höjdled'],
'</nobr>',
'<nobr>',
['', 'Förs kort åt vänster'],
['', 'Förs kort åt höger'],
'</nobr>',
'<nobr>',
['', 'Förs kort framåt'],
['', 'Förs kort inåt/bakåt'],
'</nobr>',
'<nobr>',
['', 'Förs kort uppåt'],
['', 'Förs kort nedåt'],
'</nobr>',
' ',
['', 'Divergerar'],
['', 'Konvergerar'],
['', 'Korsas'],
['', 'Hakas'],
['', 'Byter plats'],
['', 'Gör entré'],
['', 'I vinkel'],
' ',
['', 'Spelar'],
['', 'Strör'],
['', 'Böjs'],
'<nobr>',
['', 'Vinkar'],
['', 'Förändras'],
['', 'Växelvis'],
'</nobr>',
], [
{},
'<nobr>',
['', 'Båge'],
['', 'Cirkel'],
['', 'Slås'],
'</nobr>',
['', 'Vrids'],
[' ', 'Åt vänster'],
[' ', 'Åt höger'],
[' ', 'I sidled'],
[' ', 'Framåt'],
[' ', 'Inåt/bakåt'],
[' ', 'I djupled'],
[' ', 'Uppåt'],
'<nobr>',
[' ', 'Nedåt'],
[' ', 'I höjdled'],
[' ', 'Mot varandra'],
'</nobr>',
], [
{},
'<nobr>',
['', 'Upprepning'],
['', 'Separator mellan artikulationer'],
['', 'Separator mellan segment'],
['', 'Separator mellan händer'],
'</nobr>',
], [
{},
'<nobr><div style="display:inline-block;font-size:.5em;line-height:120%">Följande symboler är nya<br>och används inte i lexikon än:</div> ',
['', 'Läge: Neutrala läget'],
'</nobr>',
['', 'Medial kontakt (oberoende)'],
['', 'Flätas'],
['', ['plane-frontal', 'Cirkel i frontalplan<br>(matchar och )']],
['', ['plane-horizontal', 'Cirkel i horisontalplan<br>(matchar och )']],
'<nobr>',
['', ['plane-median', 'Cirkel i sagittalplan<br>(matchar och )']],
['', 'Studs'],
[' ', 'Från varandra'],
'</nobr>',
]].map(([meta, ...units]) => {
return '<span>'
+ units.map(unit => {
if (typeof unit === 'string') { // raw HTML
return unit
}
let [label, ...txts] = unit
let prefix = meta.prefix || ''
function imgText(txts) {
if (typeof txts[0] === 'string') { return txts[0] }
return `<div class=imgs>` + txts.map(([img, txt], i) => (
`<span><img src="pic/keyboard/${img}.png">${txt}</span>`
+ ((i % 2) ? '<br>' : '')
)).join('') + '</div>'
}
return `<button title="${prefix}${imgText(txts)}">${label}</button>`
}).join('') + '</span>'
}).join(' ')
return $(`<div id=keyboard>${keyboardHtml}</div>`)
.appendTo($wrapper)
}
$(() => {
let $keyboard = insertKeyboardInDom($wrapper)
$keyboard.on('click', 'button', onButton)
// Escape anywhere inside form: Toggle virtual keyboard.
$wrapper.on('keydown', e => {
if (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) {
return
}
let k = e.which
if (k === 27) { // Escape
e.preventDefault()
$wrapper.toggleClass('show-keyboard')
if (!$(e.target).is(':visible')) {
$input.focus()
}
}
})
// Click on keyboard icon: Toggle virtual keyboard.
$keyboardIcon.on('click', e => {
if (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) {
return
}
e.preventDefault()
$wrapper.toggleClass('show-keyboard')
})
// Click/focus outside virtual keyboard: Hide it.
$(document.body).on('click focus', e => {
if ($(e.target).closest($wrapper).length === 0) {
$wrapper.removeClass('show-keyboard')
}
})
// Submit: Hide virtual keyboard.
$wrapper.on('submit', () => {
$wrapper.removeClass('show-keyboard')
})
})
}
/*[eof]*/