From 3659c63afeb1cb4ec5f16915f7e960f43a0496f6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 2 Mar 2012 13:42:15 +0100 Subject: [PATCH] MINOR Updated jquery.form.js plugin, and re-added it to standard includes (its handy for clearing out forms, and consistently getting form field values) --- admin/code/LeftAndMain.php | 1 + thirdparty/jquery-form/jquery.form.js | 508 ++++++++++++++++++-------- 2 files changed, 358 insertions(+), 151 deletions(-) diff --git a/admin/code/LeftAndMain.php b/admin/code/LeftAndMain.php index afc1718eb..2727c0e2c 100644 --- a/admin/code/LeftAndMain.php +++ b/admin/code/LeftAndMain.php @@ -239,6 +239,7 @@ class LeftAndMain extends Controller { THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js', THIRDPARTY_DIR . '/jquery-cookie/jquery.cookie.js', THIRDPARTY_DIR . '/jquery-query/jquery.query.js', + THIRDPARTY_DIR . '/jquery-form/jquery.form.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jquery-notice/jquery.notice.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jsizes/lib/jquery.sizes.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jlayout/lib/jlayout.border.js', diff --git a/thirdparty/jquery-form/jquery.form.js b/thirdparty/jquery-form/jquery.form.js index 14e14572a..5f4c60ee2 100644 --- a/thirdparty/jquery-form/jquery.form.js +++ b/thirdparty/jquery-form/jquery.form.js @@ -1,12 +1,12 @@ /*! * jQuery Form Plugin - * version: 2.63 (29-JAN-2011) + * version: 2.96 (16-FEB-2012) * @requires jQuery v1.3.2 or later * * Examples and documentation at: http://malsup.com/jquery/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://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html */ ;(function($) { @@ -14,7 +14,7 @@ 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() { @@ -34,7 +34,15 @@ target: '#output' }); }); + + 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. */ @@ -49,22 +57,26 @@ $.fn.ajaxSubmit = function(options) { log('ajaxSubmit: skipping submit process - no element selected'); return this; } + + var method, action, url, $form = this; if (typeof options == 'function') { options = { success: options }; } - var action = this.attr('action'); - var url = (typeof action === 'string') ? $.trim(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 || ''; options = $.extend(true, { url: url, - type: this[0].getAttribute('method') || 'GET', // IE7 massage (see issue 57) + success: $.ajaxSettings.success, + type: method || 'GET', iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' }, options); @@ -83,21 +95,15 @@ $.fn.ajaxSubmit = function(options) { return this; } - var n,v,a = this.formToArray(options.semantic); + var traditional = options.traditional; + if ( traditional === undefined ) { + traditional = $.ajaxSettings.traditional; + } + + var qx,n,v,a = this.formToArray(options.semantic); if (options.data) { options.extraData = options.data; - for (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 { - v = options.data[n]; - v = $.isFunction(v) ? v() : v; // if value is fn, invoke it - a.push( { name: n, value: v } ); - } - } + qx = $.param(options.data, traditional); } // give pre-submit callback an opportunity to abort the submit @@ -113,8 +119,10 @@ $.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' @@ -123,12 +131,12 @@ $.fn.ajaxSubmit = function(options) { options.data = q; // data is the query string for 'post' } - var $form = this, callbacks = []; + var callbacks = []; if (options.resetForm) { callbacks.push(function() { $form.resetForm(); }); } if (options.clearForm) { - callbacks.push(function() { $form.clearForm(); }); + callbacks.push(function() { $form.clearForm(options.includeHidden); }); } // perform a load on the target only if dataType is not provided @@ -144,41 +152,116 @@ $.fn.ajaxSubmit = function(options) { } options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg - var context = options.context || options; // jQuery 1.4+ supports scope context + var context = options.context || options; // 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 fileInputs = $('input:file', this).length > 0; + var fileInputs = $('input:file:enabled[value]', this); // [value] (issue #113) + var hasFileInputs = fileInputs.length > 0; var mp = 'multipart/form-data'; var multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); + var fileAPI = !!(hasFileInputs && fileInputs.get(0).files && window.FormData); + log("fileAPI :" + fileAPI); + var shouldUseFrame = (hasFileInputs || multipart) && !fileAPI; + // options.iframe allows user to force iframe mode // 06-NOV-09: now defaulting to iframe mode if file input is detected - if (options.iframe !== false && (fileInputs || options.iframe || 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); + 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() { + fileUploadIframe(a); + }); } - else { - fileUpload(); - } - } - else { + else { + fileUploadIframe(a); + } + } + else if ((hasFileInputs || multipart) && fileAPI) { + options.progress = options.progress || $.noop; + fileUploadXhr(a); + } + else { $.ajax(options); + } + + // fire 'notify' event + this.trigger('form-submit-notify', [this, options]); + return this; + + // XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz) + function fileUploadXhr(a) { + var formdata = new FormData(); + + for (var i=0; i < a.length; i++) { + if (a[i].type == 'file') + continue; + formdata.append(a[i].name, a[i].value); + } + + $form.find('input:file:enabled').each(function(){ + var name = $(this).attr('name'), files = this.files; + if (name) { + for (var i=0; i < files.length; i++) + formdata.append(name, files[i]); + } + }); + + if (options.extraData) { + for (var k in options.extraData) + formdata.append(k, options.extraData[k]) + } + + options.data = null; + + var s = $.extend(true, {}, $.ajaxSettings, options, { + contentType: false, + processData: false, + cache: false, + type: 'POST' + }); + + //s.context = s.context || s; + + s.data = null; + var beforeSend = s.beforeSend; + s.beforeSend = function(xhr, o) { + o.data = formdata; + if(xhr.upload) { // unfortunately, jQuery doesn't expose this prop (http://bugs.jquery.com/ticket/10190) + xhr.upload.onprogress = function(event) { + o.progress(event.position, event.total); + }; + } + if(beforeSend) + beforeSend.call(o, xhr, options); + }; + $.ajax(s); } - // fire 'notify' event - this.trigger('form-submit-notify', [this, options]); - return this; - - // private function for handling file uploads (hat tip to YAHOO!) - function fileUpload() { - var form = $form[0]; + function fileUploadIframe(a) { + var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle; + var useProp = !!$.fn.prop; + + if (a) { + if ( useProp ) { + // ensure that every serialized input is still enabled + for (i=0; i < a.length; i++) { + el = $(form[a[i].name]); + el.prop('disabled', false); + } + } else { + for (i=0; i < a.length; i++) { + el = $(form[a[i].name]); + el.removeAttr('disabled'); + } + }; + } if ($(':input[name=submit],:input[id=submit]', form).length) { // if there is an input with a name or id of 'submit' then we won't be @@ -187,15 +270,25 @@ $.fn.ajaxSubmit = function(options) { return; } - var s = $.extend(true, {}, $.ajaxSettings, options); + s = $.extend(true, {}, $.ajaxSettings, options); s.context = s.context || s; - var id = 'jqFormIO' + (new Date().getTime()), fn = '_'+id; - var $io = $('