Dotclear

source: admin/js/jsUpload/jquery.fileupload.js @ 1320:e3b4f3b9169a

Revision 1320:e3b4f3b9169a, 56.0 KB checked in by Nicolas <nikrou77@…>, 12 years ago (diff)

Revert jquery.fileupload files and upgrade them to latest state.
Start button is allways shown but disabled if any file is in the list to be send.

Line 
1/*
2 * jQuery File Upload Plugin 5.32.2
3 * https://github.com/blueimp/jQuery-File-Upload
4 *
5 * Copyright 2010, Sebastian Tschan
6 * https://blueimp.net
7 *
8 * Licensed under the MIT license:
9 * http://www.opensource.org/licenses/MIT
10 */
11
12/*jslint nomen: true, unparam: true, regexp: true */
13/*global define, window, document, location, File, Blob, FormData */
14
15(function (factory) {
16    'use strict';
17    if (typeof define === 'function' && define.amd) {
18        // Register as an anonymous AMD module:
19        define([
20            'jquery',
21            'jquery.ui.widget'
22        ], factory);
23    } else {
24        // Browser globals:
25        factory(window.jQuery);
26    }
27}(function ($) {
28    'use strict';
29
30    // Detect file input support, based on
31    // http://viljamis.com/blog/2012/file-upload-support-on-mobile/
32    $.support.fileInput = !(new RegExp(
33        // Handle devices which give false positives for the feature detection:
34        '(Android (1\\.[0156]|2\\.[01]))' +
35            '|(Windows Phone (OS 7|8\\.0))|(XBLWP)|(ZuneWP)|(WPDesktop)' +
36            '|(w(eb)?OSBrowser)|(webOS)' +
37            '|(Kindle/(1\\.0|2\\.[05]|3\\.0))'
38    ).test(window.navigator.userAgent) ||
39        // Feature detection for all other devices:
40        $('<input type="file">').prop('disabled'));
41
42    // The FileReader API is not actually used, but works as feature detection,
43    // as e.g. Safari supports XHR file uploads via the FormData API,
44    // but not non-multipart XHR file uploads:
45    $.support.xhrFileUpload = !!(window.XMLHttpRequestUpload && window.FileReader);
46    $.support.xhrFormDataFileUpload = !!window.FormData;
47
48    // Detect support for Blob slicing (required for chunked uploads):
49    $.support.blobSlice = window.Blob && (Blob.prototype.slice ||
50        Blob.prototype.webkitSlice || Blob.prototype.mozSlice);
51
52    // The fileupload widget listens for change events on file input fields defined
53    // via fileInput setting and paste or drop events of the given dropZone.
54    // In addition to the default jQuery Widget methods, the fileupload widget
55    // exposes the "add" and "send" methods, to add or directly send files using
56    // the fileupload API.
57    // By default, files added via file input selection, paste, drag & drop or
58    // "add" method are uploaded immediately, but it is possible to override
59    // the "add" callback option to queue file uploads.
60    $.widget('blueimp.fileupload', {
61
62        options: {
63            // The drop target element(s), by the default the complete document.
64            // Set to null to disable drag & drop support:
65            dropZone: $(document),
66            // The paste target element(s), by the default the complete document.
67            // Set to null to disable paste support:
68            pasteZone: $(document),
69            // The file input field(s), that are listened to for change events.
70            // If undefined, it is set to the file input fields inside
71            // of the widget element on plugin initialization.
72            // Set to null to disable the change listener.
73            fileInput: undefined,
74            // By default, the file input field is replaced with a clone after
75            // each input field change event. This is required for iframe transport
76            // queues and allows change events to be fired for the same file
77            // selection, but can be disabled by setting the following option to false:
78            replaceFileInput: true,
79            // The parameter name for the file form data (the request argument name).
80            // If undefined or empty, the name property of the file input field is
81            // used, or "files[]" if the file input name property is also empty,
82            // can be a string or an array of strings:
83            paramName: undefined,
84            // By default, each file of a selection is uploaded using an individual
85            // request for XHR type uploads. Set to false to upload file
86            // selections in one request each:
87            singleFileUploads: true,
88            // To limit the number of files uploaded with one XHR request,
89            // set the following option to an integer greater than 0:
90            limitMultiFileUploads: undefined,
91            // Set the following option to true to issue all file upload requests
92            // in a sequential order:
93            sequentialUploads: false,
94            // To limit the number of concurrent uploads,
95            // set the following option to an integer greater than 0:
96            limitConcurrentUploads: undefined,
97            // Set the following option to true to force iframe transport uploads:
98            forceIframeTransport: false,
99            // Set the following option to the location of a redirect url on the
100            // origin server, for cross-domain iframe transport uploads:
101            redirect: undefined,
102            // The parameter name for the redirect url, sent as part of the form
103            // data and set to 'redirect' if this option is empty:
104            redirectParamName: undefined,
105            // Set the following option to the location of a postMessage window,
106            // to enable postMessage transport uploads:
107            postMessage: undefined,
108            // By default, XHR file uploads are sent as multipart/form-data.
109            // The iframe transport is always using multipart/form-data.
110            // Set to false to enable non-multipart XHR uploads:
111            multipart: true,
112            // To upload large files in smaller chunks, set the following option
113            // to a preferred maximum chunk size. If set to 0, null or undefined,
114            // or the browser does not support the required Blob API, files will
115            // be uploaded as a whole.
116            maxChunkSize: undefined,
117            // When a non-multipart upload or a chunked multipart upload has been
118            // aborted, this option can be used to resume the upload by setting
119            // it to the size of the already uploaded bytes. This option is most
120            // useful when modifying the options object inside of the "add" or
121            // "send" callbacks, as the options are cloned for each file upload.
122            uploadedBytes: undefined,
123            // By default, failed (abort or error) file uploads are removed from the
124            // global progress calculation. Set the following option to false to
125            // prevent recalculating the global progress data:
126            recalculateProgress: true,
127            // Interval in milliseconds to calculate and trigger progress events:
128            progressInterval: 100,
129            // Interval in milliseconds to calculate progress bitrate:
130            bitrateInterval: 500,
131            // By default, uploads are started automatically when adding files:
132            autoUpload: true,
133
134            // Error and info messages:
135            messages: {
136                uploadedBytes: 'Uploaded bytes exceed file size'
137            },
138
139            // Translation function, gets the message key to be translated
140            // and an object with context specific data as arguments:
141            i18n: function (message, context) {
142                message = this.messages[message] || message.toString();
143                if (context) {
144                    $.each(context, function (key, value) {
145                        message = message.replace('{' + key + '}', value);
146                    });
147                }
148                return message;
149            },
150
151            // Additional form data to be sent along with the file uploads can be set
152            // using this option, which accepts an array of objects with name and
153            // value properties, a function returning such an array, a FormData
154            // object (for XHR file uploads), or a simple object.
155            // The form of the first fileInput is given as parameter to the function:
156            formData: function (form) {
157                return form.serializeArray();
158            },
159
160            // The add callback is invoked as soon as files are added to the fileupload
161            // widget (via file input selection, drag & drop, paste or add API call).
162            // If the singleFileUploads option is enabled, this callback will be
163            // called once for each file in the selection for XHR file uploads, else
164            // once for each file selection.
165            //
166            // The upload starts when the submit method is invoked on the data parameter.
167            // The data object contains a files property holding the added files
168            // and allows you to override plugin options as well as define ajax settings.
169            //
170            // Listeners for this callback can also be bound the following way:
171            // .bind('fileuploadadd', func);
172            //
173            // data.submit() returns a Promise object and allows to attach additional
174            // handlers using jQuery's Deferred callbacks:
175            // data.submit().done(func).fail(func).always(func);
176            add: function (e, data) {
177                if (data.autoUpload || (data.autoUpload !== false &&
178                        $(this).fileupload('option', 'autoUpload'))) {
179                    data.process().done(function () {
180                        data.submit();
181                    });
182                }
183            },
184
185            // Other callbacks:
186
187            // Callback for the submit event of each file upload:
188            // submit: function (e, data) {}, // .bind('fileuploadsubmit', func);
189
190            // Callback for the start of each file upload request:
191            // send: function (e, data) {}, // .bind('fileuploadsend', func);
192
193            // Callback for successful uploads:
194            // done: function (e, data) {}, // .bind('fileuploaddone', func);
195
196            // Callback for failed (abort or error) uploads:
197            // fail: function (e, data) {}, // .bind('fileuploadfail', func);
198
199            // Callback for completed (success, abort or error) requests:
200            // always: function (e, data) {}, // .bind('fileuploadalways', func);
201
202            // Callback for upload progress events:
203            // progress: function (e, data) {}, // .bind('fileuploadprogress', func);
204
205            // Callback for global upload progress events:
206            // progressall: function (e, data) {}, // .bind('fileuploadprogressall', func);
207
208            // Callback for uploads start, equivalent to the global ajaxStart event:
209            // start: function (e) {}, // .bind('fileuploadstart', func);
210
211            // Callback for uploads stop, equivalent to the global ajaxStop event:
212            // stop: function (e) {}, // .bind('fileuploadstop', func);
213
214            // Callback for change events of the fileInput(s):
215            // change: function (e, data) {}, // .bind('fileuploadchange', func);
216
217            // Callback for paste events to the pasteZone(s):
218            // paste: function (e, data) {}, // .bind('fileuploadpaste', func);
219
220            // Callback for drop events of the dropZone(s):
221            // drop: function (e, data) {}, // .bind('fileuploaddrop', func);
222
223            // Callback for dragover events of the dropZone(s):
224            // dragover: function (e) {}, // .bind('fileuploaddragover', func);
225
226            // Callback for the start of each chunk upload request:
227            // chunksend: function (e, data) {}, // .bind('fileuploadchunksend', func);
228
229            // Callback for successful chunk uploads:
230            // chunkdone: function (e, data) {}, // .bind('fileuploadchunkdone', func);
231
232            // Callback for failed (abort or error) chunk uploads:
233            // chunkfail: function (e, data) {}, // .bind('fileuploadchunkfail', func);
234
235            // Callback for completed (success, abort or error) chunk upload requests:
236            // chunkalways: function (e, data) {}, // .bind('fileuploadchunkalways', func);
237
238            // The plugin options are used as settings object for the ajax calls.
239            // The following are jQuery ajax settings required for the file uploads:
240            processData: false,
241            contentType: false,
242            cache: false
243        },
244
245        // A list of options that require reinitializing event listeners and/or
246        // special initialization code:
247        _specialOptions: [
248            'fileInput',
249            'dropZone',
250            'pasteZone',
251            'multipart',
252            'forceIframeTransport'
253        ],
254
255        _blobSlice: $.support.blobSlice && function () {
256            var slice = this.slice || this.webkitSlice || this.mozSlice;
257            return slice.apply(this, arguments);
258        },
259
260        _BitrateTimer: function () {
261            this.timestamp = ((Date.now) ? Date.now() : (new Date()).getTime());
262            this.loaded = 0;
263            this.bitrate = 0;
264            this.getBitrate = function (now, loaded, interval) {
265                var timeDiff = now - this.timestamp;
266                if (!this.bitrate || !interval || timeDiff > interval) {
267                    this.bitrate = (loaded - this.loaded) * (1000 / timeDiff) * 8;
268                    this.loaded = loaded;
269                    this.timestamp = now;
270                }
271                return this.bitrate;
272            };
273        },
274
275        _isXHRUpload: function (options) {
276            return !options.forceIframeTransport &&
277                ((!options.multipart && $.support.xhrFileUpload) ||
278                $.support.xhrFormDataFileUpload);
279        },
280
281        _getFormData: function (options) {
282            var formData;
283            if (typeof options.formData === 'function') {
284                return options.formData(options.form);
285            }
286            if ($.isArray(options.formData)) {
287                return options.formData;
288            }
289            if ($.type(options.formData) === 'object') {
290                formData = [];
291                $.each(options.formData, function (name, value) {
292                    formData.push({name: name, value: value});
293                });
294                return formData;
295            }
296            return [];
297        },
298
299        _getTotal: function (files) {
300            var total = 0;
301            $.each(files, function (index, file) {
302                total += file.size || 1;
303            });
304            return total;
305        },
306
307        _initProgressObject: function (obj) {
308            var progress = {
309                loaded: 0,
310                total: 0,
311                bitrate: 0
312            };
313            if (obj._progress) {
314                $.extend(obj._progress, progress);
315            } else {
316                obj._progress = progress;
317            }
318        },
319
320        _initResponseObject: function (obj) {
321            var prop;
322            if (obj._response) {
323                for (prop in obj._response) {
324                    if (obj._response.hasOwnProperty(prop)) {
325                        delete obj._response[prop];
326                    }
327                }
328            } else {
329                obj._response = {};
330            }
331        },
332
333        _onProgress: function (e, data) {
334            if (e.lengthComputable) {
335                var now = ((Date.now) ? Date.now() : (new Date()).getTime()),
336                    loaded;
337                if (data._time && data.progressInterval &&
338                        (now - data._time < data.progressInterval) &&
339                        e.loaded !== e.total) {
340                    return;
341                }
342                data._time = now;
343                loaded = Math.floor(
344                    e.loaded / e.total * (data.chunkSize || data._progress.total)
345                ) + (data.uploadedBytes || 0);
346                // Add the difference from the previously loaded state
347                // to the global loaded counter:
348                this._progress.loaded += (loaded - data._progress.loaded);
349                this._progress.bitrate = this._bitrateTimer.getBitrate(
350                    now,
351                    this._progress.loaded,
352                    data.bitrateInterval
353                );
354                data._progress.loaded = data.loaded = loaded;
355                data._progress.bitrate = data.bitrate = data._bitrateTimer.getBitrate(
356                    now,
357                    loaded,
358                    data.bitrateInterval
359                );
360                // Trigger a custom progress event with a total data property set
361                // to the file size(s) of the current upload and a loaded data
362                // property calculated accordingly:
363                this._trigger('progress', e, data);
364                // Trigger a global progress event for all current file uploads,
365                // including ajax calls queued for sequential file uploads:
366                this._trigger('progressall', e, this._progress);
367            }
368        },
369
370        _initProgressListener: function (options) {
371            var that = this,
372                xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr();
373            // Accesss to the native XHR object is required to add event listeners
374            // for the upload progress event:
375            if (xhr.upload) {
376                $(xhr.upload).bind('progress', function (e) {
377                    var oe = e.originalEvent;
378                    // Make sure the progress event properties get copied over:
379                    e.lengthComputable = oe.lengthComputable;
380                    e.loaded = oe.loaded;
381                    e.total = oe.total;
382                    that._onProgress(e, options);
383                });
384                options.xhr = function () {
385                    return xhr;
386                };
387            }
388        },
389
390        _isInstanceOf: function (type, obj) {
391            // Cross-frame instanceof check
392            return Object.prototype.toString.call(obj) === '[object ' + type + ']';
393        },
394
395        _initXHRData: function (options) {
396            var that = this,
397                formData,
398                file = options.files[0],
399                // Ignore non-multipart setting if not supported:
400                multipart = options.multipart || !$.support.xhrFileUpload,
401                paramName = options.paramName[0];
402            options.headers = options.headers || {};
403            if (options.contentRange) {
404                options.headers['Content-Range'] = options.contentRange;
405            }
406            if (!multipart || options.blob || !this._isInstanceOf('File', file)) {
407                options.headers['Content-Disposition'] = 'attachment; filename="' +
408                    encodeURI(file.name) + '"';
409            }
410            if (!multipart) {
411                options.contentType = file.type;
412                options.data = options.blob || file;
413            } else if ($.support.xhrFormDataFileUpload) {
414                if (options.postMessage) {
415                    // window.postMessage does not allow sending FormData
416                    // objects, so we just add the File/Blob objects to
417                    // the formData array and let the postMessage window
418                    // create the FormData object out of this array:
419                    formData = this._getFormData(options);
420                    if (options.blob) {
421                        formData.push({
422                            name: paramName,
423                            value: options.blob
424                        });
425                    } else {
426                        $.each(options.files, function (index, file) {
427                            formData.push({
428                                name: options.paramName[index] || paramName,
429                                value: file
430                            });
431                        });
432                    }
433                } else {
434                    if (that._isInstanceOf('FormData', options.formData)) {
435                        formData = options.formData;
436                    } else {
437                        formData = new FormData();
438                        $.each(this._getFormData(options), function (index, field) {
439                            formData.append(field.name, field.value);
440                        });
441                    }
442                    if (options.blob) {
443                        formData.append(paramName, options.blob, file.name);
444                    } else {
445                        $.each(options.files, function (index, file) {
446                            // This check allows the tests to run with
447                            // dummy objects:
448                            if (that._isInstanceOf('File', file) ||
449                                    that._isInstanceOf('Blob', file)) {
450                                formData.append(
451                                    options.paramName[index] || paramName,
452                                    file,
453                                    file.name
454                                );
455                            }
456                        });
457                    }
458                }
459                options.data = formData;
460            }
461            // Blob reference is not needed anymore, free memory:
462            options.blob = null;
463        },
464
465        _initIframeSettings: function (options) {
466            var targetHost = $('<a></a>').prop('href', options.url).prop('host');
467            // Setting the dataType to iframe enables the iframe transport:
468            options.dataType = 'iframe ' + (options.dataType || '');
469            // The iframe transport accepts a serialized array as form data:
470            options.formData = this._getFormData(options);
471            // Add redirect url to form data on cross-domain uploads:
472            if (options.redirect && targetHost && targetHost !== location.host) {
473                options.formData.push({
474                    name: options.redirectParamName || 'redirect',
475                    value: options.redirect
476                });
477            }
478        },
479
480        _initDataSettings: function (options) {
481            if (this._isXHRUpload(options)) {
482                if (!this._chunkedUpload(options, true)) {
483                    if (!options.data) {
484                        this._initXHRData(options);
485                    }
486                    this._initProgressListener(options);
487                }
488                if (options.postMessage) {
489                    // Setting the dataType to postmessage enables the
490                    // postMessage transport:
491                    options.dataType = 'postmessage ' + (options.dataType || '');
492                }
493            } else {
494                this._initIframeSettings(options);
495            }
496        },
497
498        _getParamName: function (options) {
499            var fileInput = $(options.fileInput),
500                paramName = options.paramName;
501            if (!paramName) {
502                paramName = [];
503                fileInput.each(function () {
504                    var input = $(this),
505                        name = input.prop('name') || 'files[]',
506                        i = (input.prop('files') || [1]).length;
507                    while (i) {
508                        paramName.push(name);
509                        i -= 1;
510                    }
511                });
512                if (!paramName.length) {
513                    paramName = [fileInput.prop('name') || 'files[]'];
514                }
515            } else if (!$.isArray(paramName)) {
516                paramName = [paramName];
517            }
518            return paramName;
519        },
520
521        _initFormSettings: function (options) {
522            // Retrieve missing options from the input field and the
523            // associated form, if available:
524            if (!options.form || !options.form.length) {
525                options.form = $(options.fileInput.prop('form'));
526                // If the given file input doesn't have an associated form,
527                // use the default widget file input's form:
528                if (!options.form.length) {
529                    options.form = $(this.options.fileInput.prop('form'));
530                }
531            }
532            options.paramName = this._getParamName(options);
533            if (!options.url) {
534                options.url = options.form.prop('action') || location.href;
535            }
536            // The HTTP request method must be "POST" or "PUT":
537            options.type = (options.type || options.form.prop('method') || '')
538                .toUpperCase();
539            if (options.type !== 'POST' && options.type !== 'PUT' &&
540                    options.type !== 'PATCH') {
541                options.type = 'POST';
542            }
543            if (!options.formAcceptCharset) {
544                options.formAcceptCharset = options.form.attr('accept-charset');
545            }
546        },
547
548        _getAJAXSettings: function (data) {
549            var options = $.extend({}, this.options, data);
550            this._initFormSettings(options);
551            this._initDataSettings(options);
552            return options;
553        },
554
555        // jQuery 1.6 doesn't provide .state(),
556        // while jQuery 1.8+ removed .isRejected() and .isResolved():
557        _getDeferredState: function (deferred) {
558            if (deferred.state) {
559                return deferred.state();
560            }
561            if (deferred.isResolved()) {
562                return 'resolved';
563            }
564            if (deferred.isRejected()) {
565                return 'rejected';
566            }
567            return 'pending';
568        },
569
570        // Maps jqXHR callbacks to the equivalent
571        // methods of the given Promise object:
572        _enhancePromise: function (promise) {
573            promise.success = promise.done;
574            promise.error = promise.fail;
575            promise.complete = promise.always;
576            return promise;
577        },
578
579        // Creates and returns a Promise object enhanced with
580        // the jqXHR methods abort, success, error and complete:
581        _getXHRPromise: function (resolveOrReject, context, args) {
582            var dfd = $.Deferred(),
583                promise = dfd.promise();
584            context = context || this.options.context || promise;
585            if (resolveOrReject === true) {
586                dfd.resolveWith(context, args);
587            } else if (resolveOrReject === false) {
588                dfd.rejectWith(context, args);
589            }
590            promise.abort = dfd.promise;
591            return this._enhancePromise(promise);
592        },
593
594        // Adds convenience methods to the data callback argument:
595        _addConvenienceMethods: function (e, data) {
596            var that = this,
597                getPromise = function (data) {
598                    return $.Deferred().resolveWith(that, [data]).promise();
599                };
600            data.process = function (resolveFunc, rejectFunc) {
601                if (resolveFunc || rejectFunc) {
602                    data._processQueue = this._processQueue =
603                        (this._processQueue || getPromise(this))
604                            .pipe(resolveFunc, rejectFunc);
605                }
606                return this._processQueue || getPromise(this);
607            };
608            data.submit = function () {
609                if (this.state() !== 'pending') {
610                    data.jqXHR = this.jqXHR =
611                        (that._trigger('submit', e, this) !== false) &&
612                        that._onSend(e, this);
613                }
614                return this.jqXHR || that._getXHRPromise();
615            };
616            data.abort = function () {
617                if (this.jqXHR) {
618                    return this.jqXHR.abort();
619                }
620                return that._getXHRPromise();
621            };
622            data.state = function () {
623                if (this.jqXHR) {
624                    return that._getDeferredState(this.jqXHR);
625                }
626                if (this._processQueue) {
627                    return that._getDeferredState(this._processQueue);
628                }
629            };
630            data.progress = function () {
631                return this._progress;
632            };
633            data.response = function () {
634                return this._response;
635            };
636        },
637
638        // Parses the Range header from the server response
639        // and returns the uploaded bytes:
640        _getUploadedBytes: function (jqXHR) {
641            var range = jqXHR.getResponseHeader('Range'),
642                parts = range && range.split('-'),
643                upperBytesPos = parts && parts.length > 1 &&
644                    parseInt(parts[1], 10);
645            return upperBytesPos && upperBytesPos + 1;
646        },
647
648        // Uploads a file in multiple, sequential requests
649        // by splitting the file up in multiple blob chunks.
650        // If the second parameter is true, only tests if the file
651        // should be uploaded in chunks, but does not invoke any
652        // upload requests:
653        _chunkedUpload: function (options, testOnly) {
654            options.uploadedBytes = options.uploadedBytes || 0;
655            var that = this,
656                file = options.files[0],
657                fs = file.size,
658                ub = options.uploadedBytes,
659                mcs = options.maxChunkSize || fs,
660                slice = this._blobSlice,
661                dfd = $.Deferred(),
662                promise = dfd.promise(),
663                jqXHR,
664                upload;
665            if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
666                    options.data) {
667                return false;
668            }
669            if (testOnly) {
670                return true;
671            }
672            if (ub >= fs) {
673                file.error = options.i18n('uploadedBytes');
674                return this._getXHRPromise(
675                    false,
676                    options.context,
677                    [null, 'error', file.error]
678                );
679            }
680            // The chunk upload method:
681            upload = function () {
682                // Clone the options object for each chunk upload:
683                var o = $.extend({}, options),
684                    currentLoaded = o._progress.loaded;
685                o.blob = slice.call(
686                    file,
687                    ub,
688                    ub + mcs,
689                    file.type
690                );
691                // Store the current chunk size, as the blob itself
692                // will be dereferenced after data processing:
693                o.chunkSize = o.blob.size;
694                // Expose the chunk bytes position range:
695                o.contentRange = 'bytes ' + ub + '-' +
696                    (ub + o.chunkSize - 1) + '/' + fs;
697                // Process the upload data (the blob and potential form data):
698                that._initXHRData(o);
699                // Add progress listeners for this chunk upload:
700                that._initProgressListener(o);
701                jqXHR = ((that._trigger('chunksend', null, o) !== false && $.ajax(o)) ||
702                        that._getXHRPromise(false, o.context))
703                    .done(function (result, textStatus, jqXHR) {
704                        ub = that._getUploadedBytes(jqXHR) ||
705                            (ub + o.chunkSize);
706                        // Create a progress event if no final progress event
707                        // with loaded equaling total has been triggered
708                        // for this chunk:
709                        if (currentLoaded + o.chunkSize - o._progress.loaded) {
710                            that._onProgress($.Event('progress', {
711                                lengthComputable: true,
712                                loaded: ub - o.uploadedBytes,
713                                total: ub - o.uploadedBytes
714                            }), o);
715                        }
716                        options.uploadedBytes = o.uploadedBytes = ub;
717                        o.result = result;
718                        o.textStatus = textStatus;
719                        o.jqXHR = jqXHR;
720                        that._trigger('chunkdone', null, o);
721                        that._trigger('chunkalways', null, o);
722                        if (ub < fs) {
723                            // File upload not yet complete,
724                            // continue with the next chunk:
725                            upload();
726                        } else {
727                            dfd.resolveWith(
728                                o.context,
729                                [result, textStatus, jqXHR]
730                            );
731                        }
732                    })
733                    .fail(function (jqXHR, textStatus, errorThrown) {
734                        o.jqXHR = jqXHR;
735                        o.textStatus = textStatus;
736                        o.errorThrown = errorThrown;
737                        that._trigger('chunkfail', null, o);
738                        that._trigger('chunkalways', null, o);
739                        dfd.rejectWith(
740                            o.context,
741                            [jqXHR, textStatus, errorThrown]
742                        );
743                    });
744            };
745            this._enhancePromise(promise);
746            promise.abort = function () {
747                return jqXHR.abort();
748            };
749            upload();
750            return promise;
751        },
752
753        _beforeSend: function (e, data) {
754            if (this._active === 0) {
755                // the start callback is triggered when an upload starts
756                // and no other uploads are currently running,
757                // equivalent to the global ajaxStart event:
758                this._trigger('start');
759                // Set timer for global bitrate progress calculation:
760                this._bitrateTimer = new this._BitrateTimer();
761                // Reset the global progress values:
762                this._progress.loaded = this._progress.total = 0;
763                this._progress.bitrate = 0;
764            }
765            // Make sure the container objects for the .response() and
766            // .progress() methods on the data object are available
767            // and reset to their initial state:
768            this._initResponseObject(data);
769            this._initProgressObject(data);
770            data._progress.loaded = data.loaded = data.uploadedBytes || 0;
771            data._progress.total = data.total = this._getTotal(data.files) || 1;
772            data._progress.bitrate = data.bitrate = 0;
773            this._active += 1;
774            // Initialize the global progress values:
775            this._progress.loaded += data.loaded;
776            this._progress.total += data.total;
777        },
778
779        _onDone: function (result, textStatus, jqXHR, options) {
780            var total = options._progress.total,
781                response = options._response;
782            if (options._progress.loaded < total) {
783                // Create a progress event if no final progress event
784                // with loaded equaling total has been triggered:
785                this._onProgress($.Event('progress', {
786                    lengthComputable: true,
787                    loaded: total,
788                    total: total
789                }), options);
790            }
791            response.result = options.result = result;
792            response.textStatus = options.textStatus = textStatus;
793            response.jqXHR = options.jqXHR = jqXHR;
794            this._trigger('done', null, options);
795        },
796
797        _onFail: function (jqXHR, textStatus, errorThrown, options) {
798            var response = options._response;
799            if (options.recalculateProgress) {
800                // Remove the failed (error or abort) file upload from
801                // the global progress calculation:
802                this._progress.loaded -= options._progress.loaded;
803                this._progress.total -= options._progress.total;
804            }
805            response.jqXHR = options.jqXHR = jqXHR;
806            response.textStatus = options.textStatus = textStatus;
807            response.errorThrown = options.errorThrown = errorThrown;
808            this._trigger('fail', null, options);
809        },
810
811        _onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) {
812            // jqXHRorResult, textStatus and jqXHRorError are added to the
813            // options object via done and fail callbacks
814            this._trigger('always', null, options);
815        },
816
817        _onSend: function (e, data) {
818            if (!data.submit) {
819                this._addConvenienceMethods(e, data);
820            }
821            var that = this,
822                jqXHR,
823                aborted,
824                slot,
825                pipe,
826                options = that._getAJAXSettings(data),
827                send = function () {
828                    that._sending += 1;
829                    // Set timer for bitrate progress calculation:
830                    options._bitrateTimer = new that._BitrateTimer();
831                    jqXHR = jqXHR || (
832                        ((aborted || that._trigger('send', e, options) === false) &&
833                        that._getXHRPromise(false, options.context, aborted)) ||
834                        that._chunkedUpload(options) || $.ajax(options)
835                    ).done(function (result, textStatus, jqXHR) {
836                        that._onDone(result, textStatus, jqXHR, options);
837                    }).fail(function (jqXHR, textStatus, errorThrown) {
838                        that._onFail(jqXHR, textStatus, errorThrown, options);
839                    }).always(function (jqXHRorResult, textStatus, jqXHRorError) {
840                        that._onAlways(
841                            jqXHRorResult,
842                            textStatus,
843                            jqXHRorError,
844                            options
845                        );
846                        that._sending -= 1;
847                        that._active -= 1;
848                        if (options.limitConcurrentUploads &&
849                                options.limitConcurrentUploads > that._sending) {
850                            // Start the next queued upload,
851                            // that has not been aborted:
852                            var nextSlot = that._slots.shift();
853                            while (nextSlot) {
854                                if (that._getDeferredState(nextSlot) === 'pending') {
855                                    nextSlot.resolve();
856                                    break;
857                                }
858                                nextSlot = that._slots.shift();
859                            }
860                        }
861                        if (that._active === 0) {
862                            // The stop callback is triggered when all uploads have
863                            // been completed, equivalent to the global ajaxStop event:
864                            that._trigger('stop');
865                        }
866                    });
867                    return jqXHR;
868                };
869            this._beforeSend(e, options);
870            if (this.options.sequentialUploads ||
871                    (this.options.limitConcurrentUploads &&
872                    this.options.limitConcurrentUploads <= this._sending)) {
873                if (this.options.limitConcurrentUploads > 1) {
874                    slot = $.Deferred();
875                    this._slots.push(slot);
876                    pipe = slot.pipe(send);
877                } else {
878                    this._sequence = this._sequence.pipe(send, send);
879                    pipe = this._sequence;
880                }
881                // Return the piped Promise object, enhanced with an abort method,
882                // which is delegated to the jqXHR object of the current upload,
883                // and jqXHR callbacks mapped to the equivalent Promise methods:
884                pipe.abort = function () {
885                    aborted = [undefined, 'abort', 'abort'];
886                    if (!jqXHR) {
887                        if (slot) {
888                            slot.rejectWith(options.context, aborted);
889                        }
890                        return send();
891                    }
892                    return jqXHR.abort();
893                };
894                return this._enhancePromise(pipe);
895            }
896            return send();
897        },
898
899        _onAdd: function (e, data) {
900            var that = this,
901                result = true,
902                options = $.extend({}, this.options, data),
903                limit = options.limitMultiFileUploads,
904                paramName = this._getParamName(options),
905                paramNameSet,
906                paramNameSlice,
907                fileSet,
908                i;
909            if (!(options.singleFileUploads || limit) ||
910                    !this._isXHRUpload(options)) {
911                fileSet = [data.files];
912                paramNameSet = [paramName];
913            } else if (!options.singleFileUploads && limit) {
914                fileSet = [];
915                paramNameSet = [];
916                for (i = 0; i < data.files.length; i += limit) {
917                    fileSet.push(data.files.slice(i, i + limit));
918                    paramNameSlice = paramName.slice(i, i + limit);
919                    if (!paramNameSlice.length) {
920                        paramNameSlice = paramName;
921                    }
922                    paramNameSet.push(paramNameSlice);
923                }
924            } else {
925                paramNameSet = paramName;
926            }
927            data.originalFiles = data.files;
928            $.each(fileSet || data.files, function (index, element) {
929                var newData = $.extend({}, data);
930                newData.files = fileSet ? element : [element];
931                newData.paramName = paramNameSet[index];
932                that._initResponseObject(newData);
933                that._initProgressObject(newData);
934                that._addConvenienceMethods(e, newData);
935                result = that._trigger('add', e, newData);
936                return result;
937            });
938            return result;
939        },
940
941        _replaceFileInput: function (input) {
942            var inputClone = input.clone(true);
943            $('<form></form>').append(inputClone)[0].reset();
944            // Detaching allows to insert the fileInput on another form
945            // without loosing the file input value:
946            input.after(inputClone).detach();
947            // Avoid memory leaks with the detached file input:
948            $.cleanData(input.unbind('remove'));
949            // Replace the original file input element in the fileInput
950            // elements set with the clone, which has been copied including
951            // event handlers:
952            this.options.fileInput = this.options.fileInput.map(function (i, el) {
953                if (el === input[0]) {
954                    return inputClone[0];
955                }
956                return el;
957            });
958            // If the widget has been initialized on the file input itself,
959            // override this.element with the file input clone:
960            if (input[0] === this.element[0]) {
961                this.element = inputClone;
962            }
963        },
964
965        _handleFileTreeEntry: function (entry, path) {
966            var that = this,
967                dfd = $.Deferred(),
968                errorHandler = function (e) {
969                    if (e && !e.entry) {
970                        e.entry = entry;
971                    }
972                    // Since $.when returns immediately if one
973                    // Deferred is rejected, we use resolve instead.
974                    // This allows valid files and invalid items
975                    // to be returned together in one set:
976                    dfd.resolve([e]);
977                },
978                dirReader;
979            path = path || '';
980            if (entry.isFile) {
981                if (entry._file) {
982                    // Workaround for Chrome bug #149735
983                    entry._file.relativePath = path;
984                    dfd.resolve(entry._file);
985                } else {
986                    entry.file(function (file) {
987                        file.relativePath = path;
988                        dfd.resolve(file);
989                    }, errorHandler);
990                }
991            } else if (entry.isDirectory) {
992                dirReader = entry.createReader();
993                dirReader.readEntries(function (entries) {
994                    that._handleFileTreeEntries(
995                        entries,
996                        path + entry.name + '/'
997                    ).done(function (files) {
998                        dfd.resolve(files);
999                    }).fail(errorHandler);
1000                }, errorHandler);
1001            } else {
1002                // Return an empy list for file system items
1003                // other than files or directories:
1004                dfd.resolve([]);
1005            }
1006            return dfd.promise();
1007        },
1008
1009        _handleFileTreeEntries: function (entries, path) {
1010            var that = this;
1011            return $.when.apply(
1012                $,
1013                $.map(entries, function (entry) {
1014                    return that._handleFileTreeEntry(entry, path);
1015                })
1016            ).pipe(function () {
1017                return Array.prototype.concat.apply(
1018                    [],
1019                    arguments
1020                );
1021            });
1022        },
1023
1024        _getDroppedFiles: function (dataTransfer) {
1025            dataTransfer = dataTransfer || {};
1026            var items = dataTransfer.items;
1027            if (items && items.length && (items[0].webkitGetAsEntry ||
1028                    items[0].getAsEntry)) {
1029                return this._handleFileTreeEntries(
1030                    $.map(items, function (item) {
1031                        var entry;
1032                        if (item.webkitGetAsEntry) {
1033                            entry = item.webkitGetAsEntry();
1034                            if (entry) {
1035                                // Workaround for Chrome bug #149735:
1036                                entry._file = item.getAsFile();
1037                            }
1038                            return entry;
1039                        }
1040                        return item.getAsEntry();
1041                    })
1042                );
1043            }
1044            return $.Deferred().resolve(
1045                $.makeArray(dataTransfer.files)
1046            ).promise();
1047        },
1048
1049        _getSingleFileInputFiles: function (fileInput) {
1050            fileInput = $(fileInput);
1051            var entries = fileInput.prop('webkitEntries') ||
1052                    fileInput.prop('entries'),
1053                files,
1054                value;
1055            if (entries && entries.length) {
1056                return this._handleFileTreeEntries(entries);
1057            }
1058            files = $.makeArray(fileInput.prop('files'));
1059            if (!files.length) {
1060                value = fileInput.prop('value');
1061                if (!value) {
1062                    return $.Deferred().resolve([]).promise();
1063                }
1064                // If the files property is not available, the browser does not
1065                // support the File API and we add a pseudo File object with
1066                // the input value as name with path information removed:
1067                files = [{name: value.replace(/^.*\\/, '')}];
1068            } else if (files[0].name === undefined && files[0].fileName) {
1069                // File normalization for Safari 4 and Firefox 3:
1070                $.each(files, function (index, file) {
1071                    file.name = file.fileName;
1072                    file.size = file.fileSize;
1073                });
1074            }
1075            return $.Deferred().resolve(files).promise();
1076        },
1077
1078        _getFileInputFiles: function (fileInput) {
1079            if (!(fileInput instanceof $) || fileInput.length === 1) {
1080                return this._getSingleFileInputFiles(fileInput);
1081            }
1082            return $.when.apply(
1083                $,
1084                $.map(fileInput, this._getSingleFileInputFiles)
1085            ).pipe(function () {
1086                return Array.prototype.concat.apply(
1087                    [],
1088                    arguments
1089                );
1090            });
1091        },
1092
1093        _onChange: function (e) {
1094            var that = this,
1095                data = {
1096                    fileInput: $(e.target),
1097                    form: $(e.target.form)
1098                };
1099            this._getFileInputFiles(data.fileInput).always(function (files) {
1100                data.files = files;
1101                if (that.options.replaceFileInput) {
1102                    that._replaceFileInput(data.fileInput);
1103                }
1104                if (that._trigger('change', e, data) !== false) {
1105                    that._onAdd(e, data);
1106                }
1107            });
1108        },
1109
1110        _onPaste: function (e) {
1111            var items = e.originalEvent && e.originalEvent.clipboardData &&
1112                    e.originalEvent.clipboardData.items,
1113                data = {files: []};
1114            if (items && items.length) {
1115                $.each(items, function (index, item) {
1116                    var file = item.getAsFile && item.getAsFile();
1117                    if (file) {
1118                        data.files.push(file);
1119                    }
1120                });
1121                if (this._trigger('paste', e, data) === false ||
1122                        this._onAdd(e, data) === false) {
1123                    return false;
1124                }
1125            }
1126        },
1127
1128        _onDrop: function (e) {
1129            e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer;
1130            var that = this,
1131                dataTransfer = e.dataTransfer,
1132                data = {};
1133            if (dataTransfer && dataTransfer.files && dataTransfer.files.length) {
1134                e.preventDefault();
1135                this._getDroppedFiles(dataTransfer).always(function (files) {
1136                    data.files = files;
1137                    if (that._trigger('drop', e, data) !== false) {
1138                        that._onAdd(e, data);
1139                    }
1140                });
1141            }
1142        },
1143
1144        _onDragOver: function (e) {
1145            e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer;
1146            var dataTransfer = e.dataTransfer;
1147            if (dataTransfer) {
1148                if (this._trigger('dragover', e) === false) {
1149                    return false;
1150                }
1151                if ($.inArray('Files', dataTransfer.types) !== -1) {
1152                    dataTransfer.dropEffect = 'copy';
1153                    e.preventDefault();
1154                }
1155            }
1156        },
1157
1158        _initEventHandlers: function () {
1159            if (this._isXHRUpload(this.options)) {
1160                this._on(this.options.dropZone, {
1161                    dragover: this._onDragOver,
1162                    drop: this._onDrop
1163                });
1164                this._on(this.options.pasteZone, {
1165                    paste: this._onPaste
1166                });
1167            }
1168            if ($.support.fileInput) {
1169                this._on(this.options.fileInput, {
1170                    change: this._onChange
1171                });
1172            }
1173        },
1174
1175        _destroyEventHandlers: function () {
1176            this._off(this.options.dropZone, 'dragover drop');
1177            this._off(this.options.pasteZone, 'paste');
1178            this._off(this.options.fileInput, 'change');
1179        },
1180
1181        _setOption: function (key, value) {
1182            var reinit = $.inArray(key, this._specialOptions) !== -1;
1183            if (reinit) {
1184                this._destroyEventHandlers();
1185            }
1186            this._super(key, value);
1187            if (reinit) {
1188                this._initSpecialOptions();
1189                this._initEventHandlers();
1190            }
1191        },
1192
1193        _initSpecialOptions: function () {
1194            var options = this.options;
1195            if (options.fileInput === undefined) {
1196                options.fileInput = this.element.is('input[type="file"]') ?
1197                        this.element : this.element.find('input[type="file"]');
1198            } else if (!(options.fileInput instanceof $)) {
1199                options.fileInput = $(options.fileInput);
1200            }
1201            if (!(options.dropZone instanceof $)) {
1202                options.dropZone = $(options.dropZone);
1203            }
1204            if (!(options.pasteZone instanceof $)) {
1205                options.pasteZone = $(options.pasteZone);
1206            }
1207        },
1208
1209        _getRegExp: function (str) {
1210            var parts = str.split('/'),
1211                modifiers = parts.pop();
1212            parts.shift();
1213            return new RegExp(parts.join('/'), modifiers);
1214        },
1215
1216        _isRegExpOption: function (key, value) {
1217            return key !== 'url' && $.type(value) === 'string' &&
1218                /^\/.*\/[igm]{0,3}$/.test(value);
1219        },
1220
1221        _initDataAttributes: function () {
1222            var that = this,
1223                options = this.options;
1224            // Initialize options set via HTML5 data-attributes:
1225            $.each(
1226                $(this.element[0].cloneNode(false)).data(),
1227                function (key, value) {
1228                    if (that._isRegExpOption(key, value)) {
1229                        value = that._getRegExp(value);
1230                    }
1231                    options[key] = value;
1232                }
1233            );
1234        },
1235
1236        _create: function () {
1237            this._initDataAttributes();
1238            this._initSpecialOptions();
1239            this._slots = [];
1240            this._sequence = this._getXHRPromise(true);
1241            this._sending = this._active = 0;
1242            this._initProgressObject(this);
1243            this._initEventHandlers();
1244        },
1245
1246        // This method is exposed to the widget API and allows to query
1247        // the number of active uploads:
1248        active: function () {
1249            return this._active;
1250        },
1251
1252        // This method is exposed to the widget API and allows to query
1253        // the widget upload progress.
1254        // It returns an object with loaded, total and bitrate properties
1255        // for the running uploads:
1256        progress: function () {
1257            return this._progress;
1258        },
1259
1260        // This method is exposed to the widget API and allows adding files
1261        // using the fileupload API. The data parameter accepts an object which
1262        // must have a files property and can contain additional options:
1263        // .fileupload('add', {files: filesList});
1264        add: function (data) {
1265            var that = this;
1266            if (!data || this.options.disabled) {
1267                return;
1268            }
1269            if (data.fileInput && !data.files) {
1270                this._getFileInputFiles(data.fileInput).always(function (files) {
1271                    data.files = files;
1272                    that._onAdd(null, data);
1273                });
1274            } else {
1275                data.files = $.makeArray(data.files);
1276                this._onAdd(null, data);
1277            }
1278        },
1279
1280        // This method is exposed to the widget API and allows sending files
1281        // using the fileupload API. The data parameter accepts an object which
1282        // must have a files or fileInput property and can contain additional options:
1283        // .fileupload('send', {files: filesList});
1284        // The method returns a Promise object for the file upload call.
1285        send: function (data) {
1286            if (data && !this.options.disabled) {
1287                if (data.fileInput && !data.files) {
1288                    var that = this,
1289                        dfd = $.Deferred(),
1290                        promise = dfd.promise(),
1291                        jqXHR,
1292                        aborted;
1293                    promise.abort = function () {
1294                        aborted = true;
1295                        if (jqXHR) {
1296                            return jqXHR.abort();
1297                        }
1298                        dfd.reject(null, 'abort', 'abort');
1299                        return promise;
1300                    };
1301                    this._getFileInputFiles(data.fileInput).always(
1302                        function (files) {
1303                            if (aborted) {
1304                                return;
1305                            }
1306                            if (!files.length) {
1307                                dfd.reject();
1308                                return;
1309                            }
1310                            data.files = files;
1311                            jqXHR = that._onSend(null, data).then(
1312                                function (result, textStatus, jqXHR) {
1313                                    dfd.resolve(result, textStatus, jqXHR);
1314                                },
1315                                function (jqXHR, textStatus, errorThrown) {
1316                                    dfd.reject(jqXHR, textStatus, errorThrown);
1317                                }
1318                            );
1319                        }
1320                    );
1321                    return this._enhancePromise(promise);
1322                }
1323                data.files = $.makeArray(data.files);
1324                if (data.files.length) {
1325                    return this._onSend(null, data);
1326                }
1327            }
1328            return this._getXHRPromise(false, data && data.context);
1329        }
1330
1331    });
1332
1333}));
Note: See TracBrowser for help on using the repository browser.

Sites map