Created
November 24, 2011 16:02
-
-
Save btspoony/1391686 to your computer and use it in GitHub Desktop.
Some JS Utility functions
This file contains 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
var __sys = {}; | |
var ua = navigator.userAgent.toLowerCase(); | |
var s; | |
(s = ua.match(/msie ([\d.]+)/)) ? __sys.ie = s[1] : | |
(s = ua.match(/firefox\/([\d.]+)/)) ? __sys.firefox = s[1] : | |
(s = ua.match(/chrome\/([\d.]+)/)) ? __sys.chrome = s[1] : | |
(s = ua.match(/opera.([\d.]+)/)) ? __sys.opera = s[1] : | |
(s = ua.match(/version\/([\d.]+).*safari/)) ? __sys.safari = s[1] : 0; | |
function typeofUndefined(value) { | |
return typeof (value) == "undefined" || value == null; | |
}; | |
function typeofObject(value) { | |
return typeof (value) == "object" && value.constructor == Object; | |
}; | |
function typeofString(value) { | |
return typeof (value) == "string" && value.constructor == String; | |
}; | |
function typeofBoolean(value) { | |
return typeof (value) == "boolean" && value.constructor == Boolean; | |
}; | |
function typeofNumber(value) { | |
return typeof (value) == "number" && value.constructor == Number; | |
}; | |
function typeofFunction(value) { | |
return typeof (value) == "function" && value.constructor == Function; | |
}; | |
function typeofArray(value) { | |
return typeof (value) == "object" && value.constructor == Array; | |
}; | |
OptionManagement = function(options) { | |
return function(name, defaultValue) { | |
return (typeofUndefined(options) || typeofUndefined(options[name]) ? defaultValue : options[name]); | |
}; | |
}; | |
function trim(value) { | |
return value.toString().replace(/^\s+|\s+$/g, ''); | |
}; | |
function toNumber(value) { | |
return Number(value); | |
}; | |
function toBoolean(value) { | |
return value.toString().toLowerCase() == "true" && value != "0"; | |
}; | |
function isNumeric(precision) { | |
return new RegExp("^-?((\\d{1," + (precision ? precision.length - precision.scale : "") + "}((\\.\\d{0," + (precision ? precision.scale : "") + "})?)))$").test(this); | |
}; | |
function isInteger(value) { | |
return /^[0-9]{1,}$/.test(value); | |
}; | |
function endsWith(value, ends, ignoreCase) { | |
if (ignoreCase) { | |
return value.toLowerCase().charAt(value.length - str.length) == str.toLowerCase(); | |
} | |
return value.charAt(value.length - str.length) == str; | |
}; | |
function startsWith(value, stats, ignoreCase) { | |
if (ignoreCase) { | |
return value.toLowerCase().charAt(0) == str.toLowerCase(); | |
} | |
return value.charAt(0) == str; | |
}; | |
function NaN0(value) { | |
return isNaN(value) ? 0 : this; | |
}; | |
function disableKeyboardEvent() { | |
event.keyCode = 0; | |
} | |
function disableKeyboard() { | |
bindEvent(document, "keydown", disableKeyboardEvent); | |
}; | |
function enableKeyboard() { | |
unbindEvent(document, "keydown", disableKeyboardEvent); | |
}; | |
Date.parseDate = function(str, fmt) { | |
var today = new Date(); | |
var y = 0; | |
var m = -1; | |
var d = 0; | |
var a = str.split(/\W+/); | |
var b = fmt.match(/%./g); | |
var i = 0, j = 0; | |
var hr = 0; | |
var min = 0; | |
for (i = 0; i < a.length; ++i) { | |
if (!a[i]) | |
continue; | |
switch (b[i]) { | |
case "%d": | |
case "%e": | |
d = parseInt(a[i], 10); | |
break; | |
case "%m": | |
m = parseInt(a[i], 10) - 1; | |
break; | |
case "%Y": | |
case "%y": | |
y = parseInt(a[i], 10); | |
(y < 100) && (y += (y > 29) ? 1900 : 2000); | |
break; | |
case "%b": | |
case "%B": | |
for (j = 0; j < 12; ++j) { | |
if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; } | |
} | |
break; | |
case "%H": | |
case "%I": | |
case "%k": | |
case "%l": | |
hr = parseInt(a[i], 10); | |
break; | |
case "%P": | |
case "%p": | |
if (/pm/i.test(a[i]) && hr < 12) | |
hr += 12; | |
else if (/am/i.test(a[i]) && hr >= 12) | |
hr -= 12; | |
break; | |
case "%M": | |
min = parseInt(a[i], 10); | |
break; | |
} | |
} | |
if (isNaN(y)) y = today.getFullYear(); | |
if (isNaN(m)) m = today.getMonth(); | |
if (isNaN(d)) d = today.getDate(); | |
if (isNaN(hr)) hr = today.getHours(); | |
if (isNaN(min)) min = today.getMinutes(); | |
if (y != 0 && m != -1 && d != 0) | |
return new Date(y, m, d, hr, min, 0); | |
y = 0; m = -1; d = 0; | |
for (i = 0; i < a.length; ++i) { | |
if (a[i].search(/[a-zA-Z]+/) != -1) { | |
var t = -1; | |
for (j = 0; j < 12; ++j) { | |
if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; } | |
} | |
if (t != -1) { | |
if (m != -1) { | |
d = m + 1; | |
} | |
m = t; | |
} | |
} else if (parseInt(a[i], 10) <= 12 && m == -1) { | |
m = a[i] - 1; | |
} else if (parseInt(a[i], 10) > 31 && y == 0) { | |
y = parseInt(a[i], 10); | |
(y < 100) && (y += (y > 29) ? 1900 : 2000); | |
} else if (d == 0) { | |
d = a[i]; | |
} | |
} | |
if (y == 0) | |
y = today.getFullYear(); | |
if (m != -1 && d != 0) | |
return new Date(y, m, d, hr, min, 0); | |
return today; | |
}; | |
Date.prototype.getMonthDays = function(month) { | |
var year = this.getFullYear(); | |
if (typeof month == "undefined") { | |
month = this.getMonth(); | |
} | |
if (((0 == (year % 4)) && ((0 != (year % 100)) || (0 == (year % 400)))) && month == 1) { | |
return 29; | |
} else { | |
return Date._MD[month]; | |
} | |
}; | |
Date.prototype.getDayOfYear = function() { | |
var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0); | |
var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0); | |
var time = now - then; | |
return Math.floor(time / Date.DAY); | |
}; | |
Date.prototype.getWeekNumber = function() { | |
var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0); | |
var DoW = d.getDay(); | |
d.setDate(d.getDate() - (DoW + 6) % 7 + 3); | |
var ms = d.valueOf(); | |
d.setMonth(0); | |
d.setDate(4); | |
return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1; | |
}; | |
Date.prototype.equalsTo = function(date) { | |
return ((this.getFullYear() == date.getFullYear()) && | |
(this.getMonth() == date.getMonth()) && | |
(this.getDate() == date.getDate()) && | |
(this.getHours() == date.getHours()) && | |
(this.getMinutes() == date.getMinutes())); | |
}; | |
function createElement(options, document) { | |
var element = null; | |
if (typeof (document) == "undefined" || document == null) { | |
document = window.document; | |
} | |
if (document.createElementNS) { | |
element = document.createElementNS("http://www.w3.org/1999/xhtml", options.tagName); | |
} | |
else { | |
element = document.createElement(options.tagName); | |
} | |
for (var name in options) { | |
try { | |
switch (name) { | |
case "tagName": | |
continue; | |
case "style": | |
var style = options[name]; | |
for (var styleName in style) { | |
element.style[styleName] = style[styleName]; | |
} | |
break; | |
case "parent": | |
options[name].appendChild(element); | |
break; | |
default: | |
element[name] = options[name]; | |
break; | |
} | |
} | |
catch (e) { } | |
} | |
return element; | |
}; | |
var $zIndex = 10000; | |
var $getZIndex = function() { | |
return $zIndex++; | |
}; | |
function showDialog(dialog, document) { | |
if (dialog.style.display == "none") { | |
if (typeof (document) == "undefined" || document == null) { | |
document = window.document; | |
} | |
var overlayId = "$_dialog_overlay"; | |
var overlay = document.getElementById(overlayId); | |
if (typeof (overlay) == "undefined" || overlay == null) { | |
overlay = createElement( | |
{ | |
tagName: "iframe", | |
id: overlayId, | |
frameBorder: "0", | |
scrolling: "no", | |
allowTransparency: "true", | |
style: { | |
top: "0px", | |
left: "0px", | |
position: "absolute", | |
filter: "alpha(opacity=100); -moz-opacity:1.0", | |
zIndex: $getZIndex() | |
}, | |
width: document.body.scrollWidth, | |
height: document.body.scrollHeight, | |
parent: document.body | |
}, | |
document | |
); | |
} | |
overlay.document.body.bgColor = "#dddddd"; | |
dialog.overlay = overlay; | |
dialog.style.zIndex = $getZIndex(); | |
dialog.style.position = "absolute"; | |
dialog.style.top = ((document.documentElement.clientHeight - dialog.offsetHeight) / 2 + document.documentElement.scrollTop) + "px"; | |
dialog.style.left = ((document.documentElement.clientWidth - dialog.offsetWidth) / 2 + document.documentElement.scrollLeft) + "px"; | |
overlay.style.display = ""; | |
dialog.style.display = ""; | |
} | |
}; | |
function hideDialog(dialog) { | |
dialog.overlay.style.display = "none"; | |
dialog.style.display = "none"; | |
} | |
function $setInputValue(element, value) { | |
element.hasChanged = false; | |
element.originalValue = value; | |
switch (element.type) { | |
case "hidden": | |
case "text": | |
case "textarea": | |
case "select-one": | |
case "button": | |
element.value = value; | |
break; | |
case "checkbox": | |
element.checked = value != "0"; | |
element.value = value; | |
break; | |
case "radio": | |
element.checked = value != "0"; | |
element.value = value; | |
break; | |
break; | |
case "select-multiple": | |
for (var i = 0; i < element.length; i++) { | |
if (trim(element[i].value) == trim(value)) { | |
element[i].selected = true; | |
} | |
} | |
break; | |
default: | |
throw "element not input"; | |
break; | |
} | |
}; | |
function $getInputValue(element) { | |
switch (element.type) { | |
case "hidden": | |
case "text": | |
case "textarea": | |
case "checkbox": | |
case "radio": | |
case "select-one": | |
return trim(element.value); | |
case "select-multiple": | |
var values = []; | |
for (var i = 0; i < element.length; i++) { | |
if (element[i].selected) { | |
values.push(trim(element[i].value)); | |
} | |
} | |
return values; | |
} | |
}; | |
function $setValue(element, value, html) { | |
value = trim(value.toString()); | |
switch (element.tagName) { | |
case "INPUT": | |
case "TEXTAREA": | |
$setInputValue(element, value); | |
break; | |
default: | |
if (html) { | |
element.innerHTML = value; | |
} | |
else { | |
element.innerText = value; | |
} | |
break; | |
} | |
}; | |
function $getValue(element, html) { | |
switch (element.tagName) { | |
case "INPUT": | |
case "TEXTAREA": | |
return $getInputValue(element); | |
default: | |
if (html) { | |
return element.innerHTML; | |
} | |
return element.innerText; | |
} | |
}; | |
function $setValueById(id, value, html) { | |
$setValue($id(id), value, html); | |
}; | |
function $getValueById(id, value, html) { | |
$getValue($id(id), value, html); | |
}; | |
function $id(id) { | |
var element = null; | |
if (typeofUndefined(id)) { | |
return null; | |
} | |
element = document.getElementById(id); | |
if (typeofUndefined(element.setValue)) { | |
element.setValue = function(value, html) { | |
$setValue(element, value, html); | |
}; | |
} | |
if (typeofUndefined(element.getValue)) { | |
element.getValue = function(html) { | |
$getValue(element, html); | |
}; | |
} | |
return element; | |
}; | |
function $name(name) { | |
if (name) { | |
return document.getElementsByName(name); | |
} | |
return []; | |
}; | |
function $tag(tagName, parent) { | |
if (tagName) { | |
if (parent) { | |
return parent.getElementsByTagName(tagName); | |
} | |
return document.getElementsByTagName(tagName); | |
} | |
return []; | |
}; | |
$forms = {}; | |
function $form(id) { | |
var form = $forms[id]; | |
if (typeofUndefined(form)) { | |
form = $id(id); | |
$forms[id] = form; | |
} | |
return form; | |
}; | |
function bindEvent(target, name, handler) { | |
if (target.addEventListener) { | |
target.addEventListener(name, handler, false); | |
} | |
else if (target.attachEvent) { | |
target.attachEvent("on" + name, handler); | |
} | |
else { | |
target["on" + name] = handler; | |
} | |
}; | |
function unbindEvent(target, name, handler) { | |
if (target.removeEventListener) { | |
target.removeEventListener(name, handler, false); | |
} | |
else if (target.detachEvent) { | |
target.detachEvent("on" + name, handler); | |
} | |
else { | |
target["on" + name] = null; | |
} | |
}; | |
function toHtmlEncode(value) { | |
var tmp = createElement({ tagName: "div", cssText: "display: none" }); | |
tmp.innerText = value; | |
var result = tmp.innerHTML; | |
//document.removeChild(tmp); | |
return result; | |
}; | |
function generateRandom() { | |
var date = new Date(); | |
return date.getFullYear().toString() + date.getMonth().toString() + date.getDay().toString() | |
+ date.getHours().toString() + date.getMinutes().toString() + date.getSeconds().toString() | |
+ date.getMilliseconds().toString() | |
}; | |
function loadForms() { | |
for (var i = 0; i < document.forms.length; i++) { | |
var form = document.forms[i]; | |
$forms[form.id] = form; | |
} | |
}; | |
bindEvent(window, "load", loadForms); | |
Message = function(options) { | |
var option = new OptionManagement(options); | |
this.refId = option.get("refId", "message"); | |
this.id = this.refId + "__$__Message"; | |
this.text = option.get("text", ""); | |
this.cssText = option.get("cssText", ""); | |
this.color = option.get("color", "#ff0000"); | |
this.position = option.get("position", 2); | |
this.focus = option.get("focus", true); | |
this.display = option.get("display", "inline"); | |
this.wrap = option.get("wrap", true); | |
this.onshow = option.get("onshow", null); | |
}; | |
Message.prototype.getMessage = function() { | |
var message = Page.$id(this.id); | |
if (!message) { | |
var message = document.createElement("span"); | |
message.id = this.id; | |
} | |
if (message.innerText.length == 0) { | |
message.innerText = this.text; | |
} | |
else if (message.innerText.indexOf(this.text) < 0) { | |
message.innerText += (this.wrap ? "\r\n" : "") + this.text; | |
} | |
if (this.cssText) { | |
message.style.cssText = this.cssText; | |
} | |
if (this.color) { | |
message.style.color = this.color; | |
} | |
else { | |
message.style.color = "#ff0000"; | |
} | |
return message; | |
}; | |
Message.prototype.show = function() { | |
if (this.text.length > 0) { | |
var ref = $id(this.refId); | |
if (ref) { | |
var message = this.getMessage(); | |
switch (this.position) { | |
case 1: | |
ref.parentNode.insertBefore(message, ref); | |
break; | |
case 2: | |
ref.appendChild(message); | |
break; | |
case 3: | |
if (ref.nextSibling) { | |
ref.parentNode.insertBefore(message, ref.nextSibling); | |
} | |
else { | |
ref.parentNode.appendChild(message); | |
} | |
break; | |
} | |
if (this.focus) { | |
try { | |
ref.focus(); | |
} | |
catch (ex) { } | |
} | |
message.style.display = this.display; | |
if (this.onshow) { | |
this.onshow(this); | |
} | |
} | |
} | |
}; | |
Message.prototype.hide = function() { | |
var message = $id(this.id); | |
if (message) { | |
message.innerHTML = ""; | |
message.style.display = "none"; | |
} | |
}; | |
Message.pool = {}; | |
Message.show = function(parameters) { | |
var message = new Message(parameters); | |
Message.pool[message.id] = message; | |
message.show(); | |
}; | |
Message.hide = function(id) { | |
if (id && Message.pool[id]) { | |
Message.pool[id].hide(); | |
} | |
else { | |
for (var name in Message.pool) { | |
Message.pool[name].hide(); | |
} | |
} | |
}; | |
Idle = function(parameters) { | |
var parameter = new Parameter(parameters); | |
this.handler = -1; | |
this.once = true; | |
this.duration = parameter("duration", 30000); | |
this.listening = parameter("listening", null); | |
this.idle = parameter("idle", null); | |
this.params = parameter("params", null); | |
}; | |
Idle.prototype.start = function() { | |
var self = this; | |
if (self.duration > 0) { | |
if (this.once) { | |
self.handler = setTimeout(function() { self.listening(self); }, self.duration); | |
} | |
else { | |
self.handler = setInterval(function() { self.listening(self); }, self.duration); | |
} | |
if (this.idle) { | |
this.idle.start(); | |
} | |
} | |
}; | |
Idle.prototype.restart = function() { | |
this.stop(); | |
this.start(); | |
if (this.idle) { | |
this.idle.restart(); | |
} | |
}; | |
Idle.prototype.stop = function() { | |
if (this.idle) { | |
this.idle.stop(); | |
} | |
clearTimeout(this.handler); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment