Isolate generic functions into their own helper file.

This commit is contained in:
Robbie Antenesse 2016-03-11 09:54:25 -07:00
parent edadb37a6b
commit 9d9bba79d5
5 changed files with 167 additions and 155 deletions

View File

@ -282,34 +282,18 @@ require_once(SITE_LOCATION . '/php/notificationconditiontree.php');
<script src="js/defiant.js"></script>
<!-- Diacritics Removal for Exports -->
<script src="js/removeDiacritics.js"></script>
<!-- Main Script -->
<!-- Helper Functions -->
<script src="js/helpers.js"></script>
<!-- Main Functions -->
<script src="js/dictionaryBuilder.js"></script>
<!-- UI Functions -->
<script src="js/ui.js"></script>
<?php if ($_GET['adminoverride'] != "noadsortracking") { include_once("php/google/analytics.php"); } ?>
<script>
var aboutText = termsText = privacyText = loginForm = forgotForm = "Loading...";
window.onload = function () {
LoadDictionary();
ClearForm();
LoadUserDictionaries();
GetTextFile("README.md", "aboutText", true);
GetTextFile("TERMS.md", "termsText", true);
GetTextFile("PRIVACY.md", "privacyText", true);
GetTextFile("LOGIN.form", "loginForm", false);
GetTextFile("FORGOT.form", "forgotForm", false);
}
ready(function() {
Initialize();
});
</script>
</body>
</html>
<?php
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
?>

View File

@ -661,73 +661,3 @@ function CheckForPartsOfSpeechChange() {
}
}
}
function SaveScroll() {
var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
savedScroll.x = left;
savedScroll.y = top;
}
function htmlEntities(string) {
return String(string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;').replace(/\\/g, '&#92;').replace(/\n/g, '<br>');
}
function htmlEntitiesParse(string) {
return String(string).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&#92;/g, '\\').replace(/<br>/g, '\n');
}
function stripHtmlEntities(string) {
// This is for the export name.
return String(string).replace(/&amp;/g, '').replace(/&lt;/g, '').replace(/&gt;/g, '').replace(/&quot;/g, '').replace(/&apos;/g, "").replace(/&#92;/g, '').replace(/<br>/g, '');
}
function htmlEntitiesParseForSearchEntry(string) {
return String(string).replace(/"/g, '%%%%').replace(/'/g, "````");
}
function htmlEntitiesParseForSearch(string) {
return String(string).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '%%%%').replace(/&apos;/g, "````");
}
function regexParseForSearch(string) {
return String(string).replace(/([\[\\\^\$\.\|\?\*\+\(\)\{\}\]])/g, "\\$1");
}
function dynamicSort(propertiesArray) {
/* Retrieved from http://stackoverflow.com/a/30446887/3508346
Usage: theArray.sort(dynamicSort(['propertyAscending', '-propertyDescending']));*/
return function (a, b) {
return propertiesArray
.map(function (o) {
var dir = 1;
if (o[0] === '-') {
dir = -1;
o=o.substring(1);
}
if (removeDiacritics(a[o]) > removeDiacritics(b[o])) return dir;
if (removeDiacritics(a[o]) < removeDiacritics(b[o])) return -(dir);
return 0;
})
.reduce(function firstNonZeroValue (p,n) {
return p ? p : n;
}, 0);
};
}
function download(filename, text) {
/* Retrieved from http://stackoverflow.com/a/18197341/3508346
Usage: download('test.txt', 'Hello world!');*/
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}

139
js/helpers.js Normal file
View File

@ -0,0 +1,139 @@
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function getInputSelection(el) {
// Retrieved from http://stackoverflow.com/a/4207763
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
el.focus();
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function setSelectionRange(input, selectionStart, selectionEnd) {
// Retrieved from http://stackoverflow.com/a/17858641/3508346
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function SaveScroll() {
var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
savedScroll.x = left;
savedScroll.y = top;
}
function htmlEntities(string) {
return String(string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;').replace(/\\/g, '&#92;').replace(/\n/g, '<br>');
}
function htmlEntitiesParse(string) {
return String(string).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&#92;/g, '\\').replace(/<br>/g, '\n');
}
function stripHtmlEntities(string) {
// This is for the export name.
return String(string).replace(/&amp;/g, '').replace(/&lt;/g, '').replace(/&gt;/g, '').replace(/&quot;/g, '').replace(/&apos;/g, "").replace(/&#92;/g, '').replace(/<br>/g, '');
}
function htmlEntitiesParseForSearchEntry(string) {
return String(string).replace(/"/g, '%%%%').replace(/'/g, "````");
}
function htmlEntitiesParseForSearch(string) {
return String(string).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '%%%%').replace(/&apos;/g, "````");
}
function regexParseForSearch(string) {
return String(string).replace(/([\[\\\^\$\.\|\?\*\+\(\)\{\}\]])/g, "\\$1");
}
function dynamicSort(propertiesArray) {
/* Retrieved from http://stackoverflow.com/a/30446887/3508346
Usage: theArray.sort(dynamicSort(['propertyAscending', '-propertyDescending']));*/
return function (a, b) {
return propertiesArray
.map(function (o) {
var dir = 1;
if (o[0] === '-') {
dir = -1;
o=o.substring(1);
}
if (removeDiacritics(a[o]) > removeDiacritics(b[o])) return dir;
if (removeDiacritics(a[o]) < removeDiacritics(b[o])) return -(dir);
return 0;
})
.reduce(function firstNonZeroValue (p,n) {
return p ? p : n;
}, 0);
};
}
function download(filename, text) {
/* Retrieved from http://stackoverflow.com/a/18197341/3508346
Usage: download('test.txt', 'Hello world!');*/
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}

View File

@ -1,3 +1,15 @@
function Initialize() {
LoadDictionary();
ClearForm();
LoadUserDictionaries();
GetTextFile("README.md", "aboutText", true);
GetTextFile("TERMS.md", "termsText", true);
GetTextFile("PRIVACY.md", "privacyText", true);
GetTextFile("LOGIN.form", "loginForm", false);
GetTextFile("FORGOT.form", "forgotForm", false);
}
function LoadUserDictionaries() {
var getDictionariesRequest = new XMLHttpRequest();
var userDictionariesSelect = document.getElementById("userDictionaries");
@ -490,65 +502,3 @@ function NewWordNotification(word) {
function FocusAfterAddingNewWord() {
document.getElementById("word").focus();
}
function getInputSelection(el) {
// Retrieved from http://stackoverflow.com/a/4207763
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
el.focus();
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
function setSelectionRange(input, selectionStart, selectionEnd) {
// Retrieved from http://stackoverflow.com/a/17858641/3508346
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}

View File

@ -33,6 +33,15 @@ function fetch ($query_results) {
}
}
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
function ordinal($number) {
// Retrieved from http://stackoverflow.com/a/3110033/3508346
$ends = array('th','st','nd','rd','th','th','th','th','th','th');