jquery.fileupload-process.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * jQuery File Upload Processing Plugin 1.3.0
  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. * http://www.opensource.org/licenses/MIT
  10. */
  11. /* jshint nomen:false */
  12. /* global define, 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 {
  22. // Browser globals:
  23. factory(
  24. window.jQuery
  25. );
  26. }
  27. }(function ($) {
  28. 'use strict';
  29. var originalAdd = $.blueimp.fileupload.prototype.options.add;
  30. // The File Upload Processing plugin extends the fileupload widget
  31. // with file processing functionality:
  32. $.widget('blueimp.fileupload', $.blueimp.fileupload, {
  33. options: {
  34. // The list of processing actions:
  35. processQueue: [
  36. /*
  37. {
  38. action: 'log',
  39. type: 'debug'
  40. }
  41. */
  42. ],
  43. add: function (e, data) {
  44. var $this = $(this);
  45. data.process(function () {
  46. return $this.fileupload('process', data);
  47. });
  48. originalAdd.call(this, e, data);
  49. }
  50. },
  51. processActions: {
  52. /*
  53. log: function (data, options) {
  54. console[options.type](
  55. 'Processing "' + data.files[data.index].name + '"'
  56. );
  57. }
  58. */
  59. },
  60. _processFile: function (data, originalData) {
  61. var that = this,
  62. dfd = $.Deferred().resolveWith(that, [data]),
  63. chain = dfd.promise();
  64. this._trigger('process', null, data);
  65. $.each(data.processQueue, function (i, settings) {
  66. var func = function (data) {
  67. if (originalData.errorThrown) {
  68. return $.Deferred()
  69. .rejectWith(that, [originalData]).promise();
  70. }
  71. return that.processActions[settings.action].call(
  72. that,
  73. data,
  74. settings
  75. );
  76. };
  77. chain = chain.pipe(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' &&
  102. value.charAt(0) === '@') {
  103. settings[key] = options[
  104. value.slice(1) || (prefix ? prefix +
  105. key.charAt(0).toUpperCase() + key.slice(1) : 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 processsing 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. return $.Deferred()
  134. .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.pipe(func, func)
  141. .always(function () {
  142. that._processing -= 1;
  143. if (that._processing === 0) {
  144. that._trigger('processstop');
  145. }
  146. });
  147. });
  148. }
  149. return this._processingQueue;
  150. },
  151. _create: function () {
  152. this._super();
  153. this._processing = 0;
  154. this._processingQueue = $.Deferred().resolveWith(this)
  155. .promise();
  156. }
  157. });
  158. }));