2017-08-23 17:53:03 -04:00

197 lines
6.0 KiB
TypeScript

/// <reference path="../../typings/globals/jquery/index.d.ts" />
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class StrongsService
{
result: StrongsResult;
count = 0;
constructor(private http: Http)
{
}
getResultAsPromise(sn: number, dict: string): Promise<StrongsResult>
{
return new Promise((resolve, reject) => { resolve(this.getResult(sn, dict)); });
}
getResult(sn: number, dict: string): StrongsResult
{
const self = this;
this.result = {
prefix: '',
sn: -1,
strongs: [],
def: null,
rmac: null,
crossrefs: null,
rmaccode: '',
status: 0,
msg: ':)'
};
let url = dict + Math.ceil(sn / 100) + '.json';
if (dict === 'grk')
{
self.result.prefix = 'G';
if (sn > 5624 || sn < 1)
{
self.result.status = -1;
self.result.msg = "Strong's Number G" + sn + " is out of range. Strong's numbers range from 1 - 5624 in the New Testament.";
}
}
else
{
self.result.prefix = 'H';
if (sn > 8674 || sn < 1)
{
self.result.status = -1;
self.result.msg = "Strong's Number H" + sn + " is out of range. Strong's numbers range from 1 - 8674 in the Old Testament.";
}
}
this.result.sn = sn;
if (self.result.status === -1)
return self.result;
$.ajax({
async: false,
type: 'GET',
url: 'data/strongs/' + url,
dataType: 'json',
success(d: StrongsDefinition[], t, x)
{
self.result.strongs = d;
},
error(request, status, error)
{
console.log(error);
self.result.status = -1;
self.result.msg = "Unable to retrieve Strong's Data for " + self.result.prefix + self.result.sn;
}
});
if (self.result.status === -1)
return self.result;
self.result.def = self.result.strongs.find(el => (el.i === this.result.prefix + this.result.sn));
self.result.strongs = [];
$.ajax({
async: false,
type: 'GET',
url: 'data/strongscr/cr' + url,
dataType: 'json',
success(d: StrongsCrossReference[], t, x)
{
for (let cr of d)
{
if (cr.id.toUpperCase() === self.result.prefix + self.result.sn)
{
self.result.crossrefs = cr;
break;
}
}
},
error(request, status, error)
{
console.log(error);
self.result.status = -1;
self.result.msg = "Unable to retrieve Strong's Cross References for " + self.result.prefix + self.result.sn;
}
});
if (self.result.status === -1)
return self.result;
if (dict === 'grk')
{
url = 'data/rmac/rs' + (Math.ceil(sn / 1000)) + '.json';
let rmac_cross_references: RMACCrossReference[];
// rmac is a two get process.
$.ajax({
async: false,
type: 'GET',
url: url,
dataType: 'json',
success(d: RMACCrossReference[], t, x)
{
rmac_cross_references = d;
},
error(request, status, error)
{
console.log(error);
}
});
// deal with RMAC
let tmp = $.grep<RMACCrossReference>(rmac_cross_references, (el, i) => { return el.i === sn + ''; });
if (tmp.length === 0)
return this.result;
this.result.rmaccode = tmp[0].r;
if (this.result.rmaccode != undefined)
{
url = `data/rmac/r-${this.result.rmaccode.substring(0, 1)}.json`;
$.ajax({
async: false,
type: 'GET',
url: url,
dataType: 'json',
success(d: RMACDefinition[], t, x)
{
for (let rmac of d)
{
if (rmac.id.toLowerCase() === self.result.rmaccode)
{
self.result.rmac = rmac;
break;
}
}
},
error(request, status, error)
{
console.log(error);
}
});
}
}
return this.result;
}
}
export type StrongsResult =
{
prefix: string,
sn: number,
strongs: StrongsDefinition[],
def: StrongsDefinition,
rmac: RMACDefinition,
crossrefs: StrongsCrossReference,
rmaccode: string,
status: number,
msg: string,
};
type StrongsDefinition = { n: number, i: string, tr: string, de: StrongsDefinitionPart[], lemma: string, p: string }
type StrongsDefinitionPart = { sn: string, w: string }
type StrongsCrossReference =
{
id: string, // strongs id H1|G1
t: string, // strongs testament grk|heb
d: string, // strongs word/data Aaron {ah-ar-ohn'}
ss: [
{
w: string,
rs: [
{ r: string }
],
}
],
}
type RMACDefinition = { id: string, d: string[] }
type RMACCrossReference = { i: string, r: string }