1
- import { search , default as init } from '. /tinysearch_engine.js' ;
1
+ import { search , default as init } from " /tinysearch_engine.js" ;
2
2
async function lazyLoad ( ) {
3
- await init ( ' /tinysearch_engine_bg.wasm' ) ;
3
+ await init ( " /tinysearch_engine_bg.wasm" ) ;
4
4
}
5
5
6
6
var loaded = false ;
7
7
8
8
function autocomplete ( inp ) {
9
- var currentFocus ;
9
+ var currentFocus ;
10
10
11
- inp . addEventListener ( "click" , function ( e ) {
12
- // There's probably a better way to do lazy loading.
13
- // Then again, I'm not a JavaScript developer ¯\_(ツ)_/¯
14
- if ( ! loaded ) {
15
- lazyLoad ( ) ;
16
- loaded = true ;
17
- }
18
- } ) ;
11
+ inp . addEventListener ( "click" , function ( e ) {
12
+ // There's probably a better way to do lazy loading.
13
+ // Then again, I'm not a JavaScript developer ¯\_(ツ)_/¯
14
+ if ( ! loaded ) {
15
+ lazyLoad ( ) ;
16
+ loaded = true ;
17
+ }
18
+ } ) ;
19
19
20
- inp . addEventListener ( "input" , function ( e ) {
21
- var a , b , i , val = this . value ;
20
+ inp . addEventListener ( "input" , function ( e ) {
21
+ var a ,
22
+ b ,
23
+ i ,
24
+ val = this . value ;
22
25
23
- /*close any already open lists of autocompleted values*/
24
- closeAllLists ( ) ;
25
- if ( ! val ) {
26
- return false ;
27
- }
28
- currentFocus = - 1 ;
26
+ /*close any already open lists of autocompleted values*/
27
+ closeAllLists ( ) ;
28
+ if ( ! val ) {
29
+ return false ;
30
+ }
31
+ currentFocus = - 1 ;
29
32
30
- /* Create a DIV element that will contain the items (values):*/
31
- a = document . createElement ( "DIV" ) ;
32
- a . setAttribute ( "id" , this . id + "autocomplete-list" ) ;
33
- a . setAttribute ( "class" , "autocomplete-items" ) ;
33
+ /* Create a DIV element that will contain the items (values):*/
34
+ a = document . createElement ( "DIV" ) ;
35
+ a . setAttribute ( "id" , this . id + "autocomplete-list" ) ;
36
+ a . setAttribute ( "class" , "autocomplete-items" ) ;
34
37
35
- /* Append the DIV element as a child of the autocomplete container:*/
36
- this . parentNode . appendChild ( a ) ;
38
+ /* Append the DIV element as a child of the autocomplete container:*/
39
+ this . parentNode . appendChild ( a ) ;
37
40
38
- let arr = search ( val , 3 ) ;
41
+ let arr = search ( val , 3 ) ;
39
42
40
- for ( i = 0 ; i < arr . length ; i ++ ) {
41
- let elem = arr [ i ] ;
43
+ for ( i = 0 ; i < arr . length ; i ++ ) {
44
+ let elem = arr [ i ] ;
42
45
43
- b = document . createElement ( "DIV" ) ;
44
- b . innerHTML = elem [ 0 ] ;
46
+ b = document . createElement ( "DIV" ) ;
47
+ b . innerHTML = elem [ 0 ] ;
45
48
46
- b . addEventListener ( "click" , function ( e ) {
47
- window . location . href = `${ elem [ 1 ] } ?q=${ encodeURIComponent ( val ) } ` ;
48
- } ) ;
49
- a . appendChild ( b ) ;
50
- }
51
- } ) ;
49
+ b . addEventListener ( "click" , function ( e ) {
50
+ window . location . href = `${ elem [ 1 ] } ?q=${ encodeURIComponent ( val ) } ` ;
51
+ } ) ;
52
+ a . appendChild ( b ) ;
53
+ }
54
+ } ) ;
52
55
53
- inp . addEventListener ( "keydown" , function ( e ) {
54
- var x = document . getElementById ( this . id + "autocomplete-list" ) ;
55
- if ( x ) x = x . getElementsByTagName ( "div" ) ;
56
- if ( e . keyCode == 40 ) {
57
- /* If the arrow DOWN key is pressed,
56
+ inp . addEventListener ( "keydown" , function ( e ) {
57
+ var x = document . getElementById ( this . id + "autocomplete-list" ) ;
58
+ if ( x ) x = x . getElementsByTagName ( "div" ) ;
59
+ if ( e . keyCode == 40 ) {
60
+ /* If the arrow DOWN key is pressed,
58
61
increase the currentFocus variable:*/
59
- currentFocus ++ ;
60
- /* and make the current item more visible:*/
61
- addActive ( x ) ;
62
- } else if ( e . keyCode == 38 ) { //up
63
- /* If the arrow UP key is pressed,
62
+ currentFocus ++ ;
63
+ /* and make the current item more visible:*/
64
+ addActive ( x ) ;
65
+ } else if ( e . keyCode == 38 ) {
66
+ //up
67
+ /* If the arrow UP key is pressed,
64
68
decrease the currentFocus variable:*/
65
- currentFocus -- ;
66
- /* and and make the current item more visible:*/
67
- addActive ( x ) ;
68
- } else if ( e . keyCode == 13 ) {
69
- /* If the ENTER key is pressed, prevent the form from being submitted,*/
70
- e . preventDefault ( ) ;
71
- if ( currentFocus > - 1 ) {
72
- /* and simulate a click on the "active" item:*/
73
- if ( x ) x [ currentFocus ] . click ( ) ;
74
- }
75
- }
76
- } ) ;
77
-
78
- function addActive ( x ) {
79
- /* A function to classify an item as "active":*/
80
- if ( ! x ) return false ;
81
- /* Start by removing the "active" class on all items:*/
82
- removeActive ( x ) ;
83
- if ( currentFocus >= x . length ) currentFocus = 0 ;
84
- if ( currentFocus < 0 ) currentFocus = ( x . length - 1 ) ;
85
- /* Add class "autocomplete-active":*/
86
- x [ currentFocus ] . classList . add ( "autocomplete-active" ) ;
69
+ currentFocus -- ;
70
+ /* and and make the current item more visible:*/
71
+ addActive ( x ) ;
72
+ } else if ( e . keyCode == 13 ) {
73
+ /* If the ENTER key is pressed, prevent the form from being submitted,*/
74
+ e . preventDefault ( ) ;
75
+ if ( currentFocus > - 1 ) {
76
+ /* and simulate a click on the "active" item:*/
77
+ if ( x ) x [ currentFocus ] . click ( ) ;
78
+ }
87
79
}
80
+ } ) ;
81
+
82
+ function addActive ( x ) {
83
+ /* A function to classify an item as "active":*/
84
+ if ( ! x ) return false ;
85
+ /* Start by removing the "active" class on all items:*/
86
+ removeActive ( x ) ;
87
+ if ( currentFocus >= x . length ) currentFocus = 0 ;
88
+ if ( currentFocus < 0 ) currentFocus = x . length - 1 ;
89
+ /* Add class "autocomplete-active":*/
90
+ x [ currentFocus ] . classList . add ( "autocomplete-active" ) ;
91
+ }
88
92
89
- function removeActive ( x ) {
90
- /* A function to remove the "active" class from all autocomplete items:*/
91
- for ( var i = 0 ; i < x . length ; i ++ ) {
92
- x [ i ] . classList . remove ( "autocomplete-active" ) ;
93
- }
93
+ function removeActive ( x ) {
94
+ /* A function to remove the "active" class from all autocomplete items:*/
95
+ for ( var i = 0 ; i < x . length ; i ++ ) {
96
+ x [ i ] . classList . remove ( "autocomplete-active" ) ;
94
97
}
98
+ }
95
99
96
- function closeAllLists ( elmnt ) {
97
- /* Close all autocomplete lists in the document,
100
+ function closeAllLists ( elmnt ) {
101
+ /* Close all autocomplete lists in the document,
98
102
except the one passed as an argument:*/
99
- var x = document . getElementsByClassName ( "autocomplete-items" ) ;
100
- for ( var i = 0 ; i < x . length ; i ++ ) {
101
- if ( elmnt != x [ i ] && elmnt != inp ) {
102
- x [ i ] . parentNode . removeChild ( x [ i ] ) ;
103
- }
104
- }
103
+ var x = document . getElementsByClassName ( "autocomplete-items" ) ;
104
+ for ( var i = 0 ; i < x . length ; i ++ ) {
105
+ if ( elmnt != x [ i ] && elmnt != inp ) {
106
+ x [ i ] . parentNode . removeChild ( x [ i ] ) ;
107
+ }
105
108
}
106
- document . addEventListener ( "click" , function ( e ) {
107
- closeAllLists ( e . target ) ;
108
- } ) ;
109
+ }
110
+ document . addEventListener ( "click" , function ( e ) {
111
+ closeAllLists ( e . target ) ;
112
+ } ) ;
109
113
}
110
- autocomplete ( document . getElementById ( "tinysearch" ) ) ;
114
+ autocomplete ( document . getElementById ( "tinysearch" ) ) ;
0 commit comments