Js常用公共方法庫Common Helper(継続更新)
8388 ワード
ずっと小さいjsライブラリがあるべきだと思っています.jsの高級プログラミングを見てから、普段のプロジェクトの自分で使う方法を加えて、自分のアシスタントを整理して作成しました.
var LCH={
var LCH={
// remove value at array
removeArrayItem: function (array, value) {
var index = $.inArray(value, array);
if (index > -1) {
array.splice(index, 1);
}
},
//value is exist in array
isExistInArray: function (array, value) {
var index = $.inArray(value, array);
return index > -1 ? true : false;
},
// keyvaluedictionary
keyValueDictionary: function () {
var data = {};
this.set = function (key, value) {
data[key] = value;
};
this.unset = function (key) {
delete data[key];
};
this.get = function (key) {
return data[key] || null;
}
},
// get parameters from Url
getQueryString: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
else {
return undefined;
}
},
//timestamp conversion(timestamp from .Net)
getDataString: function (timestamp) {
if (CommonHelper.IsNullOrWhiteSpace(timestamp)) {
return "";
}
var timestampDate = timestamp.replace("/Date(", "").replace(")/", "");
var tempDate = new Date(parseInt(timestampDate));
Y = tempDate.getFullYear() + "-";
M = (tempDate.getMonth() + 1 < 10 ? "0" + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1) + "-";
D = tempDate.getDate() < 10 ? "0" + tempDate.getDate() : tempDate.getDate();
h = tempDate.getHours() < 10 ? "0" + tempDate.getHours() : tempDate.getHours();
m = tempDate.getMinutes() < 10 ? "0" + tempDate.getMinutes() : tempDate.getMinutes();
s = tempDate.getSeconds() < 10 ? "00" : tempDate.getSeconds();
return (Y + M + D + " " + h + ":" + m + ":" + s);
},
// indicates whether a specified string is null, empty, or consists only of white-space
IsNullOrWhiteSpace: function (stringValue) {
var returnValue = false;
if (typeof (stringValue) == "string") {
stringValue = stringValue.replace(/(^\s*)|(\s*$)/g, "");
if (stringValue == "") {
return true;
}
return false;
}
else {
return true;
}
},
//is exists Prototype
//return true in Prototype ,false not in Prototype
hasPrototypeProperty: function (object, name) {
//hasOwnProperty when name in the prototype return true
//in if name exist return true
return !object.hasOwnProperty(name) && (name in object);
},
//function currying
curry: function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
var innerArgs = Array.prototype.slice.call(arguments);
var finalArgs = args.concat(innerArgs);
return fn.apply(null, finalArgs);
};
},
//function throttling ,frequent calls can use this
throttle: function (method, context) {
clearTimeout(method.tId);
method.tId = setTimeout(function () {
method.call(context);
}, 100);
},
//IE9+、Firefox 4+ Chrome, But beyond that,use this
bind: function (fn, context) {
return function () {
return fn.apply(context, arguments);
}
},
////eg.
//// function handleMessage(event) {
//// console.log("Message received: " + event.message);
////}
//////
////var target = new LCH.EventTarget();
//////
////target.addHandler("message", handleMessage);
//////
////target.fire({ type: "message", message: "Hello world!" });
//////
////target.removeHandler("message", handleMessage);
////// ,
////target.fire({ type: "message", message: "Hello world!" });
eventTarget: function () {
this.handlers = {};
}
}
LCH.EventUtil = {
addHandler: function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
},
removeHandler: function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
}
};
LCH.CookieUtil = {
get: function (name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = null;
if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(";", cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
}
cookieValue = decodeURIComponent(document.cookie.substring(cookieStart
+ cookieName.length, cookieEnd));
}
return cookieValue;
},
set: function (name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + "=" +
encodeURIComponent(value);
if (expires instanceof Date) {
cookieText += "; expires=" + expires.toGMTString();
}
if (path) {
cookieText += "; path=" + path;
}
if (domain) {
cookieText += "; domain=" + domain;
}
if (secure) {
cookieText += "; secure";
}
document.cookie = cookieText;
},
unset: function (name, path, domain, secure) {
this.set(name, "", new Date(0), path, domain, secure);
}
};
LCH.eventTarget.prototype = {
constructor: EventTarget,
addHandler: function (type, handler) {
if (typeof this.handlers[type] == "undefined") {
this.handlers[type] = [];
}
this.handlers[type].push(handler);
},
fire: function (event) {
if (!event.target) {
event.target = this;
}
if (this.handlers[event.type] instanceof Array) {
var handlers = this.handlers[event.type];
for (var i = 0, len = handlers.length; i < len; i++) {
handlers[i](event);
}
}
},
removeHandler: function (type, handler) {
if (this.handlers[type] instanceof Array) {
var handlers = this.handlers[type];
for (var i = 0, len = handlers.length; i < len; i++) {
if (handlers[i] === handler) {
break;
}
}
handlers.splice(i, 1);
}
}
};
・