jason.wall 83b33958c4 MAID: tighten up syntax per the linter, fix a couple of minor display issues
with verse picker, removed call to stemmer.js in index.html, updated
  to ng-template tag as template had been deprecated
2017-08-24 10:54:15 -04:00

162 lines
5.1 KiB
TypeScript

/// <reference path="../../typings/globals/jquery/index.d.ts" />
/// <reference path="../../typings/globals/mathjs/index.d.ts" />
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Section, Reference } from '../libs/Reference';
@Injectable()
export class BibleService
{
chapters: BiblePassage[];
result: BiblePassageResult;
count = 0;
$: any;
constructor(private http: Http)
{
}
getResultAsPromise(section: Section): Promise<BiblePassageResult>
{
return new Promise((resolve) => { resolve(this.getResult(section)); });
}
getResult(section: Section): BiblePassageResult
{
try
{
const self = this;
this.chapters = []; // the verses from the chapter.
this.result = {
cs: [],
testament: '',
ref: Reference.toString(section),
status: 0,
msg: ':)'
};
if (Number(section.start.chapter) > section.start.book.last_chapter)
{
self.result.status = -1;
self.result.msg = 'The requested chapter for ' + section.start.book.name + ' is out of range. Please pick a chapter between 1 and ' + section.start.book.last_chapter + '.';
return self.result;
}
if (Number(section.end.chapter) > section.end.book.last_chapter)
{
self.result.status = -1;
self.result.msg = 'The requested chapter for ' + section.end.book.name + ' is out of range. Please pick a chapter between 1 and ' + section.end.book.last_chapter + '.';
return self.result;
}
this.count = Number(section.end.chapter) - Number(section.start.chapter) + 1;
for (let i = Number(section.start.chapter); i <= Number(section.end.chapter); i++)
{
const url = 'data/bibles/kjv_strongs/' + section.start.book.book_number + '-' + i + '.json';
$.ajax({
async: false,
type: 'GET',
url: url,
dataType: 'json',
success(d: BiblePassage)
{
self.chapters.push(d);
},
error()
{
self.result.status = -1;
self.result.msg = 'Unable to retrieve bible passage ' + self.result.ref + '.';
}
});
}
if (self.result.status === -1)
return self.result;
for (let j = 0; j < this.chapters.length; j++)
{
const vss: BibleVerse[] = [];
let start: number;
let end;
// figure out the start verse.
if (j === 0)
{
if (section.start.verse.indexOf('*') !== -1) // you sometimes use this as a shortcut to the last verse
{
// replace the * with the last verse, then eval the expression.
section.start.verse = section.start.verse.replace('*', (this.chapters[j].vss.length).toString());
start = math.eval(section.start.verse);
// update the section and the ref.
section.start.verse = start.toString();
this.result.ref = Reference.toString(section);
}
else
start = parseInt(section.start.verse);
}
else
start = 1;
// figure out the end verse
if ((j + 1) === this.chapters.length)
end = section.end.verse;
else
end = '*';
// get the verses requested.
const tvs = this.chapters[j].vss.length;
if (end === '*' || parseInt(end) > tvs)
end = tvs;
// we're using c based indexes here, so the index is 1 less than the verse #.
for (let i = start; i <= end; i++)
vss.push(this.chapters[j].vss[i - 1]);
this.result.cs.push({
ch: this.chapters[j].ch,
vss: vss
});
}
if (section.start.book.book_number >= 40)
this.result.testament = 'new';
else
this.result.testament = 'old';
return this.result;
}
catch (error)
{
console.log(error);
}
return null;
}
}
export type BiblePassageResult = {
cs: BiblePassage[],
testament: string,
ref: string,
status: number,
msg: string,
}
type BiblePassage = {
ch: number,
vss: BibleVerse[],
}
type BibleVerse = {
v: number,
w: [
{
t: string, s: string,
}
],
}