// trivial remote scripting library // initially based on sajax: // http://www.modernmethod.com/sajax/ // (c) copyright 2005 modernmethod, inc // (no license reported) // requires: // common_js.js // This file defines default message text in English. For translated versions, // make sure to include <script src="tajax_messages.js"></script> AFTER // including tajax.js. /* * * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ tajax = {}; tajax.request_type = "GET"; // GET / POST tajax.failure_redirect = "/error.xml"; tajax.result_id = ""; /** * private XMLHttpRequest __createRequest__(); */ tajax.__createRequest__ = function() { js.debug("tajax.__createRequest__() called.") var A; var msxmlhttp = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'); for (var i = 0; i < msxmlhttp.length; i++) { try { A = new ActiveXObject(msxmlhttp[i]); } catch (e) { A = null; } } if(!A && typeof XMLHttpRequest != "undefined") A = new XMLHttpRequest(); if (!A) js.error(tajax.messages.cant_create_connection); return A; }; tajax.requests = new Array(); /** * Cancel all pending requests. */ tajax.cancel = function() { for (var i = 0; i < tajax.requests.length; i++) { if (tajax.requests[i]) { tajax.requests[i].abort(); } } } /** * public void tajax.retrieve(String uri, Function callback, Object extra_data, * String post_data, String xmlOrText) * * Perform an asynchronous call to uri, with optional post_data. On successful * retrieval of data, the callback function will be called with arguments * (response_object, extra_data). * * If there was a server request problem (such as 404 or 500 errors), then * the response_object contains a non-null 'error' property, with details * about the error. * * The calling function is expected to properly setup the uri and post_data * based on the tajax.retrieve_type setting. * * The response_object contains the following properties: * - int status: HTTP status code * - String statusText: HTTP status text * - String error: null if there was no server-side reported error, otherwise * a string w/ a description of the server error. * - String text: raw response data. * - DomNode xml: DOM representation of the data (if valid XML). * * @param uri the URI to retrieve from the server. This can be a relative URI. * @param callback the static callback function executed on successful retrieval * of data from the URI. * @param extra_data extra data passed to the callback function. Usually, this * contains the "this" reference to the invoking object. * @param post_data (optional) if not null or empty, then this data will be * sent in the post data section of the HTTP request. */ tajax.retrieve = function(uri, callback, extra_data, post_data) { var i, x, n; js.debug("in tajax.retrieve()." + tajax.request_type); if (typeof(tajax.request_type) == "undefined" || tajax.request_type == "") tajax.request_type = "GET"; if (tajax.request_type == "GET") { post_data = null; } else if (tajax.request_type == "POST") { // ignore } else { js.error(js.msg( tajax.messages.bad_request_type, tajax.request_type)); } x = tajax.__createRequest__(); if (x == null) { if (tajax.failure_redirect != "") { location.href = tajax.failure_redirect; return false; } else { js.error(js.msg(tajax.messages.null_tajax, navigator.userAgent)); return false; } } else { x.open(tajax.request_type, uri, true); // window.open(uri); tajax.requests[tajax.requests.length] = x; if (tajax.request_type == "POST") { x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1"); x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } x.onreadystatechange = function() { if (x.readyState != 4) return; var response = { status: x.status, statusText: x.statusText, text: x.responseText, xml: x.responseXML, error: null }; if (x.status / 100 >= 4) { // error in the response response.error = js.msg(tajax.messages.bad_response, x.status, x.statusText); js.error(response.error); } js.debug("received " + x.responseText); var trimmed = x.responseText.replace(/^\s*|\s*$/g,""); if (trimmed == "") { // let's just assume this is a pre-response bailout and let it slide for now } else { try { callback(response, extra_data); } catch (e) { js.except(js.msg(tajax.messages.callback_error, response.text), e); } } for (var i = 0; i < tajax.requests.length; i++) { if (tajax.requests[i] == x) { delete tajax.requests[i]; } } } } js.debug("uri = " + uri + "/post = " + post_data); x.send(post_data); js.debug("waiting."); delete x; return true; } tajax.assembleArguments = function(args) { uri = ""; if (args) { for (var key in args) { var val = args[key]; uri += escape(key) + "=" + escape(val); } } return uri; } tajax.constructURI = function(baseURI, args) { var uri = baseURI; if (tajax.request_type == "GET") { if (uri.indexOf("?") == -1) uri += "?"; uri += tajax.assembleArguments(args); } else if (tajax.request_type == "POST") { // do nothing } else { js.failure(js.msg(tajax.messages.bad_request_type, tajax.request_type)); } return uri; } tajax.constructPOST = function(args) { var post = ""; if (tajax.request_type == "GET") { // do nothing } else if (tajax.request_type == "POST") { post = tajax.assembleArguments(args); } else { js.failure(js.msg(tajax.messages.bad_request_type, tajax.request_type)); } return post; } /** * void createUpdate(String uri, String callbackName, boolean xmlResponse) * * Creates a function for a controller object to handle the retrive / callback * setup. The function will require an optional "args" parameter, which is * expected to be a hashtable. * * This is the primary entry method everyone will use. * * @param uri the base URI for the call. * @param callbackName name of the function (must be static) to call back. * It will take as arguments (resultData, controller). * @param xmlResponse true if the response data should be send to the callback * as XML data or as */ tajax.createUpdate = function(uri, callbackName, xmlResponse) { var nuri = "'" + uri.replace(/'/,"\\'") + "'"; return new Function('args', 'var uri = tajax.constructURI(' + nuri + ', args);' + 'var post = tajax.constructPOST(args);' + 'tajax.retrieve(uri, ' + callbackName + ', this, post);' ); } /* ========================================================================= * DEFAULT MESSAGE TEXT * * Defaults to English. */ tajax.messages = {} tajax.messages.language_code = "en"; tajax.messages.language = "US English"; // internal error messages tajax.messages.bad_request_type = "Illegal request type: {1}: {2}"; tajax.messages.cant_create_connection = "Could not create connection object."; tajax.messages.null_tajax = "NULL tajax object for user agent:\n{1}"; tajax.messages.bad_response = "Bad server response ({1}): {?2}"; tajax.messages.callback_error="Caught exception during callback (data = {2})";
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 5925 | Matt Albrecht | Updates to the server status page | ||
#2 | 5816 | Matt Albrecht | Add / update the server status with a much better AJAX model. | ||
#1 | 5810 | Matt Albrecht |
New version of logDaemon that has better bug fixes. Also added server-status web page for a pure client-side AJAX app that pulls together different server-side statistics. |