/// import {Type, Component} from "@angular/core"; import {Reference} from "../../Reference"; import {BibleService} from "../../bible-service"; import {LoadingController, ModalController } from "ionic-angular"; import {StrongsService} from "../../strongs-service"; import {WordService} from "../../word-service"; import {Strongs} from "../../components/strongs/strongs"; import {Passage} from "../../components/passage/passage.ts"; import {StrongsModal} from "../../components/strongs-modal/strongs-modal.ts"; import {Storage} from '@ionic/storage'; import {Words} from '../../components/words/words.ts'; class Item { id: number; data: any; type: Type; dict: string; } @Component({ templateUrl: "search.html", providers: [BibleService, StrongsService, WordService], }) export class SearchPage { searchQuery: string = ""; last: number; user: User = { strongs_modal: true, clear_search_after_query: true, items: [] }; constructor( private strongsService: StrongsService , private bibleService: BibleService , private wordService: WordService , public loadingCtrl: LoadingController , public modalCtrl: ModalController , public local: Storage) { // Check if there is a profile saved in local storage this.local.get('profile').then(profile => { let t = this.user; if (profile === null) this.local.set('profile', JSON.stringify(this.user)); else { t = JSON.parse(profile); } this.initializeItems(t); }).catch(error => { console.log(error); }); } initializeItems(u: User) { this.last = u.items.length; this.user = u; } presentStrongsModal(strongs: StrongsResult) { let modal = this.modalCtrl.create(StrongsModal, { strongsid: strongs, onPassageClicked: this }); modal.present(); } setQuery(searchbar) { this.searchQuery = searchbar.target.value; } getQuery(searchbar) { this.getItems(this.searchQuery); } removeItem(item) { let idx = this.user.items.indexOf(item); this.user.items.splice(idx, 1); // save the users settings. this.saveSettings(); } isPassage(t: string) { return t === "Passage"; } isStrongs(t: string) { return t === "Strongs"; } isWords(t: string) { return t === "Words"; } saveSettings() { this.local.set('profile', JSON.stringify(this.user)); } getItems(search) { try { let qs = search.split(";"); for (let x in qs) { if (qs.hasOwnProperty(x)) { let q = qs[x].trim(); if (q !== "") { // its a search term. if (q.search(/[0-9]/i) === -1) { let result = this.wordService.getResult(q); this.user.items.unshift({ id: this.last++, data: result, type: "Words", dict: "na" }); } else if (q.search(/(H|G)[0-9]/i) !== -1) { // its a strongs lookup let dict = q.substring(0, 1); if (dict.search(/h/i) !== -1) { dict = "heb"; } else { dict = "grk"; } q = q.substring(1, q.length); let result = this.strongsService.getResult(parseInt(q), dict); if (this.user.strongs_modal) this.presentStrongsModal(result); else this.user.items.unshift({ id: this.last++, data: result, type: "Strongs", dict: "na" }); } else { // its a verse reference. if (q.trim() !== "") { let myref = new Reference(q.trim()); let r = this.bibleService.getResult(myref.Section); r.ref = myref.toString(); this.user.items.unshift({ id: this.last++, data: r, type: "Passage", dict: r.testament == 'new' ? "G" : "H" }); } } } } } if (this.user.clear_search_after_query) $(".searchbar-input").val(""); this.saveSettings(); } catch (error) { console.log(error); } } }