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