|
2 | 2 | * --------------------------------------------------------------------
|
3 | 3 | * jQuery customfileinput plugin
|
4 | 4 | * Author: Scott Jehl, scott@filamentgroup.com
|
5 |
| - * Copyright (c) 2009 Filament Group |
| 5 | + * Copyright (c) 2009 Filament Group |
6 | 6 | * licensed under MIT (filamentgroup.com/examples/mit-license.txt)
|
7 | 7 | * --------------------------------------------------------------------
|
8 | 8 | */
|
9 | 9 | (function( $ ){
|
10 |
| -$.fn.customFileInput = function(){ |
11 |
| - //apply events and styles for file input element |
12 |
| - var fileInput = $(this) |
13 |
| - .addClass('customfile-input') //add class for CSS |
14 |
| - .mouseover(function(){ upload.addClass('customfile-hover'); }) |
15 |
| - .mouseout(function(){ upload.removeClass('customfile-hover'); }) |
16 |
| - .focus(function(){ |
17 |
| - upload.addClass('customfile-focus'); |
18 |
| - fileInput.data('val', fileInput.val()); |
19 |
| - }) |
20 |
| - .blur(function(){ |
21 |
| - upload.removeClass('customfile-focus'); |
22 |
| - $(this).trigger('checkChange'); |
23 |
| - }) |
24 |
| - .bind('disable',function(){ |
25 |
| - fileInput.attr('disabled',true); |
26 |
| - upload.addClass('customfile-disabled'); |
27 |
| - }) |
28 |
| - .bind('enable',function(){ |
29 |
| - fileInput.removeAttr('disabled'); |
30 |
| - upload.removeClass('customfile-disabled'); |
31 |
| - }) |
32 |
| - .bind('checkChange', function(){ |
33 |
| - if(fileInput.val() && fileInput.val() != fileInput.data('val')){ |
34 |
| - fileInput.trigger('change'); |
35 |
| - } |
36 |
| - }) |
37 |
| - .bind('change',function(){ |
38 |
| - //get file name |
39 |
| - var fileName = $(this).val().split(/\\/).pop(); |
40 |
| - //get file extension |
41 |
| - var fileExt = 'customfile-ext-' + fileName.split('.').pop().toLowerCase(); |
42 |
| - //update the feedback |
43 |
| - uploadFeedback |
44 |
| - .text(fileName) //set feedback text to filename |
45 |
| - .removeClass(uploadFeedback.data('fileExt') || '') //remove any existing file extension class |
46 |
| - .addClass(fileExt) //add file extension class |
47 |
| - .data('fileExt', fileExt) //store file extension for class removal on next change |
48 |
| - .addClass('customfile-feedback-populated'); //add class to show populated state |
49 |
| - //change text of button |
50 |
| - uploadButton.text('Change'); |
51 |
| - }) |
52 |
| - .click(function(){ //for IE and Opera, make sure change fires after choosing a file, using an async callback |
53 |
| - fileInput.data('val', fileInput.val()); |
54 |
| - setTimeout(function(){ |
55 |
| - fileInput.trigger('checkChange'); |
56 |
| - },100); |
57 |
| - }); |
| 10 | + $.fn.customFileInput = function(){ |
| 11 | + //apply events and styles for file input element |
| 12 | + var fileInput = $(this) |
| 13 | + .addClass('customfile-input') //add class for CSS |
| 14 | + .mouseover(function(){ upload.addClass('customfile-hover'); }) |
| 15 | + .mouseout(function(){ upload.removeClass('customfile-hover'); }) |
| 16 | + .focus(function(){ |
| 17 | + upload.addClass('customfile-focus'); |
| 18 | + fileInput.data('val', fileInput.val()); |
| 19 | + }) |
| 20 | + .blur(function(){ |
| 21 | + upload.removeClass('customfile-focus'); |
| 22 | + $(this).trigger('checkChange'); |
| 23 | + }) |
| 24 | + .bind('disable',function(){ |
| 25 | + fileInput.attr('disabled',true); |
| 26 | + upload.addClass('customfile-disabled'); |
| 27 | + }) |
| 28 | + .bind('enable',function(){ |
| 29 | + fileInput.removeAttr('disabled'); |
| 30 | + upload.removeClass('customfile-disabled'); |
| 31 | + }) |
| 32 | + .bind('checkChange', function(){ |
| 33 | + if(fileInput.val() && fileInput.val() != fileInput.data('val')){ |
| 34 | + fileInput.trigger('change'); |
| 35 | + } |
| 36 | + }) |
| 37 | + .bind('change',function(){ |
| 38 | + //get file name |
| 39 | + var fileName = $(this).val().split(/\\/).pop(); |
| 40 | + //get file extension |
| 41 | + var fileExt = 'customfile-ext-' + fileName.split('.').pop().toLowerCase(); |
| 42 | + //update the feedback |
| 43 | + uploadFeedback |
| 44 | + .text(fileName) //set feedback text to filename |
| 45 | + .removeClass(uploadFeedback.data('fileExt') || '') //remove any existing file extension class |
| 46 | + .addClass(fileExt) //add file extension class |
| 47 | + .data('fileExt', fileExt) //store file extension for class removal on next change |
| 48 | + .addClass('customfile-feedback-populated'); //add class to show populated state |
| 49 | + //change text of button |
| 50 | + uploadButton.text('Change'); |
| 51 | + }) |
| 52 | + .click(function(){ //for IE and Opera, make sure change fires after choosing a file, using an async callback |
| 53 | + fileInput.data('val', fileInput.val()); |
| 54 | + setTimeout(function(){ |
| 55 | + fileInput.trigger('checkChange'); |
| 56 | + },100); |
| 57 | + }); |
| 58 | + |
| 59 | + //create custom control container |
| 60 | + var upload = $('<div class="customfile"></div>'); |
| 61 | + //create custom control button |
| 62 | + var uploadButton = $('<span class="customfile-button" aria-hidden="true">Browse</span>').appendTo(upload); |
| 63 | + //create custom control feedback |
| 64 | + var uploadFeedback = $('<span class="customfile-feedback" aria-hidden="true">No file selected...</span>').appendTo(upload); |
58 | 65 |
|
59 |
| - //create custom control container |
60 |
| - var upload = $('<div class="customfile"></div>'); |
61 |
| - //create custom control button |
62 |
| - var uploadButton = $('<span class="customfile-button" aria-hidden="true">Browse</span>').appendTo(upload); |
63 |
| - //create custom control feedback |
64 |
| - var uploadFeedback = $('<span class="customfile-feedback" aria-hidden="true">No file selected...</span>').appendTo(upload); |
65 |
| - |
66 |
| - //match disabled state |
67 |
| - if(fileInput.is('[disabled]')){ |
68 |
| - fileInput.trigger('disable'); |
69 |
| - } |
70 |
| - |
71 |
| - |
72 |
| - //on mousemove, keep file input under the cursor to steal click |
73 |
| - upload |
74 |
| - .mousemove(function(e){ |
75 |
| - fileInput.css({ |
76 |
| - 'left': e.pageX - upload.offset().left - fileInput.outerWidth() + 20, //position right side 20px right of cursor X) |
77 |
| - 'top': e.pageY - upload.offset().top - $(window).scrollTop() - 3 |
78 |
| - }); |
79 |
| - }) |
80 |
| - .insertAfter(fileInput); //insert after the input |
81 |
| - |
82 |
| - fileInput.appendTo(upload); |
83 |
| - |
84 |
| - //return jQuery |
85 |
| - return $(this); |
86 |
| -}; |
| 66 | + //match disabled state |
| 67 | + if(fileInput.is('[disabled]')){ |
| 68 | + fileInput.trigger('disable'); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + //on mousemove, keep file input under the cursor to steal click |
| 73 | + upload |
| 74 | + .mousemove(function(e){ |
| 75 | + fileInput.css({ |
| 76 | + 'left': e.pageX - upload.offset().left - fileInput.outerWidth() + 20, //position right side 20px right of cursor X) |
| 77 | + 'top': e.pageY - upload.offset().top - $(window).scrollTop() - 3 |
| 78 | + }); |
| 79 | + }) |
| 80 | + .insertAfter(fileInput); //insert after the input |
| 81 | + |
| 82 | + fileInput.appendTo(upload); |
| 83 | + |
| 84 | + //return jQuery |
| 85 | + return $(this); |
| 86 | + }; |
87 | 87 | })( jQuery );
|
0 commit comments