initial commit
[namibia] / public / js / vendor / jquery.fileupload-fp.js
1 /*
2  * jQuery File Upload File Processing Plugin 1.2
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, regexp: true */
13 /*global define, window, document */
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             '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';
34
35     // The File Upload FP version extends the fileupload widget
36     // with file processing functionality:
37     $.widget('blueimp.fileupload', $.blueimp.fileupload, {
38
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             ],
60
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         },
70
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                             data.img = img;
90                             dfd.resolveWith(that, [data]);
91                         }
92                     );
93                 } else {
94                     dfd.rejectWith(that, [data]);
95                 }
96                 return dfd.promise();
97             },
98             // Resizes the image given as data.img and updates
99             // data.canvas with the resized image as canvas element.
100             // Accepts the options maxWidth, maxHeight, minWidth and
101             // minHeight to scale the given image:
102             resize: function (data, options) {
103                 var img = data.img,
104                     canvas;
105                 options = $.extend({canvas: true}, options);
106                 if (img) {
107                     canvas = loadImage.scale(img, options);
108                     if (canvas.width !== img.width ||
109                             canvas.height !== img.height) {
110                         data.canvas = canvas;
111                     }
112                 }
113                 return data;
114             },
115             // Saves the processed image given as data.canvas
116             // inplace at data.index of data.files:
117             save: function (data, options) {
118                 // Do nothing if no processing has happened:
119                 if (!data.canvas) {
120                     return data;
121                 }
122                 var that = this,
123                     file = data.files[data.index],
124                     name = file.name,
125                     dfd = $.Deferred(),
126                     callback = function (blob) {
127                         if (!blob.name) {
128                             if (file.type === blob.type) {
129                                 blob.name = file.name;
130                             } else if (file.name) {
131                                 blob.name = file.name.replace(
132                                     /\..+$/,
133                                     '.' + blob.type.substr(6)
134                                 );
135                             }
136                         }
137                         // Store the created blob at the position
138                         // of the original file in the files list:
139                         data.files[data.index] = blob;
140                         dfd.resolveWith(that, [data]);
141                     };
142                 // Use canvas.mozGetAsFile directly, to retain the filename, as
143                 // Gecko doesn't support the filename option for FormData.append:
144                 if (data.canvas.mozGetAsFile) {
145                     callback(data.canvas.mozGetAsFile(
146                         (/^image\/(jpeg|png)$/.test(file.type) && name) ||
147                             ((name && name.replace(/\..+$/, '')) ||
148                                 'blob') + '.png',
149                         file.type
150                     ));
151                 } else {
152                     data.canvas.toBlob(callback, file.type);
153                 }
154                 return dfd.promise();
155             }
156         },
157
158         // Resizes the file at the given index and stores the created blob at
159         // the original position of the files list, returns a Promise object:
160         _processFile: function (files, index, options) {
161             var that = this,
162                 dfd = $.Deferred().resolveWith(that, [{
163                     files: files,
164                     index: index
165                 }]),
166                 chain = dfd.promise();
167             that._processing += 1;
168             $.each(options.process, function (i, settings) {
169                 chain = chain.pipe(function (data) {
170                     return that.processActions[settings.action]
171                         .call(this, data, settings);
172                 });
173             });
174             chain.always(function () {
175                 that._processing -= 1;
176                 if (that._processing === 0) {
177                     that.element
178                         .removeClass('fileupload-processing');
179                 }
180             });
181             if (that._processing === 1) {
182                 that.element.addClass('fileupload-processing');
183             }
184             return chain;
185         },
186
187         // Processes the files given as files property of the data parameter,
188         // returns a Promise object that allows to bind a done handler, which
189         // will be invoked after processing all files (inplace) is done:
190         process: function (data) {
191             var that = this,
192                 options = $.extend({}, this.options, data);
193             if (options.process && options.process.length &&
194                     this._isXHRUpload(options)) {
195                 $.each(data.files, function (index, file) {
196                     that._processingQueue = that._processingQueue.pipe(
197                         function () {
198                             var dfd = $.Deferred();
199                             that._processFile(data.files, index, options)
200                                 .always(function () {
201                                     dfd.resolveWith(that);
202                                 });
203                             return dfd.promise();
204                         }
205                     );
206                 });
207             }
208             return this._processingQueue;
209         },
210
211         _create: function () {
212             this._super();
213             this._processing = 0;
214             this._processingQueue = $.Deferred().resolveWith(this)
215                 .promise();
216         }
217
218     });
219
220 }));