From 0494244e8068198707bf602199413cd216b0d515 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 3 Feb 2013 18:48:30 -0500 Subject: Super first pass: - jQuery 1.90 - jQuery UI 1.10 - Superfish 1.5.1 (minus all plugins) - jQuery Form 3.26.0-2013.01.28 Deleted all other jQuery plugins for now. - Reworked autocomplete to use the latest jQuery code. - Deleted references to $.browser.msie, no longer supported - Basic CSS support for autocomplete - lots more work needed there --- lib/jquery.form.js | 932 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 707 insertions(+), 225 deletions(-) (limited to 'lib/jquery.form.js') diff --git a/lib/jquery.form.js b/lib/jquery.form.js index 40fb8b1f..64a91606 100644 --- a/lib/jquery.form.js +++ b/lib/jquery.form.js @@ -1,28 +1,31 @@ -/* +/*! * jQuery Form Plugin - * version: 2.28 (10-MAY-2009) - * @requires jQuery v1.2.2 or later + * version: 3.26.0-2013.01.28 + * @requires jQuery v1.5 or later * * Examples and documentation at: http://malsup.com/jquery/form/ + * Project repository: https://github.com/malsup/form * Dual licensed under the MIT and GPL licenses: - * http://www.opensource.org/licenses/mit-license.php - * http://www.gnu.org/licenses/gpl.html + * http://malsup.github.com/mit-license.txt + * http://malsup.github.com/gpl-license-v2.txt */ +/*global ActiveXObject alert */ ;(function($) { +"use strict"; /* Usage Note: ----------- Do not use both ajaxSubmit and ajaxForm on the same form. These - functions are intended to be exclusive. Use ajaxSubmit if you want + functions are mutually exclusive. Use ajaxSubmit if you want to bind your own submit handler to the form. For example, $(document).ready(function() { - $('#myForm').bind('submit', function() { + $('#myForm').on('submit', function(e) { + e.preventDefault(); // <-- important $(this).ajaxSubmit({ target: '#output' }); - return false; // <-- important! }); }); @@ -35,35 +38,59 @@ }); }); + You can also use ajaxForm with delegation (requires jQuery v1.7+), so the + form does not have to exist when you invoke ajaxForm: + + $('#myForm').ajaxForm({ + delegation: true, + target: '#output' + }); + When using ajaxForm, the ajaxSubmit function will be invoked for you at the appropriate time. */ +/** + * Feature detection + */ +var feature = {}; +feature.fileapi = $("").get(0).files !== undefined; +feature.formdata = window.FormData !== undefined; + /** * ajaxSubmit() provides a mechanism for immediately submitting * an HTML form using AJAX. */ $.fn.ajaxSubmit = function(options) { + /*jshint scripturl:true */ + // fast fail if nothing selected (http://dev.jquery.com/ticket/2752) if (!this.length) { log('ajaxSubmit: skipping submit process - no element selected'); return this; } - if (typeof options == 'function') + var method, action, url, $form = this; + + if (typeof options == 'function') { options = { success: options }; + } - var url = $.trim(this.attr('action')); + method = this.attr('method'); + action = this.attr('action'); + url = (typeof action === 'string') ? $.trim(action) : ''; + url = url || window.location.href || ''; if (url) { - // clean url (don't include hash vaue) - url = (url.match(/^([^#]+)/)||[])[1]; - } - url = url || window.location.href || '' + // clean url (don't include hash vaue) + url = (url.match(/^([^#]+)/)||[])[1]; + } - options = $.extend({ + options = $.extend(true, { url: url, - type: this.attr('method') || 'GET' - }, options || {}); + success: $.ajaxSettings.success, + type: method || 'GET', + iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' + }, options); // hook for manipulating the form data before it is extracted; // convenient for use with rich editors like tinyMCE or FCKEditor @@ -80,17 +107,16 @@ $.fn.ajaxSubmit = function(options) { return this; } - var a = this.formToArray(options.semantic); + var traditional = options.traditional; + if ( traditional === undefined ) { + traditional = $.ajaxSettings.traditional; + } + + var elements = []; + var qx, a = this.formToArray(options.semantic, elements); if (options.data) { options.extraData = options.data; - for (var n in options.data) { - if(options.data[n] instanceof Array) { - for (var k in options.data[n]) - a.push( { name: n, value: options.data[n][k] } ); - } - else - a.push( { name: n, value: options.data[n] } ); - } + qx = $.param(options.data, traditional); } // give pre-submit callback an opportunity to abort the submit @@ -106,83 +132,197 @@ $.fn.ajaxSubmit = function(options) { return this; } - var q = $.param(a); - + var q = $.param(a, traditional); + if (qx) { + q = ( q ? (q + '&' + qx) : qx ); + } if (options.type.toUpperCase() == 'GET') { options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; options.data = null; // data is null for 'get' } - else + else { options.data = q; // data is the query string for 'post' + } - var $form = this, callbacks = []; - if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); - if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); + var callbacks = []; + if (options.resetForm) { + callbacks.push(function() { $form.resetForm(); }); + } + if (options.clearForm) { + callbacks.push(function() { $form.clearForm(options.includeHidden); }); + } // perform a load on the target only if dataType is not provided if (!options.dataType && options.target) { var oldSuccess = options.success || function(){}; callbacks.push(function(data) { - $(options.target).html(data).each(oldSuccess, arguments); + var fn = options.replaceTarget ? 'replaceWith' : 'html'; + $(options.target)[fn](data).each(oldSuccess, arguments); }); } - else if (options.success) + else if (options.success) { callbacks.push(options.success); + } - options.success = function(data, status) { - for (var i=0, max=callbacks.length; i < max; i++) - callbacks[i].apply(options, [data, status, $form]); + options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg + var context = options.context || this ; // jQuery 1.4+ supports scope context + for (var i=0, max=callbacks.length; i < max; i++) { + callbacks[i].apply(context, [data, status, xhr || $form, $form]); + } }; // are there files to upload? - var files = $('input:file', this).fieldValue(); - var found = false; - for (var j=0; j < files.length; j++) - if (files[j]) - found = true; - var multipart = false; -// var mp = 'multipart/form-data'; -// multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); + // [value] (issue #113), also see comment: + // https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219 + var fileInputs = $('input[type=file]:enabled[value!=""]', this); + + var hasFileInputs = fileInputs.length > 0; + var mp = 'multipart/form-data'; + var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); + + var fileAPI = feature.fileapi && feature.formdata; + log("fileAPI :" + fileAPI); + var shouldUseFrame = (hasFileInputs || multipart) && !fileAPI; + + var jqxhr; // options.iframe allows user to force iframe mode - if (options.iframe || found || multipart) { - // hack to fix Safari hang (thanks to Tim Molendijk for this) - // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d - if (options.closeKeepAlive) - $.get(options.closeKeepAlive, fileUpload); - else - fileUpload(); - } - else - $.ajax(options); + // 06-NOV-09: now defaulting to iframe mode if file input is detected + if (options.iframe !== false && (options.iframe || shouldUseFrame)) { + // hack to fix Safari hang (thanks to Tim Molendijk for this) + // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d + if (options.closeKeepAlive) { + $.get(options.closeKeepAlive, function() { + jqxhr = fileUploadIframe(a); + }); + } + else { + jqxhr = fileUploadIframe(a); + } + } + else if ((hasFileInputs || multipart) && fileAPI) { + jqxhr = fileUploadXhr(a); + } + else { + jqxhr = $.ajax(options); + } + + $form.removeData('jqxhr').data('jqxhr', jqxhr); + + // clear element array + for (var k=0; k < elements.length; k++) + elements[k] = null; // fire 'notify' event this.trigger('form-submit-notify', [this, options]); return this; + // utility fn for deep serialization + function deepSerialize(extraData){ + var serialized = $.param(extraData).split('&'); + var len = serialized.length; + var result = []; + var i, part; + for (i=0; i < len; i++) { + // #252; undo param space replacement + serialized[i] = serialized[i].replace(/\+/g,' '); + part = serialized[i].split('='); + // #278; use array instead of object storage, favoring array serializations + result.push([decodeURIComponent(part[0]), decodeURIComponent(part[1])]); + } + return result; + } - // private function for handling file uploads (hat tip to YAHOO!) - function fileUpload() { - var form = $form[0]; + // XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz) + function fileUploadXhr(a) { + var formdata = new FormData(); - /* (this breaks the watermark form uploader, turn it off for now) - if ($(':input[name=submit]', form).length) { - alert('Error: Form elements must not be named "submit".'); - return; + for (var i=0; i < a.length; i++) { + formdata.append(a[i].name, a[i].value); } - */ - var opts = $.extend({}, $.ajaxSettings, options); - var s = $.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); + if (options.extraData) { + var serializedData = deepSerialize(options.extraData); + for (i=0; i < serializedData.length; i++) + if (serializedData[i]) + formdata.append(serializedData[i][0], serializedData[i][1]); + } + + options.data = null; - var id = 'jqFormIO' + (new Date().getTime()); - var $io = $('