2016-09-12 13:12:25 -04:00
|
|
|
/// <reference path="../../types.ts" />
|
2016-12-01 10:39:04 -05:00
|
|
|
import {Type, Component} from "@angular/core";
|
|
|
|
import {Reference} from "../../Reference";
|
2016-09-12 18:13:56 -04:00
|
|
|
import {BibleService} from "../../bible-service";
|
2017-01-03 23:51:25 -05:00
|
|
|
import {Loading, LoadingController, ModalController } from "ionic-angular";
|
2016-09-12 18:13:56 -04:00
|
|
|
import {StrongsService} from "../../strongs-service";
|
2016-12-27 21:10:00 -05:00
|
|
|
import {WordService} from "../../word-service";
|
2016-12-01 13:40:04 -05:00
|
|
|
import {StrongsModal} from "../../components/strongs-modal/strongs-modal.ts";
|
2016-12-01 15:21:07 -05:00
|
|
|
import {Storage} from '@ionic/storage';
|
2016-12-31 18:12:50 -05:00
|
|
|
import {UserProfile} from '../../UserProfile';
|
2016-09-12 18:13:56 -04:00
|
|
|
|
2016-12-27 21:10:00 -05:00
|
|
|
class Item
|
|
|
|
{
|
2016-09-12 18:13:56 -04:00
|
|
|
id: number;
|
|
|
|
data: any;
|
2016-12-01 10:39:04 -05:00
|
|
|
type: Type<any>;
|
|
|
|
dict: string;
|
2016-09-12 18:13:56 -04:00
|
|
|
}
|
2016-09-12 13:12:25 -04:00
|
|
|
|
2016-12-01 10:39:04 -05:00
|
|
|
@Component({
|
|
|
|
templateUrl: "search.html",
|
2016-12-27 21:10:00 -05:00
|
|
|
providers: [BibleService, StrongsService, WordService],
|
2016-12-01 10:39:04 -05:00
|
|
|
})
|
2016-12-27 21:10:00 -05:00
|
|
|
export class SearchPage
|
|
|
|
{
|
2016-12-01 10:39:04 -05:00
|
|
|
searchQuery: string = "";
|
2016-12-31 18:12:50 -05:00
|
|
|
userProfile: UserProfile;
|
2016-12-28 16:57:40 -05:00
|
|
|
last: CardItem;
|
2017-01-03 23:51:25 -05:00
|
|
|
loader: Loading;
|
2016-12-01 10:39:04 -05:00
|
|
|
|
2016-12-01 13:40:04 -05:00
|
|
|
constructor(
|
2016-12-01 15:21:07 -05:00
|
|
|
private strongsService: StrongsService
|
2016-12-01 13:40:04 -05:00
|
|
|
, private bibleService: BibleService
|
2016-12-27 21:10:00 -05:00
|
|
|
, private wordService: WordService
|
2016-12-01 13:40:04 -05:00
|
|
|
, public loadingCtrl: LoadingController
|
2016-12-01 15:21:07 -05:00
|
|
|
, public modalCtrl: ModalController
|
2016-12-27 21:10:00 -05:00
|
|
|
, public local: Storage)
|
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile = new UserProfile(UserProfile.createDefaultUser());
|
|
|
|
|
2016-12-01 15:21:07 -05:00
|
|
|
// Check if there is a profile saved in local storage
|
2016-12-27 21:10:00 -05:00
|
|
|
this.local.get('profile').then(profile =>
|
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
let t = this.userProfile;
|
|
|
|
|
|
|
|
if (profile !== null)
|
2016-12-27 21:10:00 -05:00
|
|
|
t = JSON.parse(profile);
|
2016-12-28 15:57:16 -05:00
|
|
|
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile.update(t, local);
|
2016-12-27 21:10:00 -05:00
|
|
|
|
2016-12-31 18:12:50 -05:00
|
|
|
this.initializeItems(this.userProfile);
|
2016-12-27 21:10:00 -05:00
|
|
|
}).catch(error =>
|
|
|
|
{
|
2016-12-01 15:21:07 -05:00
|
|
|
console.log(error);
|
|
|
|
});
|
2017-01-03 23:51:25 -05:00
|
|
|
|
2016-12-01 10:39:04 -05:00
|
|
|
}
|
|
|
|
|
2016-12-31 18:12:50 -05:00
|
|
|
initializeItems(u: UserProfile)
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile = u;
|
2016-12-01 10:39:04 -05:00
|
|
|
}
|
2016-12-01 13:40:04 -05:00
|
|
|
|
2016-12-27 21:10:00 -05:00
|
|
|
presentStrongsModal(strongs: StrongsResult)
|
|
|
|
{
|
2016-12-02 13:00:55 -05:00
|
|
|
let modal = this.modalCtrl.create(StrongsModal, { strongsid: strongs, onPassageClicked: this });
|
2016-12-01 13:40:04 -05:00
|
|
|
modal.present();
|
|
|
|
}
|
|
|
|
|
2016-12-27 21:10:00 -05:00
|
|
|
setQuery(searchbar)
|
|
|
|
{
|
2016-12-01 10:39:04 -05:00
|
|
|
this.searchQuery = searchbar.target.value;
|
|
|
|
}
|
2016-12-27 21:10:00 -05:00
|
|
|
getQuery(searchbar)
|
|
|
|
{
|
2016-12-01 10:39:04 -05:00
|
|
|
this.getItems(this.searchQuery);
|
|
|
|
}
|
2016-12-27 21:10:00 -05:00
|
|
|
|
|
|
|
removeItem(item)
|
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
let idx = this.userProfile.user.items.indexOf(item);
|
|
|
|
this.userProfile.user.items.splice(idx, 1);
|
2016-12-27 21:10:00 -05:00
|
|
|
|
|
|
|
// save the users settings.
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile.save(this.local);
|
2016-12-27 21:10:00 -05:00
|
|
|
}
|
|
|
|
|
2017-01-03 23:51:25 -05:00
|
|
|
isError(t: string)
|
|
|
|
{
|
|
|
|
return t === "Error";
|
|
|
|
}
|
2016-12-27 21:10:00 -05:00
|
|
|
isPassage(t: string)
|
|
|
|
{
|
|
|
|
return t === "Passage";
|
|
|
|
}
|
|
|
|
isStrongs(t: string)
|
|
|
|
{
|
|
|
|
return t === "Strongs";
|
2016-12-01 10:39:04 -05:00
|
|
|
}
|
2016-12-27 21:10:00 -05:00
|
|
|
isWords(t: string)
|
|
|
|
{
|
|
|
|
return t === "Words";
|
2016-12-01 10:39:04 -05:00
|
|
|
}
|
2017-01-03 23:51:25 -05:00
|
|
|
|
2016-12-28 15:57:16 -05:00
|
|
|
addItemToList(item)
|
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
if (this.userProfile.user.append_to_bottom)
|
2016-12-28 16:57:40 -05:00
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
if (this.last != null && this.userProfile.user.insert_next_to_item)
|
2016-12-28 16:57:40 -05:00
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
let idx = this.userProfile.user.items.indexOf(this.last);
|
|
|
|
this.userProfile.user.items.splice(idx + 1, 0, item);
|
2016-12-28 16:57:40 -05:00
|
|
|
}
|
|
|
|
else
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile.user.items.push(item);
|
2016-12-28 16:57:40 -05:00
|
|
|
}
|
2016-12-28 15:57:16 -05:00
|
|
|
else
|
2016-12-28 16:57:40 -05:00
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
if (this.last != null && this.userProfile.user.insert_next_to_item)
|
2016-12-28 16:57:40 -05:00
|
|
|
{
|
2016-12-31 18:12:50 -05:00
|
|
|
let idx = this.userProfile.user.items.indexOf(this.last);
|
|
|
|
this.userProfile.user.items.splice(idx, 0, item);
|
2016-12-28 16:57:40 -05:00
|
|
|
}
|
|
|
|
else
|
2016-12-31 18:12:50 -05:00
|
|
|
this.userProfile.user.items.unshift(item);
|
2016-12-28 16:57:40 -05:00
|
|
|
}
|
|
|
|
this.last = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getItemsNextToCard(data: OpenData)
|
|
|
|
{
|
|
|
|
this.last = data.card;
|
|
|
|
this.getItems(data.qry);
|
2016-12-28 15:57:16 -05:00
|
|
|
}
|
|
|
|
|
2017-01-03 23:51:25 -05:00
|
|
|
|
2016-12-27 21:10:00 -05:00
|
|
|
getItems(search)
|
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
this.loader = this.loadingCtrl.create({
|
|
|
|
content: "Looking up query..."
|
|
|
|
});
|
|
|
|
this.loader.present().then(
|
|
|
|
() =>
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
try
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
let qs = search.split(";");
|
|
|
|
for (let x in qs)
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
if (qs.hasOwnProperty(x))
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
let q = qs[x].trim();
|
|
|
|
if (q !== "")
|
2016-12-27 21:10:00 -05:00
|
|
|
{
|
2017-01-03 23:51:25 -05:00
|
|
|
// its a search term.
|
|
|
|
if (q.search(/[0-9]/i) === -1)
|
|
|
|
{
|
|
|
|
let result = this.wordService.getResult(q);
|
|
|
|
if (result.status == 0)
|
|
|
|
this.addItemToList({ data: result, type: "Words", dict: "na" });
|
|
|
|
else
|
|
|
|
this.addItemToList({ data: result.msg, type: "Error", 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 (result.status == -1)
|
|
|
|
this.addItemToList({ data: result.msg, type: "Error", dict: "na" });
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.userProfile.user.strongs_modal)
|
|
|
|
this.presentStrongsModal(result);
|
|
|
|
else
|
|
|
|
this.addItemToList({ 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();
|
|
|
|
if (r.status == 0)
|
|
|
|
this.addItemToList({ data: r, type: "Passage", dict: r.testament == 'new' ? "G" : "H" });
|
|
|
|
else
|
|
|
|
this.addItemToList({ data: r.msg, type: "Error", dict: "na" });
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 18:13:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 23:51:25 -05:00
|
|
|
if (this.userProfile.user.clear_search_after_query)
|
|
|
|
$(".searchbar-input").val("");
|
|
|
|
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
}
|
|
|
|
catch (error)
|
|
|
|
{
|
|
|
|
this.addItemToList({ data: error, type: "Error", dict: "na" });
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
this.loader.dismiss();
|
2016-09-12 18:13:56 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-03 23:51:25 -05:00
|
|
|
);
|
2016-12-01 10:39:04 -05:00
|
|
|
}
|
2016-05-18 17:15:23 -04:00
|
|
|
}
|