/* 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); // 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(){ $('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) { switch($(this)[0].files.length % 10){ case 1: files_name = 'Файл'; break; case 2: case 3: case 4: files_name = 'Файла'; break; default: files_name = 'Файлов'; break; } if($(this)[0].files.length>10 && $(this)[0].files.length < 20){ files_name = 'Файлов'; } fileName = $(this)[0].files.length+' '+files_name; } 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);