nscomponentsreact
Version:
React Wrapper Components for NSComponents
1,812 lines (1,722 loc) • 202 kB
JavaScript
var nsModuleExport = (function()
{
var nsModuleExport = function(root,name,prototype)
{
if(typeof exports === 'object' && typeof module === 'object')
{
module.exports[name] = prototype;
}
else if (typeof define === "function" && define.amd)
{
define(name,[], function () {return prototype;});
}
else if(typeof exports === 'object')
{
exports[name] = prototype;
}
else
{
root[name] = prototype;
}
};
return nsModuleExport;
})();
(function(root,name,prototype)
{
if(typeof exports === 'object' && typeof module === 'object')
{
module.exports[name] = prototype;
}
else if (typeof define === "function" && define.amd)
{
define(name,[], function () {return prototype;});
}
else if(typeof exports === 'object')
{
exports[name] = prototype;
}
else
{
root[name] = prototype;
}
})(this,"nsModuleExport",nsModuleExport);
var NSUtil = (function()
{
function NSUtil()
{
this.__sheetId = "nsStyle";
this.KEYCODE = {BACKSPACE:8, TAB:9, ENTER:13, SHIFT:16, CTRL:17, ALT:18, ESC:27,SPACE:32,PAGEUP:33,PAGEDOWN:34,LEFT:37,UP:38,RIGHT:39,DOWN:40,INSERT:45,DELETE:46};
this.ANIMATIONDELAY = 10;
//for nsMenu and nsPinTip
this.POS_TOP = "top";
this.POS_BOTTOM = "bottom";
this.POS_LEFT = "left";
this.POS_RIGHT = "right";
this.POS_TOPLEFT = "top-left";
this.POS_TOPRIGHT = "top-right";
this.POS_BOTTOMLEFT = "bottom-left";
this.POS_BOTTOMRIGHT = "bottom-right";
};
NSUtil.prototype.getDomVariables = function()
{
var retValue = {doc: null,win: null};
if(typeof document === 'undefined')
{
retValue.doc = {
body: {},
addEventListener: function addEventListener() {},
removeEventListener: function removeEventListener() {},
activeElement: {
blur: function blur() {},
nodeName: '',
},
querySelector: function querySelector() {
return null;
},
querySelectorAll: function querySelectorAll() {
return [];
},
getElementById: function getElementById() {
return null;
},
createEvent: function createEvent() {
return {
initEvent: function initEvent() {},
};
},
createElement: function createElement() {
return {
children: [],
childNodes: [],
style: {},
setAttribute: function setAttribute() {},
getElementsByTagName: function getElementsByTagName() {
return [];
},
};
},
location: { hash: '' },
};
}
else
{
retValue.doc = document;
}
if(typeof window === 'undefined')
{
retValue.win =
{
document: retValue.doc,
navigator: {
userAgent: '',
},
location: {},
history: {},
CustomEvent: function CustomEvent() {
return this;
},
addEventListener: function addEventListener() {},
removeEventListener: function removeEventListener() {},
getComputedStyle: function getComputedStyle() {
return {
getPropertyValue: function getPropertyValue() {
return '';
},
};
},
Image: function Image() {},
Date: function Date() {},
screen: {},
setTimeout: function setTimeout() {},
clearTimeout: function clearTimeout() {},
};
}
else
{
retValue.win = window;
}
return retValue;
};
NSUtil.prototype.getSupportedEvents = function ()
{
var obj = this.getDomVariables();
var win = obj.win;
var doc = obj.doc;
var retValue = {};
retValue.touch = (win.Modernizr && win.Modernizr.touch === true) || (!!((win.navigator.maxTouchPoints > 0) || ('ontouchstart' in win) || (win.DocumentTouch && doc instanceof win.DocumentTouch)));
retValue.pointerEvents = !!win.PointerEvent && ('maxTouchPoints' in win.navigator) && win.navigator.maxTouchPoints > 0;
retValue.passiveListener = (function checkPassiveListener() {
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
// eslint-disable-next-line
get: function get() {
supportsPassive = true;
},
});
win.addEventListener('testPassiveListener', null, opts);
} catch (e) {
// No support
}
return supportsPassive;
}());
retValue.gestures = 'ongesturestart' in win;
return retValue;
};
NSUtil.prototype.isMobile = function(win)
{
var win = this.getWin(win);
var userAgent = (win.navigator && win.navigator.userAgent) ? win.navigator.userAgent : null;
var objOS = {android:{pattern:"Android",flags:"i"} ,blackberry:{pattern:"BlackBerry",flags:"i"} ,ios:{pattern:"iPhone|iPad|iPod",flags:"i"} ,opera:{pattern:"Opera Mini",flags:"i"} ,windows:{pattern:"IEMobile",flags:"i"}};
var objRet = {};
if(userAgent)
{
var any = false;
var item = null;
for(var os in objOS)
{
item = objOS[os];
objRet[os] = userAgent.match(new RegExp(item.pattern,item.flags));
if(objRet[os])
{
any = true;
}
}
objRet["any"] = any;
}
else
{
for(var os in objOS)
{
objRet[os] = false;
}
objRet["any"] = false;
}
return objRet;
};
NSUtil.prototype.getElement = function (element,doc)
{
if(element)
{
if(this.isObject(element))
{
return element;
}
else if(element.length > 0)
{
doc = this.getDoc(doc);
return doc.getElementById(element);
}
}
return null;
};
NSUtil.prototype.getElementFromHtml = function (html)
{
if(html)
{
var div = this.createDiv();
div.innerHTML = html.toString();
var child = (div.firstChild !== div.lastChild || !div.firstChild) ? div : div.firstChild;
return child;
}
return null;
};
NSUtil.prototype.getReferenceFromHtml = function(html)
{
var comp = this.getElementFromHtml(html);
if(comp)
{
var arrElement = comp.querySelectorAll("[ns-ref],[data-ns-ref]");
var objRefs = {};
var ref = comp.getAttribute("ns-ref") || comp.getAttribute("data-ns-ref");
if(ref)
{
objRefs[ref] = comp;
}
if(arrElement && arrElement.length)
{
for(var count = 0;count < arrElement.length;count++)
{
var element = arrElement[count];
ref = element.getAttribute("ns-ref") || element.getAttribute("data-ns-ref");
objRefs[ref] = element;
}
}
return {element : comp,refs: objRefs};
}
return null;
};
NSUtil.prototype.getWebComponentElement = function (element)
{
if(element && element["__impl4cf1e782hg__"])
{
return element["__impl4cf1e782hg__"];
}
return element;
};
NSUtil.prototype.hasAttribute = function (element,attributeName)
{
if(element && attributeName && element["hasAttribute"])
{
return (element.hasAttribute(attributeName) || element.hasAttribute("data-" + attributeName));
}
return false;
};
NSUtil.prototype.getAttribute = function (element,attributeName)
{
if(element && attributeName && element["hasAttribute"])
{
return (element.hasAttribute(attributeName) ? element.getAttribute(attributeName) : element.getAttribute("data-" + attributeName));
}
return null;
};
NSUtil.prototype.removeAttribute = function (element,attributeName)
{
if(element && attributeName && element["hasAttribute"])
{
return (element.hasAttribute(attributeName) ? element.removeAttribute(attributeName) : element.removeAttribute("data-" + attributeName));
}
return null;
};
NSUtil.prototype.createDiv = function (id,styleName,doc)
{
var div = this.createElement("div",id,styleName,doc);
return div;
};
NSUtil.prototype.createElement = function(type,id,styleName,doc)
{
doc = this.getDoc(doc);
var element = doc.createElement(type);
if(id)
{
element.setAttribute("id",id);
}
if(styleName)
{
element.className = styleName;
}
return element;
};
//from JQuery
NSUtil.prototype.isElementVisible = function(element)
{
return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
};
NSUtil.prototype.setDivVisibility = function (id,isVisible,doc)
{
doc = this.getDoc(doc);
var div = doc.getElementById(id);
if(div)
{
if(isVisible)
{
div.style.display = "block";
}
else
{
div.style.display = "none";
}
}
};
NSUtil.prototype.removeDiv = function (id,doc)
{
doc = this.getDoc(doc);
var div = doc.getElementById(id);
if (div)
{
var parent = div.parentNode;
if(parent)
{
parent.removeChild(div);
}
}
};
/*Utility Methods */
NSUtil.prototype.getKeys = function (object)
{
var keys = [];
for (var property in object)
{
keys.push(property);
}
return keys;
};
NSUtil.prototype.getValues = function (object)
{
var values = [];
for (var property in object)
{
values.push(object[property]);
}
return values;
};
//from https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects/16788517#16788517
//used in frameworks wrapper
NSUtil.prototype.isObjectEqual = function(origValue, newValue)
{
if (origValue === null || origValue === undefined || newValue === null || newValue === undefined)
{
return origValue === newValue;
}
// after this just checking type of one would be enough
if (origValue.constructor !== newValue.constructor)
{
return false;
}
// if they are functions, they should exactly refer to same one (because of closures)
if (origValue instanceof Function)
{
return origValue === newValue;
}
// if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES)
if (origValue instanceof RegExp)
{
return origValue === newValue;
}
if (origValue === newValue || origValue.valueOf() === newValue.valueOf())
{
return true;
}
if (Array.isArray(origValue) && origValue.length !== newValue.length)
{
return false;
}
// if they are dates, they must had equal valueOf
if (origValue instanceof Date)
{
return false;
}
// if they are strictly equal, they both need to be object at least
if (!(origValue instanceof Object))
{
return false;
}
if (!(newValue instanceof Object))
{
return false;
}
// recursive object equality check
var arrKeys = Object.keys(origValue);
var arrNewKeys = Object.keys(newValue);
for(count = 0,len = arrNewKeys,length;count < len;count++)
{
if(arrKeys.indexOf(count) !== -1)
{
return false;
}
for(innerCount = 0,len = arrKeys,length;innerCount < len;innerCount++)
{
return this.isObjectEqual(origValue[innerCount], newValue[innerCount]);
}
}
return true;
};
NSUtil.prototype.getValueByKeys = function(item,keyParam)
{
var objReturn = {};
if(item)
{
(function flatObject(item, index)
{
var toString = Object.prototype.toString;
var suffix = toString.call(item) == "[object Array]" ? "]" : "";
for(var key in item)
{
if(!item.hasOwnProperty(key))continue;
objReturn[index+key+suffix] = item[key];
if( toString.call(item[key]) == "[object Array]" )flatObject(item[key],index+key+suffix+"[");
if( toString.call(item[key]) == "[object Object]" )flatObject(item[key],index+key+suffix+".");
}
})(item,"");
}
return objReturn[keyParam];
};
//arrIgnoreDeepCopy in case there is a circular reference in Hierarchical structure like in NSNavigation
NSUtil.prototype.cloneObject = function(objSource,deepCopy,arrIgnoreKeys,arrIgnoreDeepCopy)
{
if(!arrIgnoreKeys)
{
arrIgnoreKeys = [];
}
if(!arrIgnoreDeepCopy)
{
arrIgnoreDeepCopy = [];
}
if(!objSource || typeof objSource != "object" || Object.prototype.toString.call(objSource) === "[object Function]")
{
// null, undefined, any non-object, or function
return objSource; // anything
}
if(objSource.nodeType && "cloneNode" in objSource)
{
// DOM Node
return objSource.cloneNode(true); // Node
}
if(objSource instanceof Date)
{
// Date
return new Date(objSource.getTime()); // Date
}
if(objSource instanceof RegExp)
{
// RegExp
return new RegExp(objSource); // RegExp
}
if(objSource instanceof Array)
{
var arrClone = [];
var length = objSource.length;
for (var count = 0; count < length; count++)
{
var item = objSource[count];
var obj = this.cloneObject(item,deepCopy,arrIgnoreKeys,arrIgnoreDeepCopy);
arrClone.push(obj);
}
return arrClone;
}
// we don't clone functions for performance reasons
// }else if(d.isFunction(objSource)){
// // function
// r = function(){ return objSource.apply(this, arguments); };
// generic objects
var objClone = {};
var clonedItem = null;
for (var key in objSource)
{
var isClone = true;
for (var count = 0; count < arrIgnoreKeys.length; count++)
{
if(arrIgnoreKeys[count] == key)
{
isClone = false;
break;
}
}
if(isClone)
{
if(deepCopy)
{
var isIgnoreDeepCopy = false;
for (var ignoreDeepCount = 0; ignoreDeepCount < arrIgnoreDeepCopy.length; ignoreDeepCount++)
{
if(arrIgnoreDeepCopy[ignoreDeepCount] == key)
{
isIgnoreDeepCopy = true;
break;
}
}
clonedItem = isIgnoreDeepCopy ? objSource[key] : this.cloneObject(objSource[key],deepCopy,arrIgnoreKeys,arrIgnoreDeepCopy);
}
else
{
clonedItem = objSource[key];
}
objClone[key] = clonedItem;
}
}
return objClone;
};
NSUtil.prototype.copyElement = function(sourceElement,destinationElement)
{
if(sourceElement)
{
if(destinationElement)
{
destinationElement.innerHTML = sourceElement.innerHTML;
}
else
{
destinationElement = sourceElement.cloneNode(true);
}
for (var count = 0; count < sourceElement.attributes.length; count++)
{
var attr = sourceElement.attributes.item(count);
destinationElement.setAttribute(attr.nodeName, attr.nodeValue);
}
//destinationElement.style.cssText = sourceElement.style.cssText;
}
return destinationElement;
};
NSUtil.prototype.swapArrayItems = function(arrInput,indFirst,indSecond)
{
if(arrInput && arrInput.length > indFirst && arrInput.length > indSecond)
{
var objTemp = arrInput[indFirst];
arrInput[indFirst] = arrInput[indSecond];
arrInput[indSecond] = objTemp;
return true;
}
return false;
};
NSUtil.prototype.moveArrayItem = function(arrInput,sourceIndex,destinationIndex)
{
if(arrInput && arrInput.length > destinationIndex && arrInput.length > sourceIndex && destinationIndex > -1 && sourceIndex > -1)
{
// Remove the element from the array
var removedElement = arrInput.splice(sourceIndex, 1)[0];
// At destinationIndex, remove 0 elements, insert the removedElement
arrInput.splice(destinationIndex, 0,removedElement);
return true;
}
return false;
};
NSUtil.prototype.isObject = function (refObject)
{
if (typeof refObject === 'object' && refObject !== null)
{
return true;
}
return false;
};
NSUtil.prototype.isTextNode = function (element)
{
try
{
return (element && "nodeType" in element && element.nodeType === Node.TEXT_NODE);
}
catch(err)
{
return false;
}
};
NSUtil.prototype.isElement = function (element)
{
try
{
return (element && "nodeType" in element && element.nodeType === Node.ELEMENT_NODE);
}
catch(err)
{
return false;
}
};
NSUtil.prototype.isElementOfType = function (element,tagName)
{
if(element && tagName)
{
if(element.tagName && element.tagName.toLowerCase() === tagName.toLowerCase())
{
return true;
}
}
return false;
};
NSUtil.prototype.isArray = function (refArr)
{
return refArr != null && typeof refArr == "object" &&
"splice" in refArr && "join" in refArr;
};
NSUtil.prototype.isVariable = function (variableName,context)
{
context = this.getWin(context);
return !this.isUndefined(context[variableName]);
};
//after
NSUtil.prototype.insertAfterElement = function(referenceElement,insertElement)
{
if(referenceElement && insertElement)
{
var parentElement = this.getWebComponentElement(referenceElement.parentNode);
if(parentElement)
{
referenceElement = this.getWebComponentElement(referenceElement);
insertElement = this.getWebComponentElement(insertElement);
if (parentElement.lastChild === referenceElement)
{
parentElement.appendChild(insertElement);
}
else
{
parentElement.insertBefore(insertElement,referenceElement.nextSibling);
}
}
}
};
NSUtil.prototype.insertElementAtIndex = function(parent,element,index)
{
if (!index)
{
index = 0;
}
if (index >= parent.children.length)
{
parent.appendChild(element);
}
else
{
parent.insertBefore(element,parent.children[index]);
}
};
NSUtil.prototype.swapElements = function(element1,element2)
{
if(element1 && element2)
{
var parent2 = element2.parentNode;
var next2 = element2.nextSibling;
// special case for element1 is the next sibling of element2
if (next2 === element1)
{
// just put element1 before element2
parent2.insertBefore(element1, element2);
}
else
{
// insert element2 right before element1
element1.parentNode.insertBefore(element2, element1);
// now insert element1 where element2 was
if (next2)
{
// if there was an element after element2, then insert element1 right before that
parent2.insertBefore(element1, next2);
}
else
{
// otherwise, just append as last child
parent2.appendChild(element1);
}
}
}
};
//replace
NSUtil.prototype.replaceElement = function(element,newElement,copyAttributes,moveContent,doc)
{
if(element)
{
moveContent = this.isUndefinedOrNull(moveContent) ? true : Boolean.parse(moveContent);
newElement = this.isString(newElement) ? this.createElement(newElement,null,null,doc) : newElement;
if(moveContent)
{
while(element.firstChild)
{
newElement.appendChild(element.firstChild);
}
}
if(copyAttributes && element.hasAttributes())
{
var attrs = element.attributes;
for(var count = attrs.length - 1;count >= 0;count--)
{
newElement.setAttribute(attrs[count].name, attrs[count].value);
}
}
if(element.parentNode)
{
element.parentNode.replaceChild(newElement,element);
}
}
return newElement;
};
//unwrap
NSUtil.prototype.insertElementParentsParent = function(element)
{
if(element && element.parentNode)
{
var parent = element.parentNode;
while (element.firstChild)
{
parent.insertBefore(element.firstChild,element);
}
if(element.parentNode)
{
element.parentNode.removeChild(element);
}
}
};
NSUtil.prototype.getFunction = function (functionInstance,context)
{
if(functionInstance)
{
context = this.getWin(context);
if (typeof functionInstance === "string" || functionInstance instanceof String)
{
if(this.isFunction(functionInstance))
{
functionInstance = context[functionInstance];
}
}
}
return functionInstance;
};
NSUtil.prototype.callFunction = function (functionName,context)
{
var returnValue = null;
context = this.getWin(context);
if (typeof context[functionName] === "function")
{
returnValue = context[functionName]();
}
return returnValue;
};
NSUtil.prototype.callFunctionFromString = function (functionString,replaceParameterFunction,context)
{
var returnValue = null;
if(functionString)
{
context = this.getWin(context);
var functionName = null;
var arrParameter = null;
if(functionString.indexOf("(") > -1)
{
var param = functionString.split("(");
if(param && param.length > 0)
{
functionName = param[0];
var parameter = param[1];
if(parameter && parameter.charAt(parameter.length - 1) === ")")
{
parameter = parameter.substring(0,parameter.length - 1);
var arrParam = parameter.split(",");
if(arrParam && arrParam.length > 0)
{
arrParameter = arrParam;
}
}
}
}
else
{
functionName = functionString;
}
if(this.isFunction(functionName))
{
if(!arrParameter || arrParameter.length === 0)
{
returnValue = this.callFunction(functionName);
}
else
{
if(replaceParameterFunction)
{
for(var count = 0;count < arrParameter.length;count++)
{
var newValue = replaceParameterFunction(arrParameter[count]);
if(!this.isUndefined(newValue))
{
arrParameter[count] = newValue;
}
}
}
switch (arrParameter.length)
{
case 1:
returnValue = context[functionName](arrParameter[0]);
break;
case 2:
returnValue = context[functionName](arrParameter[0],arrParameter[1]);
break;
case 3:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2]);
break;
case 4:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3]);
break;
case 5:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4]);
break;
case 6:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4],arrParameter[5]);
break;
case 7:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4],arrParameter[5],arrParameter[6]);
break;
case 8:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4],arrParameter[5],arrParameter[6],arrParameter[7]);
break;
case 9:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4],arrParameter[5],arrParameter[6],arrParameter[7],arrParameter[8]);
break;
case 10:
returnValue = context[functionName](arrParameter[0],arrParameter[1],arrParameter[2],arrParameter[3],arrParameter[4],arrParameter[5],arrParameter[6],arrParameter[7],arrParameter[8],arrParameter[9]);
break;
}
}
}
}
return returnValue;
};
NSUtil.prototype.callFunctionOfObject = function (obj,functionName,...args)
{
var returnValue = null;
if(obj)
{
if (typeof obj[functionName] === "function")
{
returnValue = obj[functionName](args);
}
else
{
console.log(functionName + " does not exist or is not a function.");
}
}
return returnValue;
};
NSUtil.prototype.isElementInDOM = function(node,doc)
{
doc = this.getDoc(doc);
return (node === doc.body) ? false : doc.body.contains(node);
};
NSUtil.prototype.isElementInParent = function(node,parent)
{
if(node && parent)
{
while (node.parentNode)
{
if (node.parentNode === parent)
{
return true;
}
node = node.parentNode;
}
}
return false;
};
NSUtil.prototype.isElementInOrParent = function(node,parent,onlyContains)
{
if(node && parent)
{
onlyContains = Boolean.parse(onlyContains);
return ((node === parent && !onlyContains) || this.isElementInParent(node,parent));
}
return false;
};
NSUtil.prototype.isElementInView = function(element,parent,doc)
{
doc = this.getDoc(doc);
var rect = element.getBoundingClientRect();
var elem = element;
var top = rect.top;
var height = rect.height;
do
{
if (elem && elem.parentNode)
{
elem = elem.parentNode;
rect = elem.getBoundingClientRect();
if (!(top <= rect.bottom))
{
return false;
}
if (top + height <= rect.top)
{
return false;
}
}
}
while (elem && elem !== parent && elem.parentNode);
var retValue = (top <= ((doc.documentElement && doc.documentElement.clientHeight) || 0));
return retValue;
};
NSUtil.prototype.scrollIntoView = function(element,parent,doc)
{
if(!this.isElementInView(element,parent,doc))
{
if (parent.clientHeight !== parent.scrollHeight)
{
parent.scrollTop = element.offsetTop;
}
if (!this.isElementInView(element, parent, doc))
{
element.scrollIntoView();
}
}
};
NSUtil.prototype.isElementVisible = function(element,win)
{
if(element)
{
if(!element.offsetParent)
{
return false;
}
win = this.getWin(win);
var style = win.getComputedStyle(element);
if(style.display === "none" || style.visibility === "hidden")
{
return false;
}
}
return true;
};
NSUtil.prototype.isFunction = function(refFunction,context)
{
context = this.getWin(context);
if(refFunction && ((typeof refFunction == "function") || (typeof context[refFunction] === "function")))
{
return true;
}
return false;
};
NSUtil.prototype.isString = function(object)
{
return typeof object == "string";
};
NSUtil.prototype.isNumber = function(object)
{
try
{
if(typeof object == "number")
{
return true;
}
var regex = /^([+\-])?[0-9]+(\.?)([0-9]+)?(e[0-9]+)?$/;
if (!object.match(regex))
{
return false;
}
value = parseFloat(object);
return !isNaN(value) && isFinite(value);
}
catch(error)
{
}
return false;
};
NSUtil.prototype.isBoolean = function(object)
{
return typeof object == "boolean";
};
NSUtil.prototype.isUndefined = function(object)
{
return typeof object == "undefined";
};
NSUtil.prototype.isUndefinedOrNull = function(object)
{
return (this.isUndefined(object) || object === null);
};
NSUtil.prototype.isStringValid = function(objString)
{
if(!objString || objString === "")
{
return false;
}
return true;
};
NSUtil.prototype.toCamelCase = function(objString,isFirstLetterCapital)
{
if(this.isString(objString))
{
return ((isFirstLetterCapital ? "-" : "") + objString).replace(/-+([^-])/g, function(str, char)
{
return char.toUpperCase();
});
}
return objString;
};
NSUtil.prototype.fromCamelCase = function(objString)
{
if(this.isString(objString))
{
return objString.replace(/([A-Z]+)/g, function(match,letter)
{
return '-' + letter.toLowerCase();
});
}
return objString;
};
//"thisIsATest" --> "this-is-a-test"
NSUtil.prototype.toKebabCase = function(objString)
{
if(this.isString(objString))
{
objString.replace(/([A-Z])([A-Z])([a-z])/g, '$1-$2$3')
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.toLowerCase();
}
return objString;
};
/*
* 1001 - function validation failure
* 1002 - function return Parameter is not valid
*/
NSUtil.prototype.throwException = function (exceptionCode,exceptionName,exceptionDetails)
{
function nsException()
{
this.code = exceptionCode;
this.name = exceptionName;
this.details = exceptionDetails;
this.toString = function()
{
return "Exception Occured with Code::" + this.code + " and Details " + this.name + "::" + this.details;
};
}
//alert("Error Occured with details code: " + exceptionCode + ",name: " + exceptionName + ",details: " + exceptionDetails);
throw new nsException();
};
//other values of navigator.appName is "Netscape","Opera"
//From IE 11 navigator.appName returns "Netscape" instead of "Microsoft Internet Explorer"
//and From IE 11 it follows standard API.
NSUtil.prototype.isBrowserIE = function ()
{
if(navigator && ((navigator.appName && navigator.appName == "Microsoft Internet Explorer") || (navigator.userAgent && navigator.userAgent.match(/Trident/))))
{
return true;
}
return false;
};
NSUtil.prototype.getBrowser = function(win)
{
win = this.getWin(win);
var objBrowser = {code:null,name:null,version:null,appName:null,appCodeName:null,platform:null,
isChrome:false,isFirefox:false,isOpera:false,isIE:false,isMSIE:false,isSafari:false};
var userAgent = win.navigator.userAgent;
if(userAgent.indexOf("Chrome") != -1)
{
objBrowser.code = "chrome";
objBrowser.name = "Google Chrome";
objBrowser.isChrome = true;
}
else if(userAgent.indexOf("Firefox") != -1)
{
objBrowser.code = "firefox";
objBrowser.name = "Mozilla Firefox";
objBrowser.isFirefox = true;
}
else if(userAgent.indexOf("Opera")!=-1)
{
objBrowser.code = "opera";
objBrowser.name = "Opera";
objBrowser.isOpera = true;
}
//for IE version 6 to 11
else if(userAgent.indexOf('MSIE')!=-1 || navigator.userAgent.indexOf('Trident') > -1)
{
objBrowser.code = "ie";
objBrowser.name = "Internet Explorer";
objBrowser.isIE = true;
objBrowser.isMSIE = true;
}
//for IE 12
else if(/x64|x32/ig.test(userAgent))
{
objBrowser.code = "ie";
objBrowser.name = "Internet Explorer 12";
objBrowser.isIE = true;
objBrowser.isMSIE = false;
}
else if (userAgent.indexOf("Safari")!=-1)
{
objBrowser.code = "safari";
objBrowser.name = "Safari";
objBrowser.isSafari = true;
}
else
{
objBrowser.code = "-1";
objBrowser.name = "Unknown Browser";
}
objBrowser.version = navigator.appVersion;
objBrowser.appName = navigator.appName;
objBrowser.appCodeName = navigator.appCodeName;
objBrowser.platform = navigator.platform;
return objBrowser;
};
NSUtil.prototype.findParentBySelector = function (element,parentSelector,doc)
{
if(element && parentSelector)
{
var matchesFn = null;
var parent = null;
doc = this.getDoc(doc);
['matches','webkitMatchesSelector','mozMatchesSelector','msMatchesSelector','oMatchesSelector'].some(function(callback)
{
if (typeof doc.body[callback] == 'function')
{
matchesFn = callback;
return true;
}
return false;
});
// traverse parents
while (element)
{
parent = element.parentElement;
if (parent && parent[matchesFn](parentSelector))
{
return parent;
}
element = parent;
}
}
return null;
};
//navigates to the DOM Heirarchy to find the nearest parent of the specified HTML tag
NSUtil.prototype.findParent = function (element,param,querySelector,till)
{
var retElement = null;
if(element)
{
var obj = this.getDomVariables();
till = till ? till : obj.doc.body;
if(param)
{
if(this.isString(param))
{
while(element)
{
if(element.tagName && element.tagName.toLowerCase() == param.toLowerCase())
{
retElement = element;
break;
}
element = element.parentElement;
}
}
else if(this.isFunction(param))
{
var start = element;
do
{
if (param(start))
{
return start;
}
if (start == till || !start.parentNode)
{
break;
}
start = start.parentNode;
}
while (start && start !== till);
}
}
else if(querySelector)
{
while(element)
{
if(element.matches(querySelector))
{
retElement = element;
break;
}
element = element.parentElement;
}
}
}
return retElement;
};
//up
NSUtil.prototype.findParentByCallback = function(element,callback,root)
{
if (!element || !this.isElementInOrParent(element,root))
{
return null;
}
var start = element;
do
{
if (callback && callback(start))
{
return start;
}
if (start === root || !start.parentNode)
{
break;
}
start = start.parentNode;
}
while(start && start !== root);
return null;
};
NSUtil.prototype.findChild = function (element,childTag)
{
if(element && childTag && element.getElementsByTagName(childTag).length > 0)
{
return element.getElementsByTagName(childTag)[0];
}
return null;
};
//prev
NSUtil.prototype.findPrevNode = function(node,callback,root,withChild)
{
withChild = this.isUndefinedOrNull(withChild) ? true : Boolean.parse(withChild);
return this.findNode(node,callback,root,false,"previousSibling",withChild ? "lastChild" : null);
};
//next
NSUtil.prototype.findNextNode = function(node,callback,root,withChild)
{
withChild = this.isUndefinedOrNull(withChild) ? true : Boolean.parse(withChild);
return this.findNode(node,callback,root,null,null,withChild ? "firstChild" : "");
};
//find
NSUtil.prototype.findNode = function(node,callback,root,recursive,siblingProperty,childProperty)
{
recursive = Boolean.parse(recursive);
siblingProperty = this.isUndefinedOrNull(siblingProperty) ? "nextSibling" : siblingProperty;
childProperty = this.isUndefinedOrNull(childProperty) ? "firstChild" : childProperty;
if (recursive && callback(node))
{
return node;
}
var start = node;
var next = null;
do
{
next = start[siblingProperty];
if (callback(next))
{
return next ? next : null;
}
if (childProperty && next && next[childProperty])
{
var nextOne = this.findNode(next[childProperty],callback,next,true,siblingProperty,childProperty);
if (nextOne)
{
return nextOne;
}
}
if(!next)
{
next = start.parentNode;
}
start = next;
}
while (start && start !== root);
return null;
};
//findWithCurrent
NSUtil.prototype.findNodeWithCurrent = function(node,callback,root,siblingProperty,childProperty)
{
siblingProperty = this.isUndefinedOrNull(siblingProperty) ? "nextSibling" : siblingProperty;
childProperty = this.isUndefinedOrNull(childProperty) ? "firstChild" : childProperty;
var next = node;
do
{
if (callback(next))
{
return next || null;
}
if (childProperty && next && next[childProperty])
{
var nextOne = this.findNodeWithCurrent(next[childProperty],callback,next,siblingProperty,childProperty);
if (nextOne)
{
return nextOne;
}
}
while (next && !next[siblingProperty] && next != root)
{
next = next.parentNode;
}
if (next && next[siblingProperty] && next != root)
{
next = next[siblingProperty];
}
}
while (next && next != root);
return null;
};
//each
//It goes through all the children of the node calling a callback function
NSUtil.prototype.loopAllChildren = function (element,callback)
{
var node = element.firstChild;
if (node)
{
while (node)
{
var next = this.findNextNode(node,Boolean,element);
var value = callback(node);
if(value == false)
{
return false;
}
// in case node is removed inside callback
if(node.parentNode)
{
value = this.loopAllChildren(node,callback);
if(!value)
{
return false;
}
}
node = next;
}
}
return true;
};
NSUtil.prototype.getZIndex = function (element,doc)
{
doc = this.getDoc(doc);
var zIndex = doc.defaultView.getComputedStyle(element).getPropertyValue("z-index");
if(element.tagName !== "BODY" && element.tagName !== "HTML" && isNaN(zIndex))
{
return this.getZIndex(element.parentNode);
}
else
{
return zIndex;
}
};
NSUtil.prototype.getStyleValue = function(element,styleProperty,getOrignalValue,getOnlyNumber,win)
{
win = this.getWin(win);
var propertyValue = null;
if(element && styleProperty && this.isElement(element))
{
element = this.getElement(element);
getOrignalValue = this.isUndefined(getOrignalValue) ? true : getOrignalValue;
var getValue = function(styleProp)
{
var retValue = null;
if(element.style && element.style[styleProp])
{
retValue = element.style[styleProp];
}
else if(win.getComputedStyle)
{
retValue = win.getComputedStyle(element, null).getPropertyValue(styleProp);
}
return retValue;
};
if(styleProperty === "display")
{
propertyValue = getValue(styleProperty);
}
else
{
var displayValue = null;
var setStyleValue = false;
if(getOrignalValue)
{
displayValue = getValue("display");
if(element.style.display)
{
setStyleValue = true;
}
element.style.display = "none";
}
propertyValue = getValue(styleProperty);
if(getOrignalValue)
{
element.style.display = (displayValue && setStyleValue) ? displayValue: "";
}
}
if(getOnlyNumber && !this.isUndefinedOrNull(propertyValue))
{
var numberFieldsReg = /^left|top|bottom|right|width|min|max|height|margin|padding|font-size/i;
if (numberFieldsReg.test(styleProperty) && /^[\-+]?[0-9.]+px$/.test(propertyValue.toString()))
{
propertyValue = parseInt(propertyValue.toString(), 10);
propertyValue = this.isNumber(propertyValue) ? +propertyValue : propertyValue;
}
}
}
return propertyValue;
};
NSUtil.prototype.getControlValue = function(control)
{
if(control)
{
if(control.tagName == "INPUT")
{
if(control.type == "text")
{
return control.value;
}
if(control.type == "checkbox")
{
return control.checked;
}
if(control.type == "radio")
{
return control.checked;
}
}
if(control.tagName == "SELECT")
{
return control.value;
}
if(control.tagName == "TEXTAREA")
{
return control.value;
}
}
return null;
};
NSUtil.prototype.setControlValue = function(control,value)
{
var retValue = null;
if(control)
{
if(control.tagName == "INPUT")
{
if(control.type == "text")
{
retValue = value;
control.value = value;
}
if(control.type == "checkbox")
{
retValue = Boolean.parse(value);
control.checked = retValue;
}
if(control.type == "radio")
{
retValue = Boolean.parse(value);
control.checked = retValue;
}
}
if(control.tagName == "SELECT")
{
retValue = value;
control.value = value;
}
}
return retValue;
};
//returns the top y coordinate of the given div used while listening to scrolling events(vertical scrolling)
NSUtil.prototype.scrollTop = function (div)
{
if(div)
{
return (div.scrollHeight - div.clientHeight);
}
return -1;
};
//returns the top x coordinate of the given div used while listening to scrolling events(horizontal scrolling)
NSUtil.prototype.scrollLeft = function (div)
{
if(div)
{
return (div.scrollWidth - div.clientWidth);
}
return -1;
};
//adds Style class to an element's styles list
NSUtil.prototype.addStyleClass = function (element,styleClass)
{
if(element && styleClass && styleClass.length > 0)
{
var arrStyleClass = styleClass.split(" ");
if(element.classList)
{
for(var count = 0;count < arrStyleClass.length;count++)
{
if(!this.hasStyleClass(element,arrStyleClass[count]))
{
element.classList.add(arrStyleClass[count]);
}
}
}
else
{
for(var count = 0;count < arrStyleClass.length;count++)
{
if(!this.hasStyleClass(element,arrStyleClass[count]))
{
if(element.className instanceof SVGAnimatedString)
{
if(element.getAttribute("class"))
{
element.setAttribute("class", element.getAttribute("class") + " " + arrStyleClass[count]);
}
else
{
element.setAttribute("class",arrStyleClass[count]);
}
}
else
{
element.className += " " + arrStyleClass[count];
}
}
}
}
}
};
//returns true if a Style class is present in element's styles list
NSUtil.prototype.hasStyleClass = function (element,styleClass)
{
if(element && styleClass && styleClass.length > 0)
{
try
{
if(!element.classList)
{
if(element.className instanceof SVGAnimatedString)
{
return new RegExp('(\\s|^)' + styleClass + '(\\s|$)').test(element.getAttribute('class'));
}
else
{
return (element.className.indexOf(" " + styleClass) > -1);
}
}
else if(element.classList.contains)
{
return element.classList.contains(styleClass);
}
}
catch(error)
{
}
}
return false;
};
//removes Style class from an element's styles list
NSUtil.prototype.removeStyleClass = function(element,styleClass)
{
if(element && styleClass && styleClass.length > 0)
{
var arrStyleClass = styleClass.split(" ");
if(element.classList)
{
for(var count = 0;count < arrStyleClass.length;count++)
{
element.classList.remove(arrStyleClass[count]);
}
}
else if(element.className)
{
for(var count = 0;count < arrStyleClass.length;count++)
{
if(element.className instanceof SVGAnimatedString)
{
var removedClass = element.getAttribute('class').replace(new RegExp('(\\s|^)' + arrStyleClass[count] + '(\\s|$)', 'g'), '$2');
if (this.hasStyleClass(arrStyleClass[count]))
{
this.setAttribute('class', removedClass);
}
}
else
{
element.className = element.className.replace(arrStyleClass[count],"");
}
}
}
}
};
NSUtil.prototype.toggleStyleClass = function(element,styleClass)
{
if(element && styleClass && styleClass.length > 0)
{
this.hasStyleClass(element,styleClass) ? this.removeStyleClass(element,styleClass) : this.addStyleClass(element,styleClass);
}
};
//change Style class from an element's styles list
NSUtil.prototype.changeStyleClass = function (element,oldStyleClass,newStyleClass)
{
if(element && oldStyleClass && oldStyleClass.length > 0 && newStyleClass && newStyleClass.length > 0)
{
if(element.classList)
{
element.classList.remove(oldStyleClass);
if(!element.classList.contains(newStyleClass))
{
element.classList.add(newStyleClass);
}
}
else
{
if(element.className instanceof SVGAnimatedString)
{
this.removeStyleClass(element,oldStyleClass);
this.addStyleClass(element,newStyleClass);
}
else if(element.className)
{
element.className = element.className.replace(oldStyleClass,newStyleClass);
}
}
}
};
NSUtil.prototype.css = function (element,key,value)
{
var self = this;
var setStyle = function(prop)
{
var regexNumberProp = /^left|top|bottom|right|width|min|max|height|margin|padding|fontSize/i;
var tempValue = styles[prop];
if(tempValue !== undefined && tempValue !== null && regexNumberProp.test(prop) && self.isNumber(tempValue))
{
tempValue = tempValue + "px";
}
element.style[prop] = tempValue;
};
var styles;
if (this.isUndefined(value))
{
if (this.isString(key))
{
return element.style[key];
}
styles = key;
}
else
{
styles = {};
styles[key] = value;
}
for(var style in styles)
{
setStyle(style);
}
};
NSUtil.prototype.addEvent = function(arrElement,eventType,listener,option)
{
if(arrElement)
{
if(!Array.isArray(arrElement))
{
arrElement = [arrElement];
}
arrEventType = eventType.split(" ");
var type = null;
var element = null;
for(var count = 0;count < arrEventType.length;count++)
{
type = arrEventType[count];
if(type)
{
for(var eleCount = 0;eleCount < arrElement.length;eleCount++)
{
element = arrElement[eleCount];
if(element)
{
if(!element.__nsEventListeners)
{
element.__nsEventListeners = {};
}
if(!element.__nsEventListeners[type])
{
element.__nsEventListeners[type] = [];
}
(element.addEventListener) ? element.addEventListener(type,listener,option) : element.attachEvent("on" + type, listener);;
element.__nsEventListeners[type].push({listener: listener,option: option});
}
}
}
}
}
};
NSUtil.prototype.removeEvent = function(arrElement,eventType,listener,option)
{
if(arrElement)
{
function removeEvent(paramElement,paramType,paramListener,paramOption)
{
(paramElement.removeEventListener) ? paramElement.removeEventListener(paramType,paramListener,paramOption) : paramElement.detachEvent("on" + paramType, paramListener);
};
if(!Array.isArray(arrElement))
{
arrElement = [arrElement];
}
arrEventType = eventType.split(" ");
var type = null;
var element = null;
var item = null;
for(var count = 0;count < arrEventType.length;count++)
{
type = arrEventType[count];
if(type)
{
for(var eleCount = 0;eleCount < arrElement.length;eleCount++)
{
element = arrElement[eleCount];
if(element)
{
if(listener == "all")
{
for(var lstCount = 0;lstCount < element.__nsEventListeners[type].length;lstCount+