jquery.fileupload-process.js 6.0 KB

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