-
Notifications
You must be signed in to change notification settings - Fork 63
/
bootstrap.file-input.js
142 lines (114 loc) · 5.12 KB
/
bootstrap.file-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Bootstrap - File Input
======================
This is meant to convert all file input tags into a set of elements that displays consistently in all browsers.
Converts all
<input type="file">
into Bootstrap buttons
<a class="btn">Browse</a>
*/
(function($) {
$.fn.bootstrapFileInput = function() {
this.each(function(i,elem){
var $elem = $(elem);
// Add [processed] class to avoid double processing of input file element
if (typeof $elem.attr('data-bfi-processed-class') != 'undefined') {
// Check if the element already has the [processed] flag on it and skip it if it does
if ($elem.hasClass($elem.attr('data-bfi-processed-class'))) {
return;
}
$elem.addClass($elem.attr('data-bfi-processed-class'));
}
// Maybe some fields don't need to be standardized.
if (typeof $elem.attr('data-bfi-disabled') != 'undefined') {
return;
}
// Set the word to be displayed on the button
var buttonWord = 'Browse';
if (typeof $elem.attr('title') != 'undefined') {
buttonWord = $elem.attr('title');
}
var className = '';
if (!!$elem.attr('class')) {
className = ' ' + $elem.attr('class');
}
// Now we're going to wrap that input field with a Bootstrap button.
// The input will actually still be there, it will just be float above and transparent (done with the CSS).
$elem.wrap('<a class="file-input-wrapper btn btn-default ' + className + '"></a>').parent().prepend($('<span></span>').html(buttonWord));
})
// After we have found all of the file inputs let's apply a listener for tracking the mouse movement.
// 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.
.promise().done( function(){
// 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.
// This gives us the pointer cursor that FF denies us
$('.file-input-wrapper').on('mousemove', function(cursor) {
var input, wrapper,
wrapperX, wrapperY,
inputWidth, inputHeight,
cursorX, cursorY;
// This wrapper element (the button surround this file input)
wrapper = $(this);
// The invisible file input element
input = wrapper.find("input");
// The left-most position of the wrapper
wrapperX = wrapper.offset().left;
// The top-most position of the wrapper
wrapperY = wrapper.offset().top;
// The with of the browsers input field
inputWidth= input.width();
// The height of the browsers input field
inputHeight= input.height();
//The position of the cursor in the wrapper
cursorX = cursor.pageX;
cursorY = cursor.pageY;
//The positions we are to move the invisible file input
// 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
moveInputX = cursorX - wrapperX - inputWidth + 20;
// Slides the invisible input Browse button to be positioned middle under the cursor
moveInputY = cursorY- wrapperY - (inputHeight/2);
// Apply the positioning styles to actually move the invisible file input
input.css({
left:moveInputX,
top:moveInputY
});
});
$('body').on('change', '.file-input-wrapper input[type=file]', function(){
var fileName;
fileName = $(this).val();
// Remove any previous file names
$(this).parent().next('.file-input-name').remove();
if (!!$(this).prop('files') && $(this).prop('files').length > 1) {
var filesLabel = $(this).data('files-label');
if (!filesLabel) {
filesLabel = 'files';
}
fileName = $(this)[0].files.length+' '+filesLabel;
}
else {
fileName = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length);
}
// Don't try to show the name if there is none
if (!fileName) {
return;
}
var selectedFileNamePlacement = $(this).data('filename-placement');
if (selectedFileNamePlacement === 'inside') {
// Print the fileName inside
$(this).siblings('span').html(fileName);
$(this).attr('title', fileName);
} else {
// Print the fileName aside (right after the the button)
$(this).parent().after('<span class="file-input-name">'+fileName+'</span>');
}
});
});
};
// Add the styles before the first stylesheet
// This ensures they can be easily overridden with developer styles
var cssHtml = '<style>'+
'.file-input-wrapper { overflow: hidden; position: relative; cursor: pointer; z-index: 1; }'+
'.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; }'+
'.file-input-name { margin-left: 8px; }'+
'</style>';
$('link[rel=stylesheet]').eq(0).before(cssHtml);
})(jQuery);