|
1 | 1 | /*
|
2 |
| - * jQuery File Upload AngularJS Plugin 1.2.1 |
| 2 | + * jQuery File Upload AngularJS Plugin 1.3 |
3 | 3 | * https://github.com/blueimp/jQuery-File-Upload
|
4 | 4 | *
|
5 | 5 | * Copyright 2013, Sebastian Tschan
|
|
32 | 32 |
|
33 | 33 | angular.module('blueimp.fileupload', [])
|
34 | 34 |
|
| 35 | + // The fileUpload service provides configuration options |
| 36 | + // for the fileUpload directive and default handlers for |
| 37 | + // File Upload events: |
35 | 38 | .provider('fileUpload', function () {
|
36 | 39 | var scopeApply = function () {
|
37 | 40 | var scope = angular.element(this)
|
|
140 | 143 | ];
|
141 | 144 | })
|
142 | 145 |
|
| 146 | + // Format byte numbers to readable presentations: |
143 | 147 | .provider('formatFileSizeFilter', function () {
|
144 |
| - var $config = this.defaults = { |
| 148 | + var $config = { |
145 | 149 | // Byte units following the IEC format
|
146 | 150 | // http://en.wikipedia.org/wiki/Kilobyte
|
147 | 151 | units: [
|
|
150 | 154 | {size: 1000, suffix: ' KB'}
|
151 | 155 | ]
|
152 | 156 | };
|
| 157 | + this.defaults = $config; |
153 | 158 | this.$get = function () {
|
154 | 159 | return function (bytes) {
|
155 | 160 | if (!angular.isNumber(bytes)) {
|
156 | 161 | return '';
|
157 | 162 | }
|
158 | 163 | var unit = true,
|
159 |
| - i = -1; |
| 164 | + i = 0, |
| 165 | + prefix, |
| 166 | + suffix; |
160 | 167 | while (unit) {
|
161 |
| - unit = $config.units[i += 1]; |
| 168 | + unit = $config.units[i]; |
| 169 | + prefix = unit.prefix || ''; |
| 170 | + suffix = unit.suffix || ''; |
162 | 171 | if (i === $config.units.length - 1 || bytes >= unit.size) {
|
163 |
| - return (bytes / unit.size).toFixed(2) + unit.suffix; |
| 172 | + return prefix + (bytes / unit.size).toFixed(2) + suffix; |
164 | 173 | }
|
| 174 | + i += 1; |
165 | 175 | }
|
166 | 176 | };
|
167 | 177 | };
|
168 | 178 | })
|
169 | 179 |
|
| 180 | + // The FileUploadController initializes the fileupload widget and |
| 181 | + // provides scope methods to control the File Upload functionality: |
170 | 182 | .controller('FileUploadController', [
|
171 | 183 | '$scope', '$element', '$attrs', 'fileUpload',
|
172 | 184 | function ($scope, $element, $attrs, fileUpload) {
|
|
206 | 218 | length = files.length;
|
207 | 219 | }
|
208 | 220 | while (i) {
|
209 |
| - if (queue[i -= 1] === file) { |
| 221 | + i -= 1; |
| 222 | + if (queue[i] === file) { |
210 | 223 | return queue.splice(i, length);
|
211 | 224 | }
|
212 | 225 | }
|
|
293 | 306 | });
|
294 | 307 | // Observe option changes:
|
295 | 308 | $scope.$watch(
|
296 |
| - $attrs.fileupload, |
| 309 | + $attrs.fileUpload, |
297 | 310 | function (newOptions, oldOptions) {
|
298 | 311 | if (newOptions) {
|
299 | 312 | $element.fileupload('option', newOptions);
|
|
303 | 316 | }
|
304 | 317 | ])
|
305 | 318 |
|
| 319 | + // Provide File Upload progress feedback: |
306 | 320 | .controller('FileUploadProgressController', [
|
307 | 321 | '$scope', '$attrs', '$parse',
|
308 | 322 | function ($scope, $attrs, $parse) {
|
309 |
| - var fn = $parse($attrs.progress), |
| 323 | + var fn = $parse($attrs.fileUploadProgress), |
310 | 324 | update = function () {
|
311 | 325 | var progress = fn($scope);
|
312 | 326 | if (!progress || !progress.total) {
|
|
318 | 332 | };
|
319 | 333 | update();
|
320 | 334 | $scope.$watch(
|
321 |
| - $attrs.progress + '.loaded', |
| 335 | + $attrs.fileUploadProgress + '.loaded', |
322 | 336 | function (newValue, oldValue) {
|
323 | 337 | if (newValue !== oldValue) {
|
324 | 338 | update();
|
|
328 | 342 | }
|
329 | 343 | ])
|
330 | 344 |
|
| 345 | + // Display File Upload previews: |
331 | 346 | .controller('FileUploadPreviewController', [
|
332 | 347 | '$scope', '$element', '$attrs', '$parse',
|
333 | 348 | function ($scope, $element, $attrs, $parse) {
|
334 |
| - var fn = $parse($attrs.preview), |
| 349 | + var fn = $parse($attrs.fileUploadPreview), |
335 | 350 | file = fn($scope);
|
336 | 351 | if (file.preview) {
|
337 | 352 | $element.append(file.preview);
|
338 | 353 | }
|
339 | 354 | }
|
340 | 355 | ])
|
341 | 356 |
|
342 |
| - .directive('fileupload', function () { |
| 357 | + .directive('fileUpload', function () { |
343 | 358 | return {
|
344 | 359 | controller: 'FileUploadController'
|
345 | 360 | };
|
346 | 361 | })
|
347 | 362 |
|
348 |
| - .directive('progress', function () { |
| 363 | + .directive('fileUploadProgress', function () { |
349 | 364 | return {
|
350 | 365 | controller: 'FileUploadProgressController'
|
351 | 366 | };
|
352 | 367 | })
|
353 | 368 |
|
354 |
| - .directive('preview', function () { |
| 369 | + .directive('fileUploadPreview', function () { |
355 | 370 | return {
|
356 | 371 | controller: 'FileUploadPreviewController'
|
357 | 372 | };
|
358 | 373 | })
|
359 | 374 |
|
| 375 | + // Enhance the HTML5 download attribute to |
| 376 | + // allow drag&drop of files to the desktop: |
360 | 377 | .directive('download', function () {
|
361 | 378 | return function (scope, elm, attrs) {
|
362 | 379 | elm.on('dragstart', function (e) {
|
|
0 commit comments