2018-01-06 01:30:06 -05:00
/// <reference path="../../typings/globals/jquery/index.d.ts" />
2017-08-07 16:16:15 -04:00
/// <reference path="../../typings/globals/mathjs/index.d.ts" />
import { Injectable } from '@angular/core' ;
2018-01-06 01:30:06 -05:00
import { Section , Reference } from '../libs/Reference' ;
2016-05-18 17:15:23 -04:00
@Injectable ( )
2016-09-12 13:12:25 -04:00
export class BibleService
{
chapters : BiblePassage [ ] ;
result : BiblePassageResult ;
2018-01-06 01:30:06 -05:00
static paragraphs : HashTable < Paragraph > ;
2017-08-24 10:54:15 -04:00
count = 0 ;
2017-01-05 19:54:18 -05:00
$ : any ;
2016-05-18 17:15:23 -04:00
2018-04-19 11:05:23 -04:00
constructor ( )
2016-09-12 13:12:25 -04:00
{
2018-01-06 01:30:06 -05:00
this . getParagraphMarkersAsPromise ( )
}
getParagraphMarkersAsPromise ( ) : Promise < HashTable < Paragraph > >
{
return new Promise ( ( resolve ) = > { resolve ( this . getParagraphMarkers ( ) ) ; } ) ;
}
getParagraphMarkers ( ) : HashTable < Paragraph >
{
try
{
if ( BibleService . paragraphs !== undefined && '1;1;1' in BibleService . paragraphs )
return BibleService . paragraphs ;
const self = this ;
const url = 'data/bibles/paras.json' ;
$ . ajax ( {
async : false ,
type : 'GET' ,
url : url ,
dataType : 'json' ,
success ( d : HashTable < Paragraph > )
{
BibleService . paragraphs = d ;
} ,
error ( )
{
console . log ( 'Unable to retrieve paragraphs.' ) ;
}
} ) ;
return BibleService . paragraphs ;
}
catch ( error )
{
console . log ( error ) ;
}
return null ;
2016-09-12 13:12:25 -04:00
}
2017-01-18 17:51:06 -05:00
2017-01-26 21:14:55 -05:00
getResultAsPromise ( section : Section ) : Promise < BiblePassageResult >
2017-01-24 16:43:58 -05:00
{
2017-08-23 17:53:03 -04:00
return new Promise ( ( resolve ) = > { resolve ( this . getResult ( section ) ) ; } ) ;
2017-01-24 16:43:58 -05:00
}
2016-12-27 17:23:20 -05:00
getResult ( section : Section ) : BiblePassageResult
2016-09-12 13:12:25 -04:00
{
try
{
2017-08-24 10:54:15 -04:00
const self = this ;
2018-01-06 01:30:06 -05:00
this . chapters = [ ] ; // the verses from the chapter.
this . result = {
cs : [ ] ,
testament : '' ,
ref : Reference.toString ( section ) ,
status : 0 ,
msg : ':)'
} ;
2017-01-26 22:26:48 -05:00
2017-08-23 17:53:03 -04:00
if ( Number ( section . start . chapter ) > section . start . book . last_chapter )
2017-01-26 22:26:48 -05:00
{
self . result . status = - 1 ;
2017-08-23 17:53:03 -04:00
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 + '.' ;
2017-01-26 22:26:48 -05:00
return self . result ;
}
2017-08-23 17:53:03 -04:00
if ( Number ( section . end . chapter ) > section . end . book . last_chapter )
2017-01-26 22:26:48 -05:00
{
self . result . status = - 1 ;
2017-08-23 17:53:03 -04:00
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 + '.' ;
2017-01-26 22:26:48 -05:00
return self . result ;
}
2016-09-12 13:12:25 -04:00
this . count = Number ( section . end . chapter ) - Number ( section . start . chapter ) + 1 ;
2016-05-18 17:15:23 -04:00
2016-12-27 21:10:00 -05:00
for ( let i = Number ( section . start . chapter ) ; i <= Number ( section . end . chapter ) ; i ++ )
{
2017-08-23 17:53:03 -04:00
const url = 'data/bibles/kjv_strongs/' + section . start . book . book_number + '-' + i + '.json' ;
2017-08-07 16:16:15 -04:00
2017-01-05 19:54:18 -05:00
$ . ajax ( {
2016-05-18 17:15:23 -04:00
async : false ,
2017-08-07 16:16:15 -04:00
type : 'GET' ,
2016-05-18 17:15:23 -04:00
url : url ,
2017-08-07 16:16:15 -04:00
dataType : 'json' ,
2018-01-06 01:30:06 -05:00
success ( d : BiblePassage )
{
2016-05-18 17:15:23 -04:00
self . chapters . push ( d ) ;
} ,
2017-08-24 10:54:15 -04:00
error ( )
2016-09-12 13:12:25 -04:00
{
2017-01-03 23:51:25 -05:00
self . result . status = - 1 ;
2017-08-07 16:16:15 -04:00
self . result . msg = 'Unable to retrieve bible passage ' + self . result . ref + '.' ;
2016-05-18 17:15:23 -04:00
}
} ) ;
}
2017-08-07 16:16:15 -04:00
if ( self . result . status === - 1 )
2017-01-03 23:51:25 -05:00
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 ;
2016-05-18 17:15:23 -04:00
let end ;
// figure out the start verse.
2016-09-12 18:13:56 -04:00
if ( j === 0 )
2017-08-07 16:16:15 -04:00
{
if ( section . start . verse . indexOf ( '*' ) !== - 1 ) // you sometimes use this as a shortcut to the last verse
2018-01-06 01:30:06 -05:00
{
2017-08-07 16:16:15 -04:00
// replace the * with the last verse, then eval the expression.
section . start . verse = section . start . verse . replace ( '*' , ( this . chapters [ j ] . vss . length ) . toString ( ) ) ;
2018-01-06 01:30:06 -05:00
start = math . eval ( section . start . verse ) ;
2017-08-07 16:16:15 -04:00
// update the section and the ref.
section . start . verse = start . toString ( ) ;
this . result . ref = Reference . toString ( section ) ;
}
else
start = parseInt ( section . start . verse ) ;
}
2017-01-18 11:54:59 -05:00
else
2016-05-18 17:15:23 -04:00
start = 1 ;
// figure out the end verse
2016-09-12 18:13:56 -04:00
if ( ( j + 1 ) === this . chapters . length )
2016-05-18 17:15:23 -04:00
end = section . end . verse ;
2017-01-18 11:54:59 -05:00
else
2017-08-07 16:16:15 -04:00
end = '*' ;
2016-05-18 17:15:23 -04:00
// get the verses requested.
2016-09-12 18:13:56 -04:00
const tvs = this . chapters [ j ] . vss . length ;
2017-08-07 16:16:15 -04:00
if ( end === '*' || parseInt ( end ) > tvs )
2016-05-18 17:15:23 -04:00
end = tvs ;
2017-01-18 11:54:59 -05:00
// we're using c based indexes here, so the index is 1 less than the verse #.
2016-09-12 13:12:25 -04:00
for ( let i = start ; i <= end ; i ++ )
2016-05-18 17:15:23 -04:00
vss . push ( this . chapters [ j ] . vss [ i - 1 ] ) ;
2018-01-06 01:30:06 -05:00
this . result . cs . push ( {
ch : this.chapters [ j ] . ch ,
vss : vss
} ) ;
2016-05-18 17:15:23 -04:00
}
2018-01-06 01:30:06 -05:00
2016-05-18 17:15:23 -04:00
2017-08-23 17:53:03 -04:00
if ( section . start . book . book_number >= 40 )
2017-08-07 16:16:15 -04:00
this . result . testament = 'new' ;
2017-01-18 11:54:59 -05:00
else
2017-08-07 16:16:15 -04:00
this . result . testament = 'old' ;
2017-01-18 17:51:06 -05:00
2016-05-18 17:15:23 -04:00
return this . result ;
2017-01-18 17:51:06 -05:00
}
2017-01-18 11:54:59 -05:00
catch ( error )
2016-09-12 13:12:25 -04:00
{
2016-12-27 21:10:00 -05:00
console . log ( error ) ;
2016-05-18 17:15:23 -04:00
}
return null ;
}
2017-01-18 17:51:06 -05:00
}
export type BiblePassageResult = {
cs : BiblePassage [ ] ,
testament : string ,
ref : string ,
status : number ,
2017-08-23 18:02:25 -04:00
msg : string ,
2017-01-18 17:51:06 -05:00
}
2018-01-06 01:30:06 -05:00
export type BiblePassage = {
2017-01-18 17:51:06 -05:00
ch : number ,
2017-08-07 16:16:15 -04:00
vss : BibleVerse [ ] ,
2017-01-18 17:51:06 -05:00
}
2018-01-06 01:30:06 -05:00
export type BibleVerse = {
2017-01-18 17:51:06 -05:00
v : number ,
w : [
{
2017-08-07 16:16:15 -04:00
t : string , s : string ,
2017-01-18 17:51:06 -05:00
}
2017-08-07 16:16:15 -04:00
] ,
2018-01-06 01:30:06 -05:00
}
export type Paragraph = {
h : string ,
p : number
}
export interface HashTable < T >
{
[ key : string ] : T ;
}