[1144] | 1 | /* |
---|
[1320] | 2 | * jQuery File Upload Processing Plugin 1.2.2 |
---|
[1144] | 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 (data) { |
---|
| 68 | var that = this, |
---|
| 69 | dfd = $.Deferred().resolveWith(that, [data]), |
---|
| 70 | chain = dfd.promise(); |
---|
| 71 | this._trigger('process', null, data); |
---|
| 72 | $.each(data.processQueue, function (i, settings) { |
---|
| 73 | var func = function (data) { |
---|
| 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 | |
---|
| 94 | // Replaces the settings of each processQueue item that |
---|
| 95 | // are strings starting with an "@", using the remaining |
---|
| 96 | // substring as key for the option map, |
---|
| 97 | // e.g. "@autoUpload" is replaced with options.autoUpload: |
---|
| 98 | _transformProcessQueue: function (options) { |
---|
| 99 | var processQueue = []; |
---|
| 100 | $.each(options.processQueue, function () { |
---|
[1320] | 101 | var settings = {}, |
---|
| 102 | action = this.action, |
---|
| 103 | prefix = this.prefix === true ? action : this.prefix; |
---|
[1144] | 104 | $.each(this, function (key, value) { |
---|
| 105 | if ($.type(value) === 'string' && |
---|
| 106 | value.charAt(0) === '@') { |
---|
[1320] | 107 | settings[key] = options[ |
---|
| 108 | value.slice(1) || (prefix ? prefix + |
---|
| 109 | key.charAt(0).toUpperCase() + key.slice(1) : key) |
---|
| 110 | ]; |
---|
[1144] | 111 | } else { |
---|
| 112 | settings[key] = value; |
---|
| 113 | } |
---|
[1320] | 114 | |
---|
[1144] | 115 | }); |
---|
| 116 | processQueue.push(settings); |
---|
| 117 | }); |
---|
| 118 | options.processQueue = processQueue; |
---|
| 119 | }, |
---|
| 120 | |
---|
| 121 | // Returns the number of files currently in the processsing queue: |
---|
| 122 | processing: function () { |
---|
| 123 | return this._processing; |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | // Processes the files given as files property of the data parameter, |
---|
| 127 | // returns a Promise object that allows to bind callbacks: |
---|
| 128 | process: function (data) { |
---|
| 129 | var that = this, |
---|
| 130 | options = $.extend({}, this.options, data); |
---|
| 131 | if (options.processQueue && options.processQueue.length) { |
---|
| 132 | this._transformProcessQueue(options); |
---|
| 133 | if (this._processing === 0) { |
---|
| 134 | this._trigger('processstart'); |
---|
| 135 | } |
---|
[1320] | 136 | $.each(data.files, function (index) { |
---|
[1144] | 137 | var opts = index ? $.extend({}, options) : options, |
---|
| 138 | func = function () { |
---|
| 139 | return that._processFile(opts); |
---|
| 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 | |
---|
| 155 | _create: function () { |
---|
| 156 | this._super(); |
---|
| 157 | this._processing = 0; |
---|
| 158 | this._processingQueue = $.Deferred().resolveWith(this) |
---|
| 159 | .promise(); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | }); |
---|
| 163 | |
---|
| 164 | })); |
---|