2 * jQuery File Upload Processing Plugin
3 * https://github.com/blueimp/jQuery-File-Upload
5 * Copyright 2012, Sebastian Tschan
8 * Licensed under the MIT license:
9 * https://opensource.org/licenses/MIT
12 /* global define, require */
16 if (typeof define === 'function' && define.amd) {
17 // Register as an anonymous AMD module:
18 define(['jquery', './jquery.fileupload'], factory);
19 } else if (typeof exports === 'object') {
21 factory(require('jquery'), require('./jquery.fileupload'));
24 factory(window.jQuery);
29 var originalAdd = $.blueimp.fileupload.prototype.options.add;
31 // The File Upload Processing plugin extends the fileupload widget
32 // with file processing functionality:
33 $.widget('blueimp.fileupload', $.blueimp.fileupload, {
35 // The list of processing actions:
44 add: function (e, data) {
46 data.process(function () {
47 return $this.fileupload('process', data);
49 originalAdd.call(this, e, data);
55 log: function (data, options) {
56 console[options.type](
57 'Processing "' + data.files[data.index].name + '"'
63 _processFile: function (data, originalData) {
65 // eslint-disable-next-line new-cap
66 dfd = $.Deferred().resolveWith(that, [data]),
67 chain = dfd.promise();
68 this._trigger('process', null, data);
69 $.each(data.processQueue, function (i, settings) {
70 var func = function (data) {
71 if (originalData.errorThrown) {
72 // eslint-disable-next-line new-cap
73 return $.Deferred().rejectWith(that, [originalData]).promise();
75 return that.processActions[settings.action].call(
81 chain = chain[that._promisePipe](func, settings.always && func);
85 that._trigger('processdone', null, data);
86 that._trigger('processalways', null, data);
89 that._trigger('processfail', null, data);
90 that._trigger('processalways', null, data);
95 // Replaces the settings of each processQueue item that
96 // are strings starting with an "@", using the remaining
97 // substring as key for the option map,
98 // e.g. "@autoUpload" is replaced with options.autoUpload:
99 _transformProcessQueue: function (options) {
100 var processQueue = [];
101 $.each(options.processQueue, function () {
103 action = this.action,
104 prefix = this.prefix === true ? action : this.prefix;
105 $.each(this, function (key, value) {
106 if ($.type(value) === 'string' && value.charAt(0) === '@') {
111 ? prefix + key.charAt(0).toUpperCase() + key.slice(1)
115 settings[key] = value;
118 processQueue.push(settings);
120 options.processQueue = processQueue;
123 // Returns the number of files currently in the processing queue:
124 processing: function () {
125 return this._processing;
128 // Processes the files given as files property of the data parameter,
129 // returns a Promise object that allows to bind callbacks:
130 process: function (data) {
132 options = $.extend({}, this.options, data);
133 if (options.processQueue && options.processQueue.length) {
134 this._transformProcessQueue(options);
135 if (this._processing === 0) {
136 this._trigger('processstart');
138 $.each(data.files, function (index) {
139 var opts = index ? $.extend({}, options) : options,
141 if (data.errorThrown) {
142 // eslint-disable-next-line new-cap
143 return $.Deferred().rejectWith(that, [data]).promise();
145 return that._processFile(opts, data);
148 that._processing += 1;
149 that._processingQueue = that._processingQueue[that._promisePipe](
152 ).always(function () {
153 that._processing -= 1;
154 if (that._processing === 0) {
155 that._trigger('processstop');
160 return this._processingQueue;
163 _create: function () {
165 this._processing = 0;
166 // eslint-disable-next-line new-cap
167 this._processingQueue = $.Deferred().resolveWith(this).promise();