easier direct editing of list items
[sgn.git] / js / jqueryfileupload / jquery.fileupload-fp.js
blobfdf18fb5d3fa9625f624edde1f60400c06b196c4
1 /*
2  * jQuery File Upload File Processing Plugin 1.2.1
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  */
12 /*jslint nomen: true, unparam: true, regexp: true */
13 /*global define, window, document */
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             'load-image',
22             'canvas-to-blob',
23             './jquery.fileupload'
24         ], factory);
25     } else {
26         // Browser globals:
27         factory(
28             window.jQuery,
29             window.loadImage
30         );
31     }
32 }(function ($, loadImage) {
33     'use strict';
35     // The File Upload FP version extends the fileupload widget
36     // with file processing functionality:
37     $.widget('blueimp.fileupload', $.blueimp.fileupload, {
39         options: {
40             // The list of file processing actions:
41             process: [
42             /*
43                 {
44                     action: 'load',
45                     fileTypes: /^image\/(gif|jpeg|png)$/,
46                     maxFileSize: 20000000 // 20MB
47                 },
48                 {
49                     action: 'resize',
50                     maxWidth: 1920,
51                     maxHeight: 1200,
52                     minWidth: 800,
53                     minHeight: 600
54                 },
55                 {
56                     action: 'save'
57                 }
58             */
59             ],
61             // The add callback is invoked as soon as files are added to the
62             // fileupload widget (via file input selection, drag & drop or add
63             // API call). See the basic file upload widget for more information:
64             add: function (e, data) {
65                 $(this).fileupload('process', data).done(function () {
66                     data.submit();
67                 });
68             }
69         },
71         processActions: {
72             // Loads the image given via data.files and data.index
73             // as img element if the browser supports canvas.
74             // Accepts the options fileTypes (regular expression)
75             // and maxFileSize (integer) to limit the files to load:
76             load: function (data, options) {
77                 var that = this,
78                     file = data.files[data.index],
79                     dfd = $.Deferred();
80                 if (window.HTMLCanvasElement &&
81                         window.HTMLCanvasElement.prototype.toBlob &&
82                         ($.type(options.maxFileSize) !== 'number' ||
83                             file.size < options.maxFileSize) &&
84                         (!options.fileTypes ||
85                             options.fileTypes.test(file.type))) {
86                     loadImage(
87                         file,
88                         function (img) {
89                             if (!img.src) {
90                                 return dfd.rejectWith(that, [data]);
91                             }
92                             data.img = img;
93                             dfd.resolveWith(that, [data]);
94                         }
95                     );
96                 } else {
97                     dfd.rejectWith(that, [data]);
98                 }
99                 return dfd.promise();
100             },
101             // Resizes the image given as data.img and updates
102             // data.canvas with the resized image as canvas element.
103             // Accepts the options maxWidth, maxHeight, minWidth and
104             // minHeight to scale the given image:
105             resize: function (data, options) {
106                 var img = data.img,
107                     canvas;
108                 options = $.extend({canvas: true}, options);
109                 if (img) {
110                     canvas = loadImage.scale(img, options);
111                     if (canvas.width !== img.width ||
112                             canvas.height !== img.height) {
113                         data.canvas = canvas;
114                     }
115                 }
116                 return data;
117             },
118             // Saves the processed image given as data.canvas
119             // inplace at data.index of data.files:
120             save: function (data, options) {
121                 // Do nothing if no processing has happened:
122                 if (!data.canvas) {
123                     return data;
124                 }
125                 var that = this,
126                     file = data.files[data.index],
127                     name = file.name,
128                     dfd = $.Deferred(),
129                     callback = function (blob) {
130                         if (!blob.name) {
131                             if (file.type === blob.type) {
132                                 blob.name = file.name;
133                             } else if (file.name) {
134                                 blob.name = file.name.replace(
135                                     /\..+$/,
136                                     '.' + blob.type.substr(6)
137                                 );
138                             }
139                         }
140                         // Store the created blob at the position
141                         // of the original file in the files list:
142                         data.files[data.index] = blob;
143                         dfd.resolveWith(that, [data]);
144                     };
145                 // Use canvas.mozGetAsFile directly, to retain the filename, as
146                 // Gecko doesn't support the filename option for FormData.append:
147                 if (data.canvas.mozGetAsFile) {
148                     callback(data.canvas.mozGetAsFile(
149                         (/^image\/(jpeg|png)$/.test(file.type) && name) ||
150                             ((name && name.replace(/\..+$/, '')) ||
151                                 'blob') + '.png',
152                         file.type
153                     ));
154                 } else {
155                     data.canvas.toBlob(callback, file.type);
156                 }
157                 return dfd.promise();
158             }
159         },
161         // Resizes the file at the given index and stores the created blob at
162         // the original position of the files list, returns a Promise object:
163         _processFile: function (files, index, options) {
164             var that = this,
165                 dfd = $.Deferred().resolveWith(that, [{
166                     files: files,
167                     index: index
168                 }]),
169                 chain = dfd.promise();
170             that._processing += 1;
171             $.each(options.process, function (i, settings) {
172                 chain = chain.pipe(function (data) {
173                     return that.processActions[settings.action]
174                         .call(this, data, settings);
175                 });
176             });
177             chain.always(function () {
178                 that._processing -= 1;
179                 if (that._processing === 0) {
180                     that.element
181                         .removeClass('fileupload-processing');
182                 }
183             });
184             if (that._processing === 1) {
185                 that.element.addClass('fileupload-processing');
186             }
187             return chain;
188         },
190         // Processes the files given as files property of the data parameter,
191         // returns a Promise object that allows to bind a done handler, which
192         // will be invoked after processing all files (inplace) is done:
193         process: function (data) {
194             var that = this,
195                 options = $.extend({}, this.options, data);
196             if (options.process && options.process.length &&
197                     this._isXHRUpload(options)) {
198                 $.each(data.files, function (index, file) {
199                     that._processingQueue = that._processingQueue.pipe(
200                         function () {
201                             var dfd = $.Deferred();
202                             that._processFile(data.files, index, options)
203                                 .always(function () {
204                                     dfd.resolveWith(that);
205                                 });
206                             return dfd.promise();
207                         }
208                     );
209                 });
210             }
211             return this._processingQueue;
212         },
214         _create: function () {
215             this._super();
216             this._processing = 0;
217             this._processingQueue = $.Deferred().resolveWith(this)
218                 .promise();
219         }
221     });
223 }));