github.com/apremalal/vamps-core@v1.0.1-0.20161221121535-d430b56ec174/server/webapps/app/base/plugins/jquery-file-upload/js/jquery.fileupload-process.js (about) 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 12 /* jshint nomen:false */ 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.js' 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, originalData) { 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 if (originalData.errorThrown) { 75 return $.Deferred() 76 .rejectWith(that, [originalData]).promise(); 77 } 78 return that.processActions[settings.action].call( 79 that, 80 data, 81 settings 82 ); 83 }; 84 chain = chain.pipe(func, settings.always && func); 85 }); 86 chain 87 .done(function () { 88 that._trigger('processdone', null, data); 89 that._trigger('processalways', null, data); 90 }) 91 .fail(function () { 92 that._trigger('processfail', null, data); 93 that._trigger('processalways', null, data); 94 }); 95 return chain; 96 }, 97 98 // Replaces the settings of each processQueue item that 99 // are strings starting with an "@", using the remaining 100 // substring as key for the option map, 101 // e.g. "@autoUpload" is replaced with options.autoUpload: 102 _transformProcessQueue: function (options) { 103 var processQueue = []; 104 $.each(options.processQueue, function () { 105 var settings = {}, 106 action = this.action, 107 prefix = this.prefix === true ? action : this.prefix; 108 $.each(this, function (key, value) { 109 if ($.type(value) === 'string' && 110 value.charAt(0) === '@') { 111 settings[key] = options[ 112 value.slice(1) || (prefix ? prefix + 113 key.charAt(0).toUpperCase() + key.slice(1) : key) 114 ]; 115 } else { 116 settings[key] = value; 117 } 118 119 }); 120 processQueue.push(settings); 121 }); 122 options.processQueue = processQueue; 123 }, 124 125 // Returns the number of files currently in the processsing queue: 126 processing: function () { 127 return this._processing; 128 }, 129 130 // Processes the files given as files property of the data parameter, 131 // returns a Promise object that allows to bind callbacks: 132 process: function (data) { 133 var that = this, 134 options = $.extend({}, this.options, data); 135 if (options.processQueue && options.processQueue.length) { 136 this._transformProcessQueue(options); 137 if (this._processing === 0) { 138 this._trigger('processstart'); 139 } 140 $.each(data.files, function (index) { 141 var opts = index ? $.extend({}, options) : options, 142 func = function () { 143 if (data.errorThrown) { 144 return $.Deferred() 145 .rejectWith(that, [data]).promise(); 146 } 147 return that._processFile(opts, data); 148 }; 149 opts.index = index; 150 that._processing += 1; 151 that._processingQueue = that._processingQueue.pipe(func, func) 152 .always(function () { 153 that._processing -= 1; 154 if (that._processing === 0) { 155 that._trigger('processstop'); 156 } 157 }); 158 }); 159 } 160 return this._processingQueue; 161 }, 162 163 _create: function () { 164 this._super(); 165 this._processing = 0; 166 this._processingQueue = $.Deferred().resolveWith(this) 167 .promise(); 168 } 169 170 }); 171 172 }));