130 lines
4.4 KiB
TypeScript
Raw Normal View History

2016-09-12 13:12:25 -04:00
/// <reference path="../../types.ts" />
import {Type, Component} from "@angular/core";
import {Reference} from "../../Reference";
2016-09-12 18:13:56 -04:00
import {BibleService} from "../../bible-service";
import {LoadingController, ModalController } from "ionic-angular";
2016-09-12 18:13:56 -04:00
import {StrongsService} from "../../strongs-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';
2016-09-12 18:13:56 -04:00
class Item {
2016-09-12 18:13:56 -04:00
id: number;
data: any;
type: Type<any>;
dict: string;
2016-09-12 18:13:56 -04:00
}
2016-09-12 13:12:25 -04:00
@Component({
templateUrl: "search.html",
providers: [BibleService, StrongsService],
})
export class SearchPage {
searchQuery: string = "";
items: CardItem[];
last: number;
user: User = { strongs_modal: true };
constructor(
private strongsService: StrongsService
, private bibleService: BibleService
, public loadingCtrl: LoadingController
, public modalCtrl: ModalController
, public local: Storage) {
this.initializeItems();
// Check if there is a profile saved in local storage
this.local.get('profile').then(profile => {
if (profile === null) {
this.local.set('profile', JSON.stringify(this.user));
}
else
this.user = JSON.parse(profile);
}).catch(error => {
console.log(error);
});
}
initializeItems() {
this.items = [];
this.last = 0;
}
presentStrongsModal(strongs: StrongsResult) {
let modal = this.modalCtrl.create(StrongsModal, { strongsid: strongs });
modal.present();
}
setQuery(searchbar) {
this.searchQuery = searchbar.target.value;
}
getQuery(searchbar) {
this.getItems(this.searchQuery);
}
removeItem(item) {
2016-09-12 13:12:25 -04:00
let idx = this.items.indexOf(item);
this.items.splice(idx, 1);
}
isPassage(t: Type<any>) {
return t === Passage;
}
isStrongs(t: Type<any>) {
return t === Strongs;
}
getItems(search) {
try {
2016-09-12 18:13:56 -04:00
let loader = this.loadingCtrl.create({
content: "Retrieving passage...",
dismissOnPageChange: true
});
loader.present();
let qs = search.split(";");
2016-09-12 18:13:56 -04:00
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) {
// get new results.
//Words.FindReferences(q);
//$("#searchpanel").panel("open");
} 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.getStrongs(parseInt(q), dict);
if (this.user.strongs_modal)
this.presentStrongsModal(result);
else
this.items.unshift({ id: this.last++, data: result, type: Strongs, dict: "na" });
2016-09-12 18:13:56 -04:00
} else {
// its a verse reference.
if (q.trim() !== "") {
let myref = new Reference(q.trim());
let r = this.bibleService.getPassage(myref.Section);
r.ref = myref.toString();
this.items.unshift({ id: this.last++, data: r, type: Passage, dict: r.testament == 'new' ? "G" : "H" });
2016-09-12 18:13:56 -04:00
}
}
}
}
}
loader.dismiss();
//Settings.SaveResults();
}
catch (err) {
//Util.HandleError(err);
}
}
}