* Use the same brace style everywhere.
* Use semicolons everywhere.
* Add myself to the authorship for bible ref parsing.
* Don't use try catch blocks when exceptions are impossible.
* Add some returns for completeness sake when the functions return values
  elsewhere.
This commit is contained in:
Jeremy Wall (zaphar) 2013-04-10 19:39:10 -05:00
parent 6296781f95
commit 0741b50b4c
2 changed files with 446 additions and 610 deletions

View File

@ -1,7 +1,7 @@
//
// This code was written by Jason Wall. Feel free to use, and if you can, include a link back to www.walljm.com
// This code was written by Jeremy and Jason Wall.
// Feel free to use, and if you can, include a link back to www.walljm.com
// Jason@walljm.com // www.walljm.com
//
// Jeremy@marzhillstudios.com // jeremy.marzhillstudios.com
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");

View File

@ -1,60 +1,46 @@
function SortNumeric(x, y)
{
return x - y
function SortNumeric(x, y) {
return x - y;
}
String.prototype.trim = function()
{
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function()
{
};
String.prototype.ltrim = function() {
return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function()
{
};
String.prototype.rtrim = function() {
return this.replace(/\s+$/, "");
}
function Traverse(node, testament)
{
try
{
};
function Traverse(node, testament) {
try {
var treeText = "";
if (node != null)
{
if (node.hasChildNodes())
{
if (node.nodeName == "s")
{
if (node != null) {
if (node.hasChildNodes()) {
if (node.nodeName == "s") {
// you need to test if this is the OT or NT and set the attribute accordingly.
var t = "";
if (testament == "old")
{
if (testament == "old") {
t = "H";
}
if (testament == "new")
{
if (testament == "new") {
t = "G";
}
treeText += "<a href='javascript:void();' class='hiddenlink' title='Strongs #: " + node.getAttribute("n") + "'><span class='searchvalue' style='display:none'>" + t + node.getAttribute("n") + "</span>" + Traverse(node.childNodes.item(0), testament) + "</a>";
} else
{
} else {
treeText += '<' + node.nodeName + '>';
for (var i = 0; i < node.childNodes.length; i++)
{
for (var i = 0; i < node.childNodes.length; i++) {
treeText += Traverse(node.childNodes.item(i), testament);
}
treeText += '</' + node.nodeName + '>';
}
} else
{
if (node.nodeValue != null)
{
if (node.nodeValue.search(/^(\,|\.|\:|\?|\;|\!)/) != -1)
{
} else {
if (node.nodeValue != null) {
if (node.nodeValue.search(/^(\,|\.|\:|\?|\;|\!)/) != -1) {
treeText += node.nodeValue;
} else
{
} else {
treeText += " " + node.nodeValue;
}
}
@ -62,33 +48,25 @@ function Traverse(node, testament)
}
return treeText;
}
catch (err)
{
catch (err) {
Util.HandleError(err);
}
return null;
}
function Search(sv)
{
try
{
function Search(sv) {
try {
var qs = sv.split(";");
for (var x in qs)
{
for (var x in qs) {
var q = qs[x].trim();
if (q != "")
{
if (q != "") {
// its a search term.
if (q.search(/[0-9]/i) == -1)
{
if (q.search(/[0-9]/i) == -1) {
// get new results.
Words.FindReferences(q);
}
} else if (q.search(/(H|G)[0-9]/i) != -1) {
// its a strongs lookup
else if (q.search(/(H|G)[0-9]/i) != -1)
{
var dict = q.substring(0, 1);
if (dict.search(/h/i) != -1) {
dict = "heb";
@ -99,32 +77,24 @@ function Search(sv)
var Ss = q.split(' ');
var results = [];
for (var s in Ss)
{
for (var s in Ss) {
results.push(Strongs.GetStrongs(Ss[s], dict));
}
for (var result in results)
{
for (var result in results) {
// display results.
if ($("#display-strongs-as-dialog")[0].checked)
{
if ($("#display-strongs-as-dialog")[0].checked) {
Strongs.DisplayStrongsDialog(results[result]);
}
else
{
} else {
Strongs.DisplayStrongs(results[result]);
}
}
}
} else {
// its a verse reference.
else
{
var passage = "";
if (q.trim() != "")
{
if (q.trim() != "") {
var myref = new Reference(q.trim());
var r = Bible.GetPassage(myref.book, myref.startchapter, myref.endchapter, myref.startverse, myref.endverse);
@ -132,15 +102,13 @@ function Search(sv)
}
}
}
}
$( "#result" ).sortable({
axis: "x",
handle: ".handle"
});
}
catch (err)
{
catch (err) {
Util.HandleError(err);
}
@ -149,25 +117,20 @@ function Search(sv)
var Settings = {
Load: function()
{
Load: function() {
var str = $.cookie('settings');
if (str != null)
{
if (str != null) {
var s = str.split(",");
$("#resultwrap").css("float", s[0]);
$("#searchresultswrap").css("float", s[0]);
if (s[1] == "none")
{
if (s[1] == "none") {
$("#searchresultswrap").css("display", "none");
$("#showhidesearch").html("Show Search");
$("#resultwrap").css("width", "100%");
}
else
{
} else {
$("#searchresultswrap").css("display", "block");
$("#showhidesearch").html("Hide Search");
$("#resultwrap").css("width", "70%");
@ -177,8 +140,7 @@ var Settings = {
$("#result").css("font-family", s[3]);
}
},
Save: function()
{
Save: function() {
var s = {};
s.Panes = $("#resultwrap").css("float");
s.Search = $("#searchresultswrap").css("display");
@ -191,126 +153,101 @@ var Settings = {
expires: 365
});
},
ShowHideSearch: function()
{
ShowHideSearch: function() {
var o = $("#showhidesearch");
var s = $("#searchresultswrap");
var r = $("#resultwrap");
if (s.css("display") != "none")
{
if (s.css("display") != "none") {
s.css("display", "none");
o.html("Show Search");
r.css("width", "100%");
}
else
{
} else {
s.css("display", "block");
o.html("Hide Search");
r.css("width", "70%");
}
this.Save();
},
SwitchPanes: function()
{
SwitchPanes: function() {
var s = $("#searchresultswrap");
var r = $("#resultwrap");
var v = s.css("float");
if (v == "right")
{
if (v == "right") {
s.css("float", "left");
r.css("float", "left");
}
else
{
} else {
s.css("float", "right");
r.css("float", "right");
}
this.Save();
},
IncreaseResultFontSize: function()
{
IncreaseResultFontSize: function() {
var s = $("#result").css("font-size");
$("#result").css("font-size", parseInt(s) + 1);
this.Save();
},
DecreaseResultFontSize: function()
{
DecreaseResultFontSize: function() {
var s = $("#result").css("font-size");
$("#result").css("font-size", parseInt(s) - 1);
this.Save();
},
ChangeResultFont: function(fontfamily)
{
ChangeResultFont: function(fontfamily) {
$("#result").css("font-family", fontfamily);
this.Save();
}
}
};
var Util = {
HandleLink: function(e)
{
HandleLink: function(e) {
Search($(e.target).text());
},
HandleHiddenLink: function(e)
{
HandleHiddenLink: function(e) {
Search($(e.target).find(".searchvalue").text());
},
RemoveResult: function(e)
{
RemoveResult: function(e) {
$(e.target).parent().parent().remove();
},
HandleError: function(e)
{
HandleError: function(e) {
// for now we're going to put the error in the main result div.
var t = $("<div class='strongsdef result'><a href='javascript:void();' class='removeresult' style='border: 0;'><img style='border: 0px;' src='images/delete.png' width='48' height='48' /></a><span class='resultbody'>" + e + "</span><br clear='all' /></div>");
return false;
}
}
};
var Bible = {
DisplayPassage: function(cs, b, sch, ech, sv, ev, testament)
{
try
{
DisplayPassage: function(cs, b, sch, ech, sv, ev, testament) {
try {
var r = "";
// make the end verse pretty.
var tvs = cs[cs.length - 1].vss.length;
if (ev == "*" || ev > tvs)
{
if (ev == "*" || ev > tvs) {
ev = tvs;
}
for (var j = 0; j < cs.length; j++)
{
if (sch < ech)
{
for (var j = 0; j < cs.length; j++) {
if (sch < ech) {
r += "<b>Chapter: " + cs[j].ch + "</b><br />";
}
var vss = cs[j].vss;
for (var m = 0; m < vss.length; m++)
{
for (var m = 0; m < vss.length; m++) {
var v = vss[m];
r += "<b>" + v.v + ".</b> ";
for (var w = 0; w < v.w.length; w++)
{
if (v.w[w].s != undefined)
{
for (var w = 0; w < v.w.length; w++) {
if (v.w[w].s != undefined) {
var strongs_pre = "";
if (testament == "old") { strongs_pre = "H"; }
if (testament == "new") {strongs_pre = "G"; }
var sp = "";
if (v.w[w].t.substr(v.w[w].t.length-1) == " ") { sp = " "; }
r += "<a href='javascript:void();' class='hiddenlink' title='Strongs #: " + v.w[w].s + "'><span class='searchvalue' style='display:none'>" + strongs_pre + v.w[w].s + "</span>" + v.w[w].t.trim() + "</a>"+sp;
}
else
{
} else {
r += v.w[w].t;
}
}
@ -319,83 +256,66 @@ var Bible = {
}
var t = $("<div class='passage result'><a href='javascript:void();' class='removeresult' style='border: 0;'><img style='border: 0px;' src='images/delete.png' width='48' height='48' /></a><span class='resultbody'>" + "<h2>" + bookName(b) + " " + sch + ":" + sv + "-" + ech + ":" + ev + "</h2>" + r + "</span><br clear='all' /></div>");
t.find(".hiddenlink").click(function(e)
{
t.find(".hiddenlink").click(function(e) {
Util.HandleHiddenLink(e);
});
t.find(".removeresult").click(function(e)
{
t.find(".removeresult").click(function(e) {
Util.RemoveResult(e);
});
$("#result").prepend(t);
}
catch (err)
{
catch (err) {
Util.HandleError(err);
}
},
GetPassage: function(b, sch, ech, sv, ev)
{
try
{
GetPassage: function(b, sch, ech, sv, ev) {
try {
var chapters = []; // the verses from the chapter.
var cs = []; // the verses requested.
var r = {};
for (var i = sch; i <= ech; i++)
{
var url = "bibles/kjv_strongs/" + b + "-" + i + ".json"
for (var i = sch; i <= ech; i++) {
var url = "bibles/kjv_strongs/" + b + "-" + i + ".json";
$.ajax({
async: false,
type: "GET",
url: url,
dataType: "json",
success: function(d, t, x)
{
success: function(d, t, x) {
chapters.push(d);
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
}
for (var j = 0; j < chapters.length; j++)
{
for (var j = 0; j < chapters.length; j++) {
var vss = [];
var start;
var end;
// figure out the start verse.
if (j == 0)
{
if (j == 0) {
start = sv;
}
else
{
} else {
start = 1;
}
// figure out the end verse
if ((j + 1) == chapters.length)
{
if ((j + 1) == chapters.length) {
end = ev;
}
else
{
} else {
end = "*";
}
// get the verses requested.
var tvs = chapters[j].vss.length;
if (end == "*" || end > tvs)
{
if (end == "*" || end > tvs) {
end = tvs;
}
for (i = start; i <= end; i++)
{
for (i = start; i <= end; i++) {
// we're using c based indexes here, so the index is 1 less than the verse #.
vss.push(chapters[j].vss[i-1]);
}
@ -407,31 +327,25 @@ var Bible = {
}
r.cs = cs;
if (b >= 40)
{
if (b >= 40) {
r.testament = "new";
}
else
{
} else {
r.testament = "old";
}
return r;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
}
}
};
var Strongs = {
GetStrongs: function(sn, dict)
{
try
{
GetStrongs: function(sn, dict) {
try {
var self = this;
var results = {};
var url = dict + parseInt((sn - 1) / 100) + ".xml"
var url = dict + parseInt((sn - 1) / 100) + ".xml";
if (dict == "grk") {
results.prefix = "G";
} else {
@ -444,12 +358,10 @@ var Strongs = {
type: "GET",
url: "xml/" + url,
dataType: "xml",
success: function(d, t, x)
{
success: function(d, t, x) {
results.strongs = d;
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
@ -459,18 +371,15 @@ var Strongs = {
type: "GET",
url: "xml/cr" + url,
dataType: "xml",
success: function(d, t, x)
{
success: function(d, t, x) {
results.crossrefs = d;
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
if (dict == "grk")
{
if (dict == "grk") {
url = "xml/rs" + parseInt((sn - 1) / 1000) + ".xml";
// rmac is a two get process.
$.ajax({
@ -478,12 +387,10 @@ var Strongs = {
type: "GET",
url: url,
dataType: "xml",
success: function(d, t, x)
{
success: function(d, t, x) {
results.rmac = d;
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
@ -496,27 +403,22 @@ var Strongs = {
type: "GET",
url: url,
dataType: "xml",
success: function(d, t, x)
{
success: function(d, t, x) {
results.rmac = d;
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
}
return results;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
BuildStrongs: function(r)
{
try
{
BuildStrongs: function(r) {
try {
// first deal with strongs data.
var entry = $(r.strongs).find("i#" + r.prefix + r.sn);
var title = $(entry).find("t").text();
@ -533,12 +435,10 @@ var Strongs = {
var crtxt = "<div class='scr'><b>Cross References:</b> <a href='javascript:void()' class='showhide'>Show</a><br /><span class='contents'>";
cr.each(function(i)
{
cr.each(function(i) {
crtxt += "<b>" + $(this).find("t").text() + ":</b> ";
$(this).find("r").each(function(j)
{
$(this).find("r").each(function(j) {
var ref = $(this).attr("r").split(";");
crtxt += "<a href='javascript:void();' class='link'>" + bookName(ref[0]) + " " + ref[1] + ":" + ref[2] + "</a>, ";
});
@ -549,12 +449,10 @@ var Strongs = {
// ...processing statements go here...
var rtxt = "";
if (r.prefix == "G")
{
if (r.prefix == "G") {
rtxt += "<div class='rmac'><b>Robinsons Morphological Analysis Code: " + r.rmaccode + "</b> <a href='javascript:void()' class='showhide'>Show</a><br /><span class='contents'>";
;
$(r.rmac).find('i[id="' + r.rmaccode.toUpperCase() + '"]').find("d").each(function()
{
$(r.rmac).find('i[id="' + r.rmaccode.toUpperCase() + '"]').find("d").each(function() {
rtxt += $(this).text() + "<br />";
});
rtxt += "</span></div>";
@ -564,45 +462,36 @@ var Strongs = {
// ok. we have to do this because click events seem to be cumulative with jquery.
var t = $("<div class='strongsdef result'><a href='javascript:void();' class='removeresult' style='border: 0;'><img style='border: 0px;' src='images/delete.png' width='48' height='48' /></a><span class='resultbody'><b>" + trans + " (" + r.sn + ")</b> - " + pron + " - " + title + " - " + desc + "<br />" + rtxt + crtxt + "</span><br clear='all' /></div>");
t.find(".link").click(function(e)
{
t.find(".link").click(function(e) {
Util.HandleLink(e);
});
t.find(".removeresult").click(function(e)
{
t.find(".removeresult").click(function(e) {
Util.RemoveResult(e);
});
t.find(".showhide").click(function(e)
{
t.find(".showhide").click(function(e) {
Strongs.ShowHide(e);
});
return t;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
DisplayStrongs: function(r)
{
try
{
DisplayStrongs: function(r) {
try {
var t = Strongs.BuildStrongs(r);
$("#result").prepend(t);
return false;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
DisplayStrongsDialog: function(r)
{
try
{
DisplayStrongsDialog: function(r) {
try {
var t = Strongs.BuildStrongs(r);
var d = $("<div></div>").append(t);
@ -619,24 +508,19 @@ var Strongs = {
}
});
return false;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
ShowHide: function(e)
{
ShowHide: function(e) {
var o = $(e.target);
var c = o.parent().find(".contents");
if (c.css("display") != "none")
{
if (c.css("display") != "none") {
c.css("display", "none");
o.html("Show");
}
else
{
} else {
c.css("display", "inline");
o.html("Hide");
}
@ -644,29 +528,22 @@ var Strongs = {
}
var Words = {
ConvertResultsToArray: function(r)
{
try
{
ConvertResultsToArray: function(r) {
try {
var results = new Array();
$(r).each(function()
{
$(r).each(function() {
results.push([$(this).attr("b"), $(this).attr("ch"), $(this).attr("v")]);
});
return results;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
DisplayResults: function(results, q)
{
try
{
DisplayResults: function(results, q) {
try {
var txt = "<h4>Query: <a href='javascript:void();' class='link'>" + q + "</a></h4><ul>";
for (var i = 0; i < results.length; i++)
{
for (var i = 0; i < results.length; i++) {
var r = results[i].split(":");
txt += "<li /><a href='javascript:void();' class='link' alt='" + bookName(r[0]) + " " + r[1] + ":" + r[2] + "'>" + bookName(r[0]) + " " + r[1] + ":" + r[2] + "</a>";
}
@ -674,50 +551,40 @@ var Words = {
var t = $(txt);
t.find(".link").click(function(e)
{
t.find(".link").click(function(e) {
Util.HandleLink(e);
});
$("#searchresults").html(t);
$("#searchTotal").html(results.length);
return false;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
FindReferences: function(qry)
{
try
{
FindReferences: function(qry) {
try {
qry = qry.toLowerCase();
var qs = qry.split(" ");
var words = this.BuildIndexArray().sort();
var results = new Array();
// Loop through each query term.
for (i = 0; i < qs.length; i++)
{
for (i = 0; i < qs.length; i++) {
var q = qs[i].replace("'", ""); // we don't included ticks in our words.
// For each query term, figure out which xml file it is in, and get it.
// getSearchRefs returns an array of references.
for (var w = 0; w < words.length; w++)
{
for (var w = 0; w < words.length; w++) {
// If we are at the end of the array, we want to use a different test.
if (w == 0)
{
if (q <= words[w])
{
if (w == 0) {
if (q <= words[w]) {
results.unshift(this.GetSearchReferences("index/" + words[w] + "idx.json", q));
break;
}
} else
{
if (q <= words[w] && q > words[w - 1])
{
} else {
if (q <= words[w] && q > words[w - 1]) {
results.unshift(this.GetSearchReferences("index/" + words[w] + "idx.json", q));
break;
}
@ -727,26 +594,21 @@ var Words = {
// Now we need to test results. If there is more than one item in the array, we need to find the set
// that is shared by all of them. IF not, we can just return those refs.
if (results.length == 1)
{
if (results.length == 1) {
this.DisplayResults(results[0], qry);
} else
{
} else {
this.DisplayResults(this.FindSharedSet(results), qry);
}
return false;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
GetSearchReferences: function(url, query)
{
try
{
GetSearchReferences: function(url, query) {
try {
// getSearchRefs takes a url and uses ajax to retrieve the references and returns an array of references.
var r;
@ -755,33 +617,26 @@ var Words = {
type: "GET",
url: url,
dataType: "json",
success: function(d, t, x)
{
success: function(d, t, x) {
r = d;
},
error: function(request, status, error)
{
error: function(request, status, error) {
Util.HandleError(error, request);
}
});
// find the right word
var refs = $.grep(r, function(o,i)
{
var refs = $.grep(r, function(o,i) {
return o.word == query;
});
return refs[0].refs;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
BuildIndexArray: function()
{
try
{
BuildIndexArray: function() {
var words = new Array();
words.unshift('abiram');
words.unshift('accepteth');
@ -1031,25 +886,16 @@ var Words = {
words.unshift('zophim');
words.unshift('zuzims');
return words;
}
catch (err)
{
Util.HandleError(err);
}
},
FindSharedSet: function(results)
{
try
{
FindSharedSet: function(results) {
try {
// FindSharedSet takes an array of reference arrays, and figures out which references are shared
// by all arrays/sets, then returns a single array of references.
for (var j in results)
{
for (var j in results) {
var refs = results[j];
for (var i = 0; i < refs.length; i++)
{
for (var i = 0; i < refs.length; i++) {
var r = refs[i].split(":");
// convert references to single integers.
// Book * 100000, Chapter * 1000, Verse remains same, add all together.
@ -1064,29 +910,24 @@ var Words = {
var result = results[0];
// for each additional result, get the shared set
for (i = 1; i < results.length; i++)
{
for (i = 1; i < results.length; i++) {
result = this.ReturnSharedSet(results[i], result);
}
// convert the references back into book, chapter and verse.
for (i = 0; i < result.length; i++)
{
for (i = 0; i < result.length; i++) {
ref = result[i];
result[i] = parseInt(ref / 100000000)+":"+parseInt((ref % 100000000) / 10000)+":"+((ref % 100000000) % 10000);
}
return result;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
},
ReturnSharedSet: function(x, y)
{
try
{
ReturnSharedSet: function(x, y) {
try {
/// <summary>
/// Takes two javascript arrays and returns an array
/// containing a set of values shared by arrays.
@ -1096,7 +937,7 @@ var Words = {
var i = 0;
// declare terminator
var t = (x.length < y.length) ? x.length : y.length
var t = (x.length < y.length) ? x.length : y.length;
// sort the arrays
x.sort(SortNumeric);
@ -1104,18 +945,14 @@ var Words = {
// in this loop, we remove from the arrays, the
// values that aren't shared between them.
while (i < t)
{
if (x[i] == y[i])
{
while (i < t) {
if (x[i] == y[i]) {
i++;
}
if (x[i] < y[i])
{
if (x[i] < y[i]) {
x.splice(i, 1);
}
if (x[i] > y[i])
{
if (x[i] > y[i]) {
y.splice(i, 1);
}
t = (x.length < y.length) ? x.length : y.length;
@ -1133,10 +970,9 @@ var Words = {
// we could return y, because at this time, both arrays
// are identical.
return x;
}
catch (err)
{
} catch (err) {
Util.HandleError(err);
}
return null;
}
}
};