|
| 1 | +/* |
| 2 | + * jQuery File Upload AngularJS Plugin 1.0.1 |
| 3 | + * https://github.com/blueimp/jQuery-File-Upload |
| 4 | + * |
| 5 | + * Copyright 2013, 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 */ |
| 13 | +/*global angular */ |
| 14 | + |
| 15 | +(function () { |
| 16 | + 'use strict'; |
| 17 | + |
| 18 | + angular.module('blueimp.fileupload', []) |
| 19 | + |
| 20 | + .provider('fileUpload', function () { |
| 21 | + var scopeApply = function () { |
| 22 | + var scope = angular.element(this) |
| 23 | + .fileupload('option', 'scope')(); |
| 24 | + if (!scope.$$phase) { |
| 25 | + scope.$apply(); |
| 26 | + } |
| 27 | + }, |
| 28 | + $config; |
| 29 | + $config = this.defaults = { |
| 30 | + handleResponse: function (e, data) { |
| 31 | + var files = data.result && data.result.files; |
| 32 | + if (files) { |
| 33 | + data.scope().replace(data.files, files); |
| 34 | + } else if (data.errorThrown || |
| 35 | + data.textStatus === 'error') { |
| 36 | + data.files[0].error = data.errorThrown || |
| 37 | + data.textStatus; |
| 38 | + } |
| 39 | + }, |
| 40 | + add: function (e, data) { |
| 41 | + var scope = data.scope(); |
| 42 | + data.process(function () { |
| 43 | + return scope.process(data); |
| 44 | + }).always( |
| 45 | + function () { |
| 46 | + var file = data.files[0], |
| 47 | + submit = function () { |
| 48 | + return data.submit(); |
| 49 | + }; |
| 50 | + file.$cancel = function () { |
| 51 | + scope.clear(data.files); |
| 52 | + return data.abort(); |
| 53 | + }; |
| 54 | + file.$state = function () { |
| 55 | + return data.state(); |
| 56 | + }; |
| 57 | + file.$progress = function () { |
| 58 | + return data.progress(); |
| 59 | + }; |
| 60 | + file.$response = function () { |
| 61 | + return data.response(); |
| 62 | + }; |
| 63 | + if (file.$state() === 'rejected') { |
| 64 | + file._$submit = submit; |
| 65 | + } else { |
| 66 | + file.$submit = submit; |
| 67 | + } |
| 68 | + scope.$apply(function () { |
| 69 | + var method = scope.option('prependFiles') ? |
| 70 | + 'unshift' : 'push'; |
| 71 | + Array.prototype[method].apply( |
| 72 | + scope.queue, |
| 73 | + data.files |
| 74 | + ); |
| 75 | + if (file.$submit && |
| 76 | + (scope.option('autoUpload') || |
| 77 | + data.autoUpload) && |
| 78 | + data.autoUpload !== false) { |
| 79 | + file.$submit(); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + ); |
| 84 | + }, |
| 85 | + progress: function (e, data) { |
| 86 | + data.scope().$apply(); |
| 87 | + }, |
| 88 | + done: function (e, data) { |
| 89 | + var that = this; |
| 90 | + data.scope().$apply(function () { |
| 91 | + data.handleResponse.call(that, e, data); |
| 92 | + }); |
| 93 | + }, |
| 94 | + fail: function (e, data) { |
| 95 | + var that = this; |
| 96 | + if (data.errorThrown === 'abort') { |
| 97 | + return; |
| 98 | + } |
| 99 | + if (data.dataType.indexOf('json') === data.dataType.length - 4) { |
| 100 | + try { |
| 101 | + data.result = angular.fromJson(data.jqXHR.responseText); |
| 102 | + } catch (err) {} |
| 103 | + } |
| 104 | + data.scope().$apply(function () { |
| 105 | + data.handleResponse.call(that, e, data); |
| 106 | + }); |
| 107 | + }, |
| 108 | + stop: scopeApply, |
| 109 | + processstart: scopeApply, |
| 110 | + processstop: scopeApply, |
| 111 | + getNumberOfFiles: function () { |
| 112 | + return this.scope().queue.length; |
| 113 | + }, |
| 114 | + dataType: 'json', |
| 115 | + prependFiles: true, |
| 116 | + autoUpload: false |
| 117 | + }; |
| 118 | + this.$get = [ |
| 119 | + function () { |
| 120 | + return { |
| 121 | + defaults: $config |
| 122 | + }; |
| 123 | + } |
| 124 | + ]; |
| 125 | + }) |
| 126 | + |
| 127 | + .provider('formatFileSizeFilter', function () { |
| 128 | + var $config = this.defaults = { |
| 129 | + // Byte units following the IEC format |
| 130 | + // http://en.wikipedia.org/wiki/Kilobyte |
| 131 | + units: [ |
| 132 | + {size: 1000000000, suffix: ' GB'}, |
| 133 | + {size: 1000000, suffix: ' MB'}, |
| 134 | + {size: 1000, suffix: ' KB'} |
| 135 | + ] |
| 136 | + }; |
| 137 | + this.$get = function () { |
| 138 | + return function (bytes) { |
| 139 | + if (!angular.isNumber(bytes)) { |
| 140 | + return ''; |
| 141 | + } |
| 142 | + var unit = true, |
| 143 | + i = -1; |
| 144 | + while (unit) { |
| 145 | + unit = $config.units[i += 1]; |
| 146 | + if (i === $config.units.length - 1 || bytes >= unit.size) { |
| 147 | + return (bytes / unit.size).toFixed(2) + unit.suffix; |
| 148 | + } |
| 149 | + } |
| 150 | + }; |
| 151 | + }; |
| 152 | + }) |
| 153 | + |
| 154 | + .controller('FileUploadController', [ |
| 155 | + '$scope', '$element', '$attrs', 'fileUpload', |
| 156 | + function ($scope, $element, $attrs, fileUpload) { |
| 157 | + $scope.disabled = angular.element('<input type="file">') |
| 158 | + .prop('disabled'); |
| 159 | + $scope.queue = $scope.queue || []; |
| 160 | + $scope.clear = function (files) { |
| 161 | + var queue = this.queue, |
| 162 | + i = queue.length, |
| 163 | + file = files, |
| 164 | + length = 1; |
| 165 | + if (angular.isArray(files)) { |
| 166 | + file = files[0]; |
| 167 | + length = files.length; |
| 168 | + } |
| 169 | + while (i) { |
| 170 | + if (queue[i -= 1] === file) { |
| 171 | + return queue.splice(i, length); |
| 172 | + } |
| 173 | + } |
| 174 | + }; |
| 175 | + $scope.replace = function (oldFiles, newFiles) { |
| 176 | + var queue = this.queue, |
| 177 | + file = oldFiles[0], |
| 178 | + i, |
| 179 | + j; |
| 180 | + for (i = 0; i < queue.length; i += 1) { |
| 181 | + if (queue[i] === file) { |
| 182 | + for (j = 0; j < newFiles.length; j += 1) { |
| 183 | + queue[i + j] = newFiles[j]; |
| 184 | + } |
| 185 | + return; |
| 186 | + } |
| 187 | + } |
| 188 | + }; |
| 189 | + $scope.progress = function () { |
| 190 | + return $element.fileupload('progress'); |
| 191 | + }; |
| 192 | + $scope.active = function () { |
| 193 | + return $element.fileupload('active'); |
| 194 | + }; |
| 195 | + $scope.option = function (option, data) { |
| 196 | + return $element.fileupload('option', option, data); |
| 197 | + }; |
| 198 | + $scope.add = function (data) { |
| 199 | + return $element.fileupload('add', data); |
| 200 | + }; |
| 201 | + $scope.send = function (data) { |
| 202 | + return $element.fileupload('send', data); |
| 203 | + }; |
| 204 | + $scope.process = function (data) { |
| 205 | + return $element.fileupload('process', data); |
| 206 | + }; |
| 207 | + $scope.processing = function (data) { |
| 208 | + return $element.fileupload('processing', data); |
| 209 | + }; |
| 210 | + $scope.applyOnQueue = function (method) { |
| 211 | + var list = this.queue.slice(0), |
| 212 | + i, |
| 213 | + file; |
| 214 | + for (i = 0; i < list.length; i += 1) { |
| 215 | + file = list[i]; |
| 216 | + if (file[method]) { |
| 217 | + file[method](); |
| 218 | + } |
| 219 | + } |
| 220 | + }; |
| 221 | + $scope.submit = function () { |
| 222 | + this.applyOnQueue('$submit'); |
| 223 | + }; |
| 224 | + $scope.cancel = function () { |
| 225 | + this.applyOnQueue('$cancel'); |
| 226 | + }; |
| 227 | + // The fileupload widget will initialize with |
| 228 | + // the options provided via "data-"-parameters, |
| 229 | + // as well as those given via options object: |
| 230 | + $element.fileupload(angular.extend( |
| 231 | + {scope: function () { |
| 232 | + return $scope; |
| 233 | + }}, |
| 234 | + fileUpload.defaults |
| 235 | + )).on('fileuploadadd', function (e, data) { |
| 236 | + data.scope = $scope.option('scope'); |
| 237 | + }).on([ |
| 238 | + 'fileuploadadd', |
| 239 | + 'fileuploadsubmit', |
| 240 | + 'fileuploadsend', |
| 241 | + 'fileuploaddone', |
| 242 | + 'fileuploadfail', |
| 243 | + 'fileuploadalways', |
| 244 | + 'fileuploadprogress', |
| 245 | + 'fileuploadprogressall', |
| 246 | + 'fileuploadstart', |
| 247 | + 'fileuploadstop', |
| 248 | + 'fileuploadchange', |
| 249 | + 'fileuploadpaste', |
| 250 | + 'fileuploaddrop', |
| 251 | + 'fileuploaddragover', |
| 252 | + 'fileuploadchunksend', |
| 253 | + 'fileuploadchunkdone', |
| 254 | + 'fileuploadchunkfail', |
| 255 | + 'fileuploadchunkalways', |
| 256 | + 'fileuploadprocessstart', |
| 257 | + 'fileuploadprocess', |
| 258 | + 'fileuploadprocessdone', |
| 259 | + 'fileuploadprocessfail', |
| 260 | + 'fileuploadprocessalways', |
| 261 | + 'fileuploadprocessstop' |
| 262 | + ].join(' '), function (e, data) { |
| 263 | + $scope.$emit(e.type, data); |
| 264 | + }); |
| 265 | + // Observe option changes: |
| 266 | + $scope.$watch( |
| 267 | + $attrs.fileupload, |
| 268 | + function (newOptions, oldOptions) { |
| 269 | + if (newOptions) { |
| 270 | + $element.fileupload('option', newOptions); |
| 271 | + } |
| 272 | + } |
| 273 | + ); |
| 274 | + } |
| 275 | + ]) |
| 276 | + |
| 277 | + .controller('FileUploadProgressController', [ |
| 278 | + '$scope', '$attrs', '$parse', |
| 279 | + function ($scope, $attrs, $parse) { |
| 280 | + var fn = $parse($attrs.progress), |
| 281 | + update = function () { |
| 282 | + var progress = fn($scope); |
| 283 | + if (!progress || !progress.total) { |
| 284 | + return; |
| 285 | + } |
| 286 | + $scope.num = Math.floor( |
| 287 | + progress.loaded / progress.total * 100 |
| 288 | + ); |
| 289 | + }; |
| 290 | + update(); |
| 291 | + $scope.$watch( |
| 292 | + $attrs.progress + '.loaded', |
| 293 | + function (newValue, oldValue) { |
| 294 | + if (newValue !== oldValue) { |
| 295 | + update(); |
| 296 | + } |
| 297 | + } |
| 298 | + ); |
| 299 | + } |
| 300 | + ]) |
| 301 | + |
| 302 | + .controller('FileUploadPreviewController', [ |
| 303 | + '$scope', '$element', '$attrs', '$parse', |
| 304 | + function ($scope, $element, $attrs, $parse) { |
| 305 | + var fn = $parse($attrs.preview), |
| 306 | + file = fn($scope); |
| 307 | + if (file.preview) { |
| 308 | + $element.append(file.preview); |
| 309 | + } |
| 310 | + } |
| 311 | + ]) |
| 312 | + |
| 313 | + .directive('fileupload', function () { |
| 314 | + return { |
| 315 | + controller: 'FileUploadController' |
| 316 | + }; |
| 317 | + }) |
| 318 | + |
| 319 | + .directive('progress', function () { |
| 320 | + return { |
| 321 | + controller: 'FileUploadProgressController' |
| 322 | + }; |
| 323 | + }) |
| 324 | + |
| 325 | + .directive('preview', function () { |
| 326 | + return { |
| 327 | + controller: 'FileUploadPreviewController' |
| 328 | + }; |
| 329 | + }) |
| 330 | + |
| 331 | + .directive('download', function () { |
| 332 | + return function (scope, elm, attrs) { |
| 333 | + elm.on('dragstart', function (e) { |
| 334 | + try { |
| 335 | + e.originalEvent.dataTransfer.setData( |
| 336 | + 'DownloadURL', |
| 337 | + [ |
| 338 | + 'application/octet-stream', |
| 339 | + elm.prop('download'), |
| 340 | + elm.prop('href') |
| 341 | + ].join(':') |
| 342 | + ); |
| 343 | + } catch (err) {} |
| 344 | + }); |
| 345 | + }; |
| 346 | + }); |
| 347 | + |
| 348 | +}()); |
0 commit comments