Removed unnecessary instances of === or !==.
[jquery.git] / ajax / ajax.js
index d448ebb..87a90b3 100644 (file)
@@ -3,13 +3,12 @@
 // http://jquery.com/docs/ajax/
 
 if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
-       var XMLHttpRequest = function() {
+       XMLHttpRequest = function() {
                return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
                        "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
        };
 }
 
-//
 // Counter for holding the active query's
 $.xmlActive=0;
 
@@ -17,23 +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 )
                        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');
 
@@ -45,19 +32,30 @@ $.xml = function( type, url, data, ret ) {
                        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);
                        }
                };
 
@@ -72,7 +70,7 @@ $.httpData = function(r,type) {
 
 $.get = function( url, ret, type ) {
        $.xml( "GET", url, null, function(r) {
-               if ( ret ) ret( $.httpData(r,type) );
+               if ( ret ) { ret( $.httpData(r,type) ); }
        });
 };
 
@@ -82,7 +80,7 @@ $.getXML = function( url, ret ) {
 
 $.post = function( url, data, ret, type ) {
        $.xml( "POST", url, $.param(data), function(r) {
-               if ( ret ) ret( $.httpData(r,type) );
+               if ( ret ) { ret( $.httpData(r,type) ); }
        });
 };
 
@@ -90,30 +88,16 @@ $.postXML = function( url, data, ret ) {
        $.post( url, data, ret, "xml" );
 };
 
-// Dynamic Form Submission
-// Based upon the mailing list post at:
-// http://jquery.com/discuss/2006-March/000424/
-
-$.fn.serialize = function(callback) {
-       return this.each(function(){
-               var a = {};
-               $(this)
-                       .find("input:checked,hidden,text,option[@selected],textarea")
-                       .filter(":enabled").each(function() {
-                               a[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
-                       });
-               $.xml( this.method || "GET", this.action || "", $.param(a), callback );
-       });
-};
-
 $.param = function(a) {
        var s = [];
        if (a && typeof a == 'object' && a.constructor == Array) {
-               for ( var i=0; i < a.length; i++ )
-                       s[s.length] = a[i]['name'] + "=" + encodeURIComponent( a[i]['value'] );
+               for ( var i=0; i < a.length; i++ ) {
+                       s[s.length] = a[i].name + "=" + encodeURIComponent( a[i].value );
+               }
        } else {
-               for ( var i in a )
-                       s[s.length] = i + "=" + encodeURIComponent( a[i] );
+               for ( var j in a ) {
+                       s[s.length] = j + "=" + encodeURIComponent( a[j] );
+               }
        }
        return s.join("&");
 };
@@ -122,69 +106,143 @@ $.fn.load = function(a,o,f) {
        // Arrrrghhhhhhhh!!
        // I overwrote the event plugin's .load
        // this won't happen again, I hope -John
-       if ( a && a.constructor == Function )
+       if ( a && a.constructor == Function ) {
                return this.bind("load", a);
+       }
 
        var t = "GET";
        if ( o && o.constructor == Function ) {
                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){
-               var h = h.responseText;
+               h = h.responseText;
                self.html(h).find("script").each(function(){
                        try {
-                               eval( this.text || this.textContent || this.innerHTML );
+                               $.eval( this.text || this.textContent || this.innerHTML || "");
                        } catch(e){}
                });
-               if(f)f(h);
+               if(f){f(h);}
        });
        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!)
+ * name:       $.fn.formValues
+ * example:    $('#frmLogin').formValues('sButton')
+ * docs:       Gets form values and creates a key=>value array of the found values.
+ *             Optionally adds the button which is clicked if you provide it.
+ *             Only does this for ENABLED elements in the order of the form.
  */
-$.fn.formValues = function() {
-       var a = new Array();
-       this.find("input[@type='submit'],input[@type='hidden'],textarea,input[@checked],input[@type='password'],input[@type='text'],option[@selected]")
-               .filter(":enabled").each(function() {
-                       o = {};
-                       o['name'] = this.name || this.id || this.parentNode.name || this.parentNode.id;
-                       o['value'] = this.value;
-                       a.push(o);
+$.fn.formValues = function(sButton) {
+       var a = [];
+       var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
+
+       // Loop the shite
+       $('*', this).each(function() {
+               // Skip elements not of the types in ok
+               if (!ok[this.tagName.toUpperCase()])
+                       return;
+
+               // Skip disabled elements
+               if (this.disabled)
+                       return;
+
+               // Skip submit buttons and image elements
+               if ((this.type == 'submit') || (this.type == 'image'))
+                       return;
+
+               // Skip non-selected options
+               var oParent = this.parentNode;
+               var sNn = oParent.nodeName.toUpperCase();
+               if (((sNn == 'SELECT') || (sNn == 'OPTGROUP')) && (!this.selected))
+                       return;
+
+               // Skip non-checked nodes
+               if (((this.type == 'radio') || (this.type == 'checkbox')) && (!this.checked))
+                       return;
+
+               // If we come here, everything is fine
+               var sKey = this.name || this.id || oParent.name || oParent.id;
+               var sValue = this.value;
+
+               // If we don't have an ID, and the parent is an OPTGROUP,
+               // get the NAME or ID of the OPTGROUP's parent
+               if ((!sKey) && (sNn == 'OPTGROUP') && (oParent = oParent.parentNode))
+                       sKey = oParent.name || oParent.id;
+
+               // Add the data
+               a.push({ name: sKey, value: sValue });
+       });
+
+       // Add submit button if needed
+       if (sButton && (sButton !== null)) {
+               var el = $(sButton).get(0);
+               a.push({
+                       name: el.name || el.id || el.parentNode.name || el.parentNode.id,
+                       value: el.value
                });
+       }
+
+       // Done
        return a;
 };
 
 /**
- * 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.
+ * name:       $.fn.update
+ * example:    $('someJQueryObject').update('sURL', 'sAction', 'aValues', 'fCallback');
+ * docs:       Calls sURL with sAction and sends the aValues
+ *                 Puts the results from that call in the jQuery object and calls fCallback
  */
-$.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.update = function(sURL, sMethod, aValues, fCallback) {
+       var el = this;
+
+       // Process
+       $.xml(
+               sMethod || "GET",
+               sURL || "",
+               $.param(aValues),
+               function(sResult) {
+                       sResult = $.httpData(sResult);
+
+                       // Update the element with the new HTML
+                       el.html(sResult);
+
+                       // Evaluate the scripts AFTER this (so you can allready modify the new HTML!)
+                       el.html(sResult).find("script").each(function(){
+                               try { $.eval( this.text || this.textContent || this.innerHTML || "" ); } catch(e) { }
+                       });
 
-               //
-               // Callback handler
-               if (fncCallback) fncCallback();
-       });
+                       // And call the callback handler :)
+                       if (fCallback && (fCallback.constructor == Function))
+                               fCallback();
+               }
+       );
+};
+
+/**
+ * name:                       $.fn.serialize
+ * example:    $('someJQueryObject').serialize('sButton', 'fCallback');
+ * docs:       Calls the form's action with the correct method and the serialized values.
+ *             Optionally adds the button which is clicked if you provide it.
+ *             When there are results, the fCallback function is called.
+ */
+$.fn.serialize = function(sButton, fCallback) {
+       var el = this.get(0);
+
+       // Process
+       $.xml(
+               el.method || "GET",
+               el.action || "",
+               $.param(this.formValues(sButton)),
+               function(sResult) {
+                       if (fCallback && (fCallback.constructor == Function))
+                               fCallback($.httpData(sResult));
+               }
+       );
 };