193 lines
5.9 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 {WordService} from "../../word-service";
import {StrongsModal} from "../../components/strongs-modal/strongs-modal.ts";
import {Storage} from '@ionic/storage';
import {UserProfile} from '../../UserProfile';
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, WordService],
})
export class SearchPage
{
searchQuery: string = "";
userProfile: UserProfile;
last: CardItem;
constructor(
private strongsService: StrongsService
, private bibleService: BibleService
, private wordService: WordService
, public loadingCtrl: LoadingController
, public modalCtrl: ModalController
, public local: Storage)
{
this.userProfile = new UserProfile(UserProfile.createDefaultUser());
// Check if there is a profile saved in local storage
this.local.get('profile').then(profile =>
{
let t = this.userProfile;
if (profile !== null)
t = JSON.parse(profile);
this.userProfile.update(t, local);
this.initializeItems(this.userProfile);
}).catch(error =>
{
console.log(error);
});
}
initializeItems(u: UserProfile)
{
this.userProfile = 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.userProfile.user.items.indexOf(item);
this.userProfile.user.items.splice(idx, 1);
// save the users settings.
this.userProfile.save(this.local);
}
isPassage(t: string)
{
return t === "Passage";
}
isStrongs(t: string)
{
return t === "Strongs";
}
isWords(t: string)
{
return t === "Words";
}
addItemToList(item)
{
if (this.userProfile.user.append_to_bottom)
{
if (this.last != null && this.userProfile.user.insert_next_to_item)
{
let idx = this.userProfile.user.items.indexOf(this.last);
this.userProfile.user.items.splice(idx + 1, 0, item);
}
else
this.userProfile.user.items.push(item);
}
else
{
if (this.last != null && this.userProfile.user.insert_next_to_item)
{
let idx = this.userProfile.user.items.indexOf(this.last);
this.userProfile.user.items.splice(idx, 0, item);
}
else
this.userProfile.user.items.unshift(item);
}
this.last = null;
}
getItemsNextToCard(data: OpenData)
{
this.last = data.card;
this.getItems(data.qry);
}
getItems(search)
{
try
{
let qs = search.split(";");
for (let x in qs)
{
if (qs.hasOwnProperty(x))
{
2016-09-12 18:13:56 -04:00
let q = qs[x].trim();
if (q !== "")
{
2016-09-12 18:13:56 -04:00
// its a search term.
if (q.search(/[0-9]/i) === -1)
{
let result = this.wordService.getResult(q);
this.addItemToList({ data: result, type: "Words", dict: "na" });
}
else if (q.search(/(H|G)[0-9]/i) !== -1)
{
2016-09-12 18:13:56 -04:00
// its a strongs lookup
let dict = q.substring(0, 1);
if (dict.search(/h/i) !== -1)
{
2016-09-12 18:13:56 -04:00
dict = "heb";
} else
{
2016-09-12 18:13:56 -04:00
dict = "grk";
}
q = q.substring(1, q.length);
let result = this.strongsService.getResult(parseInt(q), dict);
if (this.userProfile.user.strongs_modal)
this.presentStrongsModal(result);
else
this.addItemToList({ data: result, type: "Strongs", dict: "na" });
}
else
{
2016-09-12 18:13:56 -04:00
// its a verse reference.
if (q.trim() !== "")
{
2016-09-12 18:13:56 -04:00
let myref = new Reference(q.trim());
let r = this.bibleService.getResult(myref.Section);
2016-09-12 18:13:56 -04:00
r.ref = myref.toString();
this.addItemToList({ data: r, type: "Passage", dict: r.testament == 'new' ? "G" : "H" });
2016-09-12 18:13:56 -04:00
}
}
}
}
}
if (this.userProfile.user.clear_search_after_query)
$(".searchbar-input").val("");
2016-09-12 18:13:56 -04:00
this.userProfile.save(this.local);
}
catch (error)
{
console.log(error);
}
}
}