@@ -259,6 +259,17 @@ window.search = window.search || {};
259
259
doc_urls = config . doc_urls ;
260
260
searchindex = elasticlunr . Index . load ( config . index ) ;
261
261
262
+ searchbar . removeAttribute ( "disabled" ) ;
263
+ searchbar . focus ( ) ;
264
+
265
+ const searchterm = searchbar . value . trim ( ) ;
266
+ if ( searchterm !== "" ) {
267
+ searchbar . classList . add ( "active" ) ;
268
+ doSearch ( searchterm ) ;
269
+ }
270
+ }
271
+
272
+ function initSearchInteractions ( ) {
262
273
// Set up events
263
274
searchicon . addEventListener ( 'click' , function ( e ) { searchIconClickHandler ( ) ; } , false ) ;
264
275
searchbar . addEventListener ( 'keyup' , function ( e ) { searchbarKeyUpHandler ( ) ; } , false ) ;
@@ -271,7 +282,9 @@ window.search = window.search || {};
271
282
// If reloaded, do the search or mark again, depending on the current url parameters
272
283
doSearchOrMarkFromUrl ( ) ;
273
284
}
274
-
285
+
286
+ initSearchInteractions ( ) ;
287
+
275
288
function unfocusSearchbar ( ) {
276
289
// hacky, but just focusing a div only works once
277
290
var tmp = document . createElement ( 'input' ) ;
@@ -280,7 +293,7 @@ window.search = window.search || {};
280
293
tmp . focus ( ) ;
281
294
tmp . remove ( ) ;
282
295
}
283
-
296
+
284
297
// On reload or browser history backwards/forwards events, parse the url and do search or mark
285
298
function doSearchOrMarkFromUrl ( ) {
286
299
// Check current URL for search request
@@ -313,7 +326,7 @@ window.search = window.search || {};
313
326
}
314
327
}
315
328
}
316
-
329
+
317
330
// Eventhandler for keyevents on `document`
318
331
function globalKeyHandler ( e ) {
319
332
if ( e . altKey || e . ctrlKey || e . metaKey || e . shiftKey || e . target . type === 'textarea' || e . target . type === 'text' || ! hasFocus ( ) && / ^ (?: i n p u t | s e l e c t | t e x t a r e a ) $ / i. test ( e . target . nodeName ) ) { return ; }
@@ -364,8 +377,23 @@ window.search = window.search || {};
364
377
}
365
378
}
366
379
380
+ function loadScript ( url , id ) {
381
+ if ( document . getElementById ( id ) ) {
382
+ return ;
383
+ }
384
+ const script = document . createElement ( 'script' ) ;
385
+ script . src = url ;
386
+ script . id = id ;
387
+ script . onload = ( ) => init ( window . search ) ;
388
+ script . onerror = error => {
389
+ console . error ( `Failed to load \`${ url } \`: ${ error } ` ) ;
390
+ } ;
391
+ document . head . append ( script ) ;
392
+ }
393
+
367
394
function showSearch ( yes ) {
368
395
if ( yes ) {
396
+ loadScript ( path_to_root + '{{ resource "searchindex.js" }}' , 'search-index' ) ;
369
397
search_wrap . classList . remove ( 'hidden' ) ;
370
398
searchicon . setAttribute ( 'aria-expanded' , 'true' ) ;
371
399
} else {
@@ -443,10 +471,10 @@ window.search = window.search || {};
443
471
function doSearch ( searchterm ) {
444
472
// Don't search the same twice
445
473
if ( current_searchterm == searchterm ) { return ; }
446
- else { current_searchterm = searchterm ; }
447
-
448
474
if ( searchindex == null ) { return ; }
449
475
476
+ current_searchterm = searchterm ;
477
+
450
478
// Do the actual search
451
479
var results = searchindex . search ( searchterm , search_options ) ;
452
480
var resultcount = Math . min ( results . length , results_options . limit_results ) ;
@@ -467,19 +495,6 @@ window.search = window.search || {};
467
495
showResults ( true ) ;
468
496
}
469
497
470
- function loadScript ( url , id ) {
471
- const script = document . createElement ( 'script' ) ;
472
- script . src = url ;
473
- script . id = id ;
474
- script . onload = ( ) => init ( window . search ) ;
475
- script . onerror = error => {
476
- console . error ( `Failed to load \`${ url } \`: ${ error } ` ) ;
477
- } ;
478
- document . head . append ( script ) ;
479
- }
480
-
481
- loadScript ( path_to_root + '{{ resource "searchindex.js" }}' , 'search-index' ) ;
482
-
483
498
// Exported functions
484
499
search . hasFocus = hasFocus ;
485
500
} ) ( window . search ) ;
0 commit comments