import { Component } from '@angular/core' ; import { NavController } from 'ionic-angular' ; import { Line } from '../../models/itinerary' ; import { History } from '../../models/history' ; import { FavoritesDAO } from '../../dao/favorites' ; import { HistoryDAO } from '../../dao/history' ; import { LineManager } from '../../managers/line' ; import { Analytics } from '../../core/analytics' ; import { Keyboard } from 'ionic-native' ; import { FEED_SLICE_LIMIT } from '../../const' ; import strings from '../../strings' ; class @ Component ( { templateUrl : 'template.html' , } ) export class SearchPage { private skip : number = 0 ; private itemsBkp : Line [ ] = [ ] ; private favorites : Line [ ] = [ ] ; public items : Line [ ] = [ ] ; public histories : History [ ] = [ ] ; public showSearchBox : boolean = false ; public get Text ( ) : any { return strings ; } public constructor ( private nav , private manager , private hdao , private fdao ) { Analytics . trackView ( 'SearchPage' ) ; } param { string } line return { boolean } public isFavorite ( line ) { return this . favorites . some ( fav => line . Line === fav . Line ) ; } /**
     * ion-input#searchbox
     * return {any}
     */ public get searchBox ( ) : any { let element = document . getElementById ( 'searchbox' ) ; if ( element ) element = element . getElementsByTagName ( 'input' ) ; if ( element ) element = element . item ( 0 ) ; return ( element ) ? element : null ; } return { void } public ionViewDidLoad ( ) { this . loadRecents ( ) ; this . loadLines ( ) ; } return { void } public ionViewWillEnter ( ) { document . getElementById ( 'search-view' ) . style . display = 'initial' ; } return { void } public ionViewWillLeave ( ) { document . getElementById ( 'search-view' ) . style . display = 'none' ; } param { Event } event return { void } public onSubmit ( event ) { Analytics . trackEvent ( 'searchbox' , 'submit' ) ; let query = this . searchBox . value ; if ( query . length > 0 ) { if ( this . itemsBkp . length === 0 ) this . itemsBkp = this . items ; this . manager . getAll ( ) . then ( lines => { this . items = lines . filter ( ( value ) : boolean => value . Line . toLowerCase ( ) . indexOf ( query . toLowerCase ( ) ) > - 1 || value . Description . toLowerCase ( ) . indexOf ( query . toLowerCase ( ) ) > - 1 ) ; if ( this . items . length === 0 ) this . items . push ( new Line ( query , this . Text . PAGE_SEARCH_SEARCHFOR ) ) ; } ) ; } else { this . items = this . itemsBkp ; this . itemsBkp = [ ] ; } } return { void } public showSearchBar ( ) { Analytics . trackEvent ( 'searchbox' , 'show' ) ; this . showSearchBox = true ; setTimeout ( ( ) => { this . searchBox . focus ( ) ; Keyboard . show ( ) ; } , 1 ) ; } return { void } public hideSearchBar ( ) { Analytics . trackEvent ( 'searchbox' , 'hide' ) ; if ( this . searchBox . value . length > 0 ) { this . searchBox . value = '' ; this . onSubmit ( null ) ; } this . showSearchBox = false ; } return { void } private loadRecents ( ) { this . hdao . getLimited ( 2 ) . then ( histories => this . histories = histories ) ; } return { void } public loadLines ( infiniteScroll ? ) { if ( this . showSearchBox ) { if ( infiniteScroll ) infiniteScroll . complete ( ) ; return ; } this . manager . getSlice ( FEED_SLICE_LIMIT , this . skip ) . then ( lines => { this . items = this . items . concat ( lines ) ; this . skip += FEED_SLICE_LIMIT ; if ( infiniteScroll ) infiniteScroll . complete ( ) ; } ) ; } }