Initial commit.
This commit is contained in:
29
static/js/lib/business_objects/business_objects.js
Normal file
29
static/js/lib/business_objects/business_objects.js
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
import Utils from '../utils.js';
|
||||
|
||||
export default class BusinessObjects {
|
||||
static getOptionJsonFromObjectJsonAndKeys(objectJson, keyText, keyValue, valueSelected = null) {
|
||||
return {
|
||||
text: objectJson[keyText],
|
||||
value: objectJson[keyValue],
|
||||
selected: (objectJson[keyValue] == valueSelected),
|
||||
};
|
||||
}
|
||||
static getOptionJsonFromObjectJson(objectJson, valueSelected = null) {
|
||||
let keyText = objectJson[flagNameAttrOptionText];
|
||||
let keyValue = objectJson[flagNameAttrOptionValue];
|
||||
Utils.consoleLogIfNotProductionEnvironment({objectJson, keyText, keyValue});
|
||||
return BusinessObjects.getOptionJsonFromObjectJsonAndKeys(objectJson, keyText, keyValue, valueSelected);
|
||||
}
|
||||
static getObjectText(objectJson) {
|
||||
return objectJson == null ? '' : objectJson[objectJson[flagNameAttrOptionText]];
|
||||
}
|
||||
static getListObjectsFromIdDictAndCsv(idDict, idCsv) {
|
||||
let listObjects = [];
|
||||
let ids = idCsv.split(',');
|
||||
for (let id of ids) {
|
||||
listObjects.push(idDict[id]);
|
||||
}
|
||||
return listObjects;
|
||||
}
|
||||
}
|
||||
52
static/js/lib/business_objects/store/product_permutation.js
Normal file
52
static/js/lib/business_objects/store/product_permutation.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
|
||||
export default class ProductPermutation {
|
||||
static getProductVariationsFromIdCsv(csvVariations) {
|
||||
let productVariations = [];
|
||||
if (!csvVariations) return productVariations;
|
||||
let variationPairs = csvVariations.split(',');
|
||||
if (variationPairs.length == 0) return productVariations;
|
||||
let parts;
|
||||
variationPairs.forEach((variationPair) => {
|
||||
parts = variationPair.split(':');
|
||||
if (parts.length == 2) {
|
||||
let productVariationType = productVariationTypes[parts[0]];
|
||||
productVariationType[flagProductVariations].some((productVariation) => {
|
||||
if (productVariation[attrIdProductVariation] == parts[1]) {
|
||||
productVariations.push([productVariationType, productVariation]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
});
|
||||
return productVariations;
|
||||
}
|
||||
static getProductVariationsPreviewFromIdCsv(csvVariations) {
|
||||
let variationPairs = ProductPermutation.getProductVariationsFromIdCsv(csvVariations);
|
||||
let preview = '';
|
||||
if (variationPairs.length == 0) return preview;
|
||||
let variationType, variation;
|
||||
variationPairs.forEach((variationPair) => {
|
||||
if (preview.length > 0) {
|
||||
preview += '\n';
|
||||
}
|
||||
variationType = variationPair[0];
|
||||
variation = variationPair[1];
|
||||
preview += variationType[flagName] + ': ' + variation[flagName];
|
||||
});
|
||||
return preview;
|
||||
}
|
||||
|
||||
static getProductVariationsIdCsvFromVariationTypeList(variationTypeList) {
|
||||
let csvVariations = '';
|
||||
if (Validation.isEmpty(variationTypeList)) return csvVariations;
|
||||
variationTypeList.forEach((variationType) => {
|
||||
if (csvVariations.length > 0) {
|
||||
csvVariations += ',';
|
||||
}
|
||||
csvVariations += variationType[attrIdProductVariationType] + ':' + variationType[flagProductVariations][0][attrIdProductVariation];
|
||||
});
|
||||
return csvVariations;
|
||||
}
|
||||
}
|
||||
46
static/js/lib/common.js
Normal file
46
static/js/lib/common.js
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
import Validation from "./validation.js";
|
||||
|
||||
export default class Common {
|
||||
static parseFloatWithDefault(value, defaultValue = 0.00) {
|
||||
if (!Validation.isEmpty(value) && Validation.isValidNumber(value, true)) {
|
||||
return parseFloat(value);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
static allowClick() {
|
||||
return !document.querySelectorAll("body").classList.contains(_dataLoadingFlag);
|
||||
}
|
||||
|
||||
static 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 = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
5
static/js/lib/constants.js
Normal file
5
static/js/lib/constants.js
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
const _dataLoadingFlag = 'data-loading'
|
||||
var _domParser = null;
|
||||
// var hashPageCurrent; // moved to layout
|
||||
const keyPublicStripe = 'pk_test_51OGrxlL7BuLKjoMpfpfw7bSmZZK1MhqMoQ5VhW2jUj7YtoEejO4vqnxKPiqTHHuh9U4qqkywbPCSI9TpFKtr4SYH007KHMWs68';
|
||||
10
static/js/lib/events.js
Normal file
10
static/js/lib/events.js
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
export default class Events {
|
||||
static initialiseEventHandler(selectorElement, classInitialised, eventHandler) {
|
||||
document.querySelectorAll(selectorElement).forEach(function(element) {
|
||||
if (element.classList.contains(classInitialised)) return;
|
||||
element.classList.add(classInitialised);
|
||||
eventHandler(element);
|
||||
});
|
||||
}
|
||||
}
|
||||
0
static/js/lib/extras.js
Normal file
0
static/js/lib/extras.js
Normal file
62
static/js/lib/local_storage.js
Normal file
62
static/js/lib/local_storage.js
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
import Validation from "./validation.js";
|
||||
|
||||
export default class LocalStorage {
|
||||
/*
|
||||
function getPageLocalStorage(pageHash) {
|
||||
|
||||
let ls;
|
||||
try {
|
||||
ls = JSON.parse(localStorage.getItem(pageHash));
|
||||
} catch {
|
||||
|
||||
}
|
||||
|
||||
if (Validation.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 (Validation.isEmpty(ls)) ls = {};
|
||||
|
||||
setPageLocalStorage(pageHash, ls);
|
||||
}
|
||||
*/
|
||||
|
||||
static getLocalStorage(key) {
|
||||
return JSON.parse(localStorage.getItem(key));
|
||||
}
|
||||
|
||||
static 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);
|
||||
}
|
||||
*/
|
||||
}
|
||||
24
static/js/lib/utils.js
Normal file
24
static/js/lib/utils.js
Normal file
@@ -0,0 +1,24 @@
|
||||
// Utility functions
|
||||
/*
|
||||
function $(selector) {
|
||||
return document.querySelector(selector);
|
||||
}
|
||||
|
||||
function $$(selector) {
|
||||
return document.querySelectorAll(selector);
|
||||
}
|
||||
*/
|
||||
export default class Utils {
|
||||
static getListFromDict(dict) {
|
||||
let list = [];
|
||||
for (let key in dict) {
|
||||
list.push(dict[key]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
static consoleLogIfNotProductionEnvironment(message) {
|
||||
if (!environment.is_production) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
158
static/js/lib/validation.js
Normal file
158
static/js/lib/validation.js
Normal file
@@ -0,0 +1,158 @@
|
||||
|
||||
export default class Validation {
|
||||
/*
|
||||
isNullOrWhitespace(v) {
|
||||
let txt = JSON.stringify(v).replace('/\s\g', '');
|
||||
return (txt == '' || 'null');
|
||||
}
|
||||
*/
|
||||
|
||||
static 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 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;
|
||||
}
|
||||
|
||||
static isValidNumber(value, positiveOnly) {
|
||||
return !Validation.isEmpty(value) && !isNaN(value) && (!positiveOnly || parseFloat(value) > 0);
|
||||
}
|
||||
|
||||
static getDataContentType(params) {
|
||||
|
||||
var data = null;
|
||||
var contentType = '';
|
||||
|
||||
if (!Validation.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 };
|
||||
}
|
||||
|
||||
static arrayContainsItem(array, itemValue) {
|
||||
|
||||
var hasItem = false;
|
||||
|
||||
if (!Validation.isEmpty(array) && !Validation.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;
|
||||
}
|
||||
|
||||
static dictHasKey(d, k) {
|
||||
return (k in d);
|
||||
}
|
||||
static areEqualDicts(dict1, dict2) {
|
||||
const keys1 = Object.keys(dict1);
|
||||
const keys2 = Object.keys(dict2);
|
||||
|
||||
if (keys1.length !== keys2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let key of keys1) {
|
||||
if (dict1[key] !== dict2[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static imageExists(url, callback) {
|
||||
|
||||
var img = new Image();
|
||||
|
||||
img.onload = function() { callback(true); };
|
||||
img.onerror = function() { callback(false); };
|
||||
img.src = url;
|
||||
}
|
||||
|
||||
static toFixedOrDefault(value, decimalPlaces, defaultValue = null) {
|
||||
return Validation.isValidNumber(value) ? parseFloat(value).toFixed(decimalPlaces) : defaultValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user