X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=ajax%2Fajax.js;h=21a993498c5e2c43220a0bc613e8eaa3e728d1f6;hb=582075e965a7df985aede0e088928c486fe9ce06;hp=257ae02b26582e7bc8733901aa26da30c463e5ea;hpb=4986837802a65ced713c2dac2ff7d80e42fd7807;p=jquery.git diff --git a/ajax/ajax.js b/ajax/ajax.js index 257ae02..21a9934 100644 --- a/ajax/ajax.js +++ b/ajax/ajax.js @@ -9,7 +9,6 @@ if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'fun }; } -// // Counter for holding the active query's $.xmlActive=0; @@ -17,25 +16,11 @@ $.xml = function( type, url, data, ret ) { var xml = new XMLHttpRequest(); if ( xml ) { - // - // Increase the query counter - $.xmlActive++; - - // - // Show loader if needed - if ($.xmlCreate) { - $.xmlCreate(); - } - - // // Open the socket xml.open(type || "GET", url, true); - - if ( data ) { + if ( data ) xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - } - // // Set header so calling script knows that it's an XMLHttpRequest xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); @@ -43,29 +28,38 @@ $.xml = function( type, url, data, ret ) { * a bug where XMLHttpReqeuest sends an incorrect Content-length * header. See Mozilla Bugzilla #246651. */ - if ( xml.overrideMimeType ) { + if ( xml.overrideMimeType ) xml.setRequestHeader('Connection', 'close'); - } xml.onreadystatechange = function() { - if ( xml.readyState == 4 ) { - if ( ret ) { ret(xml); } + // Socket is openend + if ( xml.readyState == 1 ) { + // Increase counter + $.xmlActive++; + + // Show loader if needed + if ( ($.xmlActive >= 1) && ($.xmlCreate) ) + $.xmlCreate(); + } - // + // Socket is closed and data is available + if ( xml.readyState == 4 ) { // Decrease counter $.xmlActive--; - // // Hide loader if needed - if ($.xmlActive <= 0) { - if ($.xmlDestroy) { - $.xmlDestroy(); - } + if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) { + $.xmlDestroy(); + $.xmlActive = 0 } + + // Process result + if ( ret ) + ret(xml); } }; - xml.send(data); + xml.send(data) } }; @@ -121,60 +115,209 @@ $.fn.load = function(a,o,f) { f = o; o = null; } - if (o !== null) { + if (typeof o !== 'undefined') { o = $.param(o); t = "POST"; } var self = this; - $.xml(t,a,o,function(h){ - h = h.responseText; - self.html(h).find("script").each(function(){ - try { - $.eval( this.text || this.textContent || this.innerHTML ); - } catch(e){} + $.xml(t,a,o,function(res){ + // Assign it and execute all scripts + self.html(res.responseText).find("script").each(function(){ + try { eval( this.text || this.textContent || this.innerHTML || ""); } catch(e){} }); - if(f){f(h);} + + // Callback function + if (f && f.constructor == Function) + f(res.responseText); }); return this; }; /** - * function: $.fn.formValues - * usage: $('#frmLogin').formValues() - * docs: Gets the form values and creates a key=>value array of the found values (only for ENABLED elements!) + * Initial frontend function to submit form variables. This function + * is for registering coordinates, in the case of an image being used + * as the submit element, and sets up an event to listen and wait for + * a form submit click. It then calls any following chained functions + * to actually gather the variables and submit them. + * + * Usage examples, when used with getForm().putForm(): + * + * 1. Just eval the results returned from the backend. + * $('#form-id').form(); + * + * 2. Render backend results directly to target ID (expects (x)HTML). + * $('#form-id').form('#target-id'); + * + * 3. Submit to backend URL (form action) then call this function. + * $('#form-id').form(post_callback); + * + * 4. Load target ID with backend results then call a function. + * $('#form-id').form('#target-id', null, post_callback); + * + * 5. Call a browser function (for validation) and then (optionally) + * load server results to target ID. + * $('#form-id').form('#target-id', pre_callback); + * + * 6. Call validation function first then load server results to + * target ID and then also call a browser function. + * $('#form-id').form('#target-id', pre_callback, post_callback); + * + * @param target arg for the target id element to render + * @param pre_cb callback function before submission + * @param post_cb callback after any results are returned + * @return "this" object + * @see getForm(), putForm() + * @author Mark Constable (markc@renta.net) + * @author G. vd Hoven, Mike Alsup, Sam Collett + * @version 20060606 */ -$.fn.formValues = function() { - var a = []; - this.find("input[@type='submit'],input[@type='hidden'],textarea,input[@checked],input[@type='password'],input[@type='text'],option[@selected]").filter(":enabled").each(function() { - var o = {}; - o.name = this.name || this.id || this.parentNode.name || this.parentNode.id; - o.value = this.value; - a.push(o); - }); - return a; +$.fn.form = function(target, pre_cb, post_cb) { + $('input[@type="submit"],input[@type="image"]', this).click(function(ev){ + this.form.clicked = this; + if (ev.offsetX != undefined) { + this.form.clicked_x = ev.offsetX; + this.form.clicked_y = ev.offsetY; + } else { + this.form.clicked_x = ev.pageX - this.offsetLeft; + this.form.clicked_y = ev.pageY - this.offsetTop; + } + }); + this.submit(function(e){ + e.preventDefault(); + $(this).getForm().putForm(target, pre_cb, post_cb); + return this; + }); }; /** - * function: $.update - * usage: $.update('someJQueryObject', 'someurl', 'array'); - * docs: Mimics the ajaxUpdater from prototype. Posts the key=>value array to the url and - * puts the results from that call in the jQuery object specified. - * --> If you set the blnNoEval to true, the script tags are NOT evaluated. + * This function gathers form element variables into an array that + * is embedded into the current "this" variable as "this.vars". It + * is normally used in conjunction with form() and putForm() but can + * be used standalone as long as an image is not used for submission. + * + * Standalone usage examples: + * + * 1. Gather form vars and return array to LHS variable. + * var myform = $('#form-id').getForm(); + * + * 2. Provide a serialized URL-ready string (after 1. above). + * var mystring = $.param(myform.vars); + * + * 3. Gather form vars and send to RHS plugin via "this.vars". + * $('#form-id').getForm().some_other_plugin(); + * + * @return "this" object + * @see form(), putForm() + * @author Mark Constable (markc@renta.net) + * @author G. vd Hoven, Mike Alsup, Sam Collett + * @version 20060606 */ -$.update = function(objElement, strURL, arrValues, fncCallback) { - $.post(strURL, arrValues, function(strHTML) { - // - // Update the element with the new HTML - objElement.html(strHTML); - - // - // Evaluate the scripts - objElement.html(strHTML).find("script").each(function(){ - try { $.eval( this.text || this.textContent || this.innerHTML ); } catch(e){} - }); +$.fn.getForm = function() { + var a = []; + var ok = {INPUT:true, TEXTAREA:true, OPTION:true}; + $('*', this).each(function() { + if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked)) + return; + + if (this.type == 'submit' || this.type == 'image') { + if (this.form.clicked != this) + return; + + if (this.type == 'image') { + if (this.form.clicked_x) { + a.push({name: this.name+'_x', value: this.form.clicked_x}); + a.push({name: this.name+'_y', value: this.form.clicked_y}); + return; + } + } + } - // - // Callback handler - if (fncCallback) { fncCallback(); } + if (!ok[this.nodeName.toUpperCase()]) + return; + + var par = this.parentNode; + var p = par.nodeName.toUpperCase(); + if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected) + return; + + var n = this.name || par.name; + if (!n && p == 'OPTGROUP' && (par = par.parentNode)) + n = par.name; + + if (n == undefined) + return; + + a.push({name: n, value: this.value}); }); -}; + + this.vars = a; + + return this; +} + +/** + * Final form submission plugin usually used in conjunction with + * form() and getForm(). If a second argument is a valid function + * then it will be called before the form vars are sent to the + * backend. If this pre-submit function returns exactly "false" + * then it will abort further processing otherwise the process + * will continue according to the first and third arguments. + * + * If the first argument is a function, and it exists, then the form + * values will be submitted and that callback function called. If + * the first argument is a string value then the "load()" plugin + * will be called which will populate the innerHTML of the indicated + * element and a callback will be called if there is third argument. + * If there are no arguments then the form values are submitted with + * an additional variable (evaljs=1) which indicates to the backend + * to to prepare the returned results for evaluation, ie; the result + * needs to be valid javascript all on a single line. + * + * Usage example: + * + * $.fn.myvars = function() { + * this.vars = []; + * for (var i in this) { + * if (this[i] instanceof Function || this[i] == null) continue; + * this.vars.push({name: i, value: this[i].length}); + * } + * return this; + * } + * + * precb = function(vars) { + * return confirm('Submit these values?\n\n'+$.param(vars)); + * } + * + * $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php'); + * + * @param target arg for the target id element to render + * @param pre_cb callback function before submission + * @param post_cb callback after any results are returned + * @param url form action override + * @param mth form method override + * @return "this" object + * @see form(), getForm(), load(), xml() + * @author Mark Constable (markc@renta.net) + * @author G. vd Hoven, Mike Alsup, Sam Collett + * @version 20060606 + */ +$.fn.putForm = function(target, pre_cb, post_cb, url, mth) { + if (pre_cb && pre_cb.constructor == Function) + if (pre_cb(this.vars) === false) + return; + + var f = this.get(0); + var url = url || f.action || ''; + var mth = mth || f.method || 'POST'; + + if (target && target.constructor == Function) { + $.xml(mth, url, $.param(this.vars), target); + } else if (target && target.constructor == String) { + $(target).load(url, this.vars, post_cb); + } else { + this.vars.push({name: 'evaljs', value: 1}); + $.xml(mth, url, $.param(this.vars), function(r) { eval(r.responseText); }); + } + + return this; +}