r76987 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76986‎ | r76987 | r76988 >
Date:00:12, 19 November 2010
Author:krinkle
Status:resolved (Comments)
Tags:
Comment:
jquery.placeholder.js rewritten
* Instead of calling placeholder() and passing the placeholder-text to it (which meant having to manage text-messages in javascript as well and calling it for each different one), it is now expected that the placeholder-attribute already exists on the page generated by PHP. placeholder() is then called on all input-elements with that attribute to provide fallback for browsers that don't support it.
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.placeholder.js (modified) (history)
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.placeholder.js
@@ -4,52 +4,58 @@
55 * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
66 *
77 * @author Trevor Parscal <tparscal@wikimedia.org>
8 - * @version 0.1.0
 8+ * @author Krinkle <krinklemail@gmail.com>
 9+ * @version 0.2.0
910 * @license GPL v2
1011 */
1112
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' );
5358 }
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 @@
5454 // Enable CheckboxShiftClick
5555 $('input[type=checkbox]:not(.noshiftselect)').checkboxShiftClick();
5656
 57+ // Emulate placeholder if not supported by browser
 58+ if ( !'placeholder' in document.createElement( 'input' ) ) {
 59+ $('input[placeholder]').placeholder();
 60+ }
 61+
5762 // Fill $content var
5863 if ( $('#bodyContent').length ) {
5964 mw.util.$content = $('#bodyContent');

Follow-up revisions

RevisionCommit summaryAuthorDate
r76988Follow-up r76987. Apparently this works better in Firefox 2.krinkle00:18, 19 November 2010
r77063Follow-up on r76987 (and it's comment) - adding dependency to make the script...krinkle19:51, 20 November 2010
r77118Updated use of placeholder plugin, as of r76987 the plugin expects the attrib...tparscal23:22, 22 November 2010

Comments

#Comment by OverlordQ (talk | contribs)   02:20, 20 November 2010
Error: $("input[placeholder]").placeholder is not a function
Line: 1795

matches up with:

if(!('placeholder'in document.createElement('input'))){
  $('input[placeholder]').placeholder();
} 

in mediawiki.util.js

#Comment by Krinkle (talk | contribs)   01:37, 23 November 2010

Fixed in r77063

Status & tagging log