Index: trunk/phase3/resources/jquery/jquery.placeholder.js |
— | — | @@ -4,52 +4,58 @@ |
5 | 5 | * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not. |
6 | 6 | * |
7 | 7 | * @author Trevor Parscal <tparscal@wikimedia.org> |
8 | | - * @version 0.1.0 |
| 8 | + * @author Krinkle <krinklemail@gmail.com> |
| 9 | + * @version 0.2.0 |
9 | 10 | * @license GPL v2 |
10 | 11 | */ |
11 | 12 | |
12 | | -jQuery.fn.placeholder = function( text ) { |
13 | | - // If the HTML5 placeholder attribute is supported, use it |
14 | | - if ( 'placeholder' in document.createElement( 'input' ) ) { |
15 | | - jQuery(this).attr( 'placeholder', text ); |
16 | | - } |
17 | | - // Otherwise, use a combination of blur and focus event handlers and a placeholder class |
18 | | - else { |
19 | | - jQuery(this).each( function() { |
20 | | - var $input = jQuery(this); |
21 | | - $input |
22 | | - // Show on blur if empty |
23 | | - .bind( 'blur', function() { |
24 | | - if ( $input.val().length == 0 ) { |
25 | | - $input |
26 | | - .val( text ) |
27 | | - .addClass( 'placeholder' ); |
28 | | - } |
29 | | - } ) |
30 | | - // Hide on focus |
31 | | - .bind( 'focus', function() { |
32 | | - if ( $input.hasClass( 'placeholder' ) ) { |
33 | | - $input |
34 | | - .val( '' ) |
35 | | - .removeClass( 'placeholder' ); |
36 | | - } |
37 | | - } ) |
38 | | - // Blank on submit -- prevents submitting with unintended value |
39 | | - .parents( 'form' ) |
40 | | - .bind( 'submit', function() { |
41 | | - // $input.trigger( 'focus' ); is problematic |
42 | | - // because it actually focuses $input, leading |
43 | | - // to nasty behavior in mobile browsers |
44 | | - if ( $input.hasClass( 'placeholder' ) ) { |
45 | | - $input |
46 | | - .val( '' ) |
47 | | - .removeClass( 'placeholder' ); |
48 | | - } |
49 | | - } ); |
50 | | - // Show initially, if empty |
51 | | - if ( $input.val() == '' ) { |
52 | | - $input.trigger( 'blur' ); |
| 13 | +jQuery.fn.placeholder = function() { |
| 14 | + |
| 15 | + return this.each( function() { |
| 16 | + |
| 17 | + // If the HTML5 placeholder attribute is supported, use it |
| 18 | + if ( this.placeholder && 'placeholder' in document.createElement( this.tagName ) ) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + var placeholder = this.getAttribute('placeholder'); |
| 23 | + var $input = jQuery(this); |
| 24 | + |
| 25 | + // Show initially, if empty |
| 26 | + if ( this.value === '' || this.value == placeholder ) { |
| 27 | + $input.addClass( 'placeholder' ).val( placeholder ); |
| 28 | + } |
| 29 | + |
| 30 | + $input |
| 31 | + // Show on blur if empty |
| 32 | + .blur( function() { |
| 33 | + if ( this.value === '' ) { |
| 34 | + this.value = placeholder; |
| 35 | + $input.addClass( 'placeholder' ); |
| 36 | + } else { |
| 37 | + $input.removeClass( 'placeholder' ); |
| 38 | + } |
| 39 | + } ) |
| 40 | + |
| 41 | + // Hide on focus |
| 42 | + .focus( function() { |
| 43 | + if ($input.hasClass('placeholder')) { |
| 44 | + this.value = ''; |
| 45 | + $input.removeClass( 'placeholder' ); |
| 46 | + } |
| 47 | + } ); |
| 48 | + |
| 49 | + // Blank on submit -- prevents submitting with unintended value |
| 50 | + this.form && $( this.form ).submit( function() { |
| 51 | + // $input.trigger( 'focus' ); would be problematic |
| 52 | + // because it actually focuses $input, leading |
| 53 | + // to nasty behavior in mobile browsers |
| 54 | + if ( $input.hasClass('placeholder') ) { |
| 55 | + $input |
| 56 | + .val( '' ) |
| 57 | + .removeClass( 'placeholder' ); |
53 | 58 | } |
54 | | - } ); |
55 | | - } |
56 | | -}; |
| 59 | + }); |
| 60 | + |
| 61 | + }); |
| 62 | +}; |
\ No newline at end of file |
Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js |
— | — | @@ -53,6 +53,11 @@ |
54 | 54 | // Enable CheckboxShiftClick |
55 | 55 | $('input[type=checkbox]:not(.noshiftselect)').checkboxShiftClick(); |
56 | 56 | |
| 57 | + // Emulate placeholder if not supported by browser |
| 58 | + if ( !'placeholder' in document.createElement( 'input' ) ) { |
| 59 | + $('input[placeholder]').placeholder(); |
| 60 | + } |
| 61 | + |
57 | 62 | // Fill $content var |
58 | 63 | if ( $('#bodyContent').length ) { |
59 | 64 | mw.util.$content = $('#bodyContent'); |