updated by GasGit automation
This commit is contained in:
parent
88bcc942fc
commit
93e24bccd0
1 changed files with 89 additions and 196 deletions
265
scripts/Utils.js
265
scripts/Utils.js
|
@ -1,199 +1,92 @@
|
||||||
var Utils= (function (utils) {
|
var Utils = (function(ns) {
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wrap up svg element in xml
|
* get the stack
|
||||||
* @param {string} content the svg stuff
|
* @return {string} the stack trace
|
||||||
* @return {string}
|
|
||||||
*/
|
*/
|
||||||
utils.svgWrap = function(content) {
|
ns.errorStack = function(e) {
|
||||||
var q = "?";
|
try {
|
||||||
|
// throw a fake error
|
||||||
return '<' + q + 'xml version="1.0" encoding="UTF-8"' + q + '>' +
|
throw new Error(); //x is undefined and will fail under use struct- ths will provoke an error so i can get the call stack
|
||||||
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' + content;
|
}
|
||||||
|
catch(err) {
|
||||||
};
|
return 'Error:' + e + '\n' + err.stack.split('\n').slice(1).join('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* a little like the jquery.extend() function
|
* @param {[*]} arguments unspecified number and type of args
|
||||||
* the first object is extended by the 2nd and subsequent objects - its always deep
|
* @return {string} a digest of the arguments to use as a key
|
||||||
* @param {object} ob to be extended
|
|
||||||
* @param {object...} repeated for as many objects as there are
|
|
||||||
* @return {object} the first object extended
|
|
||||||
*/
|
*/
|
||||||
utils.extend = function extend () {
|
ns.keyDigest = function () {
|
||||||
|
// conver args to an array and digest them
|
||||||
// we have a variable number of arguments
|
return Utilities.base64Encode (
|
||||||
if (!arguments.length) {
|
Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_1,Array.prototype.slice.call(arguments).map(function (d) {
|
||||||
// default with no arguments is to return undefined
|
return (Object(d) === d) ? JSON.stringify(d) : d.toString();
|
||||||
return undefined;
|
}).join("-")));
|
||||||
}
|
|
||||||
|
|
||||||
// validate we have all objects
|
|
||||||
var extenders = [],targetOb;
|
|
||||||
for (var i = 0; i < arguments.length; i++) {
|
|
||||||
if(arguments[i]) {
|
|
||||||
if (!utils.isObject(arguments[i])) {
|
|
||||||
throw 'extend arguments must be objects not ' + arguments[i];
|
|
||||||
}
|
|
||||||
if (i ===0 ) {
|
|
||||||
targetOb = arguments[i];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
extenders.push (arguments[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// set defaults from extender objects
|
|
||||||
extenders.forEach(function(d) {
|
|
||||||
recurse(targetOb, d);
|
|
||||||
});
|
|
||||||
|
|
||||||
return targetOb;
|
|
||||||
|
|
||||||
// run do a deep check
|
|
||||||
function recurse(tob,sob) {
|
|
||||||
Object.keys(sob).forEach(function (k) {
|
|
||||||
|
|
||||||
// if target ob is completely undefined, then copy the whole thing
|
|
||||||
if (utils.isUndefined(tob[k])) {
|
|
||||||
tob[k] = sob[k];
|
|
||||||
}
|
|
||||||
|
|
||||||
// if source ob is an object then we need to recurse to find any missing items in the target ob
|
|
||||||
else if (utils.isObject(sob[k])) {
|
|
||||||
recurse (tob[k] , sob[k]);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if item is undefined
|
* this is clone that will really be an extend
|
||||||
* @param {*} item the item to check
|
* @param {object} cloneThis
|
||||||
* @return {boolean} whether it is undefined
|
* @return {object} a clone
|
||||||
**/
|
|
||||||
utils.isUndefined = function (item) {
|
|
||||||
return typeof item === 'undefined';
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* check if item is undefined
|
|
||||||
* @param {*} item the item to check
|
|
||||||
* @param {*} defaultValue the default value if undefined
|
|
||||||
* @return {*} the value with the default applied
|
|
||||||
**/
|
|
||||||
utils.applyDefault = function (item,defaultValue) {
|
|
||||||
return utils.isUndefined(item) ? defaultValue : item;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* isObject
|
|
||||||
* check if an item is an object
|
|
||||||
* @param {object} obj an item to be tested
|
|
||||||
* @return {boolean} whether its an object
|
|
||||||
**/
|
|
||||||
utils.isObject = function (obj) {
|
|
||||||
return obj === Object(obj);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* clone
|
|
||||||
* clone a stringifyable object
|
|
||||||
* @param {object} obj an item to be cloned
|
|
||||||
* @return {object} the cloned object
|
|
||||||
**/
|
|
||||||
utils.clone = function (obj) {
|
|
||||||
return utils.isObject(obj) ? JSON.parse(JSON.stringify(obj)) : obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* convenience getbyid
|
|
||||||
* @param {string} id element id
|
|
||||||
* @return {element} the element
|
|
||||||
*/
|
*/
|
||||||
utils.el = function(id) {
|
ns.clone = function (cloneThis) {
|
||||||
return document.getElementById(id);
|
return ns.vanExtend ({} , cloneThis);
|
||||||
};
|
|
||||||
/**
|
|
||||||
* convenience aqdd element
|
|
||||||
* @param {element} parent the parent
|
|
||||||
* @param {string} type the element type
|
|
||||||
* @param {string} aclass the optional class list
|
|
||||||
* @return {element} the element
|
|
||||||
*/
|
|
||||||
utils.elAdd = function (parent, type, aclass) {
|
|
||||||
var elem = document.createElement(type || "div");
|
|
||||||
if(aclass) elem.className = aclass;
|
|
||||||
parent.appendChild(elem);
|
|
||||||
return elem;
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* convenience aqdd text element
|
|
||||||
* @param {element} parent the parent
|
|
||||||
* @param {string} text the text to assign
|
|
||||||
* @return {element} the element
|
|
||||||
*/
|
|
||||||
utils.textAdd = function (parent, text) {
|
|
||||||
var elem = document.createTextNode(text);
|
|
||||||
parent.appendChild(elem);
|
|
||||||
return elem;
|
|
||||||
};
|
|
||||||
/**
|
|
||||||
* create a column label for sheet address, starting at 1 = A, 27 = AA etc..
|
|
||||||
* @param {number} columnNumber the column number
|
|
||||||
* @return {string} the address label
|
|
||||||
*/
|
|
||||||
utils.columnLabelMaker = function (columnNumber,s) {
|
|
||||||
s = String.fromCharCode(((columnNumber-1) % 26) + 'A'.charCodeAt(0)) + ( s || '' );
|
|
||||||
return columnNumber > 26 ? utils.columnLabelMaker ( Math.floor( (columnNumber-1) /26 ) , s ) : s;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* hide an element
|
|
||||||
* @param {element} element to hide
|
|
||||||
* @return {element} for chaining
|
|
||||||
*/
|
|
||||||
utils.hide = function (element) {
|
|
||||||
return utils.show(element,"none");
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* show an element
|
|
||||||
* @param {element} element to hide
|
|
||||||
* @param {string} display style (default block)
|
|
||||||
* @return {element} for chaining
|
|
||||||
*/
|
|
||||||
utils.show = function (element,display) {
|
|
||||||
element.style.display=display || "block";
|
|
||||||
return element;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checksum
|
|
||||||
* create a checksum on some string or object
|
|
||||||
* @param {*} o the thing to generate a checksum for
|
|
||||||
* @return {number} the checksum
|
|
||||||
**/
|
|
||||||
utils.checksum = function (o) {
|
|
||||||
// just some random start number
|
|
||||||
var c = 23;
|
|
||||||
if (!utils.isUndefined(o)){
|
|
||||||
var s = (utils.isObject(o) || Array.isArray(o)) ? JSON.stringify(o) : o.toString();
|
|
||||||
for (var i = 0; i < s.length; i++) {
|
|
||||||
c += (s.charCodeAt(i) * (i + 1));
|
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* recursively extend an object with other objects
|
||||||
|
* @param {[object]} obs the array of objects to be merged
|
||||||
|
* @return {object} the extended object
|
||||||
|
*/
|
||||||
|
ns.vanMerge = function(obs) {
|
||||||
|
return (obs || []).reduce(function(p, c) {
|
||||||
|
return ns.vanExtend(p, c);
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* recursively extend a single obbject with another
|
||||||
|
* @param {object} result the object to be extended
|
||||||
|
* @param {object} opt the object to extend by
|
||||||
|
* @return {object} the extended object
|
||||||
|
*/
|
||||||
|
ns.vanExtend = function(result, opt) {
|
||||||
|
result = result || {};
|
||||||
|
opt = opt || {};
|
||||||
|
return Object.keys(opt).reduce(function(p, c) {
|
||||||
|
// if its an object
|
||||||
|
if (ns.isVanObject(opt[c])) {
|
||||||
|
p[c] = ns.vanExtend(p[c], opt[c]);
|
||||||
|
} else {
|
||||||
|
p[c] = opt[c];
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}, result);
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* use a default value if undefined
|
||||||
|
* @param {*} value the value to test
|
||||||
|
* @param {*} defValue use this one if undefined
|
||||||
|
* @return {*} the new value
|
||||||
|
*/
|
||||||
|
ns.fixDef = function(value, defValue) {
|
||||||
|
return typeof value === typeof undefined ? defValue : value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* see if something is undefined
|
||||||
|
* @param {*} value the value to check
|
||||||
|
* @return {bool} whether it was undefined
|
||||||
|
*/
|
||||||
|
ns.isUndefined = function(value) {
|
||||||
|
return typeof value === typeof undefined;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* simple test for an object type
|
||||||
|
* @param {*} the thing to test
|
||||||
|
* @return {bool} whether it was an object
|
||||||
|
*/
|
||||||
|
ns.isVanObject = function(value) {
|
||||||
|
return typeof value === "object" && !Array.isArray(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return ns;
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return utils;
|
|
||||||
|
|
||||||
})(Utils || {});
|
})(Utils || {});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue