2023-06-27 20:54:27 +02:00
document . getElementById ( 'SearchBoxInput' ) . addEventListener ( 'keyup' , handleSearchChange ) ;
document . getElementById ( 'searchForm' ) . addEventListener ( 'submit' , handleSearchSubmit ) ;
document . addEventListener ( 'keyup' , handleHotKey )
const autocompleteBox = document . getElementById ( 'autocompletBody' ) ;
autocompleteBox . style . display = 'none' ;
currentBestGuessCommand = '' ;
2023-05-02 18:23:47 +02:00
function handleSearchChange ( e ) {
console . log ( e . target . value ) ;
2023-06-06 23:49:16 +02:00
// document.getElementById("SearchBox").setAttribute("data-bs-content", "Search results will show up here soon")
2023-06-27 20:54:27 +02:00
// return; // No you won't. I'm not done yet.
2023-05-02 18:23:47 +02:00
// Check if known prefix is used (either > or #)
2023-06-27 20:54:27 +02:00
if ( e . target . value != '' ) {
autocompleteBox . style . display = 'block' ;
autocompleteBox . innerHTML = 'Search results will show up here soon <br> Trust me <br> Results' ;
2023-05-04 20:21:10 +02:00
} else {
2023-06-27 20:54:27 +02:00
autocompleteBox . style . display = 'none' ;
2023-05-04 20:21:10 +02:00
}
2023-06-27 20:54:27 +02:00
if ( e . target . value [ 0 ] == '>' ) {
// List of valid routes
urlList = {
items : { url : '/items?page=1' , alias : [ 'item' ] } ,
'storage locations' : { url : '/manage/storages' , alias : [ 'locations' , 'storage' ] } ,
'storage units' : { url : '/manage/storages#storage-unit-tab' , alias : [ 'units' ] } ,
categories : { url : '/manage/categories' , alias : [ 'category' ] }
} ;
autocompleteBox . innerHTML = 'Start typing to search for commands <br> >goto items' ;
const args = e . target . value . split ( ' ' ) ;
console . log ( args ) ;
if ( args . length > 1 ) {
if ( args [ 0 ] == '>goto' || args [ 0 ] == '>g' ) {
console . log ( 'Handling >goto' ) ;
autocompleteBox . innerHTML = 'Start typing to search for commands <br>' + Object . keys ( urlList ) . join ( '<br>' ) + '<br>' ;
if ( args . length >= 2 ) {
console . log ( "Autocomplete for 'goto' command with " + args [ 1 ] + " as the second argument" )
// Check if the second argument matches the urlList or any of its aliases
for ( const [ key , value ] of Object . entries ( urlList ) ) {
console . log ( key , value )
if ( args [ 1 ] == key || value . alias . includes ( args [ 1 ] ) ) {
// Match found
console . log ( 'Match found' ) ;
autocompleteBox . innerHTML = ` Go to <a href=" ${ value . url } "> ${ key } </a> ` ;
currentBestGuessCommand = "open;" + value . url ;
break ;
} else {
currentBestGuessCommand = '' ;
}
}
}
}
2023-05-04 20:21:10 +02:00
}
2023-06-27 20:54:27 +02:00
} else if ( e . target . value [ 0 ] == '#' ) {
2023-05-02 18:23:47 +02:00
// Search for SKU
2023-06-27 20:54:27 +02:00
const searchedSKU = e . target . value . substring ( 1 ) ;
if ( searchedSKU == '' ) {
autocompleteBox . innerHTML = 'Start typing to search for commands <br> #SKU' ;
return ;
}
2023-07-11 16:25:58 +02:00
const baseURI = window . location . origin ; // move to new fancy route
2023-06-27 20:54:27 +02:00
const url = baseURI + '/api/v1/search/sku?sku=' + searchedSKU ;
$ . ajax ( {
type : 'get' ,
url : url ,
2023-07-09 18:07:28 +02:00
success : function ( result ) {
2023-06-27 20:54:27 +02:00
let htmlResult = ""
result . forEach ( element => {
console . log ( element ) ;
2023-07-05 20:18:52 +02:00
htmlResult += ` <a href="/s/ ${ element . SKU } "> ${ element . name } </a><br> `
2023-06-27 20:54:27 +02:00
} ) ;
autocompleteBox . innerHTML = htmlResult ;
} ,
error : function ( data ) {
createNewToast ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong while searching...' , "text-bg-danger" , autoHideTime = 3000 , autoReload = false )
}
} ) ;
2023-05-02 18:23:47 +02:00
} else {
// Search for name
}
2023-06-27 20:54:27 +02:00
}
function handleSearchSubmit ( e ) {
console . log ( 'Search submitted' ) ;
if ( currentBestGuessCommand != '' ) {
console . log ( 'Submitting command ' + currentBestGuessCommand ) ;
cmdArgs = currentBestGuessCommand . split ( ';' ) ;
if ( cmdArgs [ 0 ] == 'open' ) {
// Open the url in the current tab
setTimeout ( ( ) => {
window . location . replace ( cmdArgs [ 1 ] ) ;
} , 200 ) ;
return false ;
}
}
return false ;
}
function handleHotKey ( e ) {
// If c is pressed, focus on the search box
2023-06-27 23:40:52 +02:00
if ( e . key == 'c' && e . altKey && e . ctrlKey ) {
2023-06-27 20:54:27 +02:00
// Show search_modal modal
bootstrap . Modal . getOrCreateInstance ( $ ( '#search_modal' ) ) . show ( )
document . getElementById ( 'SearchBoxInput' ) . focus ( ) ;
}
}