Created
December 6, 2012 06:55
-
-
Save romeoh/4222307 to your computer and use it in GitHub Desktop.
SeUtil
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
var SeUtil = { | |
/** | |
* date2Str | |
* Date Object or dateString을 Date Object로 변환하여 반환 | |
* | |
* params | |
* type : yyyy.MM.dd yyyyMMdd | |
*/ | |
date2date : function (date){ | |
if(date == null) return null; | |
if(date.constructor == Date) return date; | |
if((/^[0-9]{4}\.[0-9]{2}\.[0-9]{2}/).test(date)){ | |
temp = date.split("."); | |
return new Date(temp[0], temp[1] - 1, temp[2]); | |
} | |
if((/^[0-9]{8}/).test(date)){ | |
return new Date(date.substring(0,4), date.substring(4,6) - 1, date.substring(6,8)); | |
} | |
return null; | |
}, | |
/** | |
* date2Str | |
* Date Object or dateString을 원하는 형태의 String으로 변환하여 반환 | |
* | |
* params | |
* type : yyyy.MM.dd yyyyMMdd | |
*/ | |
date2str : function (date, type){ | |
date = this.date2date(date); | |
if(!date) return null; | |
var _year = date.getFullYear(); | |
var _month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1); | |
var _date = (date.getDate()) < 10 ? "0" + (date.getDate()) : (date.getDate()); | |
if(type == "yyyyMMdd"){ | |
return ""+_year+_month+_date; | |
} | |
if(type == "yyyy.MM.dd"){ | |
return _year + '.' + _month + '.' + _date; | |
} | |
if(type == "yyyy-MM-dd"){ | |
return _year + '-' + _month + '-' + _date; | |
} | |
}, | |
/** | |
* commonDataArray에서 원하는 데이터를 반환 | |
* @param {String} trCode : 전문명 | |
* @param {String} arrayCode : 어레이명 | |
*/ | |
getDataArray : function (trCode, arrayCode){ | |
if(arrayCode != null || arrayCode != "") | |
_trCode = trCode+"."+arrayCode; | |
for(index=0; index < commonDataArr.length; index++){ | |
if(commonDataArr[index].key == _trCode) | |
return commonDataArr[index].value; | |
if(commonDataArr[index].key == trCode){ | |
if(commonDataArr[index].value[arrayCode]) | |
return commonDataArr[index].value[arrayCode]; | |
} | |
} | |
return null; | |
}, | |
/** | |
* HTML엘리먼트에 클래스를 추가 | |
* @param {HTMLElement} element : 엘리먼트 | |
* @param {String} className : 클래스명 | |
*/ | |
addClass : function (element, className){ | |
var that = this; | |
if(element){ | |
if(element.constructor == NodeList || element.constructor == Array){ | |
[].forEach.call(element, function(el){ | |
that.addClass(el, className); | |
}); | |
} | |
else{ | |
if(className && element.className.length == 0){ | |
element.className = className; | |
} | |
else if(className && !this.hasClass(element, className)){ | |
element.className += " " + className; | |
} | |
} | |
} | |
return element; | |
}, | |
/** | |
* HTML엘리먼트에 클래스를 제거 | |
* @param {HTMLElement} element : 엘리먼트 | |
* @param {String} className : 클래스명 | |
*/ | |
removeClass : function (element, className){ | |
var that = this; | |
if(element){ | |
if(element.constructor == NodeList || element.constructor == Array){ | |
[].forEach.call(element, function(el){ | |
that.removeClass(el, className); | |
}); | |
} | |
else{ | |
var classes = []; | |
var arr = element.className.split(' '); | |
for(i=0; i<arr.length; i++){ | |
if(arr[i] && arr[i] != className) classes.push(arr[i]); | |
} | |
element.className = classes.join(' '); | |
} | |
} | |
return element; | |
}, | |
/** | |
* HTML엘리먼트가 클래스를 가졌는지 확인 | |
* @param {HTMLElement} element : 엘리먼트 | |
* @param {String} className : 클래스명 | |
*/ | |
hasClass : function (element, className){ | |
var arr = element.className.split(' '); | |
for(i=0; i<arr.length; i++){ | |
if(arr[i] && arr[i] == className) return true; | |
} | |
return false; | |
}, | |
/** | |
* HTML엘리먼트가 해당 클래스명을 토글 | |
* @param {HTMLElement} element : 엘리먼트 | |
* @param {String} className : 클래스명 | |
*/ | |
toggleClass : function (element, className){ | |
if(this.hasClass(element, className)) | |
this.removeClass(element, className); | |
else | |
this.addClass(element, className); | |
return element; | |
}, | |
/** | |
* 네이티브 메세지창을 통해 메세지 출력 | |
* @param {String} message : 제목 | |
* @param {String} title : 내용 | |
*/ | |
alert : function(message, title){ | |
title = title || "알림"; | |
var buttonInfo = { | |
"buttonInfo" : [ | |
{"title" : "확인", "cbFuncName" : ""}, | |
] | |
}; | |
WNConfirmPopup(title, message, buttonInfo); | |
}, | |
/** | |
* Json Object의 키값들을 배열형태로 반환 | |
* @param {Object} jsonObject : JSON 오브젝트 | |
*/ | |
getJsonKeys : function(jsonObject){ | |
if (typeof Object.keys == "function") { | |
var keys = [], name; | |
for (name in jsonObject) { | |
if (jsonObject.hasOwnProperty(name)) { | |
keys.push(name); | |
} | |
} | |
return keys; | |
} | |
}, | |
/** | |
* JSON key별로 반복 | |
* @param {Object} jsonObject : JSON 오브젝트 | |
* @param {Function} callback : 콜백함수 Function(key, value); | |
*/ | |
forEachJson : function(jsonObject, callback){ | |
var keys = this.getJsonKeys(jsonObject); | |
[].forEach.call(keys, function(key){callback(key, jsonObject[key])}); | |
}, | |
/** | |
* JSON Object를 String형태로 반환 Function이나 HTMLElement등을 무시 | |
* @param {Object} jsonObject : JSON 오브젝트 | |
*/ | |
JSONtoString : function(json){ | |
return (function oFn(o){ | |
if (o === null || o === undefined) return 'null'; | |
switch (o.constructor) { | |
case Boolean: | |
case Number: | |
return o; | |
case String: | |
return '"' + o + '"'; | |
case Date: | |
return '"' + (+o) + '"'; | |
case Array: | |
var arr = []; | |
for (var i = 0, k = o.length; i < k; i++) | |
arr[arr.length] = oFn(o[i]); | |
return '[' + arr.join(',') + ']'; | |
case Object: | |
var arr = []; | |
for (var x in o) { | |
if (o.hasOwnProperty(x)) | |
arr.push('"' + x + '"' + ':' + oFn(o[x])); | |
} | |
return '{' + arr.join(',') + '}'; | |
default: | |
return null; | |
} | |
})(json); | |
}, | |
/** | |
* JSON Object를 HTML 파라메터로 변환하여 반환 | |
* ex)a=1&b=2 | |
* @param {Object} jsonObject : JSON 오브젝트 | |
*/ | |
JSONtoParamstr : function(json){ | |
return (function oFn(o){ | |
if (o === null || o === undefined) return ''; | |
str = ""; | |
var arr = []; | |
for (var x in o) { | |
if (o.hasOwnProperty(x)){ | |
switch (o[x].constructor) { | |
case Boolean: | |
case Number: | |
arr.push(x + '=' + o[x]); | |
break; | |
case String: | |
arr.push(x + '=' + o[x]); | |
break; | |
case Date: | |
arr.push(x + '=' + o[x]); | |
break; | |
} | |
} | |
} | |
return arr.join('&'); | |
})(json); | |
}, | |
click : function(element){ | |
if(element && element.constructor == String) element = document.getElementById(element); | |
if(element){ | |
var evt = document.createEvent('Event'); | |
evt.initEvent('click'); | |
element.dispatchEvent(evt); | |
} | |
}, | |
/** | |
* customConfirm | |
*/ | |
customConfirm : (function(){ | |
var _callback = {}; | |
var _confirm = function (title, message, buttonInfo){ | |
var randKey = 'key_'+Math.round(Math.random()*100000000000); | |
_callback[randKey] = {}; | |
for(var index=0; index < buttonInfo.buttonInfo.length; index++){ | |
buttonInfo.buttonInfo[index].cbFuncName = "SeUtil.customConfirm.callback." + randKey+".idx_"+index; | |
_callback[randKey]['idx_'+index] = (function(buttonInfo, index, randKey){ | |
var cb = buttonInfo.buttonInfo[index].callback; | |
if(cb && cb.constructor == Function){ | |
return cb; | |
} | |
else{ | |
return (function(index){}); | |
} | |
})(buttonInfo, index, randKey); | |
delete buttonInfo.buttonInfo[index].callback; | |
} | |
WNConfirmPopup(title, message, buttonInfo); | |
} | |
return{ | |
confirm : _confirm, | |
callback : _callback | |
} | |
})(), | |
/** | |
* 컨펌창 | |
*/ | |
confirm : function(message, callback, title, callbackCancel){ | |
this.customConfirm.confirm( | |
title || "알림", | |
message || "", | |
{ | |
"buttonInfo" : [ | |
{title : "확인", callback : callback}, | |
{title : "취소", callback : callbackCancel} | |
] | |
} | |
); | |
}, | |
/*** | |
* 경고창 | |
*/ | |
alert : function(message, callback, title){ | |
this.customConfirm.confirm( | |
title || "알림", | |
message || "", | |
{ | |
"buttonInfo" : [ | |
{title : "확인", callback : callback} | |
] | |
} | |
); | |
}, | |
/** | |
*전문호출 | |
* Function call | |
* param : trCode String | |
* param : sendData Json Object | |
* param : callback | |
* | |
* 배열도 가능 | |
* 배열로 입력시 두번째 인수로 콜백(모든전문 종료시 추가) | |
*/ | |
telegram : (function(){ | |
var callback = function(){alert('default')}; | |
var telegramArray = []; | |
var call = function(trCode, sendData, cb){ | |
var that = this; | |
if(trCode && trCode.constructor == Array){ | |
telegramArray = trCode; | |
var data = telegramArray.shift(); | |
if(data){ | |
that.call( | |
data.trCode || "", | |
data.sendData || "", | |
data.callback || function(){} | |
); | |
} | |
else{ | |
// 배열로 입력한 경우 sendData위치에 콜백함수를 받아 모든 전문을 다 처리 한 후 호출 할 수 있음. | |
if(sendData && sendData.constructor == Function) sendData(); | |
} | |
} | |
else if(trCode && trCode.constructor == String){ | |
that.callback = function(trCode, receivedData){ | |
cb(trCode, receivedData); | |
// 공통 전문 콜백도 호출해주자 | |
//makeTelegramDataCB(trCode, receivedData) | |
var data = telegramArray.shift(); | |
if(data){ | |
that.call( | |
data.trCode || "", | |
data.sendData || "", | |
data.callback || function(){} | |
); | |
} | |
}; | |
WNRequestDataWithUserInfo(trCode, "SeUtil.telegram.callback", sendData || {}); | |
} | |
} | |
return{ | |
call : call, | |
callback : callback | |
}; | |
})(), | |
picker : (function(){ | |
var inputElement; | |
var callback; | |
var type; | |
return { | |
date : function(param){ | |
inputElement = param.element || param.element.querySelector('input'); | |
type = param.type || 'YMD'; | |
callback = param.callback || function(value){}; | |
if(inputElement){ | |
if(inputElement.constructor != HTMLInputElement) inputElement = inputElement.querySelector('input'); | |
} | |
var value = SeUtil.date2str((inputElement && inputElement.value) || new Date(), "yyyyMMdd"); | |
if(type=='HM24') | |
value = inputElement.value.replace(/:/g, ""); | |
WNRequestDatePicker( | |
'SeUtil.picker.dateCB', | |
type, | |
value | |
); | |
}, | |
dateCB : function(value){ | |
if(type == 'YMD'){ | |
var _date = value.yyyy + value.MM + value.dd; | |
if(inputElement) inputElement.value = SeUtil.date2str(_date, "yyyy.MM.dd"); | |
if(callback && callback.constructor == Function) callback(value, inputElement); | |
} | |
else if(type == 'HM24'){ | |
if(inputElement) inputElement.value = value.HH + ":" + value.mm; | |
if(callback && callback.constructor == Function) callback(value, inputElement); | |
} | |
} | |
, | |
time : function(inputElement){ | |
} | |
} | |
})() | |
} | |
/** | |
* 폼이 업데이트 되면 자료도 알서 쭉쭉쭉 | |
* @param {Object} form : input, textarea 등등의 엘리먼트 오브젝트 | |
* @param {Object} jsonObj : json Object | |
* @param {Object} value_key : 값이 저장될 json key | |
* @param {Object} code_key : 키와 매칭되는 녀석을 경우 값과 매칭되는 json key | |
* @param {Object} pair : 키와 값으로 매칭될 pair array [{CODE:"A", TEXT:"가나다"}] 형식 | |
*/ | |
function SeForm(form, jsonObj, value_key, option){ | |
//code_key, pair, date | |
option = option || {}; | |
var _form = form; | |
if(!form) return null; | |
var _isTextArea = false; | |
var _isInput = false; | |
if(_form.constructor == HTMLTextAreaElement) | |
_isTextArea = true; | |
else if(_form.constructor == HTMLInputElement) | |
_isInput = true; | |
if(_isInput || _isTextArea){ | |
_form.addEventListener("change", function(e){ | |
_parent[_value_key] = e.target.value | |
}); | |
} | |
var _parent = jsonObj || new Object(); | |
var _value_key = value_key || "VALUE"; | |
var _code_key = option.code_key || "CODE"; | |
var _pair = option.pair || []; | |
//confirm(SeUtil.JSONtoString(_parent)); | |
var valTemp = ""; | |
if(option.date){ | |
valTemp = _parent[_value_key].substring(0,4) + "." + | |
_parent[_value_key].substring(4,6) + "." + | |
_parent[_value_key].substring(6,8) | |
} | |
else | |
valTemp = _parent[_value_key]; | |
if(_form.hasOwnProperty("value")){ | |
_form.value = valTemp; | |
} | |
else{ | |
_form.innerHTML = valTemp; | |
} | |
var refresh = function(){ | |
if(_pair && _code_key) | |
setValue(_parent[_code_key]); | |
else | |
setValue(_parent[_value_key]); | |
}, | |
setValue = function(val){ | |
if(_isInput || _isTextArea){ | |
if(_pair && _code_key){ | |
_parent[_code_key] = val; | |
_parent[_value_key] = (function(pairs){ | |
for(i=0; i<pairs.length; i++) | |
if(val == pairs[i].CODE) return pairs[i].TEXT; | |
_parent[_code_key] = pairs[0].CODE; | |
return pairs[0].TEXT; | |
})(_pair); | |
var valTemp = ""; | |
if(option.date){ | |
valTemp = _parent[_value_key].substring(0,4) + "." + | |
_parent[_value_key].substring(4,6) + "." + | |
_parent[_value_key].substring(6,8) | |
} | |
else | |
valTemp = _parent[_value_key]; | |
_form.value = valTemp; | |
} | |
else{ | |
_parent[_value_key] = val; | |
_form.value = val; | |
} | |
} | |
else{ | |
switch(_form.constructor){ | |
case HTMLDivElement : | |
case HTMLSpanElement : | |
case HTMLTableCellElement : | |
case HTMLLIElement : | |
case HTMLParagraphElement : | |
case HTMLAnchorElement : | |
_form.innerHTML = val; | |
break; | |
} | |
} | |
}, | |
getValue = function(val){ | |
if(_isInput || _isTextArea){ | |
return _parent[_value_key]; | |
} | |
else{ | |
switch(_form.constructor){ | |
case HTMLDivElement : | |
case HTMLSpanElement : | |
case HTMLTableCellElement : | |
case HTMLLIElement : | |
case HTMLParagraphElement : | |
case HTMLAnchorElement : | |
return _form.innerHTML; | |
break; | |
default : | |
return null; | |
break; | |
} | |
} | |
} | |
// 자동 업데이트 할까말까, 일괄로 하자꾸나 | |
// setInterval(refresh, 500); | |
return{ | |
setValue : setValue, | |
getValue : getValue, | |
refresh : refresh | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment