/** * @file common/js/xml_handler.js * @author zero * @brief zbxe내에서 ajax기능을 이용함에 있어 module, act를 잘 사용하기 위한 자바스크립트 **/ // xml handler을 이용하는 user function var show_waiting_message = true; function exec_xml(module, act, params, callback_func, response_tags, callback_func_arg, fo_obj) { var oXml = new xml_handler(); oXml.reset(); if(typeof(params)!='undefined') { for(var key in params) { if(!params.hasOwnProperty(key)) continue; var val = params[key]; oXml.addParam(key, val); } } oXml.addParam("module", module); oXml.addParam("act", act); if(typeof(xeVid)!='undefined') oXml.addParam('vid', xeVid); if(typeof(response_tags)=="undefined" || response_tags.length<1) response_tags = new Array('error','message'); oXml.request(xml_response_filter, oXml, callback_func, response_tags, callback_func_arg, fo_obj); } // 결과 처리 후 callback_func에 넘겨줌 function xml_response_filter(oXml, callback_func, response_tags, callback_func_arg, fo_obj) { var text = oXml.getResponseText(); if(text && !/^/i.test(text)) { var waiting_obj = xGetElementById("waitingforserverresponse"); if(waiting_obj) waiting_obj.style.visibility = "hidden"; alert(text); return null; } var xmlDoc = oXml.getResponseXml(); if(!xmlDoc) return null; var waiting_obj = xGetElementById("waitingforserverresponse"); if(waiting_obj) waiting_obj.style.visibility = "hidden"; var ret_obj = oXml.toZMsgObject(xmlDoc, response_tags); if(ret_obj["error"]!=0) { alert(ret_obj["message"]); return null; } if(ret_obj["redirect_url"]) { location.href=ret_obj["redirect_url"]; return null; } if(!callback_func) return null; callback_func(ret_obj, response_tags, callback_func_arg, fo_obj); return null; } // xml handler function xml_handler() { this.objXmlHttp = null; this.method_name = null; this.xml_path = request_uri+"index.php"; this.params = new Array(); this.reset = xml_handlerReset; this.getXmlHttp = zGetXmlHttp; this.request = xml_handlerRequest; this.setPath = xml_handlerSetPath; this.addParam = xml_handlerAddParam; this.getResponseXml = xml_handlerGetResponseXML; this.getResponseText = xml_handlerGetResponseText; this.toZMsgObject = xml_handlerToZMsgObject; this.parseXMLDoc = xml_parseXmlDoc; this.objXmlHttp = this.getXmlHttp(); } function zGetXmlHttp() { if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { return new ActiveXObject("Microsoft.XMLHTTP"); } } return null; } function xml_handlerRequest(callBackFunc, xmlObj, callBackFunc2, response_tags, callback_func_arg, fo_obj) { var rd = ""; rd += "\n" + "\n" + "\n" for (var key in this.params) { if(!this.params.hasOwnProperty(key)) continue; var val = this.params[key]; rd += "<"+key+">\n"; } rd += "\n" + "\n"; // ssl action if(typeof(ssl_actions)!='undefined' && typeof(ssl_actions.length)!='undefined' && typeof(this.params['act'])!='undefined' && /^https:\/\//i.test(location.href) ) { var action = this.params['act']; for(i=0;i0) { ret_obj[name][ret_obj[name].length] = value; } else { var tmp_value = ret_obj[name]; ret_obj[name] = new Array(); ret_obj[name][ret_obj[name].length] = tmp_value; ret_obj[name][ret_obj[name].length] = value; } } } obj = obj.nextSibling; } return ret_obj; */ } function xml_handlerToZMsgObject(xmlDoc, tags) { if(!xmlDoc) return null; if(!tags) tags = new Array("error","message"); tags[tags.length] = "redirect_url"; tags[tags.length] = "act"; var parsed_array = this.parseXMLDoc(xmlDoc.getElementsByTagName('response')[0]); if(typeof(parsed_array)=='undefined') { var ret = new Array(); ret['error'] = -1; ret['message'] = "Unexpected error occured."; try{ if(typeof(xmlDoc.childNodes[0].firstChild.data)!='undefined') ret['message']+="\r\n"+xmlDoc.childNodes[0].firstChild.data; } catch(e) { } return ret; } var obj_ret = new Array(); for(var i=0; i 1) o = X.escape(X.innerXml(xml)); else for (var n=xml.firstChild; n; n=n.nextSibling){ //o["#cdata"] = X.escape(n.nodeValue); o = X.escape(n.nodeValue); } } } if (!xml.attributes.length && !xml.firstChild) o = null; } else if (xml.nodeType==9) { // document.node o = X.toObj(xml.documentElement); } else alert("unhandled node type: " + xml.nodeType); return o; }, toJson: function(o, name, ind) { var json = name ? ("\""+name+"\"") : ""; if (o instanceof Array) { for (var i=0,n=o.length; i 1 ? ("\n"+ind+"\t"+o.join(",\n"+ind+"\t")+"\n"+ind) : o.join("")) + "]"; } else if (o == null) json += (name&&":") + "null"; else if (typeof(o) == "object") { var arr = []; for (var m in o) arr[arr.length] = X.toJson(o[m], m, ind+"\t"); json += (name?":{":"{") + (arr.length > 1 ? ("\n"+ind+"\t"+arr.join(",\n"+ind+"\t")+"\n"+ind) : arr.join("")) + "}"; } else if (typeof(o) == "string") json += (name&&":") + "\"" + o.toString() + "\""; else json += (name&&":") + o.toString(); return json; }, innerXml: function(node) { var s = "" if ("innerHTML" in node) s = node.innerHTML; else { var asXml = function(n) { var s = ""; if (n.nodeType == 1) { s += "<" + n.nodeName; for (var i=0; i"; } else s += "/>"; } else if (n.nodeType == 3) s += n.nodeValue; else if (n.nodeType == 4) s += ""; return s; }; for (var c=node.firstChild; c; c=c.nextSibling) s += asXml(c); } return s; }, escape: function(txt) { return txt.replace(/[\\]/g, "\\\\") .replace(/[\"]/g, '\\"') .replace(/[\n]/g, '\\n') .replace(/[\r]/g, '\\r'); }, removeWhite: function(e) { e.normalize(); for (var n = e.firstChild; n; ) { if (n.nodeType == 3) { // text node if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) { // pure whitespace text node var nxt = n.nextSibling; e.removeChild(n); n = nxt; } else n = n.nextSibling; } else if (n.nodeType == 1) { // element node X.removeWhite(n); n = n.nextSibling; } else // any other node n = n.nextSibling; } return e; } }; if (xml.nodeType == 9) // document node xml = xml.documentElement; var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, ""); return "{" + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "}"; } /** * @brief exec_json (exec_xml와 같은 용도) **/ (function($){ $.exec_json = function(action,data,func){ if(typeof(data) == 'undefined') data = {}; action = action.split("."); if(action.length == 2){ if(show_waiting_message) { $("#waitingforserverresponse").html(waiting_message).css('top',$(document).scrollTop()+20).css('left',$(document).scrollLeft()+20).css('visibility','visible'); } $.extend(data,{module:action[0],act:action[1]}); if(typeof(xeVid)!='undefined') $.extend(data,{vid:xeVid}); $.ajax({ type:"POST" ,dataType:"json" ,url:request_uri ,contentType:"application/json" ,data:$.param(data) ,success : function(data){ $("#waitingforserverresponse").css('visibility','hidden'); if(data.error > 0) alert(data.message); if($.isFunction(func)) func(data); } }); } }; $.fn.exec_html = function(action,data,type,func,args){ if(typeof(data) == 'undefined') data = {}; if(!$.inArray(type, ['html','append','prepend'])) type = 'html'; var self = $(this); action = action.split("."); if(action.length == 2){ if(show_waiting_message) { $("#waitingforserverresponse").html(waiting_message).css('top',$(document).scrollTop()+20).css('left',$(document).scrollLeft()+20).css('visibility','visible'); } $.extend(data,{module:action[0],act:action[1]}); $.ajax({ type:"POST" ,dataType:"html" ,url:request_uri ,data:$.param(data) ,success : function(html){ $("#waitingforserverresponse").css('visibility','hidden'); self[type](html); if($.isFunction(func)) func(args); } }); } }; })(jQuery);