Skip to content

Commit 64dd32d

Browse files
author
Matt Swanson
committed
add bootstrap.file-input plug. hack in callback
1 parent 87d9436 commit 64dd32d

3 files changed

Lines changed: 123 additions & 4 deletions

File tree

‎app/public/css/styles.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ body {
3030
border-top: 7px solid #484948;
3131
}
3232

33-
.btn {
33+
#action-bar .btn {
3434
padding: 8px;
3535
height: 18px;
3636
width: 18px;
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Bootstrap - File Input
3+
======================
4+
5+
This is meant to convert all file input tags into a set of elements that displays consistently in all browsers.
6+
7+
Converts all
8+
<input type="file">
9+
into Bootstrap buttons
10+
<a class="btn">Browse</a>
11+
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+
});
79+
80+
$('.file-input-wrapper input[type=file]').change(function(){
81+
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>');
86+
}
87+
else {
88+
$(this).parent().after('<span class="file-input-name">'+$(this).val().replace('C:\\fakepath\\','')+'</span>');
89+
}
90+
91+
$(this).trigger("filePicked");
92+
});
93+
94+
95+
96+
});
97+
98+
// Add the styles before the first stylesheet
99+
// This ensures they can be easily overridden with developer styles
100+
var cssHtml = '<style>'+
101+
'.file-input-wrapper { overflow: hidden; position: relative; cursor: pointer; z-index: 1; }'+
102+
'.file-input-wrapper input[type=file], .file-input-wrapper input[type=file]:focus, .file-input-wrapper input[type=file]:hover { position: absolute; top: 0; left: 0; cursor: pointer; opacity: 0; filter: alpha(opacity=0); z-index: 99; outline: 0; }'+
103+
'.file-input-name { margin-left: 8px; }'+
104+
'</style>';
105+
$('link[rel=stylesheet]').eq(0).before(cssHtml);
106+
107+
});

‎app/views/import.erb‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1+
<% content_for :head do %>
2+
<script src="/js/bootstrap.file-input.js"></script>
3+
<% end %>
4+
5+
16
<form id="import" method="POST" action="/import" enctype="multipart/form-data">
2-
<input id="opml_file" name="opml_file" type="file" />
3-
<input type="submit" />
4-
</form>
7+
<input id="opml_file" name="opml_file" type="file" title="Browse" class="btn-primary" />
8+
</form>
9+
10+
<script type="text/javascript">
11+
$(document).ready(function() {
12+
$('#opml_file').on("filePicked", function(e) {
13+
$('form#import').submit();
14+
});
15+
});
16+
</script>

0 commit comments

Comments
 (0)