sn-jquery-fileupload-rails 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +95 -0
  3. data/Rakefile +15 -0
  4. data/app/assets/images/loading.gif +0 -0
  5. data/app/assets/images/progressbar.gif +0 -0
  6. data/app/assets/javascripts/jquery-fileupload/angularjs.js +12 -0
  7. data/app/assets/javascripts/jquery-fileupload/basic-plus.js +11 -0
  8. data/app/assets/javascripts/jquery-fileupload/basic.js +3 -0
  9. data/app/assets/javascripts/jquery-fileupload/cors/jquery.postmessage-transport.js +126 -0
  10. data/app/assets/javascripts/jquery-fileupload/cors/jquery.xdr-transport.js +89 -0
  11. data/app/assets/javascripts/jquery-fileupload/index.js +13 -0
  12. data/app/assets/javascripts/jquery-fileupload/jquery-ui.js +13 -0
  13. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-angular.js +425 -0
  14. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-audio.js +112 -0
  15. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-image.js +320 -0
  16. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-jquery-ui.js +155 -0
  17. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-process.js +175 -0
  18. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-ui.js +710 -0
  19. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-validate.js +122 -0
  20. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload-video.js +112 -0
  21. data/app/assets/javascripts/jquery-fileupload/jquery.fileupload.js +1477 -0
  22. data/app/assets/javascripts/jquery-fileupload/jquery.iframe-transport.js +217 -0
  23. data/app/assets/javascripts/jquery-fileupload/locale.js +29 -0
  24. data/app/assets/javascripts/jquery-fileupload/vendor/canvas-to-blob.js +95 -0
  25. data/app/assets/javascripts/jquery-fileupload/vendor/jquery.ui.widget.js +572 -0
  26. data/app/assets/javascripts/jquery-fileupload/vendor/load-image.all.min.js +1 -0
  27. data/app/assets/javascripts/jquery-fileupload/vendor/tmpl.js +87 -0
  28. data/app/assets/stylesheets/jquery.fileupload-noscript.scss +22 -0
  29. data/app/assets/stylesheets/jquery.fileupload-ui-noscript.scss +17 -0
  30. data/app/assets/stylesheets/jquery.fileupload-ui.scss +57 -0
  31. data/app/assets/stylesheets/jquery.fileupload.scss +37 -0
  32. data/lib/jquery-fileupload-rails.rb +8 -0
  33. data/lib/jquery/fileupload/rails/engine.rb +8 -0
  34. data/lib/jquery/fileupload/rails/middleware.rb +59 -0
  35. data/lib/jquery/fileupload/rails/upload.rb +3 -0
  36. data/lib/jquery/fileupload/rails/version.rb +7 -0
  37. metadata +135 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3ef3660ee981a48fb2a1418eafc1125c8fbfa60a5794992e7ddf291300ad0e6
4
+ data.tar.gz: 4f282065b286eebee0f39c417212ccbb84e42894ed497d4ae9ed8ef9f4068ed0
5
+ SHA512:
6
+ metadata.gz: 31876be196934b5abb91cbff98dc74788c7176a7bac7fbc42534c74b20fcc47d7077ae4b835371f32544255f6a257ef9058534985e2e14105b8a86b819352fe2
7
+ data.tar.gz: 536b3d32baa18dfe51328c17cad63aaa69c6664da7fa9a4b51e9526d6098d15ff2c16e8c58a46dacea53e8e1a78b9f0a88a7ad1408364d9cf8b6f1de48c7e716
@@ -0,0 +1,95 @@
1
+ # jQuery File Upload for Rails
2
+
3
+ [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.
4
+
5
+ jquery-fileupload-rails is a library that integrates jQuery File Upload for Rails 3.1 Asset Pipeline (Rails 3.2 supported).
6
+
7
+ ## Plugin versions
8
+
9
+ * jQuery File Upload Plugin v9.12.5
10
+
11
+ ## Installing Gem
12
+
13
+ gem "jquery-fileupload-rails"
14
+
15
+ ## Using the javascripts
16
+
17
+ Require jquery-fileupload in your app/assets/application.js file.
18
+
19
+ //= require jquery-fileupload
20
+
21
+ The snippet above will add the following js files to the manifest file.
22
+
23
+ //= require jquery-fileupload/vendor/jquery.ui.widget
24
+ //= require jquery-fileupload/vendor/tmpl
25
+ //= require jquery-fileupload/vendor/load-image.all.min
26
+ //= require jquery-fileupload/vendor/canvas-to-blob
27
+ //= require jquery-fileupload/jquery.iframe-transport
28
+ //= require jquery-fileupload/jquery.fileupload
29
+ //= require jquery-fileupload/jquery.fileupload-process
30
+ //= require jquery-fileupload/jquery.fileupload-image
31
+ //= require jquery-fileupload/jquery.fileupload-audio
32
+ //= require jquery-fileupload/jquery.fileupload-video
33
+ //= require jquery-fileupload/jquery.fileupload-validate
34
+ //= require jquery-fileupload/jquery.fileupload-ui
35
+ //= require jquery-fileupload/locale
36
+ //= require jquery-fileupload/jquery.fileupload-angular
37
+ //= require jquery-fileupload/jquery.fileupload-jquery-ui
38
+ //= require jquery-fileupload/cors/jquery.postmessage-transport
39
+ //= require jquery-fileupload/cors/jquery.xdr-transport
40
+
41
+ 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)
42
+
43
+ //= require jquery-fileupload/basic
44
+
45
+ The basic setup only includes the following files:
46
+
47
+ //= require jquery-fileupload/vendor/jquery.ui.widget
48
+ //= require jquery-fileupload/jquery.iframe-transport
49
+ //= require jquery-fileupload/jquery.fileupload
50
+
51
+ You can also require the following to get the js from the Basic-Plus, AngularJS and jQuery UI Examples:
52
+
53
+ //= require jquery-fileupload/basic-plus
54
+
55
+ //= require jquery-fileupload/angularjs
56
+
57
+ //= require jquery-fileupload/jquery-ui
58
+
59
+ ## Using the stylesheet
60
+
61
+ Require the stylesheet file to app/assets/stylesheets/application.css
62
+
63
+ *= require jquery.fileupload
64
+ *= require jquery.fileupload-ui
65
+
66
+ There are also noscript styles for Browsers with Javascript disabled, to use them create a noscript.css and add it to your precompile-list and layout inside a noscript tag:
67
+
68
+ *= require jquery.fileupload-noscript
69
+ *= require jquery.fileupload-ui-noscript
70
+
71
+ ## Using the middleware
72
+
73
+ 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.
74
+
75
+ ```ruby
76
+ Rails.application.config.middleware.use JQuery::FileUpload::Rails::Middleware
77
+ ```
78
+
79
+ ## Example apps
80
+ [jquery-fileupload-rails-paperclip-example](https://github.com/tors/jquery-fileupload-rails-paperclip-example): jQuery File Upload in Rails 3.2 with Paperclip and Bootstrap
81
+
82
+ [rails-resumable-jquery-fileupload](https://github.com/vgantchev/rails-resumable-jquery-fileupload): resumable (chunked) uploads with jQuery File Upload in Rails 4.2 using Paperclip
83
+
84
+ You can also check out Ryan Bate's RailsCast [jQuery File Upload episode](http://railscasts.com/episodes/381-jquery-file-upload).
85
+
86
+
87
+ ## Thanks
88
+ Thanks to [Sebastian Tschan](https://github.com/blueimp) for writing an awesome file upload plugin.
89
+
90
+ ## License
91
+ Copyright (c) 2012 Tors Dalid
92
+
93
+ 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:
94
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
95
+ 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.
@@ -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,12 @@
1
+ //= require jquery-fileupload/vendor/jquery.ui.widget
2
+ //= require jquery-fileupload/vendor/load-image.all.min
3
+ //= require jquery-fileupload/vendor/canvas-to-blob
4
+ //= require jquery-fileupload/jquery.iframe-transport
5
+ //= require jquery-fileupload/jquery.fileupload
6
+ //= require jquery-fileupload/jquery.fileupload-process
7
+ //= require jquery-fileupload/jquery.fileupload-image
8
+ //= require jquery-fileupload/jquery.fileupload-audio
9
+ //= require jquery-fileupload/jquery.fileupload-video
10
+ //= require jquery-fileupload/jquery.fileupload-validate
11
+ //= require jquery-fileupload/jquery.fileupload-angular
12
+ //= require jquery-fileupload/locale
@@ -0,0 +1,11 @@
1
+ //= require jquery-fileupload/vendor/jquery.ui.widget
2
+ //= require jquery-fileupload/vendor/load-image.all.min
3
+ //= require jquery-fileupload/vendor/canvas-to-blob
4
+ //= require jquery-fileupload/jquery.iframe-transport
5
+ //= require jquery-fileupload/jquery.fileupload
6
+ //= require jquery-fileupload/jquery.fileupload-process
7
+ //= require jquery-fileupload/jquery.fileupload-image
8
+ //= require jquery-fileupload/jquery.fileupload-audio
9
+ //= require jquery-fileupload/jquery.fileupload-video
10
+ //= require jquery-fileupload/jquery.fileupload-validate
11
+ //= require jquery-fileupload/locale
@@ -0,0 +1,3 @@
1
+ //= require jquery-fileupload/vendor/jquery.ui.widget
2
+ //= require jquery-fileupload/jquery.iframe-transport
3
+ //= require jquery-fileupload/jquery.fileupload
@@ -0,0 +1,126 @@
1
+ /*
2
+ * jQuery postMessage Transport Plugin
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, require, 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 if (typeof exports === 'object') {
20
+ // Node/CommonJS:
21
+ factory(require('jquery'));
22
+ } else {
23
+ // Browser globals:
24
+ factory(window.jQuery);
25
+ }
26
+ }(function ($) {
27
+ 'use strict';
28
+
29
+ var counter = 0,
30
+ names = [
31
+ 'accepts',
32
+ 'cache',
33
+ 'contents',
34
+ 'contentType',
35
+ 'crossDomain',
36
+ 'data',
37
+ 'dataType',
38
+ 'headers',
39
+ 'ifModified',
40
+ 'mimeType',
41
+ 'password',
42
+ 'processData',
43
+ 'timeout',
44
+ 'traditional',
45
+ 'type',
46
+ 'url',
47
+ 'username'
48
+ ],
49
+ convert = function (p) {
50
+ return p;
51
+ };
52
+
53
+ $.ajaxSetup({
54
+ converters: {
55
+ 'postmessage text': convert,
56
+ 'postmessage json': convert,
57
+ 'postmessage html': convert
58
+ }
59
+ });
60
+
61
+ $.ajaxTransport('postmessage', function (options) {
62
+ if (options.postMessage && window.postMessage) {
63
+ var iframe,
64
+ loc = $('<a>').prop('href', options.postMessage)[0],
65
+ target = loc.protocol + '//' + loc.host,
66
+ xhrUpload = options.xhr().upload;
67
+ // IE always includes the port for the host property of a link
68
+ // element, but not in the location.host or origin property for the
69
+ // default http port 80 and https port 443, so we strip it:
70
+ if (/^(http:\/\/.+:80)|(https:\/\/.+:443)$/.test(target)) {
71
+ target = target.replace(/:(80|443)$/, '');
72
+ }
73
+ return {
74
+ send: function (_, completeCallback) {
75
+ counter += 1;
76
+ var message = {
77
+ id: 'postmessage-transport-' + counter
78
+ },
79
+ eventName = 'message.' + message.id;
80
+ iframe = $(
81
+ '<iframe style="display:none;" src="' +
82
+ options.postMessage + '" name="' +
83
+ message.id + '"></iframe>'
84
+ ).bind('load', function () {
85
+ $.each(names, function (i, name) {
86
+ message[name] = options[name];
87
+ });
88
+ message.dataType = message.dataType.replace('postmessage ', '');
89
+ $(window).bind(eventName, function (e) {
90
+ e = e.originalEvent;
91
+ var data = e.data,
92
+ ev;
93
+ if (e.origin === target && data.id === message.id) {
94
+ if (data.type === 'progress') {
95
+ ev = document.createEvent('Event');
96
+ ev.initEvent(data.type, false, true);
97
+ $.extend(ev, data);
98
+ xhrUpload.dispatchEvent(ev);
99
+ } else {
100
+ completeCallback(
101
+ data.status,
102
+ data.statusText,
103
+ {postmessage: data.result},
104
+ data.headers
105
+ );
106
+ iframe.remove();
107
+ $(window).unbind(eventName);
108
+ }
109
+ }
110
+ });
111
+ iframe[0].contentWindow.postMessage(
112
+ message,
113
+ target
114
+ );
115
+ }).appendTo(document.body);
116
+ },
117
+ abort: function () {
118
+ if (iframe) {
119
+ iframe.remove();
120
+ }
121
+ }
122
+ };
123
+ }
124
+ });
125
+
126
+ }));
@@ -0,0 +1,89 @@
1
+ /*
2
+ * jQuery XDomainRequest Transport Plugin
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, require, 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 if (typeof exports === 'object') {
23
+ // Node/CommonJS:
24
+ factory(require('jquery'));
25
+ } else {
26
+ // Browser globals:
27
+ factory(window.jQuery);
28
+ }
29
+ }(function ($) {
30
+ 'use strict';
31
+ if (window.XDomainRequest && !$.support.cors) {
32
+ $.ajaxTransport(function (s) {
33
+ if (s.crossDomain && s.async) {
34
+ if (s.timeout) {
35
+ s.xdrTimeout = s.timeout;
36
+ delete s.timeout;
37
+ }
38
+ var xdr;
39
+ return {
40
+ send: function (headers, completeCallback) {
41
+ var addParamChar = /\?/.test(s.url) ? '&' : '?';
42
+ function callback(status, statusText, responses, responseHeaders) {
43
+ xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
44
+ xdr = null;
45
+ completeCallback(status, statusText, responses, responseHeaders);
46
+ }
47
+ xdr = new XDomainRequest();
48
+ // XDomainRequest only supports GET and POST:
49
+ if (s.type === 'DELETE') {
50
+ s.url = s.url + addParamChar + '_method=DELETE';
51
+ s.type = 'POST';
52
+ } else if (s.type === 'PUT') {
53
+ s.url = s.url + addParamChar + '_method=PUT';
54
+ s.type = 'POST';
55
+ } else if (s.type === 'PATCH') {
56
+ s.url = s.url + addParamChar + '_method=PATCH';
57
+ s.type = 'POST';
58
+ }
59
+ xdr.open(s.type, s.url);
60
+ xdr.onload = function () {
61
+ callback(
62
+ 200,
63
+ 'OK',
64
+ {text: xdr.responseText},
65
+ 'Content-Type: ' + xdr.contentType
66
+ );
67
+ };
68
+ xdr.onerror = function () {
69
+ callback(404, 'Not Found');
70
+ };
71
+ if (s.xdrTimeout) {
72
+ xdr.ontimeout = function () {
73
+ callback(0, 'timeout');
74
+ };
75
+ xdr.timeout = s.xdrTimeout;
76
+ }
77
+ xdr.send((s.hasContent && s.data) || null);
78
+ },
79
+ abort: function () {
80
+ if (xdr) {
81
+ xdr.onerror = $.noop();
82
+ xdr.abort();
83
+ }
84
+ }
85
+ };
86
+ }
87
+ });
88
+ }
89
+ }));
@@ -0,0 +1,13 @@
1
+ //= require jquery-fileupload/vendor/jquery.ui.widget
2
+ //= require jquery-fileupload/vendor/tmpl
3
+ //= require jquery-fileupload/vendor/load-image.all.min
4
+ //= require jquery-fileupload/vendor/canvas-to-blob
5
+ //= require jquery-fileupload/jquery.iframe-transport
6
+ //= require jquery-fileupload/jquery.fileupload
7
+ //= require jquery-fileupload/jquery.fileupload-process
8
+ //= require jquery-fileupload/jquery.fileupload-image
9
+ //= require jquery-fileupload/jquery.fileupload-audio
10
+ //= require jquery-fileupload/jquery.fileupload-video
11
+ //= require jquery-fileupload/jquery.fileupload-validate
12
+ //= require jquery-fileupload/jquery.fileupload-ui
13
+ //= require jquery-fileupload/locale
@@ -0,0 +1,13 @@
1
+ //= require jquery-fileupload/vendor/tmpl
2
+ //= require jquery-fileupload/vendor/load-image.all.min
3
+ //= require jquery-fileupload/vendor/canvas-to-blob
4
+ //= require jquery-fileupload/jquery.iframe-transport
5
+ //= require jquery-fileupload/jquery.fileupload
6
+ //= require jquery-fileupload/jquery.fileupload-process
7
+ //= require jquery-fileupload/jquery.fileupload-image
8
+ //= require jquery-fileupload/jquery.fileupload-audio
9
+ //= require jquery-fileupload/jquery.fileupload-video
10
+ //= require jquery-fileupload/jquery.fileupload-validate
11
+ //= require jquery-fileupload/jquery.fileupload-ui
12
+ //= require jquery-fileupload/jquery.fileupload-jquery-ui
13
+ //= require jquery-fileupload/locale
@@ -0,0 +1,425 @@
1
+ /*
2
+ * jQuery File Upload AngularJS Plugin
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.$parent.$applyAsync(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.$parent.$applyAsync(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
+ done: function (e, data) {
116
+ if (e.isDefaultPrevented()) {
117
+ return false;
118
+ }
119
+ var that = this;
120
+ data.scope.$apply(function () {
121
+ data.handleResponse.call(that, e, data);
122
+ });
123
+ },
124
+ fail: function (e, data) {
125
+ if (e.isDefaultPrevented()) {
126
+ return false;
127
+ }
128
+ var that = this,
129
+ scope = data.scope;
130
+ if (data.errorThrown === 'abort') {
131
+ scope.clear(data.files);
132
+ return;
133
+ }
134
+ scope.$apply(function () {
135
+ data.handleResponse.call(that, e, data);
136
+ });
137
+ },
138
+ stop: scopeEvalAsync,
139
+ processstart: scopeEvalAsync,
140
+ processstop: scopeEvalAsync,
141
+ getNumberOfFiles: function () {
142
+ var scope = this.scope;
143
+ return scope.queue.length - scope.processing();
144
+ },
145
+ dataType: 'json',
146
+ autoUpload: false
147
+ };
148
+ this.$get = [
149
+ function () {
150
+ return {
151
+ defaults: $config
152
+ };
153
+ }
154
+ ];
155
+ })
156
+
157
+ // Format byte numbers to readable presentations:
158
+ .provider('formatFileSizeFilter', function () {
159
+ var $config = {
160
+ // Byte units following the IEC format
161
+ // http://en.wikipedia.org/wiki/Kilobyte
162
+ units: [
163
+ {size: 1000000000, suffix: ' GB'},
164
+ {size: 1000000, suffix: ' MB'},
165
+ {size: 1000, suffix: ' KB'}
166
+ ]
167
+ };
168
+ this.defaults = $config;
169
+ this.$get = function () {
170
+ return function (bytes) {
171
+ if (!angular.isNumber(bytes)) {
172
+ return '';
173
+ }
174
+ var unit = true,
175
+ i = 0,
176
+ prefix,
177
+ suffix;
178
+ while (unit) {
179
+ unit = $config.units[i];
180
+ prefix = unit.prefix || '';
181
+ suffix = unit.suffix || '';
182
+ if (i === $config.units.length - 1 || bytes >= unit.size) {
183
+ return prefix + (bytes / unit.size).toFixed(2) + suffix;
184
+ }
185
+ i += 1;
186
+ }
187
+ };
188
+ };
189
+ })
190
+
191
+ // The FileUploadController initializes the fileupload widget and
192
+ // provides scope methods to control the File Upload functionality:
193
+ .controller('FileUploadController', [
194
+ '$scope', '$element', '$attrs', '$window', 'fileUpload',
195
+ function ($scope, $element, $attrs, $window, fileUpload) {
196
+ var uploadMethods = {
197
+ progress: function () {
198
+ return $element.fileupload('progress');
199
+ },
200
+ active: function () {
201
+ return $element.fileupload('active');
202
+ },
203
+ option: function (option, data) {
204
+ if (arguments.length === 1) {
205
+ return $element.fileupload('option', option);
206
+ }
207
+ $element.fileupload('option', option, data);
208
+ },
209
+ add: function (data) {
210
+ return $element.fileupload('add', data);
211
+ },
212
+ send: function (data) {
213
+ return $element.fileupload('send', data);
214
+ },
215
+ process: function (data) {
216
+ return $element.fileupload('process', data);
217
+ },
218
+ processing: function (data) {
219
+ return $element.fileupload('processing', data);
220
+ }
221
+ };
222
+ $scope.disabled = !$window.jQuery.support.fileInput;
223
+ $scope.queue = $scope.queue || [];
224
+ $scope.clear = function (files) {
225
+ var queue = this.queue,
226
+ i = queue.length,
227
+ file = files,
228
+ length = 1;
229
+ if (angular.isArray(files)) {
230
+ file = files[0];
231
+ length = files.length;
232
+ }
233
+ while (i) {
234
+ i -= 1;
235
+ if (queue[i] === file) {
236
+ return queue.splice(i, length);
237
+ }
238
+ }
239
+ };
240
+ $scope.replace = function (oldFiles, newFiles) {
241
+ var queue = this.queue,
242
+ file = oldFiles[0],
243
+ i,
244
+ j;
245
+ for (i = 0; i < queue.length; i += 1) {
246
+ if (queue[i] === file) {
247
+ for (j = 0; j < newFiles.length; j += 1) {
248
+ queue[i + j] = newFiles[j];
249
+ }
250
+ return;
251
+ }
252
+ }
253
+ };
254
+ $scope.applyOnQueue = function (method) {
255
+ var list = this.queue.slice(0),
256
+ i,
257
+ file;
258
+ for (i = 0; i < list.length; i += 1) {
259
+ file = list[i];
260
+ if (file[method]) {
261
+ file[method]();
262
+ }
263
+ }
264
+ };
265
+ $scope.submit = function () {
266
+ this.applyOnQueue('$submit');
267
+ };
268
+ $scope.cancel = function () {
269
+ this.applyOnQueue('$cancel');
270
+ };
271
+ // Add upload methods to the scope:
272
+ angular.extend($scope, uploadMethods);
273
+ // The fileupload widget will initialize with
274
+ // the options provided via "data-"-parameters,
275
+ // as well as those given via options object:
276
+ $element.fileupload(angular.extend(
277
+ {scope: $scope},
278
+ fileUpload.defaults
279
+ )).on('fileuploadadd', function (e, data) {
280
+ data.scope = $scope;
281
+ }).on('fileuploadfail', function (e, data) {
282
+ if (data.errorThrown === 'abort') {
283
+ return;
284
+ }
285
+ if (data.dataType &&
286
+ data.dataType.indexOf('json') === data.dataType.length - 4) {
287
+ try {
288
+ data.result = angular.fromJson(data.jqXHR.responseText);
289
+ } catch (ignore) {}
290
+ }
291
+ }).on([
292
+ 'fileuploadadd',
293
+ 'fileuploadsubmit',
294
+ 'fileuploadsend',
295
+ 'fileuploaddone',
296
+ 'fileuploadfail',
297
+ 'fileuploadalways',
298
+ 'fileuploadprogress',
299
+ 'fileuploadprogressall',
300
+ 'fileuploadstart',
301
+ 'fileuploadstop',
302
+ 'fileuploadchange',
303
+ 'fileuploadpaste',
304
+ 'fileuploaddrop',
305
+ 'fileuploaddragover',
306
+ 'fileuploadchunksend',
307
+ 'fileuploadchunkdone',
308
+ 'fileuploadchunkfail',
309
+ 'fileuploadchunkalways',
310
+ 'fileuploadprocessstart',
311
+ 'fileuploadprocess',
312
+ 'fileuploadprocessdone',
313
+ 'fileuploadprocessfail',
314
+ 'fileuploadprocessalways',
315
+ 'fileuploadprocessstop'
316
+ ].join(' '), function (e, data) {
317
+ $scope.$parent.$applyAsync(function () {
318
+ if ($scope.$emit(e.type, data).defaultPrevented) {
319
+ e.preventDefault();
320
+ }
321
+ });
322
+ }).on('remove', function () {
323
+ // Remove upload methods from the scope,
324
+ // when the widget is removed:
325
+ var method;
326
+ for (method in uploadMethods) {
327
+ if (uploadMethods.hasOwnProperty(method)) {
328
+ delete $scope[method];
329
+ }
330
+ }
331
+ });
332
+ // Observe option changes:
333
+ $scope.$watch(
334
+ $attrs.fileUpload,
335
+ function (newOptions) {
336
+ if (newOptions) {
337
+ $element.fileupload('option', newOptions);
338
+ }
339
+ }
340
+ );
341
+ }
342
+ ])
343
+
344
+ // Provide File Upload progress feedback:
345
+ .controller('FileUploadProgressController', [
346
+ '$scope', '$attrs', '$parse',
347
+ function ($scope, $attrs, $parse) {
348
+ var fn = $parse($attrs.fileUploadProgress),
349
+ update = function () {
350
+ var progress = fn($scope);
351
+ if (!progress || !progress.total) {
352
+ return;
353
+ }
354
+ $scope.num = Math.floor(
355
+ progress.loaded / progress.total * 100
356
+ );
357
+ };
358
+ update();
359
+ $scope.$watch(
360
+ $attrs.fileUploadProgress + '.loaded',
361
+ function (newValue, oldValue) {
362
+ if (newValue !== oldValue) {
363
+ update();
364
+ }
365
+ }
366
+ );
367
+ }
368
+ ])
369
+
370
+ // Display File Upload previews:
371
+ .controller('FileUploadPreviewController', [
372
+ '$scope', '$element', '$attrs',
373
+ function ($scope, $element, $attrs) {
374
+ $scope.$watch(
375
+ $attrs.fileUploadPreview + '.preview',
376
+ function (preview) {
377
+ $element.empty();
378
+ if (preview) {
379
+ $element.append(preview);
380
+ }
381
+ }
382
+ );
383
+ }
384
+ ])
385
+
386
+ .directive('fileUpload', function () {
387
+ return {
388
+ controller: 'FileUploadController',
389
+ scope: true
390
+ };
391
+ })
392
+
393
+ .directive('fileUploadProgress', function () {
394
+ return {
395
+ controller: 'FileUploadProgressController',
396
+ scope: true
397
+ };
398
+ })
399
+
400
+ .directive('fileUploadPreview', function () {
401
+ return {
402
+ controller: 'FileUploadPreviewController'
403
+ };
404
+ })
405
+
406
+ // Enhance the HTML5 download attribute to
407
+ // allow drag&drop of files to the desktop:
408
+ .directive('download', function () {
409
+ return function (scope, elm) {
410
+ elm.on('dragstart', function (e) {
411
+ try {
412
+ e.originalEvent.dataTransfer.setData(
413
+ 'DownloadURL',
414
+ [
415
+ 'application/octet-stream',
416
+ elm.prop('download'),
417
+ elm.prop('href')
418
+ ].join(':')
419
+ );
420
+ } catch (ignore) {}
421
+ });
422
+ };
423
+ });
424
+
425
+ }));