jquery.fileupload-process.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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().rejectWith(that, [originalData]).promise();
  68. }
  69. return that.processActions[settings.action].call(
  70. that,
  71. data,
  72. settings
  73. );
  74. };
  75. chain = chain[that._promisePipe](func, settings.always && func);
  76. });
  77. chain
  78. .done(function () {
  79. that._trigger('processdone', null, data);
  80. that._trigger('processalways', null, data);
  81. })
  82. .fail(function () {
  83. that._trigger('processfail', null, data);
  84. that._trigger('processalways', null, data);
  85. });
  86. return chain;
  87. },
  88. // Replaces the settings of each processQueue item that
  89. // are strings starting with an "@", using the remaining
  90. // substring as key for the option map,
  91. // e.g. "@autoUpload" is replaced with options.autoUpload:
  92. _transformProcessQueue: function (options) {
  93. var processQueue = [];
  94. $.each(options.processQueue, function () {
  95. var settings = {},
  96. action = this.action,
  97. prefix = this.prefix === true ? action : this.prefix;
  98. $.each(this, function (key, value) {
  99. if ($.type(value) === 'string' && value.charAt(0) === '@') {
  100. settings[key] =
  101. options[
  102. value.slice(1) ||
  103. (prefix
  104. ? prefix + key.charAt(0).toUpperCase() + key.slice(1)
  105. : key)
  106. ];
  107. } else {
  108. settings[key] = value;
  109. }
  110. });
  111. processQueue.push(settings);
  112. });
  113. options.processQueue = processQueue;
  114. },
  115. // Returns the number of files currently in the processing queue:
  116. processing: function () {
  117. return this._processing;
  118. },
  119. // Processes the files given as files property of the data parameter,
  120. // returns a Promise object that allows to bind callbacks:
  121. process: function (data) {
  122. var that = this,
  123. options = $.extend({}, this.options, data);
  124. if (options.processQueue && options.processQueue.length) {
  125. this._transformProcessQueue(options);
  126. if (this._processing === 0) {
  127. this._trigger('processstart');
  128. }
  129. $.each(data.files, function (index) {
  130. var opts = index ? $.extend({}, options) : options,
  131. func = function () {
  132. if (data.errorThrown) {
  133. // eslint-disable-next-line new-cap
  134. return $.Deferred().rejectWith(that, [data]).promise();
  135. }
  136. return that._processFile(opts, data);
  137. };
  138. opts.index = index;
  139. that._processing += 1;
  140. that._processingQueue = that._processingQueue[that._promisePipe](
  141. func,
  142. func
  143. ).always(function () {
  144. that._processing -= 1;
  145. if (that._processing === 0) {
  146. that._trigger('processstop');
  147. }
  148. });
  149. });
  150. }
  151. return this._processingQueue;
  152. },
  153. _create: function () {
  154. this._super();
  155. this._processing = 0;
  156. // eslint-disable-next-line new-cap
  157. this._processingQueue = $.Deferred().resolveWith(this).promise();
  158. }
  159. });
  160. });