Commits

Luke Plant committed 4d56f3d

Reordered javascript to fix jslint warnings

Comments (0)

Files changed (1)

lukeplant_me_uk/bibleverses/web/js/bibleverses.js

 var rootUrl = "/bibleverses/";
 var cgiRoot = rootUrl + "cgi-bin/main.cgi/";
 
-var verselist = new Array();
+var verselist = [];
 
 function readCookie(name) {
     var nameEQ = name + "=";
     var ca = document.cookie.split(';');
     for(var i=0;i < ca.length;i++) {
 	var c = ca[i];
-	while (c.charAt(0)==' ') c = c.substring(1,c.length);
-	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
+	while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
+	if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
     }
     return null;
 }
 
-
 function setCookie(name, value, days) {
+    var expires;
     if (days) {
 	var date = new Date();
 	date.setTime(date.getTime()+(days*24*60*60*1000));
-	var expires = "; expires="+date.toGMTString();
+	expires = "; expires="+date.toGMTString();
+    } else {
+	expires = "";
     }
-    else var expires = "";
     document.cookie = name+"="+value+expires + "; path=" + rootUrl ;
 }
 
     logError(err);
 }
 
-function doLoggedInSetup(email, password) {
-    setLoginCookie(email, password);
-    updateControls();
-    loadVerses();
-}
-
 function setLoginCookie(email, password) {
     // Security is very low (just put password in cookie).
     // But the data being protected has minimal security requirements.
     setCookie("password", password, 100000);
 }
 
-function registerResult(req) {
-    var json = evalJSONRequest(req);
-    logDebug(req.responseText);
-    if (json.success) {
-	showElement($('regresultok'));
-	hideElement($('regresultfail'));
-	hideElement($('regbuttonsdiv'));
-	showElement($('regclosediv'));
-	doLoggedInSetup($('id_reg_email').value,
-			makePasswordHash($('id_reg_email').value,
-					 $('id_reg_password').value));
+function pageNeedsVerseList() {
+    return (getElementsByTagAndClassName("span", "verseadmin").length > 0);
+}
+
+function makeVerseRef(verse) {
+    return verse.replace(/ |,|:|-/g, "_");
+}
+
+function get_email() {
+    var email = readCookie('email');
+    if (email === null) {
+	return null;
     } else {
-	var fail = $('regresultfail');
-	if (json.validation) {
-	    logDebug("Validation: " + repr(json.validation));
-	    replaceChildNodes(fail, json.validation[1]);
-	} else {
-	    replaceChildNodes(fail, "An error occurred: " + json.error);
-	}
-	showElement(fail);
+	return decodeURIComponent(email);
     }
 }
 
-function loginResult(req) {
-    var json = evalJSONRequest(req);
-    logDebug(req.responseText);
-    if (json.success) {
-	showElement($('loginresultok'));
-	hideElement($('loginresultfail'));
-	hideElement($('loginbuttonsdiv'));
-	showElement($('loginclosediv'));
-	doLoggedInSetup($('id_login_email').value,
-			makePasswordHash($('id_login_email').value,
-					 $('id_login_password').value));
-    } else {
-	var fail = $('loginresultfail');
-	if (json.validation) {
-	    logDebug("Validation: " + repr(json.validation));
-	    replaceChildNodes(fail, json.validation[1]);
-	} else {
-	    replaceChildNodes(fail, "An error occurred: " + json.error);
-	}
-	showElement(fail);
-    }
+function makePasswordHash(email, password) {
+    return SHA1(email + password);
 }
 
-
 function resetRegisterBlock() {
     $('id_reg_email').value = '';
     $('id_reg_password').value = '';
     hideElement($('loginblock'));
 }
 
-function makePasswordHash(email, password) {
-    return SHA1(email + password);
-}
-
-function onRegister2(ev) {
-    var d = doPostRequest( "register",
-	{'email': $('id_reg_email').value,
-	'password': makePasswordHash($('id_reg_email').value,
-				     $('id_reg_password').value)
-	});
-    d.addCallbacks(registerResult, standardErrorHandler);
-}
-
-function onLogin2(ev) {
-    var d = doPostRequest( "login",
-	{'email': $('id_login_email').value,
-	'password': makePasswordHash($('id_login_email').value,
-				     $('id_login_password').value)
-	});
-    d.addCallbacks(loginResult, standardErrorHandler);
-}
-
 function onCloseReg(ev) {
     hideElement($('registerblock'));
 }
     hideElement($('loginblock'));
 }
 
-function doLogout() {
-    eraseCookie('email');
-    eraseCookie('password');
-    updateControls();
-}
-
-function onLogout(ev) {
-    doLogout();
-}
-
 function showUserBar(bar) {
     // easiest way to get layout correct is to relatively
     // position these elements
     showUserBar($('userbar2'));
 }
 
+function setCheckBoxDisplay(state) {
+    // Would be much nicer to do this using a stylesheet rule
+    var vadmins = getElementsByTagAndClassName("span", "verseadmin");
+    for (var i = 0; i < vadmins.length; i++) {
+	vadmins[i].style.display = state;
+    }
+}
+
+function showCheckBoxes() {
+    setCheckBoxDisplay('inline');
+}
+
+function hideCheckBoxes() {
+    setCheckBoxDisplay('none');
+    var elems = getElementsByTagAndClassName("li", "memorised");
+    for (var i = 0; i < elems.length; i++) {
+	removeElementClass(elems[i], "memorised");
+    }
+}
+
 function updateControls() {
     var email = get_email();
-    if (email != null && email != '') {
+    if (email !== null && email !== '') {
 	addUserBar2(email);
 	showCheckBoxes();
     } else {
     }
 }
 
-function get_email() {
-    var email = readCookie('email');
-    if (email == null) {
-	return null;
+function doLogout() {
+    eraseCookie('email');
+    eraseCookie('password');
+    updateControls();
+}
+
+function onLogout(ev) {
+    doLogout();
+}
+
+function indicateFinishedVerse(verse) {
+    var refid = makeVerseRef(verse);
+    var elem = $('id_verseli_' + refid);
+    if (elem) {
+	addElementClass(elem, "memorised");
+	$('id_versecb_' + refid).checked = true;
+	removeElementClass($('id_verseli_' + refid), "manuallyshown");
+    }
+}
+
+function indicateFinishedVerses() {
+    for (var i = 0; i < verselist.length; i++) {
+	indicateFinishedVerse(verselist[i]);
+    }
+}
+
+function indicateUnfinishedVerse(verse) {
+    var refid = makeVerseRef(verse);
+    var elem = $('id_verseli_' + refid);
+    if (elem) {
+	var hidden_div = getFirstElementByTagAndClassName("div", "versebottom", elem);
+	removeElementClass(elem, "memorised");
+	$('id_versecb_' + refid).checked = false;
+	removeElementClass($('id_verseli_' + refid), "manuallyhidden");
+    }
+}
+
+function verseListArrived(req) {
+    var json = evalJSONRequest(req);
+    logDebug(req.responseText);
+    if (json.success) {
+	verselist = json.verselist;
+	indicateFinishedVerses();
     } else {
-	return decodeURIComponent(email);
+	// login details wrong.
+	doLogout();
+	// (or, TODO, network error)
     }
 }
 
+function loadVerses() {
+    var email = get_email();
+    if (email !== null && email !== "") {
+	if (pageNeedsVerseList()) {
+	    var d = doGetRequest("verses");
+	    d.addCallbacks(verseListArrived, standardErrorHandler);
+	}
+    }
+}
+
+function doLoggedInSetup(email, password) {
+    setLoginCookie(email, password);
+    updateControls();
+    loadVerses();
+}
+
+function registerResult(req) {
+    var json = evalJSONRequest(req);
+    logDebug(req.responseText);
+    if (json.success) {
+	showElement($('regresultok'));
+	hideElement($('regresultfail'));
+	hideElement($('regbuttonsdiv'));
+	showElement($('regclosediv'));
+	doLoggedInSetup($('id_reg_email').value,
+			makePasswordHash($('id_reg_email').value,
+					 $('id_reg_password').value));
+    } else {
+	var fail = $('regresultfail');
+	if (json.validation) {
+	    logDebug("Validation: " + repr(json.validation));
+	    replaceChildNodes(fail, json.validation[1]);
+	} else {
+	    replaceChildNodes(fail, "An error occurred: " + json.error);
+	}
+	showElement(fail);
+    }
+}
+
+function loginResult(req) {
+    var json = evalJSONRequest(req);
+    logDebug(req.responseText);
+    if (json.success) {
+	showElement($('loginresultok'));
+	hideElement($('loginresultfail'));
+	hideElement($('loginbuttonsdiv'));
+	showElement($('loginclosediv'));
+	doLoggedInSetup($('id_login_email').value,
+			makePasswordHash($('id_login_email').value,
+					 $('id_login_password').value));
+    } else {
+	var fail = $('loginresultfail');
+	if (json.validation) {
+	    logDebug("Validation: " + repr(json.validation));
+	    replaceChildNodes(fail, json.validation[1]);
+	} else {
+	    replaceChildNodes(fail, "An error occurred: " + json.error);
+	}
+	showElement(fail);
+    }
+}
+
+function onRegister2(ev) {
+    var d = doPostRequest( "register",
+	{'email': $('id_reg_email').value,
+	'password': makePasswordHash($('id_reg_email').value,
+				     $('id_reg_password').value)
+	});
+    d.addCallbacks(registerResult, standardErrorHandler);
+}
+
+function onLogin2(ev) {
+    var d = doPostRequest( "login",
+	{'email': $('id_login_email').value,
+	'password': makePasswordHash($('id_login_email').value,
+				     $('id_login_password').value)
+	});
+    d.addCallbacks(loginResult, standardErrorHandler);
+}
+
 function verseAdded(req) {
     var json = evalJSONRequest(req);
     if (json.success) {
     }
 }
 
+function manuallyShow(item) {
+    removeElementClass(item, "manuallyhidden");
+    addElementClass(item, "manuallyshown");
+}
+
+function manuallyHide(item) {
+    addElementClass(item, "manuallyhidden");
+    removeElementClass(item, "manuallyshown");
+}
+
 function showBtnClicked(ev) {
     var refid = ev.src().id.replace("id_show_","");
     manuallyShow($("id_verseli_" + refid));
 }
 
-function manuallyShow(item) {
-    removeElementClass(item, "manuallyhidden");
-    addElementClass(item, "manuallyshown");
-}
-
 function hideBtnClicked(ev) {
     var refid = ev.src().id.replace("id_hide_","");
     manuallyHide($("id_verseli_" + refid));
 }
 
-function manuallyHide(item) {
-    addElementClass(item, "manuallyhidden");
-    removeElementClass(item, "manuallyshown");
-}
-
 function connectCheckBoxes() {
     var vbtns = getElementsByTagAndClassName("span", "versebtns");
     var cb, show, hide;
     }
 }
 
-function pageNeedsVerseList() {
-    return (getElementsByTagAndClassName("span", "verseadmin").length > 0);
-}
-
-function setCheckBoxDisplay(state) {
-    // Would be much nicer to do this using a stylesheet rule
-    var vadmins = getElementsByTagAndClassName("span", "verseadmin");
-    for (var i = 0; i < vadmins.length; i++) {
-	vadmins[i].style.display = state;
-    }
-}
-
-function showCheckBoxes() {
-    setCheckBoxDisplay('inline');
-}
-
-function hideCheckBoxes() {
-    setCheckBoxDisplay('none');
-    var elems = getElementsByTagAndClassName("li", "memorised");
-    for (var i = 0; i < elems.length; i++) {
-	removeElementClass(elems[i], "memorised");
-    }
-}
-
-function makeVerseRef(verse) {
-    return verse.replace(/ |,|:|-/g, "_");
-}
-
-function indicateFinishedVerse(verse) {
-    var refid = makeVerseRef(verse);
-    var elem = $('id_verseli_' + refid);
-    if (elem) {
-	addElementClass(elem, "memorised");
-	$('id_versecb_' + refid).checked = true;
-	removeElementClass($('id_verseli_' + refid), "manuallyshown");
-    }
-}
-
-function indicateUnfinishedVerse(verse) {
-    var refid = makeVerseRef(verse);
-    var elem = $('id_verseli_' + refid);
-    if (elem) {
-	var hidden_div = getFirstElementByTagAndClassName("div", "versebottom", elem);
-	removeElementClass(elem, "memorised");
-	$('id_versecb_' + refid).checked = false;
-	removeElementClass($('id_verseli_' + refid), "manuallyhidden");
-    }
-}
-
-function indicateFinishedVerses() {
-    for (var i = 0; i < verselist.length; i++) {
-	indicateFinishedVerse(verselist[i]);
-    }
-}
-
-
-function verseListArrived(req) {
-    var json = evalJSONRequest(req);
-    logDebug(req.responseText);
-    if (json.success) {
-	verselist = json.verselist;
-	indicateFinishedVerses();
-    } else {
-	// login details wrong.
-	doLogout();
-	// (or, TODO, network error)
-    }
-}
-
-function loadVerses() {
-    var email = get_email();
-    if (email != null && email != "") {
-	if (pageNeedsVerseList()) {
-	    var d = doGetRequest("verses");
-	    d.addCallbacks(verseListArrived, standardErrorHandler);
-	}
-    }
-}
-
 connect(window, 'onload',
         function(ev) {
 	    connectCheckBoxes();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.