Last active
August 29, 2015 14:21
-
-
Save eyecatchup/456d1d9e06cd0811b42f to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html i18n-values="dir:textdirection"> | |
<head> | |
<meta charset="utf-8"> | |
<title i18n-content="page-title"></title> | |
</head> | |
<body> | |
<input type="hidden" name="test" id="test" value="12345"/> | |
<script type="text/javascript"> | |
// ---------------------------------------------------------------------------- | |
// BEGIN OF PROTILS | |
// ---------------------------------------------------------------------------- | |
/** | |
* The global object. | |
* @type {!Object} | |
* @const | |
*/ | |
var Protils = new (function(_win) { | |
var __self = this, | |
__win = { | |
win: _win.self, | |
doc: _win.document, | |
loc: _win.location, | |
} | |
__DOM = {}, | |
__URL = {}, | |
__REQUEST = {}; | |
// ---------------------------------------------------------------------------- | |
// Protils.DOM | |
// ---------------------------------------------------------------------------- | |
__DOM = { | |
__init__: function() { | |
return this; | |
}, | |
$: function(nodeId) { | |
return __win.doc.getElementById(nodeId); | |
}, | |
Helper: { | |
swapDomNodes: function(node1, node2) { | |
var afterA = a.nextSibling; | |
if (afterA == b) { | |
__DOM.Helper.swapDomNodes(b, a); | |
return; | |
} | |
var aParent = a.parentNode; | |
b.parentNode.replaceChild(a, b); | |
aParent.insertBefore(b, afterA); | |
}, | |
ifBodyContainsNode: function(nodeId) { | |
return __win.doc.body.contains(__DOM.$(nodeId)); | |
}, | |
ifNodeContainsNode: function(node1, node2) { | |
return __win.doc.node1.contains(node2); | |
}, | |
ifNodeExistsInDoc: function(nodeId) { | |
return !__DOM.$(nodeId) ? !1 : !0; | |
} | |
} | |
}; | |
// ---------------------------------------------------------------------------- | |
// Protils.URL | |
// ---------------------------------------------------------------------------- | |
__URL = { | |
__init__: function() { | |
return this; | |
}, | |
Helper: { | |
getAnchorStringFromQueryString: function(requestQueryString) { | |
return 0 === requestQueryString.length ? null : requestQueryString.replace(/^#/,''); | |
}, | |
getRequestURISchemeVariants: function(requestQueryString) { | |
if (null === requestQueryString || !requestQueryString.length) { | |
return !1; | |
} | |
else { | |
var noSchemeRequestURI = requestQueryString.replace(/^https?:\/\//,''); | |
return { | |
'http': 'http://' + noSchemeRequestURI, | |
'https': 'https://' + noSchemeRequestURI, | |
'noScheme': noSchemeRequestURI | |
} | |
}; | |
}, | |
getParamsObjectFromQueryString: function(requestQueryString) { | |
if (null === requestQueryString || !requestQueryString.length) { | |
return !1; | |
} | |
requestQueryString = requestQueryString.substr(1); | |
var requestQueryParams = { | |
'__Components': { | |
'__decoded': {}, | |
'__encoded': {} | |
}, | |
'__String': {} | |
}, | |
regex = /([^&=]+)=?([^&]*)/g, | |
rNull = /^\s*$/, | |
rBool = /^(true|false)$/i, | |
pair, paramsDec = {}, paramsEnc = {}; | |
function decodeTypes(val) { | |
return rNull.test(val) ? null : rBool.test(val) ? "true" === val.toLowerCase() : isFinite(val) ? parseFloat(val) : isFinite(Date.parse(val)) ? new Date(val) : val; | |
} | |
function replaceSpace(val) { | |
return rNull.test(val) || rBool.test(val) || isFinite(val) || isFinite(Date.parse(val)) ? val : val.replace(/\+/g, " "); | |
} | |
requestQueryParams.__String.__decoded = decodeURI(requestQueryString); | |
requestQueryParams.__String.__encoded = encodeURI(requestQueryParams.__String.__decoded); | |
while (pair = regex.exec(requestQueryString)) { | |
var dec1 = decodeURIComponent(pair[1]), | |
dec2 = decodeTypes(decodeURIComponent(pair[2])); | |
paramsDec[decodeURIComponent(pair[1])] = replaceSpace(dec2), | |
paramsEnc[encodeURI(dec1)] = (rNull.test(dec2) || null === dec2) ? "" : encodeURI(dec2); | |
} | |
requestQueryParams.__Components.__decoded = paramsDec; | |
requestQueryParams.__Components.__encoded = paramsEnc; | |
return requestQueryParams; | |
}, | |
addKeyValToUrlQueryString: function(url, key, val) { | |
var hasHash = __REQUEST.hasAnchor(); | |
hasHash && (url = url.replace('#' + __REQUEST.getAnchor(), "")); | |
url = url + (-1 == url.indexOf("?") ? "?" : "&") + encodeURIComponent(key) | |
+ "=" + encodeURIComponent(val); | |
hasHash && (url += '#' + __REQUEST.getAnchor()); | |
return url; | |
} | |
} | |
}; | |
// ---------------------------------------------------------------------------- | |
// Protils.Request | |
// ---------------------------------------------------------------------------- | |
__REQUEST = { | |
__Location: {}, | |
__init__: function() { | |
var loc = __win.loc; | |
(this).__Location.__Anchor = __URL.Helper.getAnchorStringFromQueryString(loc.hash); | |
(this).__Location.__Hostname = loc.hostname; | |
(this).__Location.__Pathname = loc.pathname; | |
(this).__Location.__Protocol = loc.protocol.replace(":", ""); | |
(this).__Location.__Port = "" === loc.port ? ("https" === (this).__Location.__Protocol ? "443" : "80") : loc.port; | |
(this).__Location.__Origin = loc.origin; | |
(this).__Location.__QueryString = loc.search; | |
(this).__Location.__QueryParams = __URL.Helper.getParamsObjectFromQueryString((this).__Location.__QueryString); | |
(this).__Location.__RequestURI = loc.href; | |
(this).__Location.__URIVariants = __URL.Helper.getRequestURISchemeVariants((this).__Location.__RequestURI); | |
return this; | |
}, | |
getAnchor: function() { | |
return (this).__Location.__Anchor; | |
}, | |
getRequestURL: function() { | |
return (this).__Location.__RequestURI; | |
}, | |
getDecodedQueryParamsString: function() { | |
return (this).__Location.__QueryParams.__String.__decoded; | |
}, | |
getEncodedQueryParamsString: function() { | |
return (this).__Location.__QueryParams.__String.__encoded; | |
}, | |
getDecodedQueryParamsObject: function() { | |
return (this).__Location.__QueryParams.__Components.__decoded; | |
}, | |
getEncodedQueryParamsObject: function() { | |
return (this).__Location.__QueryParams.__Components.__encoded; | |
}, | |
hasAnchor: function() { | |
return null === (this).__Location.__Anchor ? !1 : !0 | |
} | |
}; | |
// ---------------------------------------------------------------------------- | |
// Instantiate Protils | |
// ---------------------------------------------------------------------------- | |
__self = { | |
DOM: __DOM.__init__(), | |
Request: __REQUEST.__init__(), | |
URL: __URL.__init__() | |
}; | |
_win.Protils = __self; | |
return _win, __self; | |
})(window); | |
// ---------------------------------------------------------------------------- | |
// END OF PROTILS | |
// ---------------------------------------------------------------------------- | |
(function (win, Protils) { | |
// Since all we want here is forwarding of certain commands, all can be done | |
// in the anonymous function's scope. | |
var oldonload = win.onload; | |
win.onload = function(){ | |
// ------------------------------------ | |
console.dir(Protils); | |
var url = Protils.Request.getRequestURL(); | |
console.log(url); | |
var newUrl = Protils.URL.Helper.addKeyValToUrlQueryString(url, 'neuerParameter', '1'); | |
console.log(newUrl); | |
//var requestQueryParams = Protils.Request.getDecodedQueryParamsObject(); | |
//console.dir(requestQueryParams); | |
console.dir(Protils.DOM.Helper.ifBodyContainsNode('test') ); | |
console.dir(Protils.DOM.Helper.ifNodeExistsInDoc('test') ); | |
console.dir(Protils.DOM.$('test') ); | |
// ------------------------------------ | |
oldonload && oldonload() | |
}; | |
})(window, Protils); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************** | |
* ELEMENT PROTOTYPES | |
********************************************************************/ | |
/** | |
* Indicates whether the element has a given class name within its class attribute. | |
*/ | |
if (!Element.prototype.hasClassName) { | |
Element.prototype.hasClassName = function (className) { | |
return new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)').test(this.className); | |
}; | |
} | |
/** | |
* Adds the given class name to the element's class attribute if it's not already there. | |
*/ | |
if (!Element.prototype.addClassName) { | |
Element.prototype.addClassName = function (className) { | |
if (!this.hasClassName(className)) { | |
this.className = [this.className, className].join(' ').replace(/^\s*|\s*$/g, ""); | |
} | |
}; | |
} | |
/** | |
* Removes the given class name from the element's class attribute if it's there. | |
*/ | |
if (!Element.prototype.removeClassName) { | |
Element.prototype.removeClassName = function (className) { | |
if (this.hasClassName(className)) { | |
var curClasses = this.className; | |
this.className = curClasses.replace(new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)', 'g'), ' ').replace(/^\s*|\s*$/g, ""); | |
} | |
}; | |
} | |
/******************************************************************** | |
* ARRAY PROTOTYPES | |
********************************************************************/ | |
if (!Array.prototype.contains) { | |
Array.prototype.contains = function(b) { | |
for (var a = this.length; a--;) { | |
if (this[a] == b) { | |
return !0 | |
} | |
} | |
return !1 | |
}; | |
} | |
if (!Array.prototype.forEach) { | |
Array.prototype.forEach = function (fn, arg) { | |
var arr = this, | |
len = arr.length, | |
thisArg = arg ? arg : undefined, | |
i; | |
for (i = 0; i < len; i += 1) { | |
if (arr.hasOwnProperty(i)) { | |
fn.call(thisArg, arr[i], i, arr); | |
} | |
} | |
return undefined; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment