import { keepTrackApi } from '@app/js/api/keepTrackApi' ; import $ from 'jquery' ; import { CatalogManager , SearchBox } from '../api/keepTrackTypes' ; import { SpaceObjectType } from '../api/SpaceObjectType' ; import { SatGroup } from '../groupsManager/sat-group' ; let hoverSatId = - 1 ; let hovering = false ; let resultsOpen = false ; let lastResultGroup ; interface SearchResult { isBus : any ; satId : number ; type : SpaceObjectType ; isON : boolean ; strIndex : number ; patlen : number ; isSccNum : boolean ; isIntlDes : boolean ; } export const isResultBoxOpen = ( ) : boolean => resultsOpen ; export const getLastResultGroup = ( ) => lastResultGroup ; export const getHoverSat = ( ) => hoverSatId ; export const isHovering = ( val ? ) : boolean => ( hovering = typeof val !== 'undefined' ? val : hovering ) ; export const setHoverSat = ( satId ) : number => ( hoverSatId = satId ) ; export const getCurrentSearch = ( ) => ( resultsOpen ? $ ( '#search' ) . val ( ) : null ) ; export const hideResults = ( ) => { try { const { colorSchemeManager , groupsManager , dotsManager , satSet } = keepTrackApi . programs ; $ ( '#search-results' ) . slideUp ( ) ; groupsManager . clearSelect ( ) ; resultsOpen = false ; settingsManager . lastSearch = '' ; settingsManager . lastSearchResults = [ ] ; dotsManager . updateSizeBuffer ( satSet . satData . length ) ; if ( settingsManager . currentColorScheme === colorSchemeManager . group ) { satSet . setColorScheme ( colorSchemeManager . default , true ) ; } else { satSet . setColorScheme ( settingsManager . currentColorScheme , true ) ; } } catch ( error ) { console . warn ( error ) ; } } ; export const doArraySearch = ( array ) => array . reduce ( ( searchStr , i ) => `${ searchStr }${ keepTrackApi . programs . satSet . satData [ i ] . sccNum },` , '' ) . slice ( 0 , - 1 ) ; export const doSearch = ( searchString , isPreventDropDown ? ) : number [ ] => { const { satSet , dotsManager , groupsManager } = keepTrackApi . programs ; if ( satSet . satData . length === 0 ) throw new Error ( 'No sat data loaded! Check if TLEs are corrupted!' ) ; if ( searchString . length === 0 ) { settingsManager . lastSearch = '' ; settingsManager . lastSearchResults = [ ] ; dotsManager . updateSizeBuffer ( satSet . satData . length ) ; $ ( '#search' ) . val ( '' ) ; searchBox . hideResults ( ) ; return [ ] ; } $ ( '#search' ) . val ( searchString ) ; if ( searchString . length <= settingsManager . minimumSearchCharacters && searchString !== 'RV_' ) { return [ ] ; } searchString = searchString . toUpperCase ( ) ; let searchList = searchString . split ( /,/u ) ; settingsManager . lastSearch = searchList ; const satData = satSet . satData ; let results = [ ] ; searchList . forEach ( ( searchString ) => { satData . every ( ( sat , i ) => { if ( i > satSet . missileSats ) return false ; const len = searchString . length ; if ( sat . static && sat . type !== SpaceObjectType . STAR ) return true ; if ( sat . marker ) return false ; if ( settingsManager . isSatOverflyModeOn && sat . type !== SpaceObjectType . PAYLOAD ) return true ; if ( sat . missile && ! sat . active ) return true ; if ( sat . country == 'ANALSAT' && ! sat . active ) return true ; if ( ! sat . name ) return true ; if ( sat . name . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . name . indexOf ( searchString ) , type : sat . type , isON : true , patlen : len , satId : i , } ) ; return true ; } if ( typeof sat . bus !== 'undefined' && sat . bus . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . bus . indexOf ( searchString ) , type : sat . type , isBus : true , patlen : len , satId : i , } ) ; return true ; } if ( ! sat . desc ) { } else if ( sat . desc . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . desc . indexOf ( searchString ) , isMissile : true , patlen : len , satId : i , } ) ; return true ; } else { return true ; } if ( sat . sccNum && sat . sccNum . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . sccNum . indexOf ( searchString ) , isSccNum : true , patlen : len , satId : i , } ) ; return true ; } if ( sat . intlDes && sat . intlDes . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . intlDes . indexOf ( searchString ) , isIntlDes : true , patlen : len , satId : i , } ) ; return true ; } if ( sat . launchVehicle && sat . launchVehicle . toUpperCase ( ) . indexOf ( searchString ) !== - 1 ) { results . push ( { strIndex : sat . launchVehicle . indexOf ( searchString ) , isLV : true , patlen : len , satId : i , } ) ; return true ; } return true ; } ) ; } ) ; results = results . splice ( 0 , settingsManager . searchLimit ) ; const idList = results . map ( ( sat ) => sat . satId ) ; settingsManager . lastSearchResults = idList ; dotsManager . updateSizeBuffer ( satSet . satData . length ) ; const dispGroup = groupsManager . createGroup ( 'idList' , idList ) ; lastResultGroup = dispGroup ; groupsManager . selectGroup ( dispGroup ) ; if ( ! isPreventDropDown ) searchBox . fillResultBox ( results , satSet ) ; return idList ; } ; export const fillResultBox = ( results , satSet ) => { let satData = satSet . satData ; var resultBox = $ ( '#search-results' ) ; resultBox [ 0 ] . innerHTML = results . reduce ( ( html , result ) => { const sat = satData [ result . satId ] ; html += '<div class="search-result" data-obj-id="' + sat . id + '">' ; html += '<div class="truncate-search">' ; if ( sat . missile ) { html += sat . name ; } else if ( result . isON ) { html += sat . name . substring ( 0 , result . strIndex ) ; html += '<span class="search-hilight">' ; html += sat . name . substring ( result . strIndex , result . strIndex + result . patlen ) ; html += '</span>' ; html += sat . name . substring ( result . strIndex + result . patlen ) ; } else { html += sat . name ; } html += '</div>' ; html += '<div class="search-result-scc">' ; if ( sat . missile ) { html += sat . desc ; } else if ( result . isSccNum ) { result . strIndex = result . strIndex || 0 ; result . patlen = result . patlen || 5 ; html += sat . sccNum . substring ( 0 , result . strIndex ) ; html += '<span class="search-hilight">' ; html += sat . sccNum . substring ( result . strIndex , result . strIndex + result . patlen ) ; html += '</span>' ; html += sat . sccNum . substring ( result . strIndex + result . patlen ) ; } else if ( result . isIntlDes ) { result . strIndex = result . strIndex || 0 ; result . patlen = result . patlen || 5 ; html += sat . intlDes . substring ( 0 , result . strIndex ) ; html += '<span class="search-hilight">' ; html += sat . intlDes . substring ( result . strIndex , result . strIndex + result . patlen ) ; html += '</span>' ; html += sat . intlDes . substring ( result . strIndex + result . patlen ) ; } else if ( result . isBus ) { result . strIndex = result . strIndex || 0 ; result . patlen = result . patlen || 5 ; html += sat . bus . substring ( 0 , result . strIndex ) ; html += '<span class="search-hilight">' ; html += sat . bus . substring ( result . strIndex , result . strIndex + result . patlen ) ; html += '</span>' ; html += sat . bus . substring ( result . strIndex + result . patlen ) ; } else if ( result . type === SpaceObjectType . STAR ) { html += 'Star' ; } else { html += sat . sccNum ; } html += '</div></div>' ; return html ; } , '' ) ; resultBox . slideDown ( ) ; resultsOpen = true ; satSet . setColorScheme ( settingsManager . currentColorScheme , true ) ; } ; export const searchBox = { isResultBoxOpen , getLastResultGroup , getCurrentSearch , isHovering , setHoverSat , getHoverSat , hideResults , doArraySearch , doSearch , fillResultBox , } ;