2017-01-18 17:51:06 -05:00
|
|
|
import { Type, Component } from '@angular/core';
|
2017-01-20 18:57:02 -05:00
|
|
|
import { Loading, LoadingController, ModalController, NavParams } from 'ionic-angular';
|
2017-01-18 17:51:06 -05:00
|
|
|
import { Storage } from '@ionic/storage';
|
2017-01-16 21:36:22 -05:00
|
|
|
|
2017-01-18 17:51:06 -05:00
|
|
|
import { StrongsModal } from '../../components/strongs-modal/strongs-modal';
|
|
|
|
|
|
|
|
import { BiblePassageResult, BibleService } from '../../services/bible-service';
|
|
|
|
import { StrongsResult, StrongsService } from '../../services/strongs-service';
|
|
|
|
import { WordService } from '../../services/word-service';
|
|
|
|
import { PagesService } from "../../services/pages-service";
|
|
|
|
|
|
|
|
import { UserProfile } from '../../libs/UserProfile';
|
|
|
|
import { Reference } from '../../libs/Reference';
|
|
|
|
import { MyApp } from '../../app/app.component';
|
2017-01-16 21:36:22 -05:00
|
|
|
|
|
|
|
@Component({
|
2017-01-18 17:51:06 -05:00
|
|
|
templateUrl: 'search.html',
|
|
|
|
providers: [BibleService, StrongsService, WordService]
|
2017-01-16 21:36:22 -05:00
|
|
|
})
|
|
|
|
export class SearchPage
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
searchQuery = '';
|
2017-01-16 21:36:22 -05:00
|
|
|
userProfile: UserProfile;
|
|
|
|
last: CardItem;
|
|
|
|
loader: Loading;
|
2017-01-18 17:51:06 -05:00
|
|
|
saved_results_title: string = "";
|
2017-01-16 21:36:22 -05:00
|
|
|
|
|
|
|
constructor(
|
2017-01-20 18:57:02 -05:00
|
|
|
private strongsService: StrongsService
|
|
|
|
,private bibleService: BibleService
|
|
|
|
,private wordService: WordService
|
|
|
|
,private pagesService: PagesService
|
|
|
|
,public loadingCtrl: LoadingController
|
|
|
|
,public modalCtrl: ModalController
|
|
|
|
,public local: Storage
|
|
|
|
,public params: NavParams
|
2017-01-18 17:51:06 -05:00
|
|
|
)
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
});
|
2017-01-18 17:51:06 -05:00
|
|
|
}
|
2017-01-16 21:36:22 -05:00
|
|
|
|
2017-01-18 17:51:06 -05:00
|
|
|
addPage()
|
|
|
|
{
|
|
|
|
let p = { queries: this.userProfile.user.items.slice(), title: this.saved_results_title };
|
|
|
|
this.userProfile.user.saved_pages.push(p);
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
this.pagesService.addPage(p);
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
initializeItems(u: UserProfile)
|
|
|
|
{
|
|
|
|
this.userProfile = u;
|
2017-01-18 17:51:06 -05:00
|
|
|
this.pagesService.initializePages(u.user.saved_pages);
|
|
|
|
if (this.params.data.queries !== undefined)
|
|
|
|
this.userProfile.user.items = this.params.data.queries.slice();
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
presentStrongsModal(strongs: StrongsResult)
|
|
|
|
{
|
2017-01-20 18:57:02 -05:00
|
|
|
let modal = this.modalCtrl.create(StrongsModal, { strongsid: strongs, onItemClicked: this });
|
2017-01-16 21:36:22 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
isError(t: string)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
return t === 'Error';
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
isPassage(t: string)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
return t === 'Passage';
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
isStrongs(t: string)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
return t === 'Strongs';
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
isWords(t: string)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
return t === 'Words';
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
this.loader = this.loadingCtrl.create({
|
2017-01-18 17:51:06 -05:00
|
|
|
content: 'Looking up query...'
|
2017-01-16 21:36:22 -05:00
|
|
|
});
|
|
|
|
this.loader.present().then(
|
|
|
|
() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
let qs = search.split(';');
|
2017-01-16 21:36:22 -05:00
|
|
|
for (let x in qs)
|
|
|
|
{
|
|
|
|
if (qs.hasOwnProperty(x))
|
|
|
|
{
|
|
|
|
let q = qs[x].trim();
|
2017-01-18 17:51:06 -05:00
|
|
|
if (q !== '')
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
|
|
|
// its a search term.
|
|
|
|
if (q.search(/[0-9]/i) === -1)
|
|
|
|
{
|
|
|
|
let result = this.wordService.getResult(q);
|
2017-01-18 17:51:06 -05:00
|
|
|
if (result.status === 0)
|
|
|
|
this.addItemToList({ data: result, type: 'Words', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
else
|
2017-01-18 17:51:06 -05:00
|
|
|
this.addItemToList({ data: result.msg, type: 'Error', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
dict = 'heb';
|
2017-01-16 21:36:22 -05:00
|
|
|
} else
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
dict = 'grk';
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
q = q.substring(1, q.length);
|
|
|
|
let result = this.strongsService.getResult(parseInt(q), dict);
|
2017-01-18 17:51:06 -05:00
|
|
|
if (result.status === -1)
|
|
|
|
this.addItemToList({ data: result.msg, type: 'Error', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.userProfile.user.strongs_modal)
|
|
|
|
this.presentStrongsModal(result);
|
|
|
|
else
|
2017-01-18 17:51:06 -05:00
|
|
|
this.addItemToList({ data: result, type: 'Strongs', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// its a verse reference.
|
2017-01-18 17:51:06 -05:00
|
|
|
if (q.trim() !== '')
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
|
|
|
let myref = new Reference(q.trim());
|
|
|
|
let r = this.bibleService.getResult(myref.Section);
|
|
|
|
r.ref = myref.toString();
|
2017-01-18 17:51:06 -05:00
|
|
|
if (r.status === 0)
|
|
|
|
this.addItemToList({ data: r, type: 'Passage', dict: r.testament === 'new' ? 'G' : 'H' });
|
2017-01-16 21:36:22 -05:00
|
|
|
else
|
2017-01-18 17:51:06 -05:00
|
|
|
this.addItemToList({ data: r.msg, type: 'Error', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.userProfile.user.clear_search_after_query)
|
2017-01-18 17:51:06 -05:00
|
|
|
$('.searchbar-input').val('');
|
2017-01-16 21:36:22 -05:00
|
|
|
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
}
|
|
|
|
catch (error)
|
|
|
|
{
|
2017-01-18 17:51:06 -05:00
|
|
|
this.addItemToList({ data: error, type: 'Error', dict: 'na' });
|
2017-01-16 21:36:22 -05:00
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
this.loader.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 17:51:06 -05:00
|
|
|
|
|
|
|
export type OpenData = { card: CardItem, qry: string }
|
|
|
|
|
|
|
|
export type CardItem = { data: any, type: string, dict: string }
|
|
|
|
|
|
|
|
class Item
|
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
data: any;
|
|
|
|
type: Type<any>;
|
|
|
|
dict: string;
|
|
|
|
}
|