115 lines
3.5 KiB
TypeScript
Raw Normal View History

2016-09-12 13:12:25 -04:00
/// <reference path="../typings/browser/ambient/jquery/index.d.ts" />
/// <reference path="types.ts" />
2016-09-12 18:13:56 -04:00
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { Reference } from "./Reference";
@Injectable()
2016-09-12 13:12:25 -04:00
export class BibleService
{
chapters: BiblePassage[];
result: BiblePassageResult;
count: number = 0;
$: any;
2016-09-12 13:12:25 -04:00
constructor(private http: Http)
{
}
getResult(section: Section): BiblePassageResult
2016-09-12 13:12:25 -04:00
{
try
{
var self = this;
this.chapters = []; // the verses from the chapter.
this.result = {
cs: [],
testament: "",
ref: Reference.toString(section),
status: 0,
msg: ":)"
};
2016-09-12 13:12:25 -04:00
this.count = Number(section.end.chapter) - Number(section.start.chapter) + 1;
for (let i = Number(section.start.chapter); i <= Number(section.end.chapter); i++)
{
2016-09-12 18:13:56 -04:00
const url = "data/bibles/kjv_strongs/" + section.start.book + "-" + i + ".json";
$.ajax({
async: false,
type: "GET",
url: url,
dataType: "json",
2016-09-12 13:12:25 -04:00
success: function (d: BiblePassage, t, x)
{
self.chapters.push(d);
},
2016-09-12 13:12:25 -04:00
error: function (request, status, error)
{
self.result.status = -1;
self.result.msg = "Unable to retrieve bible passage " + self.result.ref + ".";
}
});
}
if (self.result.status == -1)
return self.result;
2016-09-12 13:12:25 -04:00
for (let j = 0; j < this.chapters.length; j++)
{
2016-09-12 18:13:56 -04:00
const vss: BibleVerse[] = [];
let start: number;
let end;
// figure out the start verse.
2016-09-12 18:13:56 -04:00
if (j === 0)
2016-09-12 13:12:25 -04:00
{
2016-09-12 18:13:56 -04:00
start = parseInt(section.start.verse);
2016-09-12 13:12:25 -04:00
} else
{
start = 1;
}
// figure out the end verse
2016-09-12 18:13:56 -04:00
if ((j + 1) === this.chapters.length)
2016-09-12 13:12:25 -04:00
{
end = section.end.verse;
2016-09-12 13:12:25 -04:00
} else
{
end = "*";
}
// get the verses requested.
2016-09-12 18:13:56 -04:00
const tvs = this.chapters[j].vss.length;
if (end == "*" || parseInt(end) > tvs)
2016-09-12 13:12:25 -04:00
{
end = tvs;
}
2016-09-12 13:12:25 -04:00
for (let i = start; i <= end; i++)
{
// we're using c based indexes here, so the index is 1 less than the verse #.
vss.push(this.chapters[j].vss[i - 1]);
}
this.result.cs.push({
"ch": this.chapters[j].ch,
"vss": vss
});
}
2016-09-12 13:12:25 -04:00
if (section.start.book >= 40)
{
this.result.testament = "new";
2016-09-12 13:12:25 -04:00
} else
{
this.result.testament = "old";
}
return this.result;
} catch (error)
2016-09-12 13:12:25 -04:00
{
console.log(error);
}
return null;
}
}