2017-01-24 17:47:26 -05:00
|
|
|
import { Type, Component, OnInit } from '@angular/core';
|
2017-01-21 00:15:00 -05:00
|
|
|
import { Loading, LoadingController, ModalController, NavParams, AlertController, MenuController } 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 { PagesService } from "../../services/pages-service";
|
|
|
|
|
|
|
|
import { UserProfile } from '../../libs/UserProfile';
|
|
|
|
import { Reference } from '../../libs/Reference';
|
2017-01-16 21:36:22 -05:00
|
|
|
|
|
|
|
@Component({
|
2017-01-24 16:43:58 -05:00
|
|
|
templateUrl: 'search.html'
|
2017-01-16 21:36:22 -05:00
|
|
|
})
|
2017-01-24 17:47:26 -05:00
|
|
|
export class SearchPage implements OnInit
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
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-21 00:15:00 -05:00
|
|
|
title: string;
|
2017-01-16 21:36:22 -05:00
|
|
|
|
|
|
|
constructor(
|
2017-01-24 16:43:58 -05:00
|
|
|
private pagesService: PagesService
|
2017-01-21 00:15:00 -05:00
|
|
|
, private alertCtrl: AlertController
|
|
|
|
, private menu: MenuController
|
|
|
|
, 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());
|
2017-01-24 17:47:26 -05:00
|
|
|
}
|
2017-01-16 21:36:22 -05:00
|
|
|
|
2017-01-24 17:47:26 -05:00
|
|
|
ngOnInit(): void
|
|
|
|
{
|
2017-01-16 21:36:22 -05:00
|
|
|
// 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);
|
|
|
|
|
2017-01-26 01:16:26 -05:00
|
|
|
this.loader = this.loadingCtrl.create({ content: 'Loading Page...' });
|
|
|
|
this.loader.present().then(
|
|
|
|
() =>
|
|
|
|
{
|
|
|
|
this.userProfile.update(t, this.local);
|
2017-01-16 21:36:22 -05:00
|
|
|
|
2017-01-26 01:16:26 -05:00
|
|
|
this.initializeItems(this.userProfile);
|
|
|
|
|
|
|
|
this.loader.dismiss();
|
|
|
|
});
|
2017-01-16 21:36:22 -05:00
|
|
|
}).catch(error =>
|
|
|
|
{
|
|
|
|
console.log(error);
|
2017-01-24 17:47:26 -05:00
|
|
|
});
|
|
|
|
}
|
2017-01-26 01:16:26 -05:00
|
|
|
initializeItems(u: UserProfile)
|
|
|
|
{
|
|
|
|
// migrate old way of storing card items to the new.
|
|
|
|
let has_migrated = false;
|
|
|
|
for (let i in u.user.items)
|
|
|
|
{
|
|
|
|
let ci = u.user.items[i];
|
|
|
|
if (ci["data"] !== undefined)
|
|
|
|
{
|
|
|
|
if (ci["data"].qry !== undefined)
|
|
|
|
u.user.items[i] = { qry: ci["data"].qry, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].ref !== undefined)
|
|
|
|
u.user.items[i] = { qry: ci["data"].ref, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].word !== undefined)
|
|
|
|
u.user.items[i] = { qry: ci["data"].word, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].sn !== undefined)
|
|
|
|
u.user.items[i] = { qry: ci["data"].sn, dict: ci["prefix"] === 'G' ? 'grk' : 'heb', type: ci.type };
|
|
|
|
|
|
|
|
has_migrated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let pg of u.user.saved_pages)
|
|
|
|
{
|
|
|
|
for (let i in pg.queries)
|
|
|
|
{
|
|
|
|
let ci = pg.queries[i];
|
|
|
|
if (ci["data"] !== undefined)
|
|
|
|
{
|
|
|
|
if (ci["data"].qry !== undefined)
|
|
|
|
pg.queries[i] = { qry: ci["data"].qry, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].ref !== undefined)
|
|
|
|
pg.queries[i] = { qry: ci["data"].ref, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].word !== undefined)
|
|
|
|
pg.queries[i] = { qry: ci["data"].word, dict: ci.dict, type: ci.type };
|
|
|
|
else if (ci["data"].sn !== undefined)
|
|
|
|
pg.queries[i] = { qry: ci["data"].sn, dict: ci["prefix"] === 'G' ? 'grk' : 'heb', type: ci.type };
|
|
|
|
|
|
|
|
has_migrated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// initialize the pages.
|
|
|
|
this.pagesService.initializePages(u.user.saved_pages);
|
|
|
|
if (this.params.data.queries !== undefined)
|
|
|
|
this.userProfile.user.items = this.params.data.queries.slice();
|
|
|
|
if (this.params.data.title === undefined)
|
|
|
|
this.title = "Search";
|
|
|
|
else
|
|
|
|
this.title = this.params.data.title;
|
|
|
|
|
|
|
|
if (has_migrated)
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
}
|
2017-01-21 00:15:00 -05:00
|
|
|
|
|
|
|
actionsMenu()
|
|
|
|
{
|
|
|
|
this.menu.open('actions');
|
|
|
|
}
|
2017-01-16 21:36:22 -05:00
|
|
|
|
2017-01-18 17:51:06 -05:00
|
|
|
addPage()
|
|
|
|
{
|
2017-01-21 00:15:00 -05:00
|
|
|
let alert = this.alertCtrl.create({
|
|
|
|
title: 'Save Search as Page',
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
name: 'title',
|
|
|
|
placeholder: 'Page Title'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
text: 'Cancel',
|
|
|
|
role: 'cancel',
|
|
|
|
handler: data =>
|
|
|
|
{
|
|
|
|
console.log('Cancel clicked');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'Save',
|
|
|
|
handler: data =>
|
|
|
|
{
|
|
|
|
let p = { queries: this.userProfile.user.items.slice(), title: data.title };
|
|
|
|
this.userProfile.user.saved_pages.push(p);
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
this.pagesService.addPage(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
alert.present();
|
|
|
|
}
|
|
|
|
updatePage()
|
|
|
|
{
|
2017-01-24 16:43:58 -05:00
|
|
|
let page = this.userProfile.user.saved_pages.find(
|
|
|
|
i =>
|
2017-01-21 00:15:00 -05:00
|
|
|
i.title == this.params.data.title
|
2017-01-24 16:43:58 -05:00
|
|
|
);
|
2017-01-21 00:15:00 -05:00
|
|
|
page.queries = this.userProfile.user.items.slice();
|
2017-01-18 17:51:06 -05:00
|
|
|
this.userProfile.save(this.local);
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setQuery(searchbar)
|
|
|
|
{
|
|
|
|
this.searchQuery = searchbar.target.value;
|
|
|
|
}
|
|
|
|
getQuery(searchbar)
|
|
|
|
{
|
2017-01-26 01:35:41 -05:00
|
|
|
this.updateUIwithItems(this.searchQuery, true);
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-26 01:16:26 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-01-24 16:43:58 -05:00
|
|
|
addItemToList(item: CardItem)
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
|
|
|
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;
|
2017-01-26 01:35:41 -05:00
|
|
|
this.updateUIwithItems(data.qry, data.from_search_bar);
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 01:35:41 -05:00
|
|
|
getItemList(search: string): Promise<CardItem[]>
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
{
|
|
|
|
let list: CardItem[] = [];
|
|
|
|
|
|
|
|
try
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
let qs = search.split(';');
|
|
|
|
for (let x in qs)
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
if (qs.hasOwnProperty(x))
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
let q = qs[x].trim();
|
|
|
|
if (q !== '')
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
// its a search term.
|
|
|
|
if (q.search(/[0-9]/i) === -1)
|
|
|
|
list.push({ qry: q, dict: 'na', type: 'Words' });
|
|
|
|
else if (q.search(/(H|G)[0-9]/i) !== -1)
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
// its a strongs lookup
|
|
|
|
let dict = q.substring(0, 1);
|
2017-01-24 16:43:58 -05:00
|
|
|
|
2017-01-26 01:16:26 -05:00
|
|
|
if (dict.search(/h/i) !== -1)
|
|
|
|
dict = 'heb';
|
2017-01-16 21:36:22 -05:00
|
|
|
else
|
2017-01-26 01:16:26 -05:00
|
|
|
dict = 'grk';
|
|
|
|
|
|
|
|
q = q.substring(1, q.length);
|
|
|
|
list.push({ qry: q, dict: dict, type: 'Strongs' });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// its a verse reference.
|
|
|
|
if (q.trim() !== '')
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
let myref = new Reference(q.trim());
|
|
|
|
list.push({ qry: myref.toString(), dict: myref.Section.start.book > 39 ? 'G' : 'H', type: 'Passage' });
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-26 01:16:26 -05:00
|
|
|
if (this.userProfile.user.clear_search_after_query)
|
|
|
|
$('.searchbar-input').val('');
|
|
|
|
|
|
|
|
this.userProfile.save(this.local);
|
|
|
|
}
|
|
|
|
catch (error)
|
|
|
|
{
|
|
|
|
list.push({ qry: error, type: 'Error', dict: 'na' });
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(list);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-26 01:35:41 -05:00
|
|
|
updateUIwithItems(search: string, from_search_bar: boolean)
|
2017-01-26 01:16:26 -05:00
|
|
|
{
|
2017-01-26 01:35:41 -05:00
|
|
|
this.getItemList(search).then(lst =>
|
2017-01-26 01:16:26 -05:00
|
|
|
{
|
|
|
|
this.loader = this.loadingCtrl.create({ content: 'Looking up Query...' });
|
|
|
|
this.loader.present().then(
|
|
|
|
() =>
|
2017-01-16 21:36:22 -05:00
|
|
|
{
|
2017-01-26 01:16:26 -05:00
|
|
|
for (let item of lst)
|
|
|
|
{
|
|
|
|
if (item.type == "Strongs" && this.userProfile.user.strongs_modal && !from_search_bar)
|
|
|
|
{
|
|
|
|
let modal = this.modalCtrl.create(StrongsModal, { sn: parseInt(item.qry), dict: item.dict, onItemClicked: this });
|
|
|
|
modal.present();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.addItemToList(item);
|
|
|
|
}
|
2017-01-16 21:36:22 -05:00
|
|
|
this.loader.dismiss();
|
|
|
|
}
|
2017-01-26 01:16:26 -05:00
|
|
|
);
|
|
|
|
});
|
2017-01-16 21:36:22 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 17:51:06 -05:00
|
|
|
|
2017-01-26 01:16:26 -05:00
|
|
|
export type OpenData = { card: CardItem, qry: string, from_search_bar: boolean }
|
2017-01-18 17:51:06 -05:00
|
|
|
|
2017-01-24 16:43:58 -05:00
|
|
|
export type CardItem = { qry: string, type: string, dict: string }
|
2017-01-18 17:51:06 -05:00
|
|
|
|
|
|
|
class Item
|
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
data: any;
|
|
|
|
type: Type<any>;
|
|
|
|
dict: string;
|
|
|
|
}
|