1.started removal of CDNs.\n 2. Improved modular structure for all parts of project including database.
This commit is contained in:
146
static/js/lib/validation.js
Normal file
146
static/js/lib/validation.js
Normal file
@@ -0,0 +1,146 @@
|
||||
|
||||
// Argument validation
|
||||
/*
|
||||
function isNullOrWhitespace(v) {
|
||||
let txt = JSON.stringify(v).replace('/\s\g', '');
|
||||
return (txt == '' || 'null');
|
||||
}
|
||||
*/
|
||||
|
||||
function isEmpty(object) {
|
||||
|
||||
let isEmpty = true;
|
||||
|
||||
if (object !== null && object !== "null" && object !== undefined && object !== "undefined") {
|
||||
|
||||
if (object.length == undefined) {
|
||||
isEmpty = false; // object exists but isn't a collection
|
||||
}
|
||||
else if (typeof object === "function") {
|
||||
isEmpty = false; // object is function reference
|
||||
}
|
||||
else { // string or collection
|
||||
|
||||
let isString = (typeof object == "string");
|
||||
|
||||
if (isString) object = object.trim();
|
||||
|
||||
if (object.length > 0) {
|
||||
|
||||
if (isString) {
|
||||
isEmpty = false; // String greater than length 0
|
||||
}
|
||||
else {
|
||||
|
||||
if (typeof object[0] != "string") {
|
||||
isEmpty = false;
|
||||
}
|
||||
else {
|
||||
for(let i = 0; i < object.length; i++) {
|
||||
if (object[i] != "") {
|
||||
isEmpty = false;
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isEmpty;
|
||||
}
|
||||
|
||||
function isValidNumber(value, positiveOnly) {
|
||||
return !isEmpty(value) && !isNaN(value) && (!positiveOnly || parseFloat(value) > 0);
|
||||
}
|
||||
|
||||
function getDataContentType(params) {
|
||||
|
||||
var data = null;
|
||||
var contentType = '';
|
||||
|
||||
if (!isEmpty(params)) {
|
||||
|
||||
if (typeof params === "string") {
|
||||
data = params;
|
||||
contentType = "application/x-www-form-urlencoded; charset=UTF-8";
|
||||
}
|
||||
else {
|
||||
data = JSON.stringify(params);
|
||||
contentType = "application/json; charset=UTF-8";
|
||||
}
|
||||
}
|
||||
|
||||
return { Data: data, ContentType: contentType };
|
||||
}
|
||||
|
||||
function arrayContainsItem(array, itemValue) {
|
||||
|
||||
var hasItem = false;
|
||||
|
||||
if (!isEmpty(array) && !isEmpty(itemValue)) {
|
||||
|
||||
var isJQueryElementArray = array[0] instanceof jQuery;
|
||||
|
||||
if (isJQueryElementArray) {
|
||||
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
|
||||
if (document.querySelectorAll(array[i]).is(itemValue)) {
|
||||
hasItem = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
var isDate = array[0] instanceof Date;
|
||||
|
||||
if (isDate) {
|
||||
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
|
||||
if (array[i].getTime() === itemValue.getTime()) {
|
||||
hasItem = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
|
||||
if (array[i] == itemValue) {
|
||||
hasItem = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hasItem;
|
||||
}
|
||||
|
||||
function dictHasKey(d, k) {
|
||||
return (k in d);
|
||||
}
|
||||
|
||||
function imageExists(url, callback) {
|
||||
|
||||
var img = new Image();
|
||||
|
||||
img.onload = function() { callback(true); };
|
||||
img.onerror = function() { callback(false); };
|
||||
img.src = url;
|
||||
}
|
||||
|
||||
function validateImageUrl(id, img) {
|
||||
imageExists(img, function(exists) {
|
||||
if (exists) {
|
||||
document.querySelectorAll("#" + id).css({ "background-image": "url(" + url + ")", "background-size": "35px 35px"})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user