kf-jquery-fileupload-rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +77 -0
  3. data/Rakefile +15 -0
  4. data/lib/jquery-fileupload-rails.rb +8 -0
  5. data/lib/jquery/fileupload/rails/engine.rb +8 -0
  6. data/lib/jquery/fileupload/rails/middleware.rb +59 -0
  7. data/lib/jquery/fileupload/rails/upload.rb +3 -0
  8. data/lib/jquery/fileupload/rails/version.rb +7 -0
  9. data/vendor/assets/images/loading.gif +0 -0
  10. data/vendor/assets/images/progressbar.gif +0 -0
  11. data/vendor/assets/javascripts/jquery-fileupload/basic.js +4 -0
  12. data/vendor/assets/javascripts/jquery-fileupload/cors/jquery.postmessage-transport.js +117 -0
  13. data/vendor/assets/javascripts/jquery-fileupload/cors/jquery.xdr-transport.js +86 -0
  14. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-angular.js +429 -0
  15. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-audio.js +106 -0
  16. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-image.js +315 -0
  17. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-jquery-ui.js +152 -0
  18. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-process.js +172 -0
  19. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-ui.js +699 -0
  20. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-validate.js +119 -0
  21. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-video.js +106 -0
  22. data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload.js +1426 -0
  23. data/vendor/assets/javascripts/jquery-fileupload/jquery.iframe-transport.js +214 -0
  24. data/vendor/assets/javascripts/jquery-fileupload/vendor/jquery.ui.widget.js +530 -0
  25. data/vendor/assets/stylesheets/jquery.fileupload-ui.scss +72 -0
  26. metadata +112 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d31428e3f1b2b43621c73225f7bf9549138e328
4
+ data.tar.gz: fb3e2c3dc116a53ebe65b9c4b0d125d03c1f93c9
5
+ SHA512:
6
+ metadata.gz: 730099624798d555edfa7afcac7ea124f6e014096f27518b795987c0dbbae0404acea7bbce348c86e9d39938c3e3ef9988d655d089c505971bc4c855280f14ec
7
+ data.tar.gz: 66409c40d1d2b65aee7c967b568c822e8a8bcd58ea4736e603feb3054036c952b8c02402be5e7818ed2cb651f7e9816d675bc672961f6ac9e0c488e954ebeb85
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # jQuery File Upload for Rails
2
+
3
+ (modified by @imkmf)
4
+
5
+ [jQuery-File-Plugin](https://github.com/blueimp/jQuery-File-Upload) is a file upload plugin written by [Sebastian Tschan](https://github.com/blueimp). jQuery File Upload features multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumable file uploads and client-side image resizing.
6
+
7
+ jquery-fileupload-rails is a library that integrates jQuery File Upload for Rails 3.1 Asset Pipeline (Rails 3.2 supported).
8
+
9
+ ## Plugin versions
10
+
11
+ * jQuery File Upload User Interface Plugin 9.5.2
12
+ * jQuery File Upload Plugin 5.40.1
13
+ * jQuery UI Widget 1.10.4+amd
14
+
15
+ ## Installing Gem
16
+
17
+ gem "jquery-fileupload-rails"
18
+
19
+ ## Using the javascripts
20
+
21
+ Require jquery-fileupload in your app/assets/application.js file.
22
+
23
+ //= require jquery-fileupload
24
+
25
+ The snippet above will add the following js files to the mainfest file.
26
+
27
+ //= jquery-fileupload/cors/jquery.postmessage-transport
28
+ //= jquery-fileupload/cors/jquery.xdr-transport
29
+ //= jquery-fileupload/jquery.fileupload-angular
30
+ //= jquery-fileupload/jquery.fileupload-audio
31
+ //= jquery-fileupload/jquery.fileupload-image
32
+ //= jquery-fileupload/jquery.fileupload-jquery-ui
33
+ //= jquery-fileupload/jquery.fileupload-process
34
+ //= jquery-fileupload/jquery.fileupload-ui
35
+ //= jquery-fileupload/jquery.fileupload-validate
36
+ //= jquery-fileupload/jquery.fileupload-video
37
+ //= jquery-fileupload/jquery.fileupload
38
+ //= jquery-fileupload/jquery.iframe-transport
39
+
40
+ If you only need the basic files, just add the code below to your application.js file. [Basic setup guide](https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin)
41
+
42
+ //= require jquery-fileupload/basic
43
+
44
+ The basic setup only includes the following files:
45
+
46
+ //= require jquery-fileupload/vendor/jquery.ui.widget
47
+ //= require jquery-fileupload/jquery.iframe-transport
48
+ //= require jquery-fileupload/jquery.fileupload
49
+
50
+ ## Using the stylesheet
51
+
52
+ Require the stylesheet file to app/assets/stylesheets/application.css
53
+
54
+ *= require jquery.fileupload-ui
55
+
56
+ ## Using the middleware
57
+
58
+ The `jquery.iframe-transport` fallback transport has some special caveats regarding the response data type, http status, and character encodings. `jquery-fileupload-rails` includes a middleware that handles these inconsistencies seamlessly. If you decide to use it, create an initializer that adds the middleware to your application's middleware stack.
59
+
60
+ Rails.application.config.middleware.use JQuery::FileUpload::Rails::Middleware
61
+
62
+ ## [Example app](https://github.com/tors/jquery-fileupload-rails-paperclip-example)
63
+ This app uses paperclip and twitter-bootstrap-rails
64
+
65
+ You can also check out Ryan Bate's RailsCast [jQuery File Upload episode](http://railscasts.com/episodes/381-jquery-file-upload). You will
66
+ need a pro account to watch it though.
67
+
68
+
69
+ ## Thanks
70
+ Thanks to [Sebastian Tschan](https://github.com/blueimp) for writing an awesome file upload plugin.
71
+
72
+ ## License
73
+ Copyright (c) 2012 Tors Dalid
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
76
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
77
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ desc "Bundle the gem"
6
+ task :bundle do
7
+ sh('bundle install')
8
+ sh 'gem build *.gemspec'
9
+ sh 'gem install *.gem'
10
+ sh 'rm *.gem'
11
+ end
12
+
13
+ task(:default).clear
14
+ task :default => :bundle
15
+
@@ -0,0 +1,8 @@
1
+ module JQuery
2
+ module FileUpload
3
+ module Rails
4
+ require 'jquery/fileupload/rails/engine' if defined?(Rails)
5
+ end
6
+ end
7
+ end
8
+ require 'jquery/fileupload/rails/upload' if defined?(Rails)
@@ -0,0 +1,8 @@
1
+ module JQuery
2
+ module FileUpload
3
+ module Rails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ module JQuery
2
+ module FileUpload
3
+ module Rails
4
+ class Middleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ dup._call(env)
11
+ end
12
+
13
+ def _call(env)
14
+ @status, @headers, @response = @app.call(env)
15
+ @request = Rack::Request.new(env)
16
+
17
+ if iframe_transport?
18
+ @headers['Content-Type'] = 'text/html'
19
+ [@status, @headers, self]
20
+ else
21
+ [@status, @headers, @response]
22
+ end
23
+ end
24
+
25
+ def each(&block)
26
+ block.call(html_document_left) if iframe_transport?
27
+ @response.each(&block)
28
+ block.call(html_document_right) if iframe_transport?
29
+ end
30
+
31
+ def iframe_transport?
32
+ @request.params['X-Requested-With'] == 'IFrame'
33
+ end
34
+
35
+ def html_document_left
36
+ "<!DOCTYPE html><html><body><textarea #{metadata}>"
37
+ end
38
+
39
+ def html_document_right
40
+ "</textarea></body></html>"
41
+ end
42
+
43
+ def metadata
44
+ meta = {}
45
+ meta['data-status'] = @response.status if @response.respond_to? :status
46
+ meta['data-statusText'] = @response.status_message if @response.respond_to? :status_message
47
+ meta['data-type'] = @headers['Content-Type'] if @headers.has_key?('Content-Type')
48
+ meta.map {|key,value| "#{key}='#{value}'" }.join(' ')
49
+ end
50
+
51
+ private
52
+
53
+ def method_missing(method, *args)
54
+ @response.send(method.intern, *args)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ require "jquery/fileupload/rails/engine"
2
+ require "jquery/fileupload/rails/version"
3
+ require "jquery/fileupload/rails/middleware"
@@ -0,0 +1,7 @@
1
+ module JQuery
2
+ module FileUpload
3
+ module Rails
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
7
+ end
Binary file
@@ -0,0 +1,4 @@
1
+ //=require jquery-fileupload/vendor/jquery.ui.widget
2
+ //=require jquery-fileupload/jquery.iframe-transport
3
+ //=require jquery-fileupload/jquery.fileupload
4
+
@@ -0,0 +1,117 @@
1
+ /*
2
+ * jQuery postMessage Transport Plugin 1.1.1
3
+ * https://github.com/blueimp/jQuery-File-Upload
4
+ *
5
+ * Copyright 2011, Sebastian Tschan
6
+ * https://blueimp.net
7
+ *
8
+ * Licensed under the MIT license:
9
+ * http://www.opensource.org/licenses/MIT
10
+ */
11
+
12
+ /* global define, window, document */
13
+
14
+ (function (factory) {
15
+ 'use strict';
16
+ if (typeof define === 'function' && define.amd) {
17
+ // Register as an anonymous AMD module:
18
+ define(['jquery'], factory);
19
+ } else {
20
+ // Browser globals:
21
+ factory(window.jQuery);
22
+ }
23
+ }(function ($) {
24
+ 'use strict';
25
+
26
+ var counter = 0,
27
+ names = [
28
+ 'accepts',
29
+ 'cache',
30
+ 'contents',
31
+ 'contentType',
32
+ 'crossDomain',
33
+ 'data',
34
+ 'dataType',
35
+ 'headers',
36
+ 'ifModified',
37
+ 'mimeType',
38
+ 'password',
39
+ 'processData',
40
+ 'timeout',
41
+ 'traditional',
42
+ 'type',
43
+ 'url',
44
+ 'username'
45
+ ],
46
+ convert = function (p) {
47
+ return p;
48
+ };
49
+
50
+ $.ajaxSetup({
51
+ converters: {
52
+ 'postmessage text': convert,
53
+ 'postmessage json': convert,
54
+ 'postmessage html': convert
55
+ }
56
+ });
57
+
58
+ $.ajaxTransport('postmessage', function (options) {
59
+ if (options.postMessage && window.postMessage) {
60
+ var iframe,
61
+ loc = $('<a>').prop('href', options.postMessage)[0],
62
+ target = loc.protocol + '//' + loc.host,
63
+ xhrUpload = options.xhr().upload;
64
+ return {
65
+ send: function (_, completeCallback) {
66
+ counter += 1;
67
+ var message = {
68
+ id: 'postmessage-transport-' + counter
69
+ },
70
+ eventName = 'message.' + message.id;
71
+ iframe = $(
72
+ '<iframe style="display:none;" src="' +
73
+ options.postMessage + '" name="' +
74
+ message.id + '"></iframe>'
75
+ ).bind('load', function () {
76
+ $.each(names, function (i, name) {
77
+ message[name] = options[name];
78
+ });
79
+ message.dataType = message.dataType.replace('postmessage ', '');
80
+ $(window).bind(eventName, function (e) {
81
+ e = e.originalEvent;
82
+ var data = e.data,
83
+ ev;
84
+ if (e.origin === target && data.id === message.id) {
85
+ if (data.type === 'progress') {
86
+ ev = document.createEvent('Event');
87
+ ev.initEvent(data.type, false, true);
88
+ $.extend(ev, data);
89
+ xhrUpload.dispatchEvent(ev);
90
+ } else {
91
+ completeCallback(
92
+ data.status,
93
+ data.statusText,
94
+ {postmessage: data.result},
95
+ data.headers
96
+ );
97
+ iframe.remove();
98
+ $(window).unbind(eventName);
99
+ }
100
+ }
101
+ });
102
+ iframe[0].contentWindow.postMessage(
103
+ message,
104
+ target
105
+ );
106
+ }).appendTo(document.body);
107
+ },
108
+ abort: function () {
109
+ if (iframe) {
110
+ iframe.remove();
111
+ }
112
+ }
113
+ };
114
+ }
115
+ });
116
+
117
+ }));
@@ -0,0 +1,86 @@
1
+ /*
2
+ * jQuery XDomainRequest Transport Plugin 1.1.3
3
+ * https://github.com/blueimp/jQuery-File-Upload
4
+ *
5
+ * Copyright 2011, Sebastian Tschan
6
+ * https://blueimp.net
7
+ *
8
+ * Licensed under the MIT license:
9
+ * http://www.opensource.org/licenses/MIT
10
+ *
11
+ * Based on Julian Aubourg's ajaxHooks xdr.js:
12
+ * https://github.com/jaubourg/ajaxHooks/
13
+ */
14
+
15
+ /* global define, window, XDomainRequest */
16
+
17
+ (function (factory) {
18
+ 'use strict';
19
+ if (typeof define === 'function' && define.amd) {
20
+ // Register as an anonymous AMD module:
21
+ define(['jquery'], factory);
22
+ } else {
23
+ // Browser globals:
24
+ factory(window.jQuery);
25
+ }
26
+ }(function ($) {
27
+ 'use strict';
28
+ if (window.XDomainRequest && !$.support.cors) {
29
+ $.ajaxTransport(function (s) {
30
+ if (s.crossDomain && s.async) {
31
+ if (s.timeout) {
32
+ s.xdrTimeout = s.timeout;
33
+ delete s.timeout;
34
+ }
35
+ var xdr;
36
+ return {
37
+ send: function (headers, completeCallback) {
38
+ var addParamChar = /\?/.test(s.url) ? '&' : '?';
39
+ function callback(status, statusText, responses, responseHeaders) {
40
+ xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
41
+ xdr = null;
42
+ completeCallback(status, statusText, responses, responseHeaders);
43
+ }
44
+ xdr = new XDomainRequest();
45
+ // XDomainRequest only supports GET and POST:
46
+ if (s.type === 'DELETE') {
47
+ s.url = s.url + addParamChar + '_method=DELETE';
48
+ s.type = 'POST';
49
+ } else if (s.type === 'PUT') {
50
+ s.url = s.url + addParamChar + '_method=PUT';
51
+ s.type = 'POST';
52
+ } else if (s.type === 'PATCH') {
53
+ s.url = s.url + addParamChar + '_method=PATCH';
54
+ s.type = 'POST';
55
+ }
56
+ xdr.open(s.type, s.url);
57
+ xdr.onload = function () {
58
+ callback(
59
+ 200,
60
+ 'OK',
61
+ {text: xdr.responseText},
62
+ 'Content-Type: ' + xdr.contentType
63
+ );
64
+ };
65
+ xdr.onerror = function () {
66
+ callback(404, 'Not Found');
67
+ };
68
+ if (s.xdrTimeout) {
69
+ xdr.ontimeout = function () {
70
+ callback(0, 'timeout');
71
+ };
72
+ xdr.timeout = s.xdrTimeout;
73
+ }
74
+ xdr.send((s.hasContent && s.data) || null);
75
+ },
76
+ abort: function () {
77
+ if (xdr) {
78
+ xdr.onerror = $.noop();
79
+ xdr.abort();
80
+ }
81
+ }
82
+ };
83
+ }
84
+ });
85
+ }
86
+ }));
@@ -0,0 +1,429 @@
1
+ /*
2
+ * jQuery File Upload AngularJS Plugin 2.2.0
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
+ /* jshint nomen:false */
13
+ /* global define, angular */
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
+ 'angular',
22
+ './jquery.fileupload-image',
23
+ './jquery.fileupload-audio',
24
+ './jquery.fileupload-video',
25
+ './jquery.fileupload-validate'
26
+ ], factory);
27
+ } else {
28
+ factory();
29
+ }
30
+ }(function () {
31
+ 'use strict';
32
+
33
+ angular.module('blueimp.fileupload', [])
34
+
35
+ // The fileUpload service provides configuration options
36
+ // for the fileUpload directive and default handlers for
37
+ // File Upload events:
38
+ .provider('fileUpload', function () {
39
+ var scopeEvalAsync = function (expression) {
40
+ var scope = angular.element(this)
41
+ .fileupload('option', 'scope');
42
+ // Schedule a new $digest cycle if not already inside of one
43
+ // and evaluate the given expression:
44
+ scope.$evalAsync(expression);
45
+ },
46
+ addFileMethods = function (scope, data) {
47
+ var files = data.files,
48
+ file = files[0];
49
+ angular.forEach(files, function (file, index) {
50
+ file._index = index;
51
+ file.$state = function () {
52
+ return data.state();
53
+ };
54
+ file.$processing = function () {
55
+ return data.processing();
56
+ };
57
+ file.$progress = function () {
58
+ return data.progress();
59
+ };
60
+ file.$response = function () {
61
+ return data.response();
62
+ };
63
+ });
64
+ file.$submit = function () {
65
+ if (!file.error) {
66
+ return data.submit();
67
+ }
68
+ };
69
+ file.$cancel = function () {
70
+ return data.abort();
71
+ };
72
+ },
73
+ $config;
74
+ $config = this.defaults = {
75
+ handleResponse: function (e, data) {
76
+ var files = data.result && data.result.files;
77
+ if (files) {
78
+ data.scope.replace(data.files, files);
79
+ } else if (data.errorThrown ||
80
+ data.textStatus === 'error') {
81
+ data.files[0].error = data.errorThrown ||
82
+ data.textStatus;
83
+ }
84
+ },
85
+ add: function (e, data) {
86
+ if (e.isDefaultPrevented()) {
87
+ return false;
88
+ }
89
+ var scope = data.scope,
90
+ filesCopy = [];
91
+ angular.forEach(data.files, function (file) {
92
+ filesCopy.push(file);
93
+ });
94
+ scope.$apply(function () {
95
+ addFileMethods(scope, data);
96
+ var method = scope.option('prependFiles') ?
97
+ 'unshift' : 'push';
98
+ Array.prototype[method].apply(scope.queue, data.files);
99
+ });
100
+ data.process(function () {
101
+ return scope.process(data);
102
+ }).always(function () {
103
+ scope.$apply(function () {
104
+ addFileMethods(scope, data);
105
+ scope.replace(filesCopy, data.files);
106
+ });
107
+ }).then(function () {
108
+ if ((scope.option('autoUpload') ||
109
+ data.autoUpload) &&
110
+ data.autoUpload !== false) {
111
+ data.submit();
112
+ }
113
+ });
114
+ },
115
+ progress: function (e, data) {
116
+ if (e.isDefaultPrevented()) {
117
+ return false;
118
+ }
119
+ data.scope.$apply();
120
+ },
121
+ done: function (e, data) {
122
+ if (e.isDefaultPrevented()) {
123
+ return false;
124
+ }
125
+ var that = this;
126
+ data.scope.$apply(function () {
127
+ data.handleResponse.call(that, e, data);
128
+ });
129
+ },
130
+ fail: function (e, data) {
131
+ if (e.isDefaultPrevented()) {
132
+ return false;
133
+ }
134
+ var that = this,
135
+ scope = data.scope;
136
+ if (data.errorThrown === 'abort') {
137
+ scope.clear(data.files);
138
+ return;
139
+ }
140
+ scope.$apply(function () {
141
+ data.handleResponse.call(that, e, data);
142
+ });
143
+ },
144
+ stop: scopeEvalAsync,
145
+ processstart: scopeEvalAsync,
146
+ processstop: scopeEvalAsync,
147
+ getNumberOfFiles: function () {
148
+ var scope = this.scope;
149
+ return scope.queue.length - scope.processing();
150
+ },
151
+ dataType: 'json',
152
+ autoUpload: false
153
+ };
154
+ this.$get = [
155
+ function () {
156
+ return {
157
+ defaults: $config
158
+ };
159
+ }
160
+ ];
161
+ })
162
+
163
+ // Format byte numbers to readable presentations:
164
+ .provider('formatFileSizeFilter', function () {
165
+ var $config = {
166
+ // Byte units following the IEC format
167
+ // http://en.wikipedia.org/wiki/Kilobyte
168
+ units: [
169
+ {size: 1000000000, suffix: ' GB'},
170
+ {size: 1000000, suffix: ' MB'},
171
+ {size: 1000, suffix: ' KB'}
172
+ ]
173
+ };
174
+ this.defaults = $config;
175
+ this.$get = function () {
176
+ return function (bytes) {
177
+ if (!angular.isNumber(bytes)) {
178
+ return '';
179
+ }
180
+ var unit = true,
181
+ i = 0,
182
+ prefix,
183
+ suffix;
184
+ while (unit) {
185
+ unit = $config.units[i];
186
+ prefix = unit.prefix || '';
187
+ suffix = unit.suffix || '';
188
+ if (i === $config.units.length - 1 || bytes >= unit.size) {
189
+ return prefix + (bytes / unit.size).toFixed(2) + suffix;
190
+ }
191
+ i += 1;
192
+ }
193
+ };
194
+ };
195
+ })
196
+
197
+ // The FileUploadController initializes the fileupload widget and
198
+ // provides scope methods to control the File Upload functionality:
199
+ .controller('FileUploadController', [
200
+ '$scope', '$element', '$attrs', '$window', 'fileUpload',
201
+ function ($scope, $element, $attrs, $window, fileUpload) {
202
+ var uploadMethods = {
203
+ progress: function () {
204
+ return $element.fileupload('progress');
205
+ },
206
+ active: function () {
207
+ return $element.fileupload('active');
208
+ },
209
+ option: function (option, data) {
210
+ if (arguments.length === 1) {
211
+ return $element.fileupload('option', option);
212
+ }
213
+ $element.fileupload('option', option, data);
214
+ },
215
+ add: function (data) {
216
+ return $element.fileupload('add', data);
217
+ },
218
+ send: function (data) {
219
+ return $element.fileupload('send', data);
220
+ },
221
+ process: function (data) {
222
+ return $element.fileupload('process', data);
223
+ },
224
+ processing: function (data) {
225
+ return $element.fileupload('processing', data);
226
+ }
227
+ };
228
+ $scope.disabled = !$window.jQuery.support.fileInput;
229
+ $scope.queue = $scope.queue || [];
230
+ $scope.clear = function (files) {
231
+ var queue = this.queue,
232
+ i = queue.length,
233
+ file = files,
234
+ length = 1;
235
+ if (angular.isArray(files)) {
236
+ file = files[0];
237
+ length = files.length;
238
+ }
239
+ while (i) {
240
+ i -= 1;
241
+ if (queue[i] === file) {
242
+ return queue.splice(i, length);
243
+ }
244
+ }
245
+ };
246
+ $scope.replace = function (oldFiles, newFiles) {
247
+ var queue = this.queue,
248
+ file = oldFiles[0],
249
+ i,
250
+ j;
251
+ for (i = 0; i < queue.length; i += 1) {
252
+ if (queue[i] === file) {
253
+ for (j = 0; j < newFiles.length; j += 1) {
254
+ queue[i + j] = newFiles[j];
255
+ }
256
+ return;
257
+ }
258
+ }
259
+ };
260
+ $scope.applyOnQueue = function (method) {
261
+ var list = this.queue.slice(0),
262
+ i,
263
+ file;
264
+ for (i = 0; i < list.length; i += 1) {
265
+ file = list[i];
266
+ if (file[method]) {
267
+ file[method]();
268
+ }
269
+ }
270
+ };
271
+ $scope.submit = function () {
272
+ this.applyOnQueue('$submit');
273
+ };
274
+ $scope.cancel = function () {
275
+ this.applyOnQueue('$cancel');
276
+ };
277
+ // Add upload methods to the scope:
278
+ angular.extend($scope, uploadMethods);
279
+ // The fileupload widget will initialize with
280
+ // the options provided via "data-"-parameters,
281
+ // as well as those given via options object:
282
+ $element.fileupload(angular.extend(
283
+ {scope: $scope},
284
+ fileUpload.defaults
285
+ )).on('fileuploadadd', function (e, data) {
286
+ data.scope = $scope;
287
+ }).on('fileuploadfail', function (e, data) {
288
+ if (data.errorThrown === 'abort') {
289
+ return;
290
+ }
291
+ if (data.dataType &&
292
+ data.dataType.indexOf('json') === data.dataType.length - 4) {
293
+ try {
294
+ data.result = angular.fromJson(data.jqXHR.responseText);
295
+ } catch (ignore) {}
296
+ }
297
+ }).on([
298
+ 'fileuploadadd',
299
+ 'fileuploadsubmit',
300
+ 'fileuploadsend',
301
+ 'fileuploaddone',
302
+ 'fileuploadfail',
303
+ 'fileuploadalways',
304
+ 'fileuploadprogress',
305
+ 'fileuploadprogressall',
306
+ 'fileuploadstart',
307
+ 'fileuploadstop',
308
+ 'fileuploadchange',
309
+ 'fileuploadpaste',
310
+ 'fileuploaddrop',
311
+ 'fileuploaddragover',
312
+ 'fileuploadchunksend',
313
+ 'fileuploadchunkdone',
314
+ 'fileuploadchunkfail',
315
+ 'fileuploadchunkalways',
316
+ 'fileuploadprocessstart',
317
+ 'fileuploadprocess',
318
+ 'fileuploadprocessdone',
319
+ 'fileuploadprocessfail',
320
+ 'fileuploadprocessalways',
321
+ 'fileuploadprocessstop'
322
+ ].join(' '), function (e, data) {
323
+ if ($scope.$emit(e.type, data).defaultPrevented) {
324
+ e.preventDefault();
325
+ }
326
+ }).on('remove', function () {
327
+ // Remove upload methods from the scope,
328
+ // when the widget is removed:
329
+ var method;
330
+ for (method in uploadMethods) {
331
+ if (uploadMethods.hasOwnProperty(method)) {
332
+ delete $scope[method];
333
+ }
334
+ }
335
+ });
336
+ // Observe option changes:
337
+ $scope.$watch(
338
+ $attrs.fileUpload,
339
+ function (newOptions) {
340
+ if (newOptions) {
341
+ $element.fileupload('option', newOptions);
342
+ }
343
+ }
344
+ );
345
+ }
346
+ ])
347
+
348
+ // Provide File Upload progress feedback:
349
+ .controller('FileUploadProgressController', [
350
+ '$scope', '$attrs', '$parse',
351
+ function ($scope, $attrs, $parse) {
352
+ var fn = $parse($attrs.fileUploadProgress),
353
+ update = function () {
354
+ var progress = fn($scope);
355
+ if (!progress || !progress.total) {
356
+ return;
357
+ }
358
+ $scope.num = Math.floor(
359
+ progress.loaded / progress.total * 100
360
+ );
361
+ };
362
+ update();
363
+ $scope.$watch(
364
+ $attrs.fileUploadProgress + '.loaded',
365
+ function (newValue, oldValue) {
366
+ if (newValue !== oldValue) {
367
+ update();
368
+ }
369
+ }
370
+ );
371
+ }
372
+ ])
373
+
374
+ // Display File Upload previews:
375
+ .controller('FileUploadPreviewController', [
376
+ '$scope', '$element', '$attrs',
377
+ function ($scope, $element, $attrs) {
378
+ $scope.$watch(
379
+ $attrs.fileUploadPreview + '.preview',
380
+ function (preview) {
381
+ $element.empty();
382
+ if (preview) {
383
+ $element.append(preview);
384
+ }
385
+ }
386
+ );
387
+ }
388
+ ])
389
+
390
+ .directive('fileUpload', function () {
391
+ return {
392
+ controller: 'FileUploadController',
393
+ scope: true
394
+ };
395
+ })
396
+
397
+ .directive('fileUploadProgress', function () {
398
+ return {
399
+ controller: 'FileUploadProgressController',
400
+ scope: true
401
+ };
402
+ })
403
+
404
+ .directive('fileUploadPreview', function () {
405
+ return {
406
+ controller: 'FileUploadPreviewController'
407
+ };
408
+ })
409
+
410
+ // Enhance the HTML5 download attribute to
411
+ // allow drag&drop of files to the desktop:
412
+ .directive('download', function () {
413
+ return function (scope, elm) {
414
+ elm.on('dragstart', function (e) {
415
+ try {
416
+ e.originalEvent.dataTransfer.setData(
417
+ 'DownloadURL',
418
+ [
419
+ 'application/octet-stream',
420
+ elm.prop('download'),
421
+ elm.prop('href')
422
+ ].join(':')
423
+ );
424
+ } catch (ignore) {}
425
+ });
426
+ };
427
+ });
428
+
429
+ }));