1.started removal of CDNs.\n 2. Improved modular structure for all parts of project including database.
This commit is contained in:
142
static/js/lib/common.js
Normal file
142
static/js/lib/common.js
Normal file
@@ -0,0 +1,142 @@
|
||||
|
||||
function getElementCurrentValue(element) {
|
||||
let returnVal = '';
|
||||
|
||||
if (!isEmpty(element)) {
|
||||
|
||||
if (element.type === "checkbox") {
|
||||
returnVal = element.checked;
|
||||
}
|
||||
/*
|
||||
else if (element.classList.contains(flagIsDatePicker)) {
|
||||
returnVal = getDatePickerDate(element, adjust4DayLightSavings);
|
||||
}
|
||||
*/
|
||||
else if (element.tagName === 'INPUT' || element.tagName === 'textarea' || element.tagName === 'select') {
|
||||
returnVal = element.value;
|
||||
}
|
||||
else {
|
||||
returnVal = element.textContent;
|
||||
}
|
||||
}
|
||||
|
||||
if (isEmpty(returnVal)) returnVal = '';
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
function parseCSSPropertyToFloat(element, propertyName) {
|
||||
var propertyText = element.css(propertyName);
|
||||
|
||||
if (!isEmpty(propertyText)) {
|
||||
|
||||
propertyText = propertyText.replace('px', '');
|
||||
|
||||
if (!isValidNumber(propertyText, true)) return parseFloat(propertyText);
|
||||
}
|
||||
|
||||
return 0.00;
|
||||
}
|
||||
|
||||
function scrollToElement(parent, element) {
|
||||
// REQUIRED: parent has scroll-bar
|
||||
parent.scrollTop(parent.scrollTop() + (element.offset().top - parent.offset().top));
|
||||
}
|
||||
|
||||
function isElementInContainer(container, element) {
|
||||
|
||||
if (typeof jQuery === 'function') {
|
||||
if (container instanceof jQuery) container = container[0];
|
||||
if (element instanceof jQuery) element = element[0];
|
||||
}
|
||||
|
||||
var containerBounds = container.getBoundingClientRect();
|
||||
var elementBounds = element.getBoundingClientRect();
|
||||
|
||||
return (
|
||||
containerBounds.top <= elementBounds.top &&
|
||||
containerBounds.left <= elementBounds.left &&
|
||||
((elementBounds.top + elementBounds.height) <= (containerBounds.top + containerBounds.height)) &&
|
||||
((elementBounds.left + elementBounds.width) <= (containerBounds.left + containerBounds.width))
|
||||
);
|
||||
}
|
||||
|
||||
function getRowFromElement(element) {
|
||||
return document.querySelectorAll(element).closest('tr');
|
||||
}
|
||||
|
||||
function getCellFromElement(element) {
|
||||
return document.querySelectorAll(element).closest('td');
|
||||
}
|
||||
|
||||
function alertError(errorType, errorText) {
|
||||
alert(errorType + '\n' + errorText);
|
||||
}
|
||||
|
||||
function setPageToLoading(isLoading) {
|
||||
|
||||
if (isLoading) {
|
||||
document.querySelectorAll(document.body).classList.add(_dataLoadingFlag);
|
||||
}
|
||||
else {
|
||||
document.querySelectorAll(document.body).classList.remove(_dataLoadingFlag);
|
||||
}
|
||||
}
|
||||
|
||||
function setBackgroundToLoading(elId, isLoading) {
|
||||
|
||||
if (isEmpty(el)) {
|
||||
|
||||
var elObj = document.querySelectorAll(elId);
|
||||
|
||||
if (isLoading) {
|
||||
|
||||
setTimeout(function() {
|
||||
elObj.innerHTML = "";
|
||||
elObj.css({
|
||||
"background-image": "url(" + urlImgLoading + ")",
|
||||
"background-position": "center",
|
||||
"background-repeat": "no-repeat"
|
||||
});
|
||||
}, 0);
|
||||
}
|
||||
else {
|
||||
elObj.css("background-image", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function allowClick() {
|
||||
return !document.querySelectorAll("body").classList.contains(_dataLoadingFlag);
|
||||
}
|
||||
|
||||
function displayOverlay(message, show, force) {
|
||||
|
||||
if (show) {
|
||||
_overlayLoadingCount += 1;
|
||||
}
|
||||
else if (force) {
|
||||
_overlayLoadingCount = 0;
|
||||
}
|
||||
else {
|
||||
_overlayLoadingCount -= 1;
|
||||
if (_overlayLoadingCount < 0) _overlayLoadingCount = 0;
|
||||
}
|
||||
|
||||
var loadingImg = document.querySelectorAll(idImageLoading);
|
||||
var overlay = document.querySelectorAll(loadingImg.closest("div.overlay"));
|
||||
|
||||
if (_overlayLoadingCount == 0) {
|
||||
|
||||
// Prevent short glimpse of prev. content before switch to new content
|
||||
// caused by data load but not fully rendered
|
||||
setTimeout(function() {
|
||||
overlay.fadeOut();
|
||||
}, 100);
|
||||
}
|
||||
else if (show && _overlayLoadingCount == 1) {
|
||||
// only show once
|
||||
loadingImg.innerHTML = message;
|
||||
overlay.style.display = "";
|
||||
}
|
||||
}
|
||||
7
static/js/lib/constants.js
Normal file
7
static/js/lib/constants.js
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
const _dataLoadingFlag = 'data-loading'
|
||||
var _domParser = null;
|
||||
// var hashPageCurrent; // moved to layout
|
||||
const keyPublicStripe = 'pk_test_51OGrxlL7BuLKjoMpfpfw7bSmZZK1MhqMoQ5VhW2jUj7YtoEejO4vqnxKPiqTHHuh9U4qqkywbPCSI9TpFKtr4SYH007KHMWs68';
|
||||
var _pageCurrent = null;
|
||||
var _verbose = true;
|
||||
9
static/js/lib/events.js
Normal file
9
static/js/lib/events.js
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
function initialiseEventHandler(selectorElement, classInitialised, eventHandler) {
|
||||
document.querySelectorAll(selectorElement).forEach(function(element) {
|
||||
if (!element.classList.contains(classInitialised)) {
|
||||
element.classList.add(classInitialised);
|
||||
eventHandler(element);
|
||||
}
|
||||
});
|
||||
}
|
||||
0
static/js/lib/extras.js
Normal file
0
static/js/lib/extras.js
Normal file
3
static/js/lib/init.js
Normal file
3
static/js/lib/init.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// Shared JS file names: routing, main, shared, localStorage, appDialogs
|
||||
|
||||
|
||||
59
static/js/lib/local_storage.js
Normal file
59
static/js/lib/local_storage.js
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
// Local storage
|
||||
/*
|
||||
function getPageLocalStorage(pageHash) {
|
||||
|
||||
let ls;
|
||||
try {
|
||||
ls = JSON.parse(localStorage.getItem(pageHash));
|
||||
} catch {
|
||||
|
||||
}
|
||||
|
||||
if (isEmpty(ls)) return {}
|
||||
|
||||
return ls;
|
||||
}
|
||||
function getPageLocalStorageCurrent() {
|
||||
|
||||
return JSON.parse(localStorage.getItem(hashPageCurrent));
|
||||
}
|
||||
|
||||
function setPageLocalStorage(pageHash, newLS) {
|
||||
|
||||
localStorage.setItem(pageHash, JSON.stringify(newLS));
|
||||
}
|
||||
|
||||
function clearPageLocalStorage(pageHash) {
|
||||
localStorage.removeItem(pageHash);
|
||||
}
|
||||
|
||||
function setupPageLocalStorage(pageHash) {
|
||||
|
||||
let ls = getPageLocalStorage(pageHash);
|
||||
|
||||
if (isEmpty(ls)) ls = {};
|
||||
|
||||
setPageLocalStorage(pageHash, ls);
|
||||
}
|
||||
*/
|
||||
|
||||
function getLocalStorage(key) {
|
||||
return JSON.parse(localStorage.getItem(key));
|
||||
}
|
||||
|
||||
function setLocalStorage(key, newLS) {
|
||||
localStorage.setItem(key, JSON.stringify(newLS));
|
||||
}
|
||||
|
||||
/*
|
||||
function setupPageLocalStorageNext(pageHashNext) {
|
||||
let lsOld = getPageLocalStorage(hashPageCurrent);
|
||||
hashPageCurrent = pageHashNext;
|
||||
clearPageLocalStorage(hashPageCurrent);
|
||||
setupPageLocalStorage(hashPageCurrent);
|
||||
let lsNew = getPageLocalStorage(hashPageCurrent);
|
||||
lsNew[keyBasket] = (keyBasket in lsOld) ? lsOld[keyBasket] : {'items': []};
|
||||
setPageLocalStorage(hashPageCurrent, lsNew);
|
||||
}
|
||||
*/
|
||||
10
static/js/lib/utils.js
Normal file
10
static/js/lib/utils.js
Normal file
@@ -0,0 +1,10 @@
|
||||
// Utility functions
|
||||
/*
|
||||
function $(selector) {
|
||||
return document.querySelector(selector);
|
||||
}
|
||||
|
||||
function $$(selector) {
|
||||
return document.querySelectorAll(selector);
|
||||
}
|
||||
*/
|
||||
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