/** * Functions for sending AJAX requests to the server. */ (function($){ /** * Set this variable to false to hide the "waiting for server response" layer. */ window.show_waiting_message = true; /** * Set this variable to false to hide the "do you want to leave the page?" dialog. */ window.show_leaving_warning = true; /** * This variable stores the .wfsr jQuery object. */ var waiting_obj; /** * Function for compatibility with XE's exec_xml() */ window.exec_xml = $.exec_xml = function(module, act, params, callback_success, return_fields, callback_success_arg, fo_obj) { // Convert params to object and fill in the module and act. params = params ? ($.isArray(params) ? arr2obj(params) : params) : {}; params.module = module; params.act = act; // Fill in the XE vid. if (typeof(xeVid) != "undefined") params.vid = xeVid; // Add 'error' and 'message' to response tags. if (typeof(return_fields) == "undefined" || return_fields.length < 1) { return_fields = ["error", "message"]; } else { return_fields.push("error", "message"); } // Decide whether or not to use SSL. var url = request_uri; if ($.isArray(ssl_actions) && params.act && $.inArray(params.act, ssl_actions) >= 0) { url = default_url || request_uri; var port = window.https_port || 443; var _ul = $("").attr("href", url)[0]; var target = "https://" + _ul.hostname.replace(/:\d+$/, ""); if (port != 443) target += ":" + port; if (_ul.pathname[0] != "/") target += "/"; target += _ul.pathname; url = target.replace(/\/$/, "") + "/"; } // Check whether this is a cross-domain request. If so, use an alternative method. var _u1 = $("").attr("href", location.href)[0]; var _u2 = $("").attr("href", url)[0]; if (_u1.protocol != _u2.protocol || _u1.port != _u2.port) return send_by_form(url, params); // Delay the waiting message for 1 second to prevent rapid blinking. waiting_obj.css("opacity", 0.0); var wfsr_timeout = setTimeout(function() { if (show_waiting_message) { waiting_obj.css("opacity", "").html(waiting_message).show(); } }, 1000); // Define the success handler. var successHandler = function(data, textStatus, xhr) { // Hide the waiting message. clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); // Copy data to the result object. var result = {}; $.each(data, function(key, val) { if (key == "act" || key == "redirect_url" || $.inArray(key, return_fields)) { if ($.isArray(val)) { result[key] = { item: val }; } else { result[key] = val; } } }); // If the response contains an error, display the error message. if (data.error != "0") { // This way of calling an error handler is deprecated. Do not use it. if ($.isFunction($.exec_xml.onerror)) { if (typeof console == "object" && typeof console.log == "function") { console.log("DEPRECATED : $.exec_xml.onerror() is deprecated in Rhymix."); } return $.exec_xml.onerror(module, act, data, callback_success, return_fields, callback_success_arg, fo_obj); } // Display the error message, or a generic stub if there is no error message. if (data.message) { alert(data.message.replace(/\\n/g, "\n")); } else { alert("AJAX communication error while requesting " + params.module + "." + params.act); } return null; } // If the response contains a redirect URL, redirect immediately. if (result.redirect_url) { window.location = result.redirect_url.replace(/&/g, "&"); return null; } // If there was a success callback, call it. if ($.isFunction(callback_success)) { callback_success(result, return_fields, callback_success_arg, fo_obj); } }; // Define the error handler. var errorHandler = function(xhr, textStatus) { clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); alert("AJAX communication error while requesting " + params.module + "." + params.act + "\n\n" + xhr.status + " " + xhr.statusText); }; // Send the AJAX request. try { $.ajax({ url : url, type : "POST", dataType : "json", data : params, success : successHandler, error : errorHandler }); } catch(e) { alert(e); return; } }; /** * Function for compatibility with XE's exec_json() */ window.exec_json = $.exec_json = function(action, params, callback_success, callback_error) { // Convert params to object and fill in the module and act. params = params ? ($.isArray(params) ? arr2obj(params) : params) : {}; action = action.split("."); if (action.length != 2) return; params.module = action[0]; params.act = action[1]; // Fill in the XE vid. if (typeof(xeVid) != "undefined") params.vid = xeVid; // Delay the waiting message for 1 second to prevent rapid blinking. waiting_obj.css("opacity", 0.0); var wfsr_timeout = setTimeout(function() { if (show_waiting_message) { waiting_obj.css("opacity", "").html(waiting_message).show(); } }, 1000); // Define the success handler. var successHandler = function(data, textStatus, xhr) { // Hide the waiting message. clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); // If the response contains an error, display the error message. if(data.error != "0" && data.error > -1000) { if(data.error == -1 && data.message == "msg_is_not_administrator") { alert("You are not logged in as an administrator."); if ($.isFunction(callback_error)) { callback_error(data); } return; } else { if (data.message) { alert(data.message.replace(/\\n/g, "\n")); } else { alert("AJAX communication error while requesting " + data.module + "." + data.act); } if ($.isFunction(callback_error)) { callback_error(data); } return; } } // If there was a success callback, call it. if($.isFunction(callback_success)) { clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); callback_success(data); } }; // Define the error handler. var errorHandler = function(xhr, textStatus) { alert("AJAX communication error while requesting " + params.module + "." + params.act + "\n\n" + xhr.status + " " + xhr.statusText); }; // Send the AJAX request. try { $.ajax({ type: "POST", dataType: "json", url: request_uri, data: params, success : successHandler, error : errorHandler }); } catch(e) { alert(e); return; } }; /** * Function for compatibility with XE's exec_html() */ window.exec_html = $.fn.exec_html = function(action, params, type, callback_func, callback_args) { // Convert params to object and fill in the module and act. params = params ? ($.isArray(params) ? arr2obj(params) : params) : {}; action = action.split("."); if (action.length != 2) return; params.module = action[0]; params.act = action[1]; // Fill in the XE vid. if (typeof(xeVid) != "undefined") params.vid = xeVid; // Determine the request type. if(!$.inArray(type, ["html", "append", "prepend"])) type = "html"; var self = $(this); // Delay the waiting message for 1 second to prevent rapid blinking. waiting_obj.css("opacity", 0.0); var wfsr_timeout = setTimeout(function() { if (show_waiting_message) { waiting_obj.css("opacity", "").html(waiting_message).show(); } }, 1000); // Define the success handler. var successHandler = function(data, textStatus, xhr) { clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); if (self && self[type]) { self[type](html); } if ($.isFunction(callback_func)) { callback_func(callback_args); } }; // Define the error handler. var errorHandler = function(xhr, textStatus) { clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); alert("AJAX communication error while requesting " + params.module + "." + params.act + "\n\n" + xhr.status + " " + xhr.statusText); }; // Send the AJAX request. try { $.ajax({ type: "POST", dataType: "html", url: request_uri, data: params, success: successHandler, error: errorHandler }); } catch(e) { alert(e); return; } }; /** * Empty placeholder for beforeUnload handler. */ var beforeUnloadHandler = function() { return ""; }; /** * Register the beforeUnload handler. */ $(function() { waiting_obj = $(".wfsr"); if (show_leaving_warning) { $(document).ajaxStart(function() { $(window).bind("beforeunload", beforeUnloadHandler); }).bind("ajaxStop cancel_confirm", function() { $(window).unbind("beforeunload", beforeUnloadHandler); }); } }); })(jQuery); /** * This function simulates AJAX requests with HTML forms. * It was meant for cross-domain requests, but should be replaced with JSONP. */ function send_by_form(url, params) { // This function is deprecated! if (typeof console == "object" && typeof console.log == "function") { console.log("DEPRECATED : send_by_form() is deprecated in Rhymix."); } // Create the hidden iframe. var frame_id = "xeTmpIframe"; if (!$("#" + frame_id).length) { $(''.replace(/%id%/g, frame_id)).appendTo(document.body); } // Create the hidden form. var form_id = "xeVirtualForm"; $("#" + form_id).remove(); var form = $('
'.replace(/%id%/g, form_id)).attr({ "id" : form_id, "method" : "post", "action" : url, "target" : frame_id }); // Add virtual XML parameters. params.xeVirtualRequestMethod = "xml"; params.xeRequestURI = location.href.replace(/#(.*)$/i, ""); params.xeVirtualRequestUrl = request_uri; // Add inputs to the form. $.each(params, function(key, value){ $('').attr("name", key).attr("value", value).appendTo(form); }); // Submit the hidden form. form.appendTo(document.body).submit(); } /** * This function converts arrays into objects. */ function arr2obj(arr) { var ret = {}; for (var key in arr) { if (arr.hasOwnProperty(key)) { ret[key] = arr[key]; } } return ret; } /** * This work is licensed under Creative Commons GNU LGPL License. * License: http://creativecommons.org/licenses/LGPL/2.1/ * Version: 0.9 * Author: Stefan Goessner/2006 * Web: http://goessner.net/ **/ function xml2json(xml, tab, ignoreAttrib) { var X = { toObj: function(xml) { var o = {}; if (xml.nodeType==1) { // element node .. if (ignoreAttrib && xml.attributes.length) { // element with attributes .. for (var i=0; i 1) { o = X.escape(X.innerXml(xml)); } else { for (var n2=xml.firstChild; n2; n2=n2.nextSibling) { // o["#cdata"] = X.escape(n2.nodeValue); o = X.escape(n2.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 n3 = e.firstChild; n3; ) { if (n3.nodeType == 3) { // text node if (!n3.nodeValue.match(/[^ \f\n\r\t\v]/)) { // pure whitespace text node var nxt = n3.nextSibling; e.removeChild(n3); n3 = nxt; } else { n3 = n3.nextSibling; } } else if (n3.nodeType == 1) { // element node X.removeWhite(n3); n3 = n3.nextSibling; } else { // any other node n3 = n3.nextSibling; } } return e; } }; // document node if (xml.nodeType == 9) xml = xml.documentElement; var json_obj = X.toObj(X.removeWhite(xml)), json_str; if (typeof(JSON)=='object' && jQuery.isFunction(JSON.stringify) && false) { var obj = {}; obj[xml.nodeName] = json_obj; json_str = JSON.stringify(obj); return json_str; } else { json_str = X.toJson(json_obj, xml.nodeName, ""); return "{" + (tab ? json_str.replace(/\t/g, tab) : json_str.replace(/\t|\n/g, "")) + "}"; } }