jquery.fileupload-process.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * jQuery File Upload Processing Plugin
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2012, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, require */
  12. (function(factory) {
  13. 'use strict';
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['jquery', './jquery.fileupload'], factory);
  17. } else if (typeof exports === 'object') {
  18. // Node/CommonJS:
  19. factory(require('jquery'), require('./jquery.fileupload'));
  20. } else {
  21. // Browser globals:
  22. factory(window.jQuery);
  23. }
  24. })(function($) {
  25. 'use strict';
  26. var originalAdd = $.blueimp.fileupload.prototype.options.add;
  27. // The File Upload Processing plugin extends the fileupload widget
  28. // with file processing functionality:
  29. $.widget('blueimp.fileupload', $.blueimp.fileupload, {
  30. options: {
  31. // The list of processing actions:
  32. processQueue: [
  33. /*
  34. {
  35. action: 'log',
  36. type: 'debug'
  37. }
  38. */
  39. ],
  40. add: function(e, data) {
  41. var $this = $(this);
  42. data.process(function() {
  43. return $this.fileupload('process', data);
  44. });
  45. originalAdd.call(this, e, data);
  46. }
  47. },
  48. processActions: {
  49. /*
  50. log: function (data, options) {
  51. console[options.type](
  52. 'Processing "' + data.files[data.index].name + '"'
  53. );
  54. }
  55. */
  56. },
  57. _processFile: function(data, originalData) {
  58. var that = this,
  59. // eslint-disable-next-line new-cap
  60. dfd = $.Deferred().resolveWith(that, [data]),
  61. chain = dfd.promise();
  62. this._trigger('process', null, data);
  63. $.each(data.processQueue, function(i, settings) {
  64. var func = function(data) {
  65. if (originalData.errorThrown) {
  66. // eslint-disable-next-line new-cap
  67. return $.Deferred()
  68. .rejectWith(that, [originalData])
  69. .promise();
  70. }
  71. return that.processActions[settings.action].call(
  72. that,
  73. data,
  74. settings
  75. );
  76. };
  77. chain = chain.then(func, settings.always && func);
  78. });
  79. chain
  80. .done(function() {
  81. that._trigger('processdone', null, data);
  82. that._trigger('processalways', null, data);
  83. })
  84. .fail(function() {
  85. that._trigger('processfail', null, data);
  86. that._trigger('processalways', null, data);
  87. });
  88. return chain;
  89. },
  90. // Replaces the settings of each processQueue item that
  91. // are strings starting with an "@", using the remaining
  92. // substring as key for the option map,
  93. // e.g. "@autoUpload" is replaced with options.autoUpload:
  94. _transformProcessQueue: function(options) {
  95. var processQueue = [];
  96. $.each(options.processQueue, function() {
  97. var settings = {},
  98. action = this.action,
  99. prefix = this.prefix === true ? action : this.prefix;
  100. $.each(this, function(key, value) {
  101. if ($.type(value) === 'string' && value.charAt(0) === '@') {
  102. settings[key] =
  103. options[
  104. value.slice(1) ||
  105. (prefix
  106. ? prefix + key.charAt(0).toUpperCase() + key.slice(1)
  107. : key)
  108. ];
  109. } else {
  110. settings[key] = value;
  111. }
  112. });
  113. processQueue.push(settings);
  114. });
  115. options.processQueue = processQueue;
  116. },
  117. // Returns the number of files currently in the processsing queue:
  118. processing: function() {
  119. return this._processing;
  120. },
  121. // Processes the files given as files property of the data parameter,
  122. // returns a Promise object that allows to bind callbacks:
  123. process: function(data) {
  124. var that = this,
  125. options = $.extend({}, this.options, data);
  126. if (options.processQueue && options.processQueue.length) {
  127. this._transformProcessQueue(options);
  128. if (this._processing === 0) {
  129. this._trigger('processstart');
  130. }
  131. $.each(data.files, function(index) {
  132. var opts = index ? $.extend({}, options) : options,
  133. func = function() {
  134. if (data.errorThrown) {
  135. // eslint-disable-next-line new-cap
  136. return $.Deferred()
  137. .rejectWith(that, [data])
  138. .promise();
  139. }
  140. return that._processFile(opts, data);
  141. };
  142. opts.index = index;
  143. that._processing += 1;
  144. that._processingQueue = that._processingQueue
  145. .then(func, func)
  146. .always(function() {
  147. that._processing -= 1;
  148. if (that._processing === 0) {
  149. that._trigger('processstop');
  150. }
  151. });
  152. });
  153. }
  154. return this._processingQueue;
  155. },
  156. _create: function() {
  157. this._super();
  158. this._processing = 0;
  159. // eslint-disable-next-line new-cap
  160. this._processingQueue = $.Deferred()
  161. .resolveWith(this)
  162. .promise();
  163. }
  164. });
  165. });