1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* jQuery File Upload AngularJS Plugin
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2013, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
/* jshint nomen:false */
/* global define, angular, require */
;(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// Register as an anonymous AMD module:
define([
'jquery',
'angular',
'./jquery.fileupload-image',
'./jquery.fileupload-audio',
'./jquery.fileupload-video',
'./jquery.fileupload-validate'
], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS:
factory(
require('jquery'),
require('angular'),
require('./jquery.fileupload-image'),
require('./jquery.fileupload-audio'),
require('./jquery.fileupload-video'),
require('./jquery.fileupload-validate')
);
} else {
factory();
}
}(function () {
'use strict';
angular.module('blueimp.fileupload', [])
// The fileUpload service provides configuration options
// for the fileUpload directive and default handlers for
// File Upload events:
.provider('fileUpload', function () {
var scopeEvalAsync = function (expression) {
var scope = angular.element(this)
.fileupload('option', 'scope');
// Schedule a new $digest cycle if not already inside of one
// and evaluate the given expression:
scope.$evalAsync(expression);
},
addFileMethods = function (scope, data) {
var files = data.files,
file = files[0];
angular.forEach(files, function (file, index) {
file._index = index;
file.$state = function () {
return data.state();
};
file.$processing = function () {
return data.processing();
};
file.$progress = function () {
return data.progress();
};
file.$response = function () {
return data.response();
};
});
file.$submit = function () {
if (!file.error) {
return data.submit();
}
};
file.$cancel = function () {
return data.abort();
};
},
$config;
$config = this.defaults = {
handleResponse: function (e, data) {
var files = data.result && data.result.files;
if (files) {
data.scope.replace(data.files, files);
} else if (data.errorThrown ||
data.textStatus === 'error') {
data.files[0].error = data.errorThrown ||
data.textStatus;
}
},
add: function (e, data) {
if (e.isDefaultPrevented()) {
return false;
}
var scope = data.scope,
filesCopy = [];
angular.forEach(data.files, function (file) {
filesCopy.push(file);
});
scope.$parent.$applyAsync(function () {
addFileMethods(scope, data);
var method = scope.option('prependFiles') ?
'unshift' : 'push';
Array.prototype[method].apply(scope.queue, data.files);
});
data.process(function () {
return scope.process(data);
}).always(function () {
scope.$parent.$applyAsync(function () {
addFileMethods(scope, data);
scope.replace(filesCopy, data.files);
});
}).then(function () {
if ((scope.option('autoUpload') ||
data.autoUpload) &&
data.autoUpload !== false) {
data.submit();
}
});
},
done: function (e, data) {
if (e.isDefaultPrevented()) {
return false;
}
var that = this;
data.scope.$apply(function () {
data.handleResponse.call(that, e, data);
});
},
fail: function (e, data) {
if (e.isDefaultPrevented()) {
return false;
}
var that = this,
scope = data.scope;
if (data.errorThrown === 'abort') {
scope.clear(data.files);
return;
}
scope.$apply(function () {
data.handleResponse.call(that, e, data);
});
},
stop: scopeEvalAsync,
processstart: scopeEvalAsync,
processstop: scopeEvalAsync,
getNumberOfFiles: function () {
var scope = this.scope;
return scope.queue.length - scope.processing();
},
dataType: 'json',
autoUpload: false
};
this.$get = [
function () {
return {
defaults: $config
};
}
];
})
// Format byte numbers to readable presentations:
.provider('formatFileSizeFilter', function () {
var $config = {
// Byte units following the IEC format
// http://en.wikipedia.org/wiki/Kilobyte
units: [
{size: 1000000000, suffix: ' GB'},
{size: 1000000, suffix: ' MB'},
{size: 1000, suffix: ' KB'}
]
};
this.defaults = $config;
this.$get = function () {
return function (bytes) {
if (!angular.isNumber(bytes)) {
return '';
}
var unit = true,
i = 0,
prefix,
suffix;
while (unit) {
unit = $config.units[i];
prefix = unit.prefix || '';
suffix = unit.suffix || '';
if (i === $config.units.length - 1 || bytes >= unit.size) {
return prefix + (bytes / unit.size).toFixed(2) + suffix;
}
i += 1;
}
};
};
})
// The FileUploadController initializes the fileupload widget and
// provides scope methods to control the File Upload functionality:
.controller('FileUploadController', [
'$scope', '$element', '$attrs', '$window', 'fileUpload','$q',
function ($scope, $element, $attrs, $window, fileUpload, $q) {
var uploadMethods = {
progress: function () {
return $element.fileupload('progress');
},
active: function () {
return $element.fileupload('active');
},
option: function (option, data) {
if (arguments.length === 1) {
return $element.fileupload('option', option);
}
$element.fileupload('option', option, data);
},
add: function (data) {
return $element.fileupload('add', data);
},
send: function (data) {
return $element.fileupload('send', data);
},
process: function (data) {
return $element.fileupload('process', data);
},
processing: function (data) {
return $element.fileupload('processing', data);
}
};
$scope.disabled = !$window.jQuery.support.fileInput;
$scope.queue = $scope.queue || [];
$scope.clear = function (files) {
var queue = this.queue,
i = queue.length,
file = files,
length = 1;
if (angular.isArray(files)) {
file = files[0];
length = files.length;
}
while (i) {
i -= 1;
if (queue[i] === file) {
return queue.splice(i, length);
}
}
};
$scope.replace = function (oldFiles, newFiles) {
var queue = this.queue,
file = oldFiles[0],
i,
j;
for (i = 0; i < queue.length; i += 1) {
if (queue[i] === file) {
for (j = 0; j < newFiles.length; j += 1) {
queue[i + j] = newFiles[j];
}
return;
}
}
};
$scope.applyOnQueue = function (method) {
var list = this.queue.slice(0),
i,
file,
promises = [];
for (i = 0; i < list.length; i += 1) {
file = list[i];
if (file[method]) {
promises.push(file[method]());
}
}
return $q.all(promises);
};
$scope.submit = function () {
return this.applyOnQueue('$submit');
};
$scope.cancel = function () {
return this.applyOnQueue('$cancel');
};
// Add upload methods to the scope:
angular.extend($scope, uploadMethods);
// The fileupload widget will initialize with
// the options provided via "data-"-parameters,
// as well as those given via options object:
$element.fileupload(angular.extend(
{scope: $scope},
fileUpload.defaults
)).on('fileuploadadd', function (e, data) {
data.scope = $scope;
}).on('fileuploadfail', function (e, data) {
if (data.errorThrown === 'abort') {
return;
}
if (data.dataType &&
data.dataType.indexOf('json') === data.dataType.length - 4) {
try {
data.result = angular.fromJson(data.jqXHR.responseText);
} catch (ignore) {}
}
}).on([
'fileuploadadd',
'fileuploadsubmit',
'fileuploadsend',
'fileuploaddone',
'fileuploadfail',
'fileuploadalways',
'fileuploadprogress',
'fileuploadprogressall',
'fileuploadstart',
'fileuploadstop',
'fileuploadchange',
'fileuploadpaste',
'fileuploaddrop',
'fileuploaddragover',
'fileuploadchunkbeforesend',
'fileuploadchunksend',
'fileuploadchunkdone',
'fileuploadchunkfail',
'fileuploadchunkalways',
'fileuploadprocessstart',
'fileuploadprocess',
'fileuploadprocessdone',
'fileuploadprocessfail',
'fileuploadprocessalways',
'fileuploadprocessstop'
].join(' '), function (e, data) {
$scope.$parent.$applyAsync(function () {
if ($scope.$emit(e.type, data).defaultPrevented) {
e.preventDefault();
}
});
}).on('remove', function () {
// Remove upload methods from the scope,
// when the widget is removed:
var method;
for (method in uploadMethods) {
if (uploadMethods.hasOwnProperty(method)) {
delete $scope[method];
}
}
});
// Observe option changes:
$scope.$watch(
$attrs.fileUpload,
function (newOptions) {
if (newOptions) {
$element.fileupload('option', newOptions);
}
}
);
}
])
// Provide File Upload progress feedback:
.controller('FileUploadProgressController', [
'$scope', '$attrs', '$parse',
function ($scope, $attrs, $parse) {
var fn = $parse($attrs.fileUploadProgress),
update = function () {
var progress = fn($scope);
if (!progress || !progress.total) {
return;
}
$scope.num = Math.floor(
progress.loaded / progress.total * 100
);
};
update();
$scope.$watch(
$attrs.fileUploadProgress + '.loaded',
function (newValue, oldValue) {
if (newValue !== oldValue) {
update();
}
}
);
}
])
// Display File Upload previews:
.controller('FileUploadPreviewController', [
'$scope', '$element', '$attrs',
function ($scope, $element, $attrs) {
$scope.$watch(
$attrs.fileUploadPreview + '.preview',
function (preview) {
$element.empty();
if (preview) {
$element.append(preview);
}
}
);
}
])
.directive('fileUpload', function () {
return {
controller: 'FileUploadController',
scope: true
};
})
.directive('fileUploadProgress', function () {
return {
controller: 'FileUploadProgressController',
scope: true
};
})
.directive('fileUploadPreview', function () {
return {
controller: 'FileUploadPreviewController'
};
})
// Enhance the HTML5 download attribute to
// allow drag&drop of files to the desktop:
.directive('download', function () {
return function (scope, elm) {
elm.on('dragstart', function (e) {
try {
e.originalEvent.dataTransfer.setData(
'DownloadURL',
[
'application/octet-stream',
elm.prop('download'),
elm.prop('href')
].join(':')
);
} catch (ignore) {}
});
};
});
}));
|