bootstrap_file_input_rails 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a412afd9650077ea55f308aa796edeb5d1620797
4
- data.tar.gz: 60b5116be836fafbc418b6371e02e2b532f1c077
2
+ SHA256:
3
+ metadata.gz: a31e914cdceed61f110fc6324366c1a5652652fcf80e4520245bc5d092b2c00a
4
+ data.tar.gz: ade69a9b0db7c062223628bb6f1c2565f27db19b1e3cfc396fc51a81a15d271a
5
5
  SHA512:
6
- metadata.gz: 71eb4f5b4aaa5e7d4e91e888f21e8a147d9fbeb357a6f3e094fbdc452c37425093b1f8ba35d5c766d8c3372b8086ba88b51605d3c7a79c0743439cedc6a37d67
7
- data.tar.gz: 7eb744b0d11539d541d58caf1f0f1defc16ea544d7657c21b477d85aac226c62135d01a388092987963aba42d3f1a80dc8d1b43024f8eb3dbc708a5af768ee16
6
+ metadata.gz: a9729f7e2b0295780ef96a11b4df7c4210645120509d405b34889d8e1c68322ddbb18d4b5ba22f7e1c78160b1c311e0961c0a56733e69a08081f017de0d54979
7
+ data.tar.gz: e34d57bb2b3a731646a98fbe17acd0109b4a63425c5447ae49957960dffe05a9e40f1877380b5aec3f2f55d86aa9885d0473a7c6d49cac8a060fdf53df8a341e
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # BootstrapFileInputRails
1
+ # Bootstrap file input, for rails !
2
2
 
3
- TODO: Write a gem description
3
+ This gem is a port of the repository : [Bootstrap file input](https://github.com/grevory/bootstrap-file-input)
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Make sure bootstrap is installed and required.
22
+
23
+ In your applications.js file
24
+
25
+ //= require bootstrap.file-input
22
26
 
23
27
  ## Contributing
24
28
 
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency 'railties', '>= 3.1'
21
+ spec.add_dependency 'railties', '>= 3.1', '< 6'
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -1,3 +1,3 @@
1
1
  module BootstrapFileInputRails
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -10,89 +10,125 @@
10
10
  <a class="btn">Browse</a>
11
11
 
12
12
  */
13
- $(function() {
14
-
15
- $('input[type=file]').each(function(i,elem){
16
-
17
- // Maybe some fields don't need to be standardized.
18
- if (typeof $(this).attr('data-bfi-disabled') != 'undefined') {
19
- return;
20
- }
21
-
22
- // Set the word to be displayed on the button
23
- var buttonWord = 'Browse';
24
-
25
- if (typeof $(this).attr('title') != 'undefined') {
26
- buttonWord = $(this).attr('title');
27
- }
28
-
29
- // Start by getting the HTML of the input element.
30
- // Thanks for the tip http://stackoverflow.com/a/1299069
31
- var $elem = $(elem);
32
- var input = $('<div>').append( $elem.eq(0).clone() ).html();
33
-
34
- // Now we're going to replace that input field with a Bootstrap button.
35
- // The input will actually still be there, it will just be float above and transparent (done with the CSS).
36
- $elem.replaceWith('<a class="file-input-wrapper btn ' + $elem.attr('class') + '">'+buttonWord+input+'</a>');
37
- })
38
- // After we have found all of the file inputs let's apply a listener for tracking the mouse movement.
39
- // This is important because the in order to give the illusion that this is a button in FF we actually need to move the button from the file input under the cursor. Ugh.
40
- .promise().done( function(){
41
-
42
- // As the cursor moves over our new Bootstrap button we need to adjust the position of the invisible file input Browse button to be under the cursor.
43
- // This gives us the pointer cursor that FF denies us
44
- $('.file-input-wrapper').mousemove(function(cursor) {
45
-
46
- var input, wrapper,
47
- wrapperX, wrapperY,
48
- inputWidth, inputHeight,
49
- cursorX, cursorY;
50
-
51
- // This wrapper element (the button surround this file input)
52
- wrapper = $(this);
53
- // The invisible file input element
54
- input = wrapper.find("input");
55
- // The left-most position of the wrapper
56
- wrapperX = wrapper.offset().left;
57
- // The top-most position of the wrapper
58
- wrapperY = wrapper.offset().top;
59
- // The with of the browsers input field
60
- inputWidth= input.width();
61
- // The height of the browsers input field
62
- inputHeight= input.height();
63
- //The position of the cursor in the wrapper
64
- cursorX = cursor.pageX;
65
- cursorY = cursor.pageY;
66
-
67
- //The positions we are to move the invisible file input
68
- // The 20 at the end is an arbitrary number of pixels that we can shift the input such that cursor is not pointing at the end of the Browse button but somewhere nearer the middle
69
- moveInputX = cursorX - wrapperX - inputWidth + 20;
70
- // Slides the invisible input Browse button to be positioned middle under the cursor
71
- moveInputY = cursorY- wrapperY - (inputHeight/2);
72
-
73
- // Apply the positioning styles to actually move the invisible file input
74
- input.css({
75
- left:moveInputX,
76
- top:moveInputY
77
- });
78
- });
13
+ (function($) {
14
+
15
+ $.fn.bootstrapFileInput = function() {
16
+
17
+ this.each(function(i,elem){
79
18
 
80
- $('.file-input-wrapper input[type=file]').change(function(){
19
+ var $elem = $(elem);
81
20
 
82
- // Remove any previous file names
83
- $(this).parent().next('.file-input-name').remove();
84
- if ($(this).prop('files').length > 1) {
85
- $(this).parent().after('<span class="file-input-name">'+$(this)[0].files.length+' files</span>');
21
+ // Add [processed] class to avoid double processing of input file element
22
+ if (typeof $elem.attr('data-bfi-processed-class') != 'undefined') {
23
+ // Check if the element already has the [processed] flag on it and skip it if it does
24
+ if ($elem.hasClass($elem.attr('data-bfi-processed-class'))) {
25
+ return;
26
+ }
27
+ $elem.addClass($elem.attr('data-bfi-processed-class'));
86
28
  }
87
- else {
88
- $(this).parent().after('<span class="file-input-name">'+$(this).val().replace('C:\\fakepath\\','')+'</span>');
29
+
30
+ // Maybe some fields don't need to be standardized.
31
+ if (typeof $elem.attr('data-bfi-disabled') != 'undefined') {
32
+ return;
89
33
  }
90
34
 
91
- });
35
+ // Set the word to be displayed on the button
36
+ var buttonWord = 'Browse';
37
+
38
+ if (typeof $elem.attr('title') != 'undefined') {
39
+ buttonWord = $elem.attr('title');
40
+ }
92
41
 
93
-
42
+ var className = '';
43
+
44
+ if (!!$elem.attr('class')) {
45
+ className = ' ' + $elem.attr('class');
46
+ }
47
+
48
+ // Now we're going to wrap that input field with a Bootstrap button.
49
+ // The input will actually still be there, it will just be float above and transparent (done with the CSS).
50
+ $elem.wrap('<a class="file-input-wrapper btn btn-default ' + className + '"></a>').parent().prepend($('<span></span>').html(buttonWord));
51
+ })
52
+
53
+ // After we have found all of the file inputs let's apply a listener for tracking the mouse movement.
54
+ // This is important because the in order to give the illusion that this is a button in FF we actually need to move the button from the file input under the cursor. Ugh.
55
+ .promise().done( function(){
56
+
57
+ // As the cursor moves over our new Bootstrap button we need to adjust the position of the invisible file input Browse button to be under the cursor.
58
+ // This gives us the pointer cursor that FF denies us
59
+ $('.file-input-wrapper').on('mousemove', function(cursor) {
60
+
61
+ var input, wrapper,
62
+ wrapperX, wrapperY,
63
+ inputWidth, inputHeight,
64
+ cursorX, cursorY;
65
+
66
+ // This wrapper element (the button surround this file input)
67
+ wrapper = $(this);
68
+ // The invisible file input element
69
+ input = wrapper.find("input");
70
+ // The left-most position of the wrapper
71
+ wrapperX = wrapper.offset().left;
72
+ // The top-most position of the wrapper
73
+ wrapperY = wrapper.offset().top;
74
+ // The with of the browsers input field
75
+ inputWidth= input.width();
76
+ // The height of the browsers input field
77
+ inputHeight= input.height();
78
+ //The position of the cursor in the wrapper
79
+ cursorX = cursor.pageX;
80
+ cursorY = cursor.pageY;
81
+
82
+ //The positions we are to move the invisible file input
83
+ // The 20 at the end is an arbitrary number of pixels that we can shift the input such that cursor is not pointing at the end of the Browse button but somewhere nearer the middle
84
+ moveInputX = cursorX - wrapperX - inputWidth + 20;
85
+ // Slides the invisible input Browse button to be positioned middle under the cursor
86
+ moveInputY = cursorY- wrapperY - (inputHeight/2);
87
+
88
+ // Apply the positioning styles to actually move the invisible file input
89
+ input.css({
90
+ left:moveInputX,
91
+ top:moveInputY
92
+ });
93
+ });
94
+
95
+ $('body').on('change', '.file-input-wrapper input[type=file]', function(){
96
+
97
+ var fileName;
98
+ fileName = $(this).val();
99
+
100
+ // Remove any previous file names
101
+ $(this).parent().next('.file-input-name').remove();
102
+ if (!!$(this).prop('files') && $(this).prop('files').length > 1) {
103
+ var filesLabel = $(this).data('files-label');
104
+ if (!filesLabel) {
105
+ filesLabel = 'files';
106
+ }
107
+ fileName = $(this)[0].files.length+' '+filesLabel;
108
+ }
109
+ else {
110
+ fileName = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length);
111
+ }
112
+
113
+ // Don't try to show the name if there is none
114
+ if (!fileName) {
115
+ return;
116
+ }
117
+
118
+ var selectedFileNamePlacement = $(this).data('filename-placement');
119
+ if (selectedFileNamePlacement === 'inside') {
120
+ // Print the fileName inside
121
+ $(this).siblings('span').html(fileName);
122
+ $(this).attr('title', fileName);
123
+ } else {
124
+ // Print the fileName aside (right after the the button)
125
+ $(this).parent().after('<span class="file-input-name">'+fileName+'</span>');
126
+ }
127
+ });
128
+
129
+ });
94
130
 
95
- });
131
+ };
96
132
 
97
133
  // Add the styles before the first stylesheet
98
134
  // This ensures they can be easily overridden with developer styles
@@ -103,4 +139,4 @@ var cssHtml = '<style>'+
103
139
  '</style>';
104
140
  $('link[rel=stylesheet]').eq(0).before(cssHtml);
105
141
 
106
- });
142
+ })(jQuery);
metadata CHANGED
@@ -1,55 +1,61 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstrap_file_input_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrien Siami
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-07 00:00:00.000000000 Z
11
+ date: 2018-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - '>='
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '3.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - ~>
37
+ - - "~>"
32
38
  - !ruby/object:Gem::Version
33
39
  version: '1.3'
34
40
  type: :development
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
- - - ~>
44
+ - - "~>"
39
45
  - !ruby/object:Gem::Version
40
46
  version: '1.3'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: rake
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
- - - '>='
51
+ - - ">="
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - '>='
58
+ - - ">="
53
59
  - !ruby/object:Gem::Version
54
60
  version: '0'
55
61
  description: 'A gem for the bootstrap plugin : bootstrap-file-input'
@@ -59,7 +65,7 @@ executables: []
59
65
  extensions: []
60
66
  extra_rdoc_files: []
61
67
  files:
62
- - .gitignore
68
+ - ".gitignore"
63
69
  - Gemfile
64
70
  - LICENSE.txt
65
71
  - README.md
@@ -78,19 +84,18 @@ require_paths:
78
84
  - lib
79
85
  required_ruby_version: !ruby/object:Gem::Requirement
80
86
  requirements:
81
- - - '>='
87
+ - - ">="
82
88
  - !ruby/object:Gem::Version
83
89
  version: '0'
84
90
  required_rubygems_version: !ruby/object:Gem::Requirement
85
91
  requirements:
86
- - - '>='
92
+ - - ">="
87
93
  - !ruby/object:Gem::Version
88
94
  version: '0'
89
95
  requirements: []
90
96
  rubyforge_project:
91
- rubygems_version: 2.0.0
97
+ rubygems_version: 2.7.6
92
98
  signing_key:
93
99
  specification_version: 4
94
100
  summary: 'A gem for the bootstrap plugin : bootstrap-file-input'
95
101
  test_files: []
96
- has_rdoc: