/** * JavaScript library for use with HTML_AJAX * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to: * Free Software Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * @category HTML * @package Ajax * @author Joshua Eichorn * @author Arpad Ray * @author David Coallier * @author Elizabeth Smith * @copyright 2005 Joshua Eichorn, Arpad Ray, David Coallier, Elizabeth Smith * @license http://www.opensource.org/licenses/lgpl-license.php LGPL */ /** * HTML_AJAX static methods, this is the main proxyless api, it also handles global error and event handling */ var HTML_AJAX = { defaultServerUrl: false, defaultEncoding: 'JSON', queues: false, clientPools: {}, // get an HttpClient, supply a name to use the pool of that name or the default if it isn't found httpClient: function(name) { if (name) { if (this.clientPools[name]) { return this.clientPools[name].getClient(); } } return this.clientPools['default'].getClient(); }, // Pushing the given request to queue specified by it, in default operation this will immediately make a request // request might be delayed or never happen depending on the queue setup // making a sync request to a non immediate queue will cause you problems so just don't do it makeRequest: function(request) { if (!HTML_AJAX.queues[request.queue]) { var e = new Error('Unknown Queue: '+request.queue); if (HTML_AJAX.onError) { HTML_AJAX.onError(e); return false; } else { throw(e); } } else { var qn = request.queue; var q = HTML_AJAX.queues[qn]; HTML_AJAX.queues[request.queue].addRequest(request); return HTML_AJAX.queues[request.queue].processRequest(); } }, // get a serializer object for a specific encoding serializerForEncoding: function(encoding) { for(var i in HTML_AJAX.contentTypeMap) { if (encoding == HTML_AJAX.contentTypeMap[i] || encoding == i) { return eval("new HTML_AJAX_Serialize_"+i+";"); } } return new HTML_AJAX_Serialize_Null(); }, fullcall: function(url,encoding,className,method,callback,args, options) { var serializer = HTML_AJAX.serializerForEncoding(encoding); var request = new HTML_AJAX_Request(serializer); if (callback) { request.isAsync = true; } request.requestUrl = url; request.className = className; request.methodName = method; request.callback = callback; request.args = args; if (options) { for(var i in options) { request[i] = options[i]; } if (options.grab) { if (!request.args || !request.args.length) { request.requestType = 'GET'; } } } return HTML_AJAX.makeRequest(request); }, callPhpCallback: function(phpCallback, jsCallback, url) { var args = new Array(); for (var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } if (HTML_AJAX_Util.getType(phpCallback[0]) == 'object') { jsCallback(phpCallback[0][phpCallback[1]](args)); return; } if (!url) { url = HTML_AJAX.defaultServerUrl; } HTML_AJAX.fullcall(url, HTML_AJAX.defaultEncoding, false, false, jsCallback, args, {phpCallback: phpCallback}); }, call: function(className,method,callback) { var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } return HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,className,method,callback,args); }, grab: function(url,callback,options) { if (!options) { options = {grab:true}; } else { options['grab'] = true; } return HTML_AJAX.fullcall(url,'Null',false,null,callback, '', options); }, post: function(url,payload,callback,options) { var serializer = 'Null'; if (HTML_AJAX_Util.getType(payload) == 'object') { serializer = 'Urlencoded'; } return HTML_AJAX.fullcall(url,serializer,false,null,callback, payload, options); }, replace: function(id) { var callback = function(result) { HTML_AJAX_Util.setInnerHTML(document.getElementById(id),result); } if (arguments.length == 2) { // grab replacement HTML_AJAX.grab(arguments[1],callback); } else { // call replacement var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,arguments[1],arguments[2],callback,args, {grab:true}); } }, append: function(id) { var callback = function(result) { HTML_AJAX_Util.setInnerHTML(document.getElementById(id),result,'append'); } if (arguments.length == 2) { // grab replacement HTML_AJAX.grab(arguments[1],callback); } else { // call replacement var args = new Array(); for(var i = 3; i < arguments.length; i++) { args.push(arguments[i]); } HTML_AJAX.fullcall(HTML_AJAX.defaultServerUrl,HTML_AJAX.defaultEncoding,arguments[1],arguments[2],callback,args, {grab:true}); } }, // override to add top level loading notification (start) Open: function(request) { }, // override to add top level loading notification (finish) Load: function(request) { }, /* // A really basic error handler onError: function(e) { msg = ""; for(var i in e) { msg += i+':'+e[i]+"\n"; } alert(msg); }, */ // Class postfix to content-type map contentTypeMap: { 'JSON': 'application/json', 'Null': 'text/plain', 'Error': 'application/error', 'PHP': 'application/php-serialized', 'HA' : 'application/html_ajax_action', 'Urlencoded': 'application/x-www-form-urlencoded' }, // used internally to make queues work, override Load or onError to perform custom events when a request is complete // fires on success and error requestComplete: function(request,error) { for(var i in HTML_AJAX.queues) { if (HTML_AJAX.queues[i].requestComplete) { HTML_AJAX.queues[i].requestComplete(request,error); } } }, // turns a form into a urlencoded string formEncode: function(form, array_format) { form = HTML_AJAX_Util.getElement(form); var el, inpType, value, name; var out = (array_format) ? {} : ''; var inputTags = form.getElementsByTagName('INPUT'); var selectTags = form.getElementsByTagName('SELECT'); var buttonTags = form.getElementsByTagName('BUTTON'); var textareaTags = form.getElementsByTagName('TEXTAREA'); var arrayRegex = /(.+)%5B%5D/; var validElement = function (element) { if (!element || !element.getAttribute) { return false; } el = element; name = HTML_AJAX_Util.encodeUrl(el.getAttribute('name')); if (!name) { // no element name so skip return false; } if (element.disabled) { return false; } if (!array_format) { value = HTML_AJAX_Util.encodeUrl(el.value); } else { value = el.value; } inpType = el.getAttribute('type'); return true; } inputLoop: for (var i=0; i < inputTags.length; i++) { if (!validElement(inputTags[i])) { continue; } if (inpType == 'checkbox' || inpType == 'radio') { if (!el.checked) { // unchecked radios/checkboxes don't get submitted continue inputLoop; } var arr_var = arrayRegex.exec(name); if (array_format && arr_var) { if (!out[arr_var[1]]) { out[arr_var[1]] = new Array(); } out[arr_var[1]].push(value); continue inputLoop; } } // add element to output array if (array_format) { out[name] = value; } else { out += name + '=' + value + '&'; } } // end inputLoop selectLoop: for (var i=0; i