2021-09-23 03:53:57 +00:00
// good luck reading through this
2021-06-03 01:57:01 +00:00
( function ( ) {
var client = new XMLHttpRequest ( ) ;
2021-08-08 01:43:23 +00:00
client . open ( "GET" , "/pages.json" ) ;
2021-09-14 03:55:02 +00:00
client . onreadystatechange = ( ) => {
if ( client . readyState === 4 )
fuzzyInit ( client . responseText ) ;
2021-06-03 01:57:01 +00:00
}
client . send ( ) ;
2021-09-23 03:53:57 +00:00
document . querySelector ( "#search" ) . focus ( ) ;
2021-06-03 01:57:01 +00:00
} ) ( ) ;
2021-09-14 03:55:02 +00:00
function fuzzyInit ( pagesFileName ) {
if ( pagesFileName == "" )
2021-06-03 01:57:01 +00:00
return ;
var pages ;
try {
2021-09-14 03:55:02 +00:00
pages = JSON . parse ( pagesFileName ) ;
2021-06-03 01:57:01 +00:00
} catch ( e ) {
2021-09-14 03:55:02 +00:00
console . error ( e ) ;
document . body . innerHTML = e . message ;
2021-06-03 01:57:01 +00:00
return ;
}
pages . sort ( ) ;
document . querySelector ( "#search" )
. addEventListener ( "keyup" , ( e ) => {
2021-09-14 03:55:02 +00:00
switch ( e . code ) {
case "Enter" :
if ( document . querySelector ( "#results" ) . childNodes [ 0 ] . href === undefined )
return ;
window . location = document . querySelector ( "#results" ) . childNodes [ 0 ] . href ;
break ;
case "ArrowDown" :
console . log ( "S" ) ;
break ;
case "ArrowUp" :
console . log ( "W" ) ;
break ;
}
2021-09-23 03:53:57 +00:00
// help
if ( document . querySelector ( "#search" ) . value === "?" || document . querySelector ( "#search" ) . value == "help" ) {
document . querySelector ( "#results" ) . innerHTML = "Enter a page or directory name. If do not know any, clear the search field to list everything. Using the <code>Enter</code> key would take you to the first page in list, if it is not empty."
2021-09-14 03:55:02 +00:00
return ;
}
2021-06-03 01:57:01 +00:00
2021-09-23 03:53:57 +00:00
// if (document.querySelector("#search").value === ""){
// document.querySelector("#results").innerHTML = "Try entering something into the input field...";
// return;
// }
2021-06-03 01:57:01 +00:00
let results = [ ] ;
for ( const [ i , [ title , page ] ] of pages . entries ( ) ) {
2021-09-14 03:55:02 +00:00
ret = fuzzySearch ( title , document . querySelector ( "#search" ) . value ) ;
if ( ret === null )
2021-06-03 01:57:01 +00:00
continue ;
results . push ( [ ret , page ] ) ;
}
results . sort ( ( x , y ) => { return x [ 0 ] . second - y [ 0 ] . second } ) ;
let output = "" ;
for ( const [ hfRet , rlink ] of results ) {
output += ` <a class="hyperlink" href=" ${ rlink } "><div class="name"> ${ hfRet . first } </div><div class="link"> ${ rlink } </div></a> ` ;
}
if ( output == "" )
output = "No matches found."
document . querySelector ( "#results" ) . innerHTML = output ;
}
) ;
}
2021-09-14 03:55:02 +00:00
function fuzzySearch ( list , input ) {
2021-06-03 01:57:01 +00:00
let search = input . replace ( /\s/g , "" ) ;
search = search . toLowerCase ( ) ;
let tokens = list . split ( '' ) ;
let pc = 0 ;
let score = 0 ;
for ( const [ i , ch ] of tokens . entries ( ) ) {
if ( ch . toLowerCase ( ) == search [ pc ] ) {
score += i - pc ;
tokens [ i ] = ` <span class="highlight"> ${ ch } </span> ` ;
pc ++ ;
if ( search . length < pc )
2021-09-14 03:55:02 +00:00
return null ;
2021-06-03 01:57:01 +00:00
}
}
if ( search . length != pc )
2021-09-14 03:55:02 +00:00
return null ;
2021-06-03 01:57:01 +00:00
return { first : tokens . join ( '' ) , second : ( score / search . length ) } ;
}