Path: blob/main/website/GAUSS/js/bootstrap.file-input.js
2941 views
/*1Bootstrap - File Input2======================34This is meant to convert all file input tags into a set of elements that displays consistently in all browsers.56Converts all7<input type="file">8into Bootstrap buttons9<a class="btn">Browse</a>1011*/12(function($) {1314$.fn.bootstrapFileInput = function() {1516this.each(function(i,elem){1718var $elem = $(elem);1920// Maybe some fields don't need to be standardized.21if (typeof $elem.attr('data-bfi-disabled') != 'undefined') {22return;23}2425// Set the word to be displayed on the button26var buttonWord = 'Browse';2728if (typeof $elem.attr('title') != 'undefined') {29buttonWord = $elem.attr('title');30}3132var className = '';3334if (!!$elem.attr('class')) {35className = ' ' + $elem.attr('class');36}3738// Now we're going to wrap that input field with a Bootstrap button.39// The input will actually still be there, it will just be float above and transparent (done with the CSS).40$elem.wrap('<a class="file-input-wrapper btn btn-default ' + className + '"></a>').parent().prepend($('<span></span>').html(buttonWord));41})4243// After we have found all of the file inputs let's apply a listener for tracking the mouse movement.44// 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.45.promise().done( function(){4647// 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.48// This gives us the pointer cursor that FF denies us49$('.file-input-wrapper').mousemove(function(cursor) {5051var input, wrapper,52wrapperX, wrapperY,53inputWidth, inputHeight,54cursorX, cursorY;5556// This wrapper element (the button surround this file input)57wrapper = $(this);58// The invisible file input element59input = wrapper.find("input");60// The left-most position of the wrapper61wrapperX = wrapper.offset().left;62// The top-most position of the wrapper63wrapperY = wrapper.offset().top;64// The with of the browsers input field65inputWidth= input.width();66// The height of the browsers input field67inputHeight= input.height();68//The position of the cursor in the wrapper69cursorX = cursor.pageX;70cursorY = cursor.pageY;7172//The positions we are to move the invisible file input73// 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 middle74moveInputX = cursorX - wrapperX - inputWidth + 20;75// Slides the invisible input Browse button to be positioned middle under the cursor76moveInputY = cursorY- wrapperY - (inputHeight/2);7778// Apply the positioning styles to actually move the invisible file input79input.css({80left:moveInputX,81top:moveInputY82});83});8485$('body').on('change', '.file-input-wrapper input[type=file]', function(){8687var fileName;88fileName = $(this).val();8990// Remove any previous file names91$(this).parent().next('.file-input-name').remove();92if (!!$(this).prop('files') && $(this).prop('files').length > 1) {93fileName = $(this)[0].files.length+' files';94}95else {96fileName = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length);97}9899// Don't try to show the name if there is none100if (!fileName) {101return;102}103104var selectedFileNamePlacement = $(this).data('filename-placement');105if (selectedFileNamePlacement === 'inside') {106// Print the fileName inside107$(this).siblings('span').html(fileName);108$(this).attr('title', fileName);109} else {110// Print the fileName aside (right after the the button)111$(this).parent().after('<span class="file-input-name">'+fileName+'</span>');112}113});114115});116117};118119// Add the styles before the first stylesheet120// This ensures they can be easily overridden with developer styles121var cssHtml = '<style>'+122'.file-input-wrapper { overflow: hidden; position: relative; cursor: pointer; z-index: 1; }'+123'.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; }'+124'.file-input-name { margin-left: 8px; }'+125'</style>';126$('link[rel=stylesheet]').eq(0).before(cssHtml);127128})(jQuery);129130131