2018-12-26 07:45:53 +00:00
|
|
|
var helper = (function() {
|
|
|
|
|
|
|
|
// methods on this object
|
2019-01-03 22:30:27 +00:00
|
|
|
var e = function(selector) {
|
2018-12-26 07:45:53 +00:00
|
|
|
return document.querySelector(selector);
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var eA = function(selector) {
|
2018-12-26 07:45:53 +00:00
|
|
|
return document.querySelectorAll(selector);
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var toggleClass = function(element, theClassName) {
|
2018-12-26 07:45:53 +00:00
|
|
|
element.classList.toggle(theClassName);
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var addClass = function(element, theClassName) {
|
2018-12-26 07:45:53 +00:00
|
|
|
element.classList.add(theClassName);
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var removeClass = function(element, theClassName) {
|
2018-12-26 07:45:53 +00:00
|
|
|
element.classList.remove(theClassName);
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var getDateTime = function() {
|
2018-12-26 07:45:53 +00:00
|
|
|
var dateStamp = new Date();
|
|
|
|
var object = {
|
|
|
|
// string: dateStamp.constructor(),
|
|
|
|
// time: dateStamp.getTime()
|
|
|
|
date: dateStamp.getDate(),
|
|
|
|
day: dateStamp.getDay(),
|
|
|
|
year: dateStamp.getFullYear(),
|
|
|
|
hours: dateStamp.getHours(),
|
|
|
|
milliseconds: dateStamp.getMilliseconds(),
|
|
|
|
minutes: dateStamp.getMinutes(),
|
|
|
|
month: dateStamp.getMonth(),
|
|
|
|
seconds: dateStamp.getSeconds()
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
2019-01-05 20:57:21 +00:00
|
|
|
var month = function(index) {
|
|
|
|
var all = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
|
|
return all[index];
|
|
|
|
};
|
|
|
|
|
|
|
|
var day = function(index) {
|
2019-01-06 05:08:26 +00:00
|
|
|
var all = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
2019-01-05 20:57:21 +00:00
|
|
|
return all[index];
|
|
|
|
};
|
|
|
|
|
2018-12-26 07:45:53 +00:00
|
|
|
var sortObject = function(object, key) {
|
|
|
|
object.sort(function(a, b) {
|
2019-01-03 22:30:27 +00:00
|
|
|
var textA = a[key];
|
|
|
|
if (typeof textA == "string") {
|
|
|
|
textA = textA.toLowerCase();
|
|
|
|
};
|
|
|
|
var textB = b[key];
|
|
|
|
if (typeof textB == "string") {
|
|
|
|
textB = textB.toLowerCase();
|
|
|
|
};
|
2018-12-26 07:45:53 +00:00
|
|
|
if (textA < textB) {
|
|
|
|
return -1;
|
|
|
|
} else if (textA > textB) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var applyOptions = function(defaultOptions, options) {
|
2018-12-26 07:45:53 +00:00
|
|
|
if (defaultOptions && options) {
|
|
|
|
if (options) {
|
|
|
|
for (var key in options) {
|
|
|
|
if (key in defaultOptions) {
|
|
|
|
defaultOptions[key] = options[key];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return defaultOptions;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var hexToRgb = function(hex) {
|
2018-12-26 07:45:53 +00:00
|
|
|
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
|
|
if (result) {
|
|
|
|
result = {
|
|
|
|
r: parseInt(result[1], 16),
|
|
|
|
g: parseInt(result[2], 16),
|
|
|
|
b: parseInt(result[3], 16)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var rgbToHex = function(rgbObject) {
|
2018-12-26 07:45:53 +00:00
|
|
|
var componentToHex = function(hexPart) {
|
|
|
|
hexPart = hexPart.toString(16);
|
|
|
|
if (hexPart.length == 1) {
|
|
|
|
hexPart = "0" + hexPart
|
|
|
|
};
|
|
|
|
return hexPart;
|
|
|
|
};
|
|
|
|
var result = "#" + componentToHex(rgbObject.r) + componentToHex(rgbObject.g) + componentToHex(rgbObject.b);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-01-03 22:30:27 +00:00
|
|
|
var makeNode = function(override) {
|
|
|
|
var options = {
|
|
|
|
tag: null,
|
|
|
|
classes: null,
|
|
|
|
text: null,
|
|
|
|
url: null,
|
|
|
|
index: null,
|
|
|
|
attr: null
|
|
|
|
};
|
|
|
|
if (override) {
|
|
|
|
options = helper.applyOptions(options, override);
|
|
|
|
};
|
|
|
|
var element = document.createElement(options.tag);
|
|
|
|
if (options.text != null) {
|
|
|
|
element.textContent = options.text;
|
|
|
|
};
|
|
|
|
if (options.attr != null) {
|
|
|
|
options.attr.forEach(function(arrayItem, index) {
|
|
|
|
if ("key" in arrayItem && "value" in arrayItem) {
|
|
|
|
element.setAttribute(arrayItem.key, arrayItem.value);
|
|
|
|
} else if ("key" in arrayItem) {
|
|
|
|
element.setAttribute(arrayItem.key, "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return element;
|
|
|
|
};
|
|
|
|
|
|
|
|
function _makeAddress(path) {
|
|
|
|
var array;
|
|
|
|
if (path.indexOf("[") != -1 && path.indexOf("]") != -1) {
|
|
|
|
array = path.split(".").join(",").split("[").join(",").split("]").join(",").split(",");
|
|
|
|
for (var i = 0; i < array.length; i++) {
|
|
|
|
if (array[i] == "") {
|
|
|
|
array.splice(i, 1);
|
|
|
|
};
|
|
|
|
if (!isNaN(parseInt(array[i], 10))) {
|
|
|
|
array[i] = parseInt(array[i], 10);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
array = path.split(".");
|
|
|
|
};
|
|
|
|
return array;
|
|
|
|
};
|
|
|
|
|
|
|
|
function setObject(options) {
|
|
|
|
var defaultOptions = {
|
|
|
|
path: null,
|
|
|
|
object: null,
|
|
|
|
newValue: null
|
|
|
|
};
|
|
|
|
if (options) {
|
|
|
|
var defaultOptions = applyOptions(defaultOptions, options);
|
|
|
|
};
|
|
|
|
var address = _makeAddress(defaultOptions.path);
|
|
|
|
var _setData = function() {
|
|
|
|
while (address.length > 1) {
|
|
|
|
// shift off and store the first key
|
|
|
|
var currentKey = address.shift();
|
|
|
|
// if the key is not found make a new object
|
|
|
|
if (!(currentKey in defaultOptions.object)) {
|
|
|
|
// make an empty object in the current object level
|
|
|
|
if (isNaN(currentKey)) {
|
|
|
|
defaultOptions.object[currentKey] = {};
|
|
|
|
} else {
|
|
|
|
defaultOptions.object[currentKey] = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// drill down the object with the first key
|
|
|
|
defaultOptions.object = defaultOptions.object[currentKey];
|
|
|
|
};
|
|
|
|
var finalKey = address.shift();
|
|
|
|
defaultOptions.object[finalKey] = defaultOptions.newValue;
|
|
|
|
};
|
|
|
|
if (defaultOptions.object != null && defaultOptions.path != null && defaultOptions.newValue != null) {
|
|
|
|
_setData();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function getObject(options) {
|
|
|
|
var defaultOptions = {
|
|
|
|
object: null,
|
|
|
|
path: null
|
|
|
|
};
|
|
|
|
if (options) {
|
|
|
|
var defaultOptions = applyOptions(defaultOptions, options);
|
|
|
|
};
|
|
|
|
var address = _makeAddress(defaultOptions.path);
|
|
|
|
var _getData = function() {
|
|
|
|
while (address.length > 1) {
|
|
|
|
// shift off and store the first key
|
|
|
|
var currentKey = address.shift();
|
|
|
|
// if the key is not found make a new object
|
|
|
|
if (!(currentKey in defaultOptions.object)) {
|
|
|
|
// make an empty object in the current object level
|
|
|
|
if (isNaN(currentKey)) {
|
|
|
|
defaultOptions.object[currentKey] = {};
|
|
|
|
} else {
|
|
|
|
defaultOptions.object[currentKey] = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// drill down the object with the first key
|
|
|
|
defaultOptions.object = defaultOptions.object[currentKey];
|
|
|
|
};
|
|
|
|
var finalKey = address.shift();
|
|
|
|
if (!(finalKey in defaultOptions.object)) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return defaultOptions.object[finalKey];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (defaultOptions.object != null && defaultOptions.path != null) {
|
|
|
|
return _getData();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function makeObject(string) {
|
|
|
|
var _stringOrBooleanOrNumber = function(stringToTest) {
|
|
|
|
if (stringToTest == "true") {
|
|
|
|
return true;
|
|
|
|
} else if (stringToTest == "false") {
|
|
|
|
return false;
|
|
|
|
} else if (stringToTest.indexOf("#") != -1) {
|
|
|
|
return stringToTest.substr(1, kevValuePair[1].length);
|
|
|
|
} else {
|
|
|
|
return "\"" + stringToTest + "\"";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// if argument is a string
|
|
|
|
if (typeof string == "string") {
|
|
|
|
// start building the object
|
|
|
|
var objectString = "{";
|
|
|
|
// split the string on comma not followed by a space
|
|
|
|
// split on character if not followed by a space
|
|
|
|
var items = string.split(/,(?=\S)/);
|
|
|
|
// loop over each item
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
// split each would be object key values pair
|
|
|
|
// split on character if not followed by a space
|
|
|
|
var kevValuePair = items[i].split(/:(?=\S)/);
|
|
|
|
// get the key
|
|
|
|
var key = "\"" + kevValuePair[0] + "\"";
|
|
|
|
var value;
|
|
|
|
// if the value has + with a space after it
|
|
|
|
if (/\+(?=\S)/.test(kevValuePair[1])) {
|
|
|
|
// remove first + symbol
|
|
|
|
kevValuePair[1] = kevValuePair[1].substr(1, kevValuePair[1].length);
|
|
|
|
// split the would be values
|
|
|
|
// split on character if not followed by a space
|
|
|
|
var all_value = kevValuePair[1].split(/\+(?=\S)/);
|
|
|
|
// if there are multiple values make an array
|
|
|
|
value = "["
|
|
|
|
for (var q = 0; q < all_value.length; q++) {
|
|
|
|
value += _stringOrBooleanOrNumber(all_value[q]) + ",";
|
|
|
|
};
|
|
|
|
// remove last comma
|
|
|
|
value = value.substr(0, value.length - 1);
|
|
|
|
// close array
|
|
|
|
value += "]"
|
|
|
|
} else {
|
|
|
|
value = _stringOrBooleanOrNumber(kevValuePair[1]);
|
|
|
|
};
|
|
|
|
objectString += key + ":" + value + ",";
|
|
|
|
};
|
|
|
|
// remove last comma
|
|
|
|
objectString = objectString.substr(0, objectString.length - 1);
|
|
|
|
// close object
|
|
|
|
objectString += "}";
|
|
|
|
var object = JSON.parse(objectString);
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var allEqual = function(array) {
|
|
|
|
return array.every(function(arrayItem) {
|
|
|
|
return arrayItem === array[0];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-26 07:45:53 +00:00
|
|
|
// exposed methods
|
|
|
|
return {
|
|
|
|
e: e,
|
|
|
|
eA: eA,
|
|
|
|
toggleClass: toggleClass,
|
|
|
|
addClass: addClass,
|
|
|
|
removeClass: removeClass,
|
2019-01-03 22:30:27 +00:00
|
|
|
allEqual: allEqual,
|
2018-12-26 07:45:53 +00:00
|
|
|
getDateTime: getDateTime,
|
2019-01-05 20:57:21 +00:00
|
|
|
month: month,
|
|
|
|
day: day,
|
2018-12-26 07:45:53 +00:00
|
|
|
sortObject: sortObject,
|
|
|
|
applyOptions: applyOptions,
|
|
|
|
hexToRgb: hexToRgb,
|
2019-01-03 22:30:27 +00:00
|
|
|
rgbToHex: rgbToHex,
|
|
|
|
makeNode: makeNode,
|
|
|
|
setObject: setObject,
|
|
|
|
getObject: getObject,
|
|
|
|
makeObject: makeObject
|
2018-12-26 07:45:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|