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