// Javascript Language Prototype // 문성광, 2007-11-06 // functiona.extend(functionb) // function 상속 // jsimport('nets.im'); // namespace object creation // nets.im.util = new function() { ... } // namespace class definition Function.prototype.extend = function(baseClass) { this.prototype = new baseClass; this.prototype.parent = this.prototype; this.prototype.base = baseClass; }; function jsimport(namespace) { var obj = '', ns = namespace.split('.'); for (var i = 0, iend = ns.length; i < iend; i++) { if (eval('typeof(' + obj + ns[i] + ') == "undefined"')) eval(obj + ns[i] + ' = new function() {}'); obj = obj + ns[i] + '.'; } } (function() { this.$ = function(id) { function getElement(id) { if (typeof(id) == 'object') return id; var obj = document.getElementById(id); if (obj == null) { obj = document.getElementsByName(id); if (obj != null) obj = obj[0]; } return obj; } var obj = typeof(id) == 'string' ? getElement(id) : id; return obj != null ? new E(obj) : null; }; this.$$ = this.$; function E(obj) { this.object = obj; this.object.val = function(val) { if (typeof(val) != 'undefined') { this.value = val; return this; } return this.value; }; this.object.len = function() { return typeof(this.length) != 'undefined' ? this.length : this.value.length; }; this.object.isCheckBox = function() { return typeof(this.type) != 'undefined' && this.type.toLowerCase() == 'checkbox'; }; this.object.isRadioButton = function() { return typeof(this.type) != 'undefined' && this.toLowerCase() == 'radio'; }; this.object.isSelectBox = function() { return this.tagName.toUpperCase() == 'SELECT'; }; this.object.checkable = function() { if (typeof(this.type) != 'undefined') { var type = this.type.toLowerCase(); return type == 'checkbox' || type == 'radio'; } return false; }; this.object.getChecked = function() { if (this.checkable()) { var elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (elems[i].checked) return elems[i]; } return null; }; this.object.getCheckedValue = function() { if (this.checkable()) { var elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (elems[i].checked) return elems[i].value; } return null; }; this.object.getCheckedAll = function() { if (this.isCheckBox()) { var array = [], elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (elems[i].checked) array.push(elems[i]); return array; } return null; }; this.object.getCheckedValueAll = function() { if (this.isCheckBox()) { var array = [], elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (elems[i].checked) array.push(elems[i].value); return array; } return null; }; this.object.check = function(val) { if (this.checkable()) { var elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (elems[i].value == val) elems[i].checked = true; } return this; }; this.object.hasChecked = function(msg) { if (this.getChecked() == null) { alert(typeof(msg) != 'undefined' ? msg : '항목을 선택합니다.'); return false; } return true; }; this.object.checkAll = function(checked) { if (this.isCheckBox()) { var elems = document.getElementsByName(this.name); for (var i = 0, iend = elems.length; i < iend; i++) if (!elems[i].disabled) elems[i].checked = checked; } return this; }; this.object.getSelected = function() { return this.isSelectBox() ? this.options[this.selectedIndex] : null; }; this.object.getSelectedValue = function() { return this.isSelectBox() ? this.options[this.selectedIndex].value : null; }; this.object.getSelectedAll = function() { if (this.isSelectBox()) { var array = []; var opts = this.options; for (var i = 0, iend = opts.length; i < iend; i++) if (opts[i].selected) array.push(opts); return array; } return null; }; this.object.select = function(val) { if (this.isSelectBox()) { var opts = this.options; var v; for (var i = 0, iend = opts.length; i < iend; i++) { v = opts[i].value; if (!v.isEmpty()) { if (v == val) opts[i].selected = true; } else { if (opts[i].innerText == val) opts[i].selected = true; } } } return this; }; this.object.addOption = function(text, value, selected) { if (this.isSelectBox()) { var opts = this.options; opts[opts.length] = new Option(text, value); if (typeof(selected) != 'undefined') opts[opts.length - 1].selected = true; } return this; }; this.object.deleteSelectedOption = function() { if (this.isSelectBox()) { var opts = this.options; for (var i = opts.length - 1; i >= 0; i--) if (opts[i].selected) opts[i] = null; } return this; }; this.object.selectAll = function() { if (this.isSelectBox()) { var opts = this.options; for (var i = 0, iend = opts.length; i < iend; i++) opts[i].selected = true; } return this; }; this.object.html = function(html) { if (typeof(html) != 'undefined') { this.innerHTML = html; return this; } return this.innerHTML; }; this.object.e = this; //해당 object를 보여준다. this.object.show = function(val) { this.style.display = typeof(val) != 'undefined' ? val : 'block'; return this; }; //해당 object를 숨긴다. this.object.hide = function() { this.style.display = 'none'; return this; }; this.object.toggle = function(val) { return this.style.display == 'none' ? this.show(val) : this.hide(); }; this.object.disable = function() { if(typeof(this.disabled) != 'undefined') this.disabled = true; }; this.object.enable = function() { if(typeof(this.disabled) != 'undefined') this.disabled = false; }; this.object.attr = function(name, value) { if (typeof(value) == 'undefined') return eval('this.' + name); else { eval('this.' + name + '=value'); return this; } }; this.object.parent = function() { return new E(this.parentNode); }; this.object.next = function() { var elem = this.nextSibling; while (typeof(elem.tagName) == 'undefined') elem = elem.nextSibling; return new E(elem); }; this.object.pre = function() { var elem = this.previousSibling; while (typeof(elem.tagName) == 'undefined') elem = elem.previousSibling; return new E(elem); }; this.object.first = function() { var elem = this.firstChild; while (typeof(elem.tagName) == 'undefined') elem = elem.nextSibling; return new E(elem); }; this.object.last = function() { var elem = this.lastChild; while (typeof(elem.tagName) == 'undefined') elem = elem.previousSibling; return new E(elem); }; this.object.child = function() { var nodes = this.childNodes; var arrays = []; for (var i = 0, iend = nodes.length; i < iend; ++i) arrays.push(new E(nodes.item(i))); return arrays; }; return this.object; } })(); // get element by ID or Name(IE는 name 속성의 값을 id 속성의 기본 값으로 사용하지만 타 브라우저는 id 와 name을 엄격히 구분한다. //function $(id) //{ // var obj = document.getElementById(id); // if (obj == null) // { // obj = document.getElementsByName(id); // if (obj != null) // obj = obj[0]; // } // return obj; //} function $PARENT(id) { var obj = parent.document.getElementById(id); if (obj == null) { obj = parent.document.getElementsByName(id); if (obj != null) obj = obj[0]; } return obj; } // get element by name function $NAME(name) { return document.getElementsByName(name); } // get checked radio function $CHECKED(name) { return $$(name).getChecked(); } // get checked radio function $CHECK(name, value) { $$(name).check(value); } // get checked all checkbox function $ALLCHECKED(name) { var obj = $$(name); return obj != null ? obj.getCheckedAll() : []; } //check all checkbox function $ALLCHECK(name, checked) { if($$(name) != null) $$(name).checkAll(checked); } // get selected option function $SELECTED(name) { return $$(name).getSelected(); } function $SELECT(name, value) { $$(name).select(value); } function $SELECTADD(name, text, value, selected) { $$(name).addOption(text, value, selected); } function $DELETE_SELECTED(name) { $$(name).deleteSelectedOption(); } function $SELECTALL(name) { var opts = $(name).options; for (var i = 0, iend = opts.length; i < iend; i++) opts[i].selected = true; //$(name).selectAll(); //TODO::리소스목록에서 전체 페이지에 checkbox와 동일한 name의 hidden이 있어 호환이 안되어 주석처리. } // extend String: append format method String.prototype.format = function() { var s = this; for (var i = 0, iend = arguments.length; i < iend; i++) while (s.indexOf("{" + i + "}") >= 0) s = s.replace("{" + i + "}", arguments[i]); return s; }; // extend String: append startsWith method String.prototype.startsWith = function(str) { return str != null && this.length >= str.length && this.substring(0, str.length) == str; }; String.prototype.endsWith = function(str) { return str != null && this.length >= str.length && this.lastIndexOf(str) == (this.length - str.length); }; // 문자열 앞/뒤의 공백을 제거한 문자열을 리턴한다. String.prototype.trim = function() { var s = this; return s.replace(/(^\s*)|(\s*$)/g, ""); }; String.prototype.fill = function(length) { var s = this; for (var i = s.length; i < length; i++) s += ' '; return s; }; String.prototype.toHTML = function() { var s = this; return s.replace(/(<|>|&|\"|\')/g, function($1) { switch ($1) { case '<': return "<"; case '>': return ">" case '&': return "&"; case '"': return """ case '\'':return "'"; } } ); }; String.prototype.toInt = function() { return parseInt(this); }; String.prototype.toFloat = function() { return parseFloat(this); }; String.prototype.toUTF8 = function() { return unescape(encodeURIComponent(this)); }; String.prototype.lengthUTF8 = function() { var size = 0; for(var i=0, iend=this.length; i < iend; ++i) { var code = this.charCodeAt(i); if(code < 128 ) size += 1; else if(code < 2048) size += 2; else if(code < 65536) size +=3; else size += 4; } return size; }; String.prototype.isEmpty = function() { return this.length == 0; }; String.prototype.contains = function(str) { return this.indexOf(str) > -1; }; String.prototype.leftTrim = function() { return this.replace(/^\s+/,""); }; String.prototype.rightTrim = function() { return this.replace(/\s+$/,""); }; Array.prototype.contains = function(elem) { return this.indexOf(elem) >= 0; }; Array.prototype.indexOf = function(elem) { for (var i = 0, iend = this.length; i < iend; i++) if (elem == this[i]) return i; return -1; }; Array.prototype.each = function(iter) { for (var i = 0,iend = this.length; i < iend; i++) iter(this[i], i); }; Array.prototype.first = function() { return this[0]; }; Array.prototype.last = function() { return this[this.length - 1]; }; Array.prototype.size = function() { return this.length; }; Array.prototype.isEmpty = function() { return this.length == 0; }; Array.prototype.removeElement = function(e) { for(var i=0,iend=this.length; i < iend; ++i) { if(this[i] == e) { return this.splice(i, 1); } } }; Array.prototype.removeIndex = function(index) { return this.splice(index, 1); }; // Date를 형식화 하여 문자열로 리턴한다. Date.prototype.format = function(f) { if (!this.valueOf()) return ' '; var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var d = this; return f.replace(/(yyyy|yy|y|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|SSS|a)/g, function($1) { function char3(s) { var t = '000' + s; return t.substr(t.length - 3, 3); } function char2(s) { var t = '00' + s; return t.substr(t.length - 2, 2); } switch ($1) { case 'yyyy': return d.getFullYear(); case 'yy': return char2(d.getFullYear() % 100); case 'y': return (d.getFullYear() % 100); case 'MMMM': return monthNames[d.getMonth()]; case 'MMM': return monthNames[d.getMonth()].substr(0, 3); case 'MM': return char2(d.getMonth() + 1); case 'M': return (d.getMonth() + 1); case 'dddd': return dayNames[d.getDay()]; case 'ddd': return dayNames[d.getDay()].substr(0, 3); case 'dd': return char2(d.getDate()); case 'd': return d.getDate(); case 'HH': return char2(d.getHours()); case 'H': return d.getHours(); case 'hh': return char2((h = d.getHours() % 12) ? h : 12); case 'h': return ((h = d.getHours() % 12) ? h : 12); case 'mm': return char2(d.getMinutes()); case 'm': return d.getMinutes(); case 'ss': return char2(d.getSeconds()); case 's': return d.getSeconds(); case 'SSS': return char3(d.getMilliseconds()); case 'a': return d.getHours() < 12 ? 'AM' : 'PM'; } } ); }; // JavaScript implementation of Java StringBuffer class function StringBuffer(str) { this.strings = new Array(''); this.append = function(str) { if (str != null) this.strings.push(str); return this; }; this.clear = function() { this.strings.length = 1; }; this.toString = function() { return this.strings.join(''); }; this.append(str); } function debugDOM(obj, all) { function substr(val) { return val.length > 50 ? val.substring(0, 50) + '...' : val; } var val = []; for (p in obj) try { if (typeof(obj[p]) != 'function' && (all == true || (obj[p] != null && obj[p] != ''))) val.push(p + '=' + substr(obj[p])); } catch (e) { } val.sort(); alert(val.join('\n')); } function debugFunctions(obj) { var val = []; for (p in obj) try { if (typeof(obj[p]) == 'function') val.push(p); } catch (e) { } val.sort(); alert(val.join('\n')); } function isIE() { return navigator.appName == 'Microsoft Internet Explorer'; } (function () { this.$F = function(idx) { return new Form(idx).getForm(); }; function Form(idx) { this.form = document.forms[typeof(idx) != 'undefined' ? idx : 0]; this.form.f = this; this.getForm = function() { return this.form; }; this.form.get = function(name) { return this[name]; }; this.form.set = function(name, value) { this[name].value = value; }; this.form.getType = function(item) { if (item.tagName != 'SELECT' && typeof(item.length) != 'undefined') for (var i = 0,iend = item.length; i < iend; ++i) { return item[i].type; } return item.type; }; this.form.getName = function(item) { if (item.tagName != 'SELECT' && typeof(item.length) != 'undefined') { for (var i = 0; i < item.length; i++) return item[i].name; } return item.name; }; this.form.getCheckboxValue = function(item) { if (typeof(item.length) != 'undefined') { var array = []; for (var i = 0; i < item.length; i++) if (item[i].checked) array.push(item[i].value); return array.join(','); } else if (item.checked) { return item.value; } return null; }; this.form.getSelectValue = function(item) { if (typeof(item.length) != 'undefined') { var array = []; for (var i = 0; i < item.length; i++) if (item[i].selected) array.push(item[i].value); return array.join(','); } else if (item.selected) { return item.value; } return null; }; this.form.getNames = function() { var os = []; for (var i = 0,iend = this.elements; i < iend; ++i) { var item = this.elements[i]; var name = item.name; if (!os.contains(name)) os.push(name); } return os; }; this.form.toJSON = function(includes) { var ret = {}; for (var i = 0,iend = this.elements.length; i < iend; ++i) { var item = this.elements[i]; var type = this.getType(item); var name = null, val = null; switch (type) { case "radio" : case "checkbox" : { name = this.getName(item); val = this.getCheckboxValue(item); if(val == null) name = null; break; } case "select-multiple" : case "select-one" : { name = this.getName(item); val = this.getSelectValue(item); break; } case "text" : case "password" : case "hidden" : case "textarea" : { name = this.getName(item); val = item.value; break; } default: continue; } if (name != null) { if (typeof(includes) != 'undefined' && !includes.contains(name)) continue; var t = eval('ret.' + name); if (typeof(t) != 'undefined') { if (t != null) { if (val != null) eval('ret.' + name + '+= ("," + val)'); } else if (val != null) { eval('ret.' + name + '=val'); } } else eval('ret.' + name + '=val'); } } return ret; }; this.form.toJSONString = function(isValid, includes) { if(typeof(isValid) != 'undefined') isValid = true; var params = this.toJSON(includes); var sb = new StringBuffer(), cnt = 0; for (var param in params) { var val = eval('params.' + param); if(isValid && val == null) continue; if (cnt++ > 0) sb.append("&"); sb.append(param).append("=").append(encodeURIComponent(val)); } return sb.toString(); }; this.form.setValues = function(obj) { for (var p in obj) { var e = this[p]; if (e != null) { var val = eval('obj.' + p); if (val == null) continue; var type = this.getType(e); switch (type) { case "radio" : case "checkbox" : this.setCheckboxValue(e, val); break; case "select-multiple" : case "select-one" : this.setSelectValue(e, val); break; case "text" : case "password" : case "hidden" : case "textarea" : e.value = val; break; default: break; } } } }; this.form.setCheckboxValue = function(obj, val) { var vals = val.split(','); vals.each(function(v) { if (obj.length) { for (var i = 0; i < obj.length; i++) { if (obj[i].value == v) obj[i].checked = true; } } else { if (obj.value == v) obj.checked = true; } }); }; this.form.setSelectValue = function(obj, val) { val.split(',').each(function(v) { if (obj.length) { for (var i = 0; i < obj.length; i++) { if (obj[i].value == v) obj[i].selected = true; } } else { if (obj.value == v) obj.selected = true; } }); } } })(); function _openWin(url, width, height, name, params) { var features = null; if(typeof(params) != 'undefined' && params != null) { var sb = new StringBuffer(), cnt = 0; for (var param in params) { if (cnt++ > 0) sb.append(","); sb.append(param).append("=").append(encodeURIComponent(eval('params.' + param))); } features = sb.toString(); } else { features = 'scrollbars=no,menubar=no,toolbar=no,location=no,directories=no,resizable=no,status=yes'; } var left =0, top = 0; if(screen.width > 1025) { /* // Fixes dual-screen position Most browsers Firefox var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left; var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top; left = ((screen.width / 2) - (width / 2)) + dualScreenLeft; top = ((screen.height / 2) - (height / 2));*/ left=(screen.width)?(screen.width-width)/2:100; top=(screen.height)?(screen.height-height)/2:100; } var win = window.open(url, '_blank', 'width='+width+'px,height='+height+'px,top='+top+',left='+left+','+features); win.focus(); return win; } function _openModalWin(url, width, heigth, args, params) { args = typeof(args) == 'undefined' || args == null ? {} : args; var features = null; if(typeof(params) == 'undefined' || params == null) { var sb = new StringBuffer(), cnt = 0; for (var param in params) { if (cnt++ > 0) sb.append(";"); sb.append(param).append(":").append(encodeURIComponent(eval('params.' + param))); } features = sb.append(';dialogWidth:').append(width).append('px;dialogHeight:').append(heigth).append('px;').toString(); } else { features = 'dialogWidth:'+width+'px;dialogHeight:'+heigth+'px;scroll:no;status:no;help:no;scrollbars:no'; } return window.showModalDialog(url, args, features); } // Dialog.show(url, 400, 180, function(value) { alert(value); }); var Dialog = { _dialog:null, _returnValue:null, _parameter:null, _callback:null, _width:0, _height:0, _oldOnFocus:null, // for all except Opera _oldOnClick:null, // for FireFox _oldOnBodyClick:null, // for IE getWidth: function() { return this._width; }, getHeight: function() { return this._height; }, getParameter: function(defaultValue) { return this._parameter != null ? this._parameter : defaultValue; }, show: function(url, width, height, parameter, callback) { this._width = width; this._height = height; this._parameter = parameter; this._callback = callback; var x = (screen.width - width)/2, y = (screen.height - height)/2; this._dialog = window.open(url, 'dialog', 'height=' + height + ',width=' + width + ',left=' + x + ',top=' + y + ',location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); this._oldOnFocus = window.onfocus; this._oldOnClick = window.onclick; this._oldOnBodyClick = document.body.onclick; window.focus = window.onclick = document.body.onclick = function() { Dialog.focus(); } this.ensureClosed(); }, focus: function() { try { if (this._dialog != null) { this._dialog.blur(); // for chrome this._dialog.focus(); } } catch(e) { // ignore } }, onclose: function() { if (this._dialog != null) { if (typeof(this._callback) != 'undefined') this._callback(this._returnValue); this._dialog = null; this._returnValue = null; window.focus(); } }, setReturn: function(value) { // copy only root values - return value must be simple object function copy(value) { if (value != null && typeof(value) == 'object') { if (value instanceof Array) { var array = []; for (var i=0, iend=value.length; i