import { Component , OnDestroy , OnInit } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { HS_PRMS } from '../permalink/get-params' ; import { HsEventBusService } from '../core/event-bus.service' ; import { HsSearchService } from './search.service' ; import { HsShareUrlService } from '../permalink/share-url.service' ; /**
 * Add search input template to page
 */ @ Component ( { selector : 'hs-search-input' , templateUrl : './partials/search-input.component.html' , } ) export class HsSearchInputComponent implements OnInit , OnDestroy { query = '' ; searchInputVisible : boolean ; clearVisible = false ; searchResultsReceivedSubscription : Subscription ; constructor ( private hsSearchService , private hsEventBusService , private hsShareUrlService ) { this . searchResultsReceivedSubscription = this . hsEventBusService . searchResultsReceived . subscribe ( ( _ ) => { this . clearVisible = true ; } ) ; } ngOnDestroy ( ) { this . searchResultsReceivedSubscription . unsubscribe ( ) ; } ngOnInit ( ) { const query = this . hsShareUrlService . getParamValue ( HS_PRMS . search ) ; if ( query ) { this . query = query ; this . queryChanged ( ) ; } window . innerWidth < 767 ? ( this . searchInputVisible = false ) : ( this . searchInputVisible = true ) ; } /**
     * Handler of search input, request search service and display results div
     */ async queryChanged ( ) { await this . hsSearchService . hsMapService . loaded ( ) ; if ( this . query . length == 0 ) { this . clear ( ) ; return ; } this . hsSearchService . request ( this . query ) ; } /**
     * Remove previous search and search results
     */ clear ( ) { this . query = '' ; this . clearVisible = false ; this . hsSearchService . cleanResults ( ) ; this . hsEventBusService . clearSearchResults . next ( ) ; } toggleSearchInput ( ) { this . searchInputVisible = ! this . searchInputVisible ; if ( this . query != '' ) { this . clear ( ) ; } } }