-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexicon.js
904 lines (850 loc) · 30.2 KB
/
lexicon.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/* -*- js-indent-level: 2 -*- */
/* global $:readonly, jQuery:readonly */
/* globals lexicon, lexiconDate */
// String method `STR.fmt(OBJ)`. Replace all {...} expressions in STR with OBJ
// property of same name. Return the new string.
//
// 'Hello {str}!'.fmt({str: 'world'}) => 'Hello world!'
// 'Hello {0} & {1}!'.fmt(['Alice', 'Bob']) => 'Hello Alice & Bob!'
//
String.prototype.fmt = function (o) { // eslint-disable-line no-extend-native
'use strict'
return this.replace(/\{([^{}]*)\}/g, (a, b) => {
let r = o[b]
return typeof r === 'string' || typeof r === 'number' ? r : a
})
}
// jQuery plugin to insert a string at caret position in <textarea> &
// <input> and also scroll element content to caret after paste. (Does
// not work with 'contenteditable'.)
//
// Uses .selectionStart/.selectionEnd, i.e. works on:
// Chrome, Firefox, MSIE 9+, Edge, Safari 4+, Opera, Android etc.
jQuery.fn.paste = function (str) {
'use strict'
let prefocused = document.activeElement
str += '' // coerce into string
this.each((_, el) => {
let beg = el.selectionStart
let val = el.value
// Insert string & move caret.
el.value = `${val.slice(0, beg)}${str}${val.slice(el.selectionEnd)}`
el.selectionEnd = el.selectionStart = beg + str.length
// Scroll content to caret (non-DOM, non-chainable).
el.blur()
el.focus()
})
prefocused.focus()
return this
}
// Toggle fullscreen for one element or (with no arg) the whole window.
// [thewebflash.com/toggling-fullscreen-mode-using-the-html5-fullscreen-api]
function toggleFullscreen(elem) {
'use strict'
elem = elem || document.documentElement
if (
document.fullscreenElement
|| document.mozFullScreenElement
|| document.webkitFullscreenElement
|| document.msFullscreenElement
) {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.msExitFullscreen) {
document.msExitFullscreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
}
} else {
if (elem.requestFullscreen) {
elem.requestFullscreen()
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen()
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen()
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// URL fragment state module
// =========================
// Update URL fragment & trigger on URL fragment change.
//
// URL fragment syntax: {#|##}<query>
// ----------------------------------------------
// * A single '#' (default) indicates that matches should be displayed with
// videos, while '##' indicate a text only listing is desired.
// * <query> is a search query (described elsewhere).
//
// Functions
// ---------
// .set(STATE) -- Change URL to reflect state (without triggering hooks).
// .onQueryChange(FUNC)
// .onVideoToggle(FUNC)
//
// Register FUNC as callback, called when corresponding part of the URL change.
//
// State
// -----
// The state object reflects the URL but the separators are removed, and all
// strings have been decoded. The 'video' value is javascript boolean.
//
// {
// base : 'http://zrajm.github.io/teckentranskription/lexicon.html',
// query : 'buss,taxi',
// video : true,
// }
//
let urlFragment = (() => {
'use strict'
let state = { query: undefined, video: true }
function getStateFromUrl() {
let [_, base, video, query]
= window.location.href.match(/^([^#]*)(#{0,2})(.*)/u)
return {
base,
video: video !== '##',
query: decodeURIComponent(query),
}
}
function setUrlFromState(state) {
let str = encodeURIComponent(state.query)
if (str || state.video !== undefined) {
str = (state.video ? '#' : '##') + str
}
return state.base + str
}
function getHashFromStr(hashStr) {
return (state.video ? '#' : '##')
+ encodeURIComponent(hashStr || state.query)
}
// .set({ query: STR, video: BOOL })
// Update internal state + URL, without triggering hashchange event.
function setState(partial) {
let modified = false
;['query', 'video'].forEach(n => {
if (partial[n] !== state[n] && partial[n] !== undefined) {
state[n] = partial[n]
modified = true
}
})
if (modified) { // update URL
window.history.pushState({}, '', setUrlFromState(state))
return true
}
return false
}
let hooks = {}
function onQueryChange(callback) { hooks.query = callback }
function onVideoToggle(callback) { hooks.video = callback }
$(window).on('hashchange', () => {
let newState = getStateFromUrl()
let run = []
;['base', 'query', 'video'].forEach(n => {
if (newState[n] !== state[n]) { // save all state first
if (hooks[n] instanceof Function) {
run.push(n) // register callback to run
}
state[n] = newState[n] // save state
}
})
run.forEach(n => hooks[n](state[n])) // run callbacks
})
// Trigger hashchange on initial page load.
$(() => { $(window).trigger('hashchange') })
return {
set: setState,
getHash: getHashFromStr,
onQueryChange: onQueryChange,
onVideoToggle: onVideoToggle,
}
})()
////////////////////////////////////////////////////////////////////////////////
// Escape regex delimiter '/' or meta char.
function escape(x) {
'use strict'
return x.replace(/^[*+?^$.[\]{}()|/\\]$/u, '\\$&')
}
// Test if char is 0-9, a-ö, or non-separator transcription char.
const wordCharRe = /^[\d\p{Letter}\u{10c900}-\u{10c95f}\u{10c961}-\u{10c9ff}]$/u
// Unquoted special characters: Expand unquoted char into character class.
let charClass = {
'a': '[aàáâã]',
'c': '[cç]',
'e': '[eèéêë]',
'i': '[iìíîï]',
'n': '[nñ]',
'o': '[oòóôõ]',
'u': '[uùúûü]',
'y': '[yýü]',
'ä': '[äæ]',
'ö': '[öø]',
'': '[][]?', // face
'': '[][]?', // upper face
'': '[][]?', // lower face
'': '[][]?', // arm
'': '[][]?', // shoulders
'': '[][]?', // chest
'': '[][]?', // hips
'*': '[^ /.,:;?!()]*', // all non-space, non-'/' delimiter
// one place symbol (+ optional relation)
'@': '(?:@|[][]?)',
// like '*' but single char + optional for relation (5), attitude turn (6), medial
// contact (1), or motion modifier (11)
'?': '[^ /.,:;?!()][]?',
// one handshape symbol (+ optional relation)
'#': '(?:#|[][]?)',
'^': '[]', // one relation symbol
':': '[][]', // one attitude symbol
'': '(?:[]?|[])', // circle in frontal plane
'': '(?:[]?|[])', // circle in horisontal plane
'': '(?:[]?|[])', // circle in saggital plane
}
// Unquoted place/handshape symbols should also match a following
// (optional) relation symbol.
for (let c of '') {
charClass[c] = `${c}[]?`
}
// All unquoted hand-external motion symbols (circling/bouncing/curving/
// hitting/twisting/divering/converging) should also match a following
// (optional) motion direction symbol.
for (let c of '') { charClass[c] = `${c}[]?` }
{
// After motion symbol, match optional medial contact.
let x = { '':'', '':'', '':'', '':'', '':'', '':'' }
for (let c of '') { charClass[c] = `[${c}${x[c] ?? ''}]?` }
// Repeated motion = double arrow, or normal arrow + repeat symbol.
let y = { '': '', '': '', '': '' }
for (let c of '') {
charClass[c] = `(${c}?|[${y[c]}]?(?=|[^]*(?:$|)))`
}
}
// Attitude symbols: Pointing symbol match (optional) following turn symbol,
// and turn symbol match (optional) preceding point symbol.
for (let c of '') { charClass[c] = `${c}[]?` }
for (let c of '') { charClass[c] = `[]?${c}` }
function finalizeTerm(state) {
'use strict'
const {
plain = '',
regex = '',
field = false,
not = false,
tag = false,
wordBeg, wordEnd,
} = state
function wordRegex(regex) {
const noWord = '[ ,:!?/.’()[\\]&+–-]'
return (wordBeg ? `(^|${noWord})` : '()')
+ `(${regex})`
+ (wordEnd ? `(?=${noWord}|$)` : '')
}
return {
regex: regex
? RegExp(field ? `^${regex}$` : wordRegex(regex), 'ui')
: null,
plain, not, tag,
}
}
// Parse user's query string, return QUERY object with following root methods:
//
// * hilite(): Return regex for highlighting matches in output.
// * search(ENTRIES): Return list entries matching query. ENTRIES is list where
// each entry is a list of fields; each field is a string.
// (ie a list-of-lists, but depth does not vary.)
//
// QUERY also contains a nested list of search terms (regexes with 'g' and 'y'
// flags UNSET, and the added property 'not'). Multiple terms are wrapped in
// (possibly nested) lists (with added properties 'not', 'or' and 'own'). Added
// properties indicate: 'not' = mark that item must NOT be be found to match;
// 'or' = subitems in list are OR:ed, rather than AND:ed; 'own' = list
// correspond to a parethesis entered in query by user (user-entered/explicit
// paretheses are removed if redundant, and implicit lists are added to
// disambiguate order of AND/OR precedence).
//
// Query parsing is sloppy = all input is valid (eg incomplete parentheses and
// quotes). Operator precedence is: NOT -> AND -> OR (`a, -b c` = `a, (-b c)`).
function parseQuery(queryStr) {
'use strict'
let query = (() => {
function p(x) {
return x.split(' ').reduce((a, p) => { a[p] = true; return a }, [])
}
function and_() { return p('') }
function or_() { return p('or') }
function or() { return p('or own') }
function nor() { return p('or own not') }
let q = [[]] // query stack
add(or_())
add(and_())
function add(x) { // add new parenthesis
q[q.length - 1].push(x)
if (Array.isArray(x)) { q.push(x) }
}
function end() { // end parenthesis
let z = q.pop() // pop last paren on stack
let p = q[q.length - 1] // parent paren
// Cleanup: Remove empty parens, and parens around single terms.
switch (z.length) {
case 0:
p.pop()
break
case 1:
let [c] = z // eslint-disable-line no-case-declarations
c.own = !!(c.own || z.own) // OR
c.not = !!(c.not ^ z.not) // XOR
p[p.length - 1] = c
}
// Cleanup: Remove parens around children with same AND/OR as parent.
z.forEach((c, i) => {
// c.length is always >1 here (cleaned up above)
//
// Process children (c) of current parenthesis (z) here, since AND/OR
// status of child's parent must be known. (The current parenthesis
// will not have AND/OR set for first item, since comma/space which
// specifies this comes AFTER first item in the query language.)
if (Array.isArray(c) && (z.or === c.or && !c.not)) {
z.splice(i, 1, ...c)
}
})
}
function wrap() { // wrap query in extra paren
if (Array.isArray(q[0])) {
q[0].own = true
q.unshift([q[0]])
q.unshift([q[0]])
q[0].or = true
q.unshift([q[0]])
}
}
function done() {
while (q.length > 1) { end() } // trim all remaining parens
// FIXME: Why is this necessary? Can this be handled by end()?
if (q[0].length === 1 && Array.isArray(q[0][0])) {
let [z] = q
let [c] = z
c.own = !!(c.own || z.own) // OR
c.not = !!(c.not ^ z.not) // XOR
q[0] = c
}
}
function addParen(not) {
add(not ? nor() : or())
add(and_())
}
function endParen() {
while (q.length > 1 && !q[q.length - 1].own) { end() } // trim all parens
if (q.length === 1) { wrap() }
end()
}
function orParen() {
end()
add(and_())
}
function addTerm(cb) {
const { regex, plain, not, tag } = cb()
if (regex) { add(Object.assign(regex, { not, plain, tag })) }
return this
}
// Test if single entry 'e' (list of strings) match query (<q>). Return
// true on match, false otherwise.
function searchEntry(q, e) {
let x = Array.isArray(q)
? q[q.or ? 'some' : 'every'](q => searchEntry(q, e)) // subquery
: e.some(f => ((f[0] === '/') === q.tag) && q.test(f)) // base case
return q.not ? !x : x
}
function flat(q) {
return q.reduce(
(a, q) => a.concat(Array.isArray(q) ? flat(q) : q.source), [])
}
function get() {
done()
let x = q[q.length - 1]
return Object.assign(x, {
// NOTE: Keep negated terms in hilite() (since '-(-a,-b)' match 'a b').
hilite: () => RegExp(flat(x).join('|'), 'gui'),
search: e => x.length ? e.filter(e => searchEntry(x, e)) : [],
})
}
return { addTerm, addParen, endParen, orParen, get }
})()
let fsa = {
'(': s => { query.addTerm(() => finalizeTerm(s)).addParen(s.not) },
')': s => { query.addTerm(() => finalizeTerm(s)).endParen() },
',': s => { query.addTerm(() => finalizeTerm(s)).orParen() },
' ': s => { query.addTerm(() => finalizeTerm(s)) },
'"': (s, c) => { s.quote = c; return s },
"'": (s, c) => { s.quote = c; return s },
'QUOTED': (s, c) => {
if (c === s.quote) {
delete s.quote
} else {
s.regex = (s.regex || '') + escape(c)
s.plain = (s.plain || '') + c
s.wordEnd = wordCharRe.test(c)
s.wordBeg ??= s.wordEnd
}
return s
},
'UNQUOTED': (s, c) => {
if (!s.regex) { // before word
if (c === '-') { // '-' negated
s.not = !s.not
return s
} else if (c === '=') { // '=' match whole field
s.field = true
return s
} else if (c === '/') { // '/' match tag field
s.tag = true // (also register as char)
s.regex = (s.regex || '') + '/?'
return s
}
}
s.regex = (s.regex || '') + (charClass[c] || escape(c))
s.plain = (s.plain || '') + c
s.wordEnd = !!charClass[c] || wordCharRe.test(c)
s.wordBeg ??= s.wordEnd
return s
},
}
let state = {} // contains: 'plain', 'regex', 'field', 'not', 'tag', 'quote'
for (let c of queryStr.normalize()) { // process char-by-char in FSA
state = fsa[state.quote ? 'QUOTED' : fsa[c] ? c : 'UNQUOTED'](state, c) || {}
}
return query.addTerm(() => finalizeTerm(state)).get()
}
function hilite(str, regex, func) {
'use strict'
return str.replace(regex, (match, ...parts) => {
parts = parts.slice(0, -2).filter(p => p !== undefined)
if (func) {
func()
}
// Lookbehind (?<=...) isn't supported in Safari (and was only added to
// Edge and Firefox in summer 2020), therefore we use regex subgroups
// instead.
return '{0}<mark>{1}</mark>'.fmt(
(parts.length === 2) ? parts : ['', match]
)
})
}
function htmlifyTags(tags, hiliteRegex) {
'use strict'
let count = { tag: 0, warn: 0 }
let match = { tag: false, warn: false }
if (tags.length === 1) {
return ''
}
// Generate tag list and count matches, and number of tags/warnings.
let html = tags.map(tag => {
// Determine tag type (warning = add warning icon).
let tagType = tag.match(/\/ovanligt/) ? 'warn' : 'tag'
count[tagType] += 1
return hilite(tag, hiliteRegex, () => {
match[tagType] = true
}).replace(/(^|[^<])\//g, '$1<span class=sep>/</span>')
+ (tagType === 'warn' ? ' <span class=sep>▲</span>' : '')
})
return '<span class=tags title="{tags}{help}">{icons}</span>'.fmt({
tags: html.join('<br>'),
help: ' <span class=sep>(antal taggar)</span>',
icons: ['warn', 'tag'].map(tagType => {
return count[tagType] === 0
? ''
: '<img src="pic/{type}{match}.svg" alt="{alt}">'.fmt({
type: tagType,
match: match[tagType] ? '-marked' : '',
alt: match[tagType] ? 'Ovanligt' : 'Taggar',
})
}).join(''),
})
}
// Turn a (hilited) transcription string into HTML.
function htmlifyTranscription(hilitedTransStr) {
'use strict'
return hilitedTransStr
// Insert <wbr> tag after all segment separators.
.replace(//gu, '<wbr>')
}
// Downcase string, remove all non-alphanumenic characters and space (by
// replacing Swedish chars with aao, and space with '-') and collapse all
// repetitions of '-'.
function unicodeTo7bit(str) {
'use strict'
return str.toLowerCase().replace(/[^a-z0-9-]/gu, m => ({
' ': '-', 'é': 'e', 'ü': 'u', 'å': 'a', 'ä': 'a', 'ö': 'o', '–': '-',
}[m] || '')).replace(/-{2,}/, '-')
}
function htmlifyMatch(entry, hiliteRegex) {
'use strict'
let [id, transcr, ...rest] = entry
let swe = rest.filter(x => x[0] !== '/') // Swedish
let tags = rest.filter(x => x[0] === '/') // /tags
return (
/* NB: Whitespace below shows up in search result's 'text' mode. */
'<div class=match>'
+ '<div class="video-container is-loading">'
+ '<img src="{baseUrl}/photos/{dir}/{file}-{id}-photo-1-medium.jpg"'
+ ' data-video="{baseUrl}/movies/{dir}/{file}-{id}-tecken.mp4" alt="">'
+ '<div class=video-feedback></div>'
+ '<div class=top-right style="text-align:right">'
+ '<a class=video-id href="{baseUrl}/ord/{id}"'
+ ' title="Öppna i Svenskt teckenspråkslexikon (i ny tabb)"'
+ ' target=_blank rel="noopener">{htmlId}</a>\n'
+ '{htmlTags}\n'
+ '</div>'
+ '<div class=video-subs>'
+ '<a data-href="="{transcr}"" title="{htmlTranscr}">'
+ '{htmlTranscr}</a>'
+ '</div>'
+ '</div> '
+ '<span title="{htmlSwedish}">{htmlSwedish}</span>'
+ '</div>\n'
).fmt({
htmlTags: htmlifyTags(tags, hiliteRegex),
id: id,
htmlId: hilite(id, hiliteRegex),
baseUrl: 'https://teckensprakslexikon.su.se',
dir: id.substring(0, 2),
file: unicodeTo7bit(swe[0]),
transcr: transcr,
htmlTranscr: htmlifyTranscription(hilite(transcr, hiliteRegex)),
htmlSwedish: swe.map(txt => hilite(txt, hiliteRegex)).join(', '),
})
}
// A function that interatively displays the result of a search. `chunksize`
// items are displayed at a time, thereafter an additional `chunksize` items
// are shown if user scrolls to the end of the page or press the 'Show more…'
// button.
//
// If the function is invoked again with a new search result, then any still
// ongoing processing is aborted and only the new result is displayed.
let outputMatching = (() => {
'use strict'
let chunksize = 100 // setting (never changes)
let hasListener = false
let $status
let $result
let $button
let htmlQueue
let startSize
let count
function scrolledToBottom() {
let pageOffset = window.pageYOffset || window.scrollY
let pageHeight = document.body.offsetHeight
let winHeight = window.innerHeight
return (pageOffset + winHeight) >= (pageHeight - 2)
}
function outputNext(args) {
if (args) {
$status = args.status
$result = args.result
$button = args.button
htmlQueue = args.html
startSize = htmlQueue.length
count = 0
}
// Output one chunk of search result.
let chunk = htmlQueue.splice(0, chunksize)
count += chunk.length
if (count === startSize) {
$status.html(`${count} träffar (visar alla)`)
} else {
$status.html(
`${startSize} träffar (visar ${count}) – <a>Visa ${chunksize} till</a>`)
$('>a', $status).click(() => { outputNext() })
}
$result.append(chunk.join(''))
$result.imagesLoaded().progress(onImageLoad)
if (htmlQueue.length === 0) { // nothing more to display
$button.hide()
$(window).off('scroll')
} else { // moar to display
if (!hasListener) {
$button.click(() => { outputNext() })
hasListener = true
}
if (args) {
$button.show()
$(window).on('scroll', () => {
if (scrolledToBottom()) {
outputNext()
}
})
}
}
}
return outputNext
})()
function onImageLoad(imgLoad, image) {
'use strict'
// change class if image is loaded or broken
let $parent = $(image.img).parent()
$parent.removeClass('is-loading')
if (!image.isLoaded) {
$parent.addClass('is-broken')
}
}
function searchLexicon(queryStr) {
'use strict'
let $body = $(document.body)
$('#q').val(queryStr)
// No query, add 'noquery' to body element.
$body[queryStr.trim() ? 'removeClass' : 'addClass']('noquery')
setTimeout(() => {
let query = parseQuery(queryStr)
let matches = query.search(lexicon)
// Query without matches, add 'nomatch' to <body>.
$body[(queryStr && !matches.length) ? 'addClass' : 'removeClass']('nomatch')
// Output search result.
outputMatching({
status: $('#search-count'),
result: $('#search-result').empty(),
button: $('#more'),
html: matches.map(entry => htmlifyMatch(entry, query.hilite())),
})
}, 0)
}
function onPlayPauseToggle(event) {
'use strict'
if ($(event.target).closest('a').length) { // link clicked: don't play
return
}
let $container = $(event.currentTarget)
let $video = $('>video,>img[data-video]', $container)
if ($video.is('img')) { // replace <img> with <video>
$video = $(
'<video loop muted playsinline src="{0}" poster="{1}"></video>'
.fmt([$video.data('video'), $video.attr('src')])
).replaceAll($video).on('canplay error', e => {
$video.off('canplay error')
$container.removeClass('is-loading-video is-broken')
if (e.type === 'error') {
$container.addClass('is-broken')
}
})
$container.addClass('is-loading-video')
}
// Get state of video and toggle play/pause state.
// (Everything that remains after this is visual feedback.)
let action = $video.prop('paused') ? 'play' : 'pause'
$video.trigger(action)
// Add icon to feedback overlay & animate it.
let $feedback = $('>.video-feedback', $container)
.removeClass('anim pause play')
.addClass(action) // display play/pause icon
setTimeout(() => { // animate icon
$feedback
.addClass('anim')
.one('transitionend', () => {
$feedback.removeClass('anim pause play')
})
}, 10)
}
$('#search-result')
.on('click', '.video-container', onPlayPauseToggle)
.on('dblclick', '.video-container', event => {
'use strict'
toggleFullscreen($('>video', event.currentTarget)[0])
})
// On window resize: Rescale #search-result element in steps.
{
const $win = $(window)
const $div = $('#search-result')
const gapWidth = parseInt($div.css('word-spacing'), 10)
const colWidth = 270 // same as .video-container in CSS
let oldWidth
let oldCols
$win.on('load resize', () => {
'use strict'
const width = $win.width()
if (width === oldWidth) { // window width unchanged
return
}
const cols = Math.floor((width + gapWidth) / (colWidth + gapWidth)) || 1
if (cols === oldCols) { // number of columns unchanged
return
}
$div.css('width', (gapWidth * (cols - 1)) + (colWidth * cols))
oldWidth = width
oldCols = cols
})
}
// Tooltips: These imitate Chromium tooltip behaviour, but allow us to use any
// font -- so that sign language transcriptions can be displayed.
(() => {
'use strict'
let $tooltip = $('<div class=tooltip></div>')
.css({
display: 'none',
color: '#fff',
background: '#555',
borderRadius: 2,
boxShadow: '0 2px 6px #0004',
fontSize: 16,
lineHeight: '1.6',
padding: '.5em',
position: 'fixed',
zIndex: 2147483647, /* max allowed */
})
.appendTo(document.body)
// Trigger event if pointer is non-moving for half a second.
let timeout
$(document.body).on('mouseover', '[title],[data-title]', event => {
let $e = $(event.currentTarget)
let value = $e.attr('title')
// Change attribute 'title' => 'data-title' to suppress browser tooltip.
if (value !== undefined) {
$e.attr('data-title', value).removeAttr('title')
}
clearTimeout(timeout)
timeout = setTimeout(() => {
if ($e.is(':visible') && $e.is(':hover')) {
onMouseStill(event)
}
}, 500)
})
let shown = false
function onMouseStill(event) {
// Display topleft to get height + width.
shown = true
$tooltip
.css({ left: 0, top: 0 })
.html($(event.currentTarget).data('title'))
.show()
// Now use height and width of displayed tooltip, to move it to the
// right place (making sure it doesn't stick out of right/bottom corner
// of window).
let xMax = $(window).width() - $tooltip.outerWidth()
let yMax = $(window).height() - $tooltip.outerHeight()
let x = event.clientX + 10 // mouse coord
let y = event.clientY + 10
$tooltip.css({
left: x < xMax ? x : (xMax < 0 ? 0 : xMax),
top: y < yMax ? y : (yMax < 0 ? 0 : yMax),
})
}
function hideTooltip() {
if (shown) {
shown = false
$tooltip.hide()
}
}
$(window).on('hashchange resize', hideTooltip)
$(document).on('scroll mousemove mousedown keydown', hideTooltip)
})()
////////////////////////////////////////////////////////////////////////////////
//
// Main program
//
// URL update events, these are triggered when user follows a page internal
// link, when user manually edits URL, and on initial page load. These hooks
// update internal state to reflect URL change (without causing any additional
// changes to URL).
urlFragment.onQueryChange(searchLexicon)
urlFragment.onVideoToggle(showVideos)
// Form submission.
$(() => {
'use strict'
let $form = $('#search')
.on('submit', onSubmit)
let $q = $('#q')
.on('focus blur', onFocus)
.on('keydown', onKey)
.on('paste', onPaste)
onFocus()
// Text input '#q' focused = set 'focused' class on wrapper element.
function onFocus() {
$form.toggleClass('focus', $q.is(':focus'))
}
// Return key in textarea = submit form.
function onKey(e) {
if (e.which === 13) { // Enter
e.preventDefault() // don't insert key
if (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) {
return
}
$form.submit()
}
}
// Form submission.
function onSubmit(e) {
let queryStr = ($q.val() || '')
e.preventDefault() // don't submit to server
// On touchscreen devices (where no input type has hover).
if (window.matchMedia('not all and (any-hover:hover)').matches) {
$q.blur() // hide soft keyboard
}
urlFragment.set({ query: queryStr })
searchLexicon(queryStr)
}
// Paste in textarea = filter out newlines (use jQuery .paste plugin).
function onPaste(e) {
if ($q[0].selectionEnd === undefined) { // if unsupported: do nothing
return
}
e.preventDefault()
$q.paste(
(
window.clipboardData !== undefined
? window.clipboardData // MSIE, Safari, Chrome
: e.originalEvent.clipboardData // WebKit
).getData('text').replace(/[\n\r\u0020]+/g, ' ')
).trigger('input')
}
});
// Update lexicon date in page footer.
(() => {
'use strict'
function updateLexiconDate() {
if (lexiconDate === undefined) {
setTimeout(updateLexiconDate, 250)
} else {
$('#lexicon-date').html(lexiconDate.toLocaleString(
'sv', { year: 'numeric', month: 'long', day: 'numeric' }
))
$('#lexicon-size').html(
`${Object.keys(lexicon).length}`.replace(/(?=(\d{3})+$)/g, ' ')
)
}
}
$(updateLexiconDate)
})()
function showVideos(bool) {
'use strict'
$('#search-wrapper')
.removeClass('video-view text-view')
.addClass(bool ? 'video-view' : 'text-view')
}
$('#search-wrapper .selector').on('click keypress', e => {
'use strict'
if (e.type === 'keypress') {
if (e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) { return }
if (e.which !== 13 && e.which !== 32) { return }
}
let hasVideo = $('#search-wrapper')
.toggleClass('video-view text-view')
.hasClass('video-view')
urlFragment.set({ video: hasVideo })
})
// Update 'href' attr when mouse enters <a data-href=…> tag (used to retain
// current video/text result setting).
$(() => {
'use strict'
// 'mouseenter' used here since it does not trigger when child elements are
// entered, and the event does not bubble.
$('#search-result').on('mouseenter', 'a[data-href]', e => {
let $e = $(e.currentTarget)
let hashref = $e.data('href') || ''
if (hashref) {
$e.attr('href', urlFragment.getHash(hashref))
}
})
})
//[eof]