<commit_before>import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Line } from '../../models/itinerary';
import { History } from '../../models/history';
import { MapPage } from '../map/page';
import { FavoritesDAO } from '../../dao/favorites';
import { HistoryDAO } from '../../dao/history';
import { LineManager } from '../../managers/line';
import { FavoriteButton } from '../../components/favorite-button/controller';
import { LineItem } from '../../components/line-item/controller';
import { BasePage } from '../../core/page';

/**
 * SearchPage represents the search view in the app.
 * @class {SearchPage}
 */
@Component({
    templateUrl: 'build/pages/search/template.html',
    directives: [FavoriteButton, LineItem],
})
export class SearchPage extends BasePage {

    private nav: NavController;

    private fdao: FavoritesDAO;
    private hdao: HistoryDAO;
    private manager: LineManager;

    private itemsBkp: Line[] = [];
    private favorites: Line[] = [];
    public items: Line[] = [];
    public histories: History[] = [];

    public showSearchBox: boolean = false;

    public constructor(nav: NavController, manager: LineManager) {
        super();
        this.nav = nav;
        this.manager = manager;
        this.fdao = new FavoritesDAO();
        this.hdao = new HistoryDAO();
    }

    /**
     * Checks if the given line is favorite.
     * @param {string} line - line identifier
     * @return {boolean}
     */
    public isFavorite(line: Line): boolean {
        return this.favorites.some(fav => line.Line === fav.Line);
    }

    /**
     * ion-input#searchbox
     * return {any}
     */
    public get searchBox(): any {
        let element: any = document.getElementById('searchbox');
        if (element) element = element.getElementsByTagName('input');
        if (element) element = element.item(0);
        return (element) ? element : null;
    }

    /**
     * Part of Ionic lifecycle. Runs when the view was just presented.
     * @return {void}
     */
    public ionViewLoaded(): void {
        this.loadRecents();
        this.loadFavorites();
    }

    /**
     * Part of Ionic lifecycle. Runs when the view is about to be presented.
     * @return {void}
     */
    public ionViewWillEnter(): void {
        document.getElementById('search-view').style.display = 'initial';
    }

    /**
     * Part of Ionic lifecycle. Runs when the view is about to be hidden.
     * @return {void}
     */
    public ionViewWillLeave(): void {
        document.getElementById('search-view').style.display = 'none';
    }

    /**
     * Handles the click event triggered when the 'search for ...' item is pressed.
     * @return {void}
     */
    public onClickFind(): void {
        let query: string = this.searchBox.value.replace('  ', ' ').replace(' , ', ',').replace(', ', ',').replace(' ,', ',');
        let line: Line = new Line(query, '');
        let history: History = new History(line, new Date());
        this.hdao.save(history).then(saved => {
            if (saved) console.log(`Saved ${line.Line} to history.`);
            this.nav.push(MapPage, { query: query });
        });
    }

    /**
     * Handles the event triggered when there is a new input in the searchbox.
     * @param {Event} event - triggered event
     * @return {void}
     */
    public onInput(event: Event): void {
        let query: string = 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: Line): boolean =>
                        value.Line.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
                        value.Description.toLowerCase().indexOf(query.toLowerCase()) > -1
                );
                this.items.push(new Line(query, this.Text.PAGE_SEARCH_SEARCHFOR));
            });
        } else {
            this.items = this.itemsBkp;
            this.itemsBkp = [];
        }
    }

    /**
     * Displays the searchbox
     * @return {void}
     */
    public showSearchBar(): void {
        this.showSearchBox = true;
        setTimeout(() => this.searchBox.focus(), 1);
    }

    /**
     * Hides the searchbox
     * @return {void}
     */
    public hideSearchBar(): void {
        if (this.searchBox.value.length > 0) {
            this.searchBox.value = '';
            this.onInput(null);
        }
        this.showSearchBox = false;
    }

    /**<commit_msg>Forcing Keyboard to show up when search bar is displayed<commit_after>import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Line } from '../../models/itinerary';
import { History } from '../../models/history';
import { MapPage } from '../map/page';
import { FavoritesDAO } from '../../dao/favorites';
import { HistoryDAO } from '../../dao/history';
import { LineManager } from '../../managers/line';
import { FavoriteButton } from '../../components/favorite-button/controller';
import { LineItem } from '../../components/line-item/controller';
import { BasePage } from '../../core/page';
import { Keyboard } from 'ionic-native';
/**
 * SearchPage represents the search view in the app.
 * @class {SearchPage}
 */
@Component({
    templateUrl: 'build/pages/search/template.html',
    directives: [FavoriteButton, LineItem],
})
export class SearchPage extends BasePage {

    private nav: NavController;

    private fdao: FavoritesDAO;
    private hdao: HistoryDAO;
    private manager: LineManager;

    private itemsBkp: Line[] = [];
    private favorites: Line[] = [];
    public items: Line[] = [];
    public histories: History[] = [];

    public showSearchBox: boolean = false;

    public constructor(nav: NavController, manager: LineManager) {
        super();
        this.nav = nav;
        this.manager = manager;
        this.fdao = new FavoritesDAO();
        this.hdao = new HistoryDAO();
    }

    /**
     * Checks if the given line is favorite.
     * @param {string} line - line identifier
     * @return {boolean}
     */
    public isFavorite(line: Line): boolean {
        return this.favorites.some(fav => line.Line === fav.Line);
    }

    /**
     * ion-input#searchbox
     * return {any}
     */
    public get searchBox(): any {
        let element: any = document.getElementById('searchbox');
        if (element) element = element.getElementsByTagName('input');
        if (element) element = element.item(0);
        return (element) ? element : null;
    }

    /**
     * Part of Ionic lifecycle. Runs when the view was just presented.
     * @return {void}
     */
    public ionViewLoaded(): void {
        this.loadRecents();
        this.loadFavorites();
    }

    /**
     * Part of Ionic lifecycle. Runs when the view is about to be presented.
     * @return {void}
     */
    public ionViewWillEnter(): void {
        document.getElementById('search-view').style.display = 'initial';
    }

    /**
     * Part of Ionic lifecycle. Runs when the view is about to be hidden.
     * @return {void}
     */
    public ionViewWillLeave(): void {
        document.getElementById('search-view').style.display = 'none';
    }

    /**
     * Handles the click event triggered when the 'search for ...' item is pressed.
     * @return {void}
     */
    public onClickFind(): void {
        let query: string = this.searchBox.value.replace('  ', ' ').replace(' , ', ',').replace(', ', ',').replace(' ,', ',');
        let line: Line = new Line(query, '');
        let history: History = new History(line, new Date());
        this.hdao.save(history).then(saved => {
            if (saved) console.log(`Saved ${line.Line} to history.`);
            this.nav.push(MapPage, { query: query });
        });
    }

    /**
     * Handles the event triggered when there is a new input in the searchbox.
     * @param {Event} event - triggered event
     * @return {void}
     */
    public onInput(event: Event): void {
        let query: string = 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: Line): boolean =>
                        value.Line.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
                        value.Description.toLowerCase().indexOf(query.toLowerCase()) > -1
                );
                this.items.push(new Line(query, this.Text.PAGE_SEARCH_SEARCHFOR));
            });
        } else {
            this.items = this.itemsBkp;
            this.itemsBkp = [];
        }
    }

    /**
     * Displays the searchbox
     * @return {void}
     */
    public showSearchBar(): void {
        this.showSearchBox = true;
        setTimeout(() => {
            this.searchBox.focus();
            Keyboard.show();
        }, 1);
    }

    /**
     * Hides the searchbox
     * @return {void}
     */
    public hideSearchBar(): void {
        if (this.searchBox.value.length > 0) {
            this.searchBox.value = '';
            this.onInput(null);
        }
        this.showSearchBox = false;
    }

    /**
{'fraction_non_alphanumeric': 0.14774561497901648, 'fraction_numerical': 0.0015065102765522435, 'mean_word_length': 2.1441136671177268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4003992', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2659, 'n_words': 692}