JCG-jquery-fileupload 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +72 -0
- data/Rakefile +15 -0
- data/lib/jquery-fileupload-rails.rb +8 -0
- data/lib/jquery/fileupload/rails/engine.rb +8 -0
- data/lib/jquery/fileupload/rails/middleware.rb +59 -0
- data/lib/jquery/fileupload/rails/upload.rb +3 -0
- data/lib/jquery/fileupload/rails/version.rb +7 -0
- data/vendor/assets/images/loading.gif +0 -0
- data/vendor/assets/images/progressbar.gif +0 -0
- data/vendor/assets/javascripts/jquery-fileupload/basic.js +4 -0
- data/vendor/assets/javascripts/jquery-fileupload/cors/jquery.postmessage-transport.js +117 -0
- data/vendor/assets/javascripts/jquery-fileupload/cors/jquery.xdr-transport.js +87 -0
- data/vendor/assets/javascripts/jquery-fileupload/index.js +9 -0
- data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-fp.js +223 -0
- data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload-ui.js +799 -0
- data/vendor/assets/javascripts/jquery-fileupload/jquery.fileupload.js +1164 -0
- data/vendor/assets/javascripts/jquery-fileupload/jquery.iframe-transport.js +185 -0
- data/vendor/assets/javascripts/jquery-fileupload/locale.js +29 -0
- data/vendor/assets/javascripts/jquery-fileupload/vendor/canvas-to-blob.js +91 -0
- data/vendor/assets/javascripts/jquery-fileupload/vendor/jquery.ui.widget.js +530 -0
- data/vendor/assets/javascripts/jquery-fileupload/vendor/load-image.js +121 -0
- data/vendor/assets/javascripts/jquery-fileupload/vendor/tmpl.js +86 -0
- data/vendor/assets/stylesheets/jquery.fileupload-ui.scss +84 -0
- metadata +117 -0
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
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 User Interface Plugin 6.11
|
10
|
+
* jQuery File Upload Plugin 5.19.3
|
11
|
+
* jQuery UI Widget 1.9.1+amd
|
12
|
+
|
13
|
+
## Installing Gem
|
14
|
+
|
15
|
+
gem "jquery-fileupload-rails"
|
16
|
+
|
17
|
+
## Using the javascripts
|
18
|
+
|
19
|
+
Require jquery-fileupload in your app/assets/application.js file.
|
20
|
+
|
21
|
+
//= require jquery-fileupload
|
22
|
+
|
23
|
+
The snippet above will add the following js files to the mainfest file.
|
24
|
+
|
25
|
+
//=require jquery-fileupload/vendor/jquery.ui.widget
|
26
|
+
//=require jquery-fileupload/vendor/load-image
|
27
|
+
//=require jquery-fileupload/vendor/canvas-to-blob
|
28
|
+
//=require jquery-fileupload/vendor/tmpl
|
29
|
+
//=require jquery-fileupload/jquery.iframe-transport
|
30
|
+
//=require jquery-fileupload/jquery.fileupload
|
31
|
+
//=require jquery-fileupload/jquery.fileupload-fp
|
32
|
+
//=require jquery-fileupload/jquery.fileupload-ui
|
33
|
+
//=require jquery-fileupload/locale
|
34
|
+
|
35
|
+
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)
|
36
|
+
|
37
|
+
//=require jquery-fileupload/basic
|
38
|
+
|
39
|
+
The basic setup only includes the following files:
|
40
|
+
|
41
|
+
//=require jquery-fileupload/vendor/jquery.ui.widget
|
42
|
+
//=require jquery-fileupload/jquery.iframe-transport
|
43
|
+
//=require jquery-fileupload/jquery.fileupload
|
44
|
+
|
45
|
+
## Using the stylesheet
|
46
|
+
|
47
|
+
Require the stylesheet file to app/assets/stylesheets/application.css
|
48
|
+
|
49
|
+
*= require jquery.fileupload-ui
|
50
|
+
|
51
|
+
## Using the middleware
|
52
|
+
|
53
|
+
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.
|
54
|
+
|
55
|
+
Rails.application.config.middleware.use JQuery::FileUpload::Rails::Middleware
|
56
|
+
|
57
|
+
## [Example app](https://github.com/tors/jquery-fileupload-rails-paperclip-example)
|
58
|
+
This app uses paperclip and twitter-bootstrap-rails
|
59
|
+
|
60
|
+
You can also check out Ryan Bate's RailsCast [jQuery File Upload episode](http://railscasts.com/episodes/381-jquery-file-upload). You will
|
61
|
+
need a pro account to watch it though.
|
62
|
+
|
63
|
+
|
64
|
+
## Thanks
|
65
|
+
Thanks to [Sebastian Tschan](https://github.com/blueimp) for writing an awesome file upload plugin.
|
66
|
+
|
67
|
+
## License
|
68
|
+
Copyright (c) 2012 Tors Dalid
|
69
|
+
|
70
|
+
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:
|
71
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
72
|
+
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,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
|
Binary file
|
Binary file
|
@@ -0,0 +1,117 @@
|
|
1
|
+
/*
|
2
|
+
* jQuery postMessage Transport Plugin 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
|
+
/*jslint unparam: true, nomen: true */
|
13
|
+
/*global define, window, document */
|
14
|
+
|
15
|
+
(function (factory) {
|
16
|
+
'use strict';
|
17
|
+
if (typeof define === 'function' && define.amd) {
|
18
|
+
// Register as an anonymous AMD module:
|
19
|
+
define(['jquery'], factory);
|
20
|
+
} else {
|
21
|
+
// Browser globals:
|
22
|
+
factory(window.jQuery);
|
23
|
+
}
|
24
|
+
}(function ($) {
|
25
|
+
'use strict';
|
26
|
+
|
27
|
+
var counter = 0,
|
28
|
+
names = [
|
29
|
+
'accepts',
|
30
|
+
'cache',
|
31
|
+
'contents',
|
32
|
+
'contentType',
|
33
|
+
'crossDomain',
|
34
|
+
'data',
|
35
|
+
'dataType',
|
36
|
+
'headers',
|
37
|
+
'ifModified',
|
38
|
+
'mimeType',
|
39
|
+
'password',
|
40
|
+
'processData',
|
41
|
+
'timeout',
|
42
|
+
'traditional',
|
43
|
+
'type',
|
44
|
+
'url',
|
45
|
+
'username'
|
46
|
+
],
|
47
|
+
convert = function (p) {
|
48
|
+
return p;
|
49
|
+
};
|
50
|
+
|
51
|
+
$.ajaxSetup({
|
52
|
+
converters: {
|
53
|
+
'postmessage text': convert,
|
54
|
+
'postmessage json': convert,
|
55
|
+
'postmessage html': convert
|
56
|
+
}
|
57
|
+
});
|
58
|
+
|
59
|
+
$.ajaxTransport('postmessage', function (options) {
|
60
|
+
if (options.postMessage && window.postMessage) {
|
61
|
+
var iframe,
|
62
|
+
loc = $('<a>').prop('href', options.postMessage)[0],
|
63
|
+
target = loc.protocol + '//' + loc.host,
|
64
|
+
xhrUpload = options.xhr().upload;
|
65
|
+
return {
|
66
|
+
send: function (_, completeCallback) {
|
67
|
+
var message = {
|
68
|
+
id: 'postmessage-transport-' + (counter += 1)
|
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,87 @@
|
|
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
|
+
/*jslint unparam: true */
|
16
|
+
/*global define, window, XDomainRequest */
|
17
|
+
|
18
|
+
(function (factory) {
|
19
|
+
'use strict';
|
20
|
+
if (typeof define === 'function' && define.amd) {
|
21
|
+
// Register as an anonymous AMD module:
|
22
|
+
define(['jquery'], factory);
|
23
|
+
} else {
|
24
|
+
// Browser globals:
|
25
|
+
factory(window.jQuery);
|
26
|
+
}
|
27
|
+
}(function ($) {
|
28
|
+
'use strict';
|
29
|
+
if (window.XDomainRequest && !$.support.cors) {
|
30
|
+
$.ajaxTransport(function (s) {
|
31
|
+
if (s.crossDomain && s.async) {
|
32
|
+
if (s.timeout) {
|
33
|
+
s.xdrTimeout = s.timeout;
|
34
|
+
delete s.timeout;
|
35
|
+
}
|
36
|
+
var xdr;
|
37
|
+
return {
|
38
|
+
send: function (headers, completeCallback) {
|
39
|
+
var addParamChar = /\?/.test(s.url) ? '&' : '?';
|
40
|
+
function callback(status, statusText, responses, responseHeaders) {
|
41
|
+
xdr.onload = xdr.onerror = xdr.ontimeout = $.noop;
|
42
|
+
xdr = null;
|
43
|
+
completeCallback(status, statusText, responses, responseHeaders);
|
44
|
+
}
|
45
|
+
xdr = new XDomainRequest();
|
46
|
+
// XDomainRequest only supports GET and POST:
|
47
|
+
if (s.type === 'DELETE') {
|
48
|
+
s.url = s.url + addParamChar + '_method=DELETE';
|
49
|
+
s.type = 'POST';
|
50
|
+
} else if (s.type === 'PUT') {
|
51
|
+
s.url = s.url + addParamChar + '_method=PUT';
|
52
|
+
s.type = 'POST';
|
53
|
+
} else if (s.type === 'PATCH') {
|
54
|
+
s.url = s.url + addParamChar + '_method=PATCH';
|
55
|
+
s.type = 'POST';
|
56
|
+
}
|
57
|
+
xdr.open(s.type, s.url);
|
58
|
+
xdr.onload = function () {
|
59
|
+
callback(
|
60
|
+
200,
|
61
|
+
'OK',
|
62
|
+
{text: xdr.responseText},
|
63
|
+
'Content-Type: ' + xdr.contentType
|
64
|
+
);
|
65
|
+
};
|
66
|
+
xdr.onerror = function () {
|
67
|
+
callback(404, 'Not Found');
|
68
|
+
};
|
69
|
+
if (s.xdrTimeout) {
|
70
|
+
xdr.ontimeout = function () {
|
71
|
+
callback(0, 'timeout');
|
72
|
+
};
|
73
|
+
xdr.timeout = s.xdrTimeout;
|
74
|
+
}
|
75
|
+
xdr.send((s.hasContent && s.data) || null);
|
76
|
+
},
|
77
|
+
abort: function () {
|
78
|
+
if (xdr) {
|
79
|
+
xdr.onerror = $.noop();
|
80
|
+
xdr.abort();
|
81
|
+
}
|
82
|
+
}
|
83
|
+
};
|
84
|
+
}
|
85
|
+
});
|
86
|
+
}
|
87
|
+
}));
|
@@ -0,0 +1,9 @@
|
|
1
|
+
//=require jquery-fileupload/vendor/jquery.ui.widget
|
2
|
+
//=require jquery-fileupload/vendor/load-image
|
3
|
+
//=require jquery-fileupload/vendor/canvas-to-blob
|
4
|
+
//=require jquery-fileupload/vendor/tmpl
|
5
|
+
//=require jquery-fileupload/jquery.iframe-transport
|
6
|
+
//=require jquery-fileupload/jquery.fileupload
|
7
|
+
//=require jquery-fileupload/jquery.fileupload-fp
|
8
|
+
//=require jquery-fileupload/jquery.fileupload-ui
|
9
|
+
//=require jquery-fileupload/locale
|
@@ -0,0 +1,223 @@
|
|
1
|
+
/*
|
2
|
+
* jQuery File Upload File Processing Plugin 1.2.1
|
3
|
+
* https://github.com/blueimp/jQuery-File-Upload
|
4
|
+
*
|
5
|
+
* Copyright 2012, 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, regexp: true */
|
13
|
+
/*global define, window, document */
|
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
|
+
'load-image',
|
22
|
+
'canvas-to-blob',
|
23
|
+
'./jquery.fileupload'
|
24
|
+
], factory);
|
25
|
+
} else {
|
26
|
+
// Browser globals:
|
27
|
+
factory(
|
28
|
+
window.jQuery,
|
29
|
+
window.loadImage
|
30
|
+
);
|
31
|
+
}
|
32
|
+
}(function ($, loadImage) {
|
33
|
+
'use strict';
|
34
|
+
|
35
|
+
// The File Upload FP version extends the fileupload widget
|
36
|
+
// with file processing functionality:
|
37
|
+
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
|
38
|
+
|
39
|
+
options: {
|
40
|
+
// The list of file processing actions:
|
41
|
+
process: [
|
42
|
+
/*
|
43
|
+
{
|
44
|
+
action: 'load',
|
45
|
+
fileTypes: /^image\/(gif|jpeg|png)$/,
|
46
|
+
maxFileSize: 20000000 // 20MB
|
47
|
+
},
|
48
|
+
{
|
49
|
+
action: 'resize',
|
50
|
+
maxWidth: 7200,
|
51
|
+
maxHeight: 3600,
|
52
|
+
minWidth: 800,
|
53
|
+
minHeight: 600
|
54
|
+
},
|
55
|
+
{
|
56
|
+
action: 'save'
|
57
|
+
}
|
58
|
+
*/
|
59
|
+
],
|
60
|
+
|
61
|
+
// The add callback is invoked as soon as files are added to the
|
62
|
+
// fileupload widget (via file input selection, drag & drop or add
|
63
|
+
// API call). See the basic file upload widget for more information:
|
64
|
+
add: function (e, data) {
|
65
|
+
$(this).fileupload('process', data).done(function () {
|
66
|
+
data.submit();
|
67
|
+
});
|
68
|
+
}
|
69
|
+
},
|
70
|
+
|
71
|
+
processActions: {
|
72
|
+
// Loads the image given via data.files and data.index
|
73
|
+
// as img element if the browser supports canvas.
|
74
|
+
// Accepts the options fileTypes (regular expression)
|
75
|
+
// and maxFileSize (integer) to limit the files to load:
|
76
|
+
load: function (data, options) {
|
77
|
+
var that = this,
|
78
|
+
file = data.files[data.index],
|
79
|
+
dfd = $.Deferred();
|
80
|
+
if (window.HTMLCanvasElement &&
|
81
|
+
window.HTMLCanvasElement.prototype.toBlob &&
|
82
|
+
($.type(options.maxFileSize) !== 'number' ||
|
83
|
+
file.size < options.maxFileSize) &&
|
84
|
+
(!options.fileTypes ||
|
85
|
+
options.fileTypes.test(file.type))) {
|
86
|
+
loadImage(
|
87
|
+
file,
|
88
|
+
function (img) {
|
89
|
+
if (!img.src) {
|
90
|
+
return dfd.rejectWith(that, [data]);
|
91
|
+
}
|
92
|
+
data.img = img;
|
93
|
+
dfd.resolveWith(that, [data]);
|
94
|
+
}
|
95
|
+
);
|
96
|
+
} else {
|
97
|
+
dfd.rejectWith(that, [data]);
|
98
|
+
}
|
99
|
+
return dfd.promise();
|
100
|
+
},
|
101
|
+
// Resizes the image given as data.img and updates
|
102
|
+
// data.canvas with the resized image as canvas element.
|
103
|
+
// Accepts the options maxWidth, maxHeight, minWidth and
|
104
|
+
// minHeight to scale the given image:
|
105
|
+
resize: function (data, options) {
|
106
|
+
var img = data.img,
|
107
|
+
canvas;
|
108
|
+
options = $.extend({canvas: true}, options);
|
109
|
+
if (img) {
|
110
|
+
canvas = loadImage.scale(img, options);
|
111
|
+
if (canvas.width !== img.width ||
|
112
|
+
canvas.height !== img.height) {
|
113
|
+
data.canvas = canvas;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
return data;
|
117
|
+
},
|
118
|
+
// Saves the processed image given as data.canvas
|
119
|
+
// inplace at data.index of data.files:
|
120
|
+
save: function (data, options) {
|
121
|
+
// Do nothing if no processing has happened:
|
122
|
+
if (!data.canvas) {
|
123
|
+
return data;
|
124
|
+
}
|
125
|
+
var that = this,
|
126
|
+
file = data.files[data.index],
|
127
|
+
name = file.name,
|
128
|
+
dfd = $.Deferred(),
|
129
|
+
callback = function (blob) {
|
130
|
+
if (!blob.name) {
|
131
|
+
if (file.type === blob.type) {
|
132
|
+
blob.name = file.name;
|
133
|
+
} else if (file.name) {
|
134
|
+
blob.name = file.name.replace(
|
135
|
+
/\..+$/,
|
136
|
+
'.' + blob.type.substr(6)
|
137
|
+
);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
// Store the created blob at the position
|
141
|
+
// of the original file in the files list:
|
142
|
+
data.files[data.index] = blob;
|
143
|
+
dfd.resolveWith(that, [data]);
|
144
|
+
};
|
145
|
+
// Use canvas.mozGetAsFile directly, to retain the filename, as
|
146
|
+
// Gecko doesn't support the filename option for FormData.append:
|
147
|
+
if (data.canvas.mozGetAsFile) {
|
148
|
+
callback(data.canvas.mozGetAsFile(
|
149
|
+
(/^image\/(jpeg|png)$/.test(file.type) && name) ||
|
150
|
+
((name && name.replace(/\..+$/, '')) ||
|
151
|
+
'blob') + '.png',
|
152
|
+
file.type
|
153
|
+
));
|
154
|
+
} else {
|
155
|
+
data.canvas.toBlob(callback, file.type);
|
156
|
+
}
|
157
|
+
return dfd.promise();
|
158
|
+
}
|
159
|
+
},
|
160
|
+
|
161
|
+
// Resizes the file at the given index and stores the created blob at
|
162
|
+
// the original position of the files list, returns a Promise object:
|
163
|
+
_processFile: function (files, index, options) {
|
164
|
+
var that = this,
|
165
|
+
dfd = $.Deferred().resolveWith(that, [{
|
166
|
+
files: files,
|
167
|
+
index: index
|
168
|
+
}]),
|
169
|
+
chain = dfd.promise();
|
170
|
+
that._processing += 1;
|
171
|
+
$.each(options.process, function (i, settings) {
|
172
|
+
chain = chain.pipe(function (data) {
|
173
|
+
return that.processActions[settings.action]
|
174
|
+
.call(this, data, settings);
|
175
|
+
});
|
176
|
+
});
|
177
|
+
chain.always(function () {
|
178
|
+
that._processing -= 1;
|
179
|
+
if (that._processing === 0) {
|
180
|
+
that.element
|
181
|
+
.removeClass('fileupload-processing');
|
182
|
+
}
|
183
|
+
});
|
184
|
+
if (that._processing === 1) {
|
185
|
+
that.element.addClass('fileupload-processing');
|
186
|
+
}
|
187
|
+
return chain;
|
188
|
+
},
|
189
|
+
|
190
|
+
// Processes the files given as files property of the data parameter,
|
191
|
+
// returns a Promise object that allows to bind a done handler, which
|
192
|
+
// will be invoked after processing all files (inplace) is done:
|
193
|
+
process: function (data) {
|
194
|
+
var that = this,
|
195
|
+
options = $.extend({}, this.options, data);
|
196
|
+
if (options.process && options.process.length &&
|
197
|
+
this._isXHRUpload(options)) {
|
198
|
+
$.each(data.files, function (index, file) {
|
199
|
+
that._processingQueue = that._processingQueue.pipe(
|
200
|
+
function () {
|
201
|
+
var dfd = $.Deferred();
|
202
|
+
that._processFile(data.files, index, options)
|
203
|
+
.always(function () {
|
204
|
+
dfd.resolveWith(that);
|
205
|
+
});
|
206
|
+
return dfd.promise();
|
207
|
+
}
|
208
|
+
);
|
209
|
+
});
|
210
|
+
}
|
211
|
+
return this._processingQueue;
|
212
|
+
},
|
213
|
+
|
214
|
+
_create: function () {
|
215
|
+
this._super();
|
216
|
+
this._processing = 0;
|
217
|
+
this._processingQueue = $.Deferred().resolveWith(this)
|
218
|
+
.promise();
|
219
|
+
}
|
220
|
+
|
221
|
+
});
|
222
|
+
|
223
|
+
}));
|