walljm e444a5fddf FEATURE: Move Help and Settings pages to modals
* fixed ton of bugs... apparently i didn't push up the work i did on the mac... and i don't have that anymore.
2019-01-02 15:18:49 -05:00

227 lines
7.6 KiB
TypeScript

import { Component, EventEmitter, Output, Input, OnInit, ElementRef } from '@angular/core';
import { OpenData, CardItem } from '../../pages/search/search';
import { BiblePassageResult, BibleService, BiblePassage, BibleVerse, Paragraph } from '../../services/bible-service';
import { Reference } from '../../libs/Reference';
import { ProfileService } from '../../services/profile-service';
import { ActionSheetController, AlertController } from 'ionic-angular';
import { PagesService } from '../../services/pages-service';
import { cardContextMenu } from '../../libs/Common';
@Component({
selector: 'passage',
templateUrl: 'passage.html',
providers: [BibleService]
})
export class Passage implements OnInit {
@Output()
onItemClicked = new EventEmitter<OpenData>();
@Output()
onClose = new EventEmitter<CardItem>();
@Input()
cardItem: CardItem;
data: BiblePassageResult;
withParas: BibleParaPassage[];
ref: Reference;
constructor(
public profileService: ProfileService,
private _elementRef: ElementRef,
private _bibleService: BibleService,
private _actionSheet: ActionSheetController,
private _pagesSvc: PagesService,
private _alertCtrl: AlertController
) {
}
ngOnInit(): void {
this.ref = new Reference(this.cardItem.qry);
this._bibleService.getResultAsPromise(this.ref.Section).then(data => {
this.setData(data);
});
}
setData(data: BiblePassageResult) {
this.data = data;
this.withParas = this.getParaPassages(data.cs);
}
contextMenu() {
cardContextMenu(this.profileService, this._actionSheet, this._pagesSvc, this._alertCtrl, this.cardItem);
}
close(ev) {
let translate = 'translate3d(110%, 0, 0)';
if (ev != null && ev.direction === 2) {
translate = 'translate3d(-110%, 0, 0)';
}
let d = 250;
this._elementRef.nativeElement.parentElement.animate({
transform: ['none', translate]
}, {
fill: 'forwards',
duration: d,
iterations: 1,
easing: 'ease-in-out'
});
setTimeout(() => {
this.onClose.emit(this.cardItem);
}, d);
}
next() {
const last_verse_for_end = this.ref.Section.end.book.chapters[parseInt(this.ref.Section.end.chapter)].toString();
if (this.ref.Section.end.verse !== '*' && this.ref.Section.end.verse !== last_verse_for_end)
this.ref.Section.end.chapter = this.ref.Section.end.chapter;
else
this.ref.Section.end.chapter = (parseInt(this.ref.Section.end.chapter) + 1).toString();
this.ref.Section.start.chapter = this.ref.Section.end.chapter;
this.ref.Section.start.verse = '1';
this.ref.Section.end.verse = '*';
this._bibleService.getResultAsPromise(this.ref.Section).then(data => {
this.setData(data);
this.cardItem.qry = data.ref;
this.ref = new Reference(data.ref);
});
}
prev() {
if (this.ref.Section.start.verse !== '1')
this.ref.Section.start.chapter = this.ref.Section.start.chapter;
else
this.ref.Section.start.chapter = (parseInt(this.ref.Section.start.chapter) - 1).toString();
this.ref.Section.end.chapter = this.ref.Section.start.chapter;
this.ref.Section.start.verse = '1';
this.ref.Section.end.verse = '*';
this._bibleService.getResultAsPromise(this.ref.Section).then(data => {
this.setData(data);
this.cardItem.qry = data.ref;
this.ref = new Reference(data.ref);
});
}
expand() {
const last_verse_for_end = this.ref.Section.end.book.chapters[parseInt(this.ref.Section.end.chapter)];
// if your verse is at the beginning, to go the prev chapter and add 3 verses from that
if (parseInt(this.ref.Section.start.verse) < 4) {
this.ref.Section.start.chapter = (parseInt(this.ref.Section.start.chapter) - 1).toString();
this.ref.Section.start.verse = '*-' + (3 - parseInt(this.ref.Section.start.verse));
if (this.ref.Section.start.chapter === '0') {
this.ref.Section.start.chapter = '1';
this.ref.Section.start.verse = '1';
}
}
else // or go back 3 verses
this.ref.Section.start.verse = (parseInt(this.ref.Section.start.verse) - 3).toString();
// if your verse is at the end, go to the next chapter
if (this.ref.Section.end.verse === '*' || parseInt(this.ref.Section.end.verse) + 3 > last_verse_for_end) {
this.ref.Section.end.chapter = (parseInt(this.ref.Section.end.chapter) + 1).toString();
if (this.ref.Section.end.verse === '*')
this.ref.Section.end.verse = '3';
else
this.ref.Section.end.verse = (parseInt(this.ref.Section.end.verse) + 3 - last_verse_for_end).toString();
if (this.ref.Section.end.chapter === (this.ref.Section.end.book.last_chapter + 1).toString()) {
this.ref.Section.end.chapter = this.ref.Section.end.book.last_chapter.toString();
this.ref.Section.end.verse = last_verse_for_end.toString();
}
}
else // or add 3 verses
this.ref.Section.end.verse = (parseInt(this.ref.Section.end.verse) + 3).toString();
if (this.ref.Section.start.verse === '0')
this.ref.Section.start.verse = '1';
this._bibleService.getResultAsPromise(this.ref.Section).then(data => {
this.setData(data);
this.cardItem.qry = data.ref;
this.ref = new Reference(data.ref);
});
}
openStrongs(strongs: string) {
this.onItemClicked.emit({ card: this.cardItem, qry: this.cardItem.dict + strongs, from_search_bar: false });
}
isPunct(c: string) {
return new RegExp('^[\.\,\;\:\?\!]$').test(c);
}
hasHeader(p: Paragraph) {
if (p === undefined)
return false;
return p.h.length > 0;
}
isPara(vs: BibleVerse) {
return this.getRefKey(this.ref.Section.start.chapter, vs) in BibleService.paragraphs;
}
getRefKey(ch: string, vs: BibleVerse) {
return this.ref.Section.start.book.book_number + ';' + ch + ';' + vs.v;
}
getParaPassages(chapters: BiblePassage[]) {
let passages: BibleParaPassage[] = [];
for (let ch of chapters) {
let para = {
ch: ch.ch,
paras: this.getParas(ch)
};
passages.push(para);
}
return passages;
}
getParas(ch: BiblePassage) {
// group the verses into paragraphs.
// create an initial paragraph to hold verses that might come before a paragraph.
let para: BiblePara = { p: { h: '', p: 0 }, vss: [] };
let paras: BiblePara[] = [];
// for each verse in the chapter, break them into paragraphs.
for (let v of ch.vss) {
if (this.isPara(v)) {
paras.push(para);
para = { p: BibleService.paragraphs[this.getRefKey(ch.ch.toString(), v)], vss: [v] };
if (para.p === undefined)
para.p = { h: '', p: 0 }; // just in case you can't find a paragraph.
}
else {
para.vss.push(v);
}
}
// add the final paragraph if it has verses.
if (para.vss.length > 0)
paras.push(para);
return paras;
}
}
type BiblePara = {
p: Paragraph,
vss: BibleVerse[],
}
type BibleParaPassage = {
ch: number;
paras: BiblePara[];
}