progress_upload_field 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Andi Altendorfer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ progress_upload_field (for Rails 3.1)
2
+ =====================
3
+
4
+ _progress_upload_field_ provides a FormHelper function to display all necessary divtags and a javascript
5
+ for progress-bar display while uploading files.
6
+
7
+ Usage
8
+ -----
9
+
10
+ **Gemfile**
11
+
12
+ Add the following line
13
+
14
+ gem 'progress_upload_field', '~> 0.0.1'
15
+
16
+ **application.js**
17
+
18
+ Add the following line
19
+
20
+ //= require progress_upload_field
21
+
22
+ **application.css**
23
+
24
+ Add the following line
25
+
26
+ @import "progress.css.scss";
27
+
28
+
29
+ **Use the gem in your view**
30
+
31
+ Here an example how to use it with HAML. Obviosly it works with html.erm too
32
+
33
+
34
+ = form_for([@posting,@attachment], :html => { :multipart => true, :name => 'new_attachment' } ) do |f|
35
+ .field
36
+ =f.label :file
37
+ =f.file_field :file, :onchange => "fileSelected('new_attachment','attachment_file','#{posting_attachments_path(@posting)}');"
38
+
39
+ .field
40
+ =f.label :submit
41
+ =f.submit t(:submit), :onclick => "uploadFile('new_attachment'); return false;"
42
+ =progress_upload_field('attachment_file')
43
+
44
+ The keywords are 'new_attachment' and 'attachment_file'.
45
+ You can choose any names you want.
46
+ The name of the form should be provided at `:onclick => "uploadFile('your_forms_name')"`
47
+ The name of the model + the name of the upload field should be provided at `progress_upload_field('your_model_your_field')`
48
+
49
+
50
+ Contributing to progress_upload_field
51
+ =====================================
52
+
53
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
54
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
55
+ * Fork the project
56
+ * Start a feature/bugfix branch
57
+ * Commit and push until you are happy with your contribution
58
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
59
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
60
+
61
+ Copyright
62
+ =========
63
+
64
+ Copyright (c) 2011 Andi Altendorfer. See LICENSE.txt for further details.
@@ -0,0 +1,43 @@
1
+ require 'rails'
2
+
3
+ module ProgressUploadField
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+
7
+ desc "This generator installs ProgressUploadField for rails < 3.1"
8
+ def copy_files
9
+ say_status("copying", "NOT IMPLEMENTED YET. Please copy assets manually")
10
+ end
11
+
12
+ #class_option :ui, :type => :boolean, :default => false, :desc => "Include jQueryUI"
13
+ #source_root File.expand_path('../../../../../vendor/assets/javascripts', __FILE__)
14
+ #
15
+ #def remove_prototype
16
+ # Rails::PROTOTYPE_JS.each do |name|
17
+ # remove_file "public/javascripts/#{name}.js"
18
+ # end
19
+ #end
20
+ #
21
+ #def copy_jquery
22
+ # say_status("copying", "jQuery (#{Jquery::Rails::JQUERY_VERSION})", :green)
23
+ # copy_file "jquery.js", "public/javascripts/jquery.js"
24
+ # copy_file "jquery.min.js", "public/javascripts/jquery.min.js"
25
+ #end
26
+ #
27
+ #def copy_jquery_ui
28
+ # if options.ui?
29
+ # say_status("copying", "jQuery UI (#{Jquery::Rails::JQUERY_UI_VERSION})", :green)
30
+ # copy_file "jquery-ui.js", "public/javascripts/jquery-ui.js"
31
+ # copy_file "jquery-ui.min.js", "public/javascripts/jquery-ui.min.js"
32
+ # end
33
+ #end
34
+ #
35
+ #def copy_ujs_driver
36
+ # say_status("copying", "jQuery UJS adapter (#{Jquery::Rails::JQUERY_UJS_VERSION[0..5]})", :green)
37
+ # remove_file "public/javascripts/rails.js"
38
+ # copy_file "jquery_ujs.js", "public/javascripts/jquery_ujs.js"
39
+ #end
40
+
41
+ end
42
+ end
43
+ end if ::Rails.version < "3.1"
@@ -0,0 +1,27 @@
1
+ require "progress_upload_field/rails"
2
+
3
+ module ActionView
4
+ module Helpers
5
+ module FormHelper
6
+ def progress_upload_field(div_name)
7
+ fields = ""
8
+ fields += "<div id='progressIndicator'>"
9
+ fields += "<div id='#{div_name}Bar'>"
10
+ fields += "<div id='#{div_name}Response'></div>"
11
+ fields += "<div id='#{div_name}Number'></div>"
12
+ fields += "</div>"
13
+ fields += "<div id='#{div_name}UploadInfo'>"
14
+ fields += "<span id='#{div_name}TransferSpeedInfo'></span>"
15
+ fields += "<span id='#{div_name}TimeRemainingInfo'></span>"
16
+ fields += "<span id='#{div_name}BytesInfo'></span>"
17
+ fields += "</div>"
18
+ fields += "</div>"
19
+ fields += "<div id='#{div_name}Info'></div>"
20
+ fields += "<div id='#{div_name}Name'></div>"
21
+ fields += "<div id='#{div_name}Size'></div>"
22
+ fields += "<div id='#{div_name}Type'></div>"
23
+ raw(fields)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module ProgressUploadField
2
+ module Rails
3
+ if ::Rails.version < "3.1"
4
+ require 'progress_upload_field/rails/railtie'
5
+ else
6
+ require 'progress_upload_field/rails/engine'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module ProgressUploadField
2
+ module Rails
3
+ # Let Rails 3.1 include our asset-path
4
+ class Engine < ::Rails::Engine
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,117 @@
1
+ var bytesUploaded = 0;
2
+ var bytesTotal = 0;
3
+ var previousBytesLoaded = 0;
4
+ var intervalTimer = 0;
5
+ var div_id = "";
6
+ var post_action = "";
7
+ var form_div_id = "form1";
8
+
9
+ function fileSelected(form_id,id,action) {
10
+ var file = document.getElementById(id).files[0];
11
+ var fileSize = 0;
12
+ div_id = id;
13
+ post_action = action;
14
+ form_div_id = form_id;
15
+ if (file.size > 1024 * 1024)
16
+ fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
17
+ else
18
+ fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
19
+ document.getElementById(div_id+'Info').style.display = 'block';
20
+ document.getElementById(div_id+'Name').innerHTML = 'Name: ' + file.name;
21
+ document.getElementById(div_id+'Size').innerHTML = 'Size: ' + fileSize;
22
+ document.getElementById(div_id+'Type').innerHTML = 'Type: ' + file.type;
23
+ }
24
+
25
+ function uploadFile(formName) {
26
+ previousBytesLoaded = 0;
27
+ document.getElementById(div_id+'Response').style.display = 'none';
28
+ document.getElementById(div_id+'Number').innerHTML = '';
29
+ var progressBar = document.getElementById(div_id+'Bar');
30
+ progressBar.style.display = 'block';
31
+ progressBar.style.width = '0px';
32
+
33
+ var myform = document.forms[formName];
34
+ var fd = new FormData(myform);
35
+
36
+ var xhr = new XMLHttpRequest();
37
+ xhr.upload.addEventListener("progress", uploadProgress, false);
38
+ xhr.addEventListener("load", uploadComplete, false);
39
+ xhr.addEventListener("error", uploadFailed, false);
40
+ xhr.addEventListener("abort", uploadCanceled, false);
41
+ xhr.open("POST", post_action);
42
+ xhr.send(fd);
43
+
44
+ intervalTimer = setInterval(updateTransferSpeed, 500);
45
+ }
46
+
47
+ function updateTransferSpeed() {
48
+ var currentBytes = bytesUploaded;
49
+ var bytesDiff = currentBytes - previousBytesLoaded;
50
+ if (bytesDiff == 0) return;
51
+ previousBytesLoaded = currentBytes;
52
+ bytesDiff = bytesDiff * 2;
53
+ var bytesRemaining = bytesTotal - previousBytesLoaded;
54
+ var secondsRemaining = bytesRemaining / bytesDiff;
55
+
56
+ var speed = "";
57
+ if (bytesDiff > 1024 * 1024)
58
+ speed = (Math.round(bytesDiff * 100/(1024*1024))/100).toString() + 'MBps';
59
+ else if (bytesDiff > 1024)
60
+ speed = (Math.round(bytesDiff * 100/1024)/100).toString() + 'KBps';
61
+ else
62
+ speed = bytesDiff.toString() + 'Bps';
63
+ document.getElementById(div_id+'TransferSpeedInfo').innerHTML = speed;
64
+ document.getElementById(div_id+'TimeRemainingInfo').innerHTML = '| ' + secondsToString(secondsRemaining);
65
+ }
66
+
67
+ function secondsToString(seconds) {
68
+ var h = Math.floor(seconds / 3600);
69
+ var m = Math.floor(seconds % 3600 / 60);
70
+ var s = Math.floor(seconds % 3600 % 60);
71
+ return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
72
+ }
73
+
74
+ function uploadProgress(evt) {
75
+ if (evt.lengthComputable) {
76
+ bytesUploaded = evt.loaded;
77
+ bytesTotal = evt.total;
78
+ var percentComplete = Math.round(evt.loaded * 100 / evt.total);
79
+ var bytesTransfered = '';
80
+ if (bytesUploaded > 1024*1024)
81
+ bytesTransfered = (Math.round(bytesUploaded * 100/(1024*1024))/100).toString() + 'MB';
82
+ else if (bytesUploaded > 1024)
83
+ bytesTransfered = (Math.round(bytesUploaded * 100/1024)/100).toString() + 'KB';
84
+ else
85
+ bytesTransfered = (Math.round(bytesUploaded * 100)/100).toString() + 'Bytes';
86
+
87
+ document.getElementById(div_id+'Number').innerHTML = percentComplete.toString() + '%';
88
+ document.getElementById(div_id+'Bar').style.width = percentComplete.toString() + '%';
89
+ document.getElementById(div_id+'BytesInfo').innerHTML = bytesTransfered;
90
+ if (percentComplete == 100) {
91
+ $('#'+div_id+'Info').hide();
92
+ var uploadResponse = document.getElementById(div_id+'Response');
93
+ uploadResponse.innerHTML = '<span style="font-size: 10pt; font-weight: bold;">Please wait...</span>';
94
+ uploadResponse.style.display = 'block';
95
+ }
96
+ }
97
+ else {
98
+ document.getElementById(div_id+'Bar').innerHTML = 'unable to process upload';
99
+ }
100
+ }
101
+
102
+ function uploadComplete(evt) {
103
+ clearInterval(intervalTimer);
104
+ var uploadResponse = document.getElementById(div_id+'Response');
105
+ eval(evt.target.responseText);
106
+ uploadResponse.style.display = 'block';
107
+ }
108
+
109
+ function uploadFailed(evt) {
110
+ clearInterval(intervalTimer);
111
+ alert("An error occurred while uploading the file.");
112
+ }
113
+
114
+ function uploadCanceled(evt) {
115
+ clearInterval(intervalTimer);
116
+ alert("The upload has been canceled by the user or your browser dropped the connection.");
117
+ }
@@ -0,0 +1,25 @@
1
+ #progressIndicator {
2
+
3
+ width: 325px;
4
+ background: #eee;
5
+ overflow: hidden;
6
+
7
+ #attachment_fileName{}
8
+ #attachment_fileSize{}
9
+ #attachment_fileType{}
10
+
11
+ #attachment_fileBar {
12
+ color: white;
13
+ display: block;
14
+ height: 25px;
15
+ width: 0px;
16
+ background: #264799;
17
+ text-align: right;
18
+ border-radius: 5px;
19
+ }
20
+ #attachment_fileNumber{display: inline;}
21
+ #attachment_fileTransferSpeedInfo{display: inline;}
22
+ #attachment_fileTimeRemainingInfo{display: inline;}
23
+ #attachment_fileBytesInfo{display: inline;}
24
+ #attachment_fileResponse{display: inline;}
25
+ }
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: progress_upload_field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andi Altendorfer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-18 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jquery-rails
17
+ requirement: &70110061111500 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70110061111500
26
+ - !ruby/object:Gem::Dependency
27
+ name: shoulda
28
+ requirement: &70110061109500 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70110061109500
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &70110061103300 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70110061103300
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &70110061101520 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.4
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70110061101520
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &70110061096500 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70110061096500
70
+ description: use 'progress_upload_field' in your view, next your file_field
71
+ email: andi@iboard.cc
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.md
77
+ files:
78
+ - lib/generators/progress_upload_field/install/install_generator.rb
79
+ - lib/progress_upload_field.rb
80
+ - lib/progress_upload_field/rails.rb
81
+ - lib/progress_upload_field/rails/engine.rb
82
+ - vendor/assets/javascripts/progress_upload_field.js
83
+ - vendor/assets/stylesheets/progress.css.scss
84
+ - LICENSE.txt
85
+ - README.md
86
+ has_rdoc: true
87
+ homepage: http://github.com/iboard/progress_upload_field
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 2605619411404494244
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.6.2
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: FormHelper to display div-tags and a javascript to show a progress-bar while
115
+ uploading files
116
+ test_files: []