|
| 1 | +/* |
| 2 | + * jQuery File Upload Processing Plugin 1.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 | + |
| 12 | +/*jslint nomen: true, unparam: true */ |
| 13 | +/*global define, window */ |
| 14 | + |
| 15 | +(function (factory) { |
| 16 | + 'use strict'; |
| 17 | + if (typeof define === 'function' && define.amd) { |
| 18 | + // Register as an anonymous AMD module: |
| 19 | + define([ |
| 20 | + 'jquery', |
| 21 | + './jquery.fileupload' |
| 22 | + ], factory); |
| 23 | + } else { |
| 24 | + // Browser globals: |
| 25 | + factory( |
| 26 | + window.jQuery |
| 27 | + ); |
| 28 | + } |
| 29 | +}(function ($) { |
| 30 | + 'use strict'; |
| 31 | + |
| 32 | + var originalAdd = $.blueimp.fileupload.prototype.options.add; |
| 33 | + |
| 34 | + // The File Upload Processing plugin extends the fileupload widget |
| 35 | + // with file processing functionality: |
| 36 | + $.widget('blueimp.fileupload', $.blueimp.fileupload, { |
| 37 | + |
| 38 | + options: { |
| 39 | + // The list of processing actions: |
| 40 | + processQueue: [ |
| 41 | + /* |
| 42 | + { |
| 43 | + action: 'log', |
| 44 | + type: 'debug' |
| 45 | + } |
| 46 | + */ |
| 47 | + ], |
| 48 | + add: function (e, data) { |
| 49 | + var $this = $(this); |
| 50 | + data.process(function () { |
| 51 | + return $this.fileupload('process', data); |
| 52 | + }); |
| 53 | + originalAdd.call(this, e, data); |
| 54 | + } |
| 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 | + |
| 67 | + _processFile: function (files, index, options) { |
| 68 | + var that = this, |
| 69 | + data = { |
| 70 | + files: files, |
| 71 | + index: index, |
| 72 | + options: options |
| 73 | + }, |
| 74 | + dfd = $.Deferred().resolveWith(that, [data]), |
| 75 | + chain = dfd.promise(); |
| 76 | + this._trigger('process', null, data); |
| 77 | + $.each(options.processQueue, function (i, settings) { |
| 78 | + var func = function (data) { |
| 79 | + return that.processActions[settings.action].call( |
| 80 | + that, |
| 81 | + data, |
| 82 | + settings |
| 83 | + ); |
| 84 | + }; |
| 85 | + chain = chain.pipe(func, settings.always && func); |
| 86 | + }); |
| 87 | + chain |
| 88 | + .done(function () { |
| 89 | + that._trigger('processdone', null, data); |
| 90 | + that._trigger('processalways', null, data); |
| 91 | + }) |
| 92 | + .fail(function () { |
| 93 | + that._trigger('processfail', null, data); |
| 94 | + that._trigger('processalways', null, data); |
| 95 | + }); |
| 96 | + return chain; |
| 97 | + }, |
| 98 | + |
| 99 | + // Replaces the settings of each processQueue item that |
| 100 | + // are strings starting with an "@", using the remaining |
| 101 | + // substring as key for the option map, |
| 102 | + // e.g. "@autoUpload" is replaced with options.autoUpload: |
| 103 | + _transformProcessQueue: function (options) { |
| 104 | + var processQueue = []; |
| 105 | + $.each(options.processQueue, function () { |
| 106 | + var settings = {}; |
| 107 | + $.each(this, function (key, value) { |
| 108 | + if ($.type(value) === 'string' && |
| 109 | + value.charAt(0) === '@') { |
| 110 | + settings[key] = options[value.slice(1)]; |
| 111 | + } else { |
| 112 | + settings[key] = value; |
| 113 | + } |
| 114 | + }); |
| 115 | + processQueue.push(settings); |
| 116 | + }); |
| 117 | + options.processQueue = processQueue; |
| 118 | + }, |
| 119 | + |
| 120 | + // Returns the number of files currently in the processsing queue: |
| 121 | + processing: function () { |
| 122 | + return this._processing; |
| 123 | + }, |
| 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, file) { |
| 136 | + that._processing += 1; |
| 137 | + var func = function () { |
| 138 | + return that._processFile(data.files, index, options); |
| 139 | + }; |
| 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 | + |
| 152 | + _create: function () { |
| 153 | + this._super(); |
| 154 | + this._processing = 0; |
| 155 | + this._processingQueue = $.Deferred().resolveWith(this) |
| 156 | + .promise(); |
| 157 | + } |
| 158 | + |
| 159 | + }); |
| 160 | + |
| 161 | +})); |
0 commit comments