r74333 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74332‎ | r74333 | r74334 >
Date:23:24, 5 October 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Moved the HTML5 placeholder attribute emulation in ext.vector.simpleSearch into it's own jQuery plugin called jquery.placeholder.
Modified paths:
  • /trunk/extensions/Vector/Vector.hooks.php (modified) (history)
  • /trunk/extensions/Vector/modules/ext.vector.simpleSearch.js (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.placeholder.js (added) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.placeholder.js
@@ -0,0 +1,48 @@
 2+/**
 3+ * HTML5 placeholder emulation for jQuery plugin
 4+ *
 5+ * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
 6+ *
 7+ * @author Trevor Parscal <tparscal@wikimedia.org>
 8+ * @version 0.1.0
 9+ * @license GPL v2
 10+ */
 11+
 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+ $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' );
 42+ } );
 43+ // Show initially, if empty
 44+ if ( $input.val() == '' ) {
 45+ $input.trigger( 'blur' );
 46+ }
 47+ } );
 48+ }
 49+};
Property changes on: trunk/phase3/resources/jquery/jquery.placeholder.js
___________________________________________________________________
Added: svn:eol-style
150 + native
Index: trunk/phase3/resources/Resources.php
@@ -48,6 +48,9 @@
4949 'jquery.highlightText' => new ResourceLoaderFileModule(
5050 array( 'scripts' => 'resources/jquery/jquery.highlightText.js' )
5151 ),
 52+ 'jquery.placeholder' => new ResourceLoaderFileModule(
 53+ array( 'scripts' => 'resources/jquery/jquery.placeholder.js' )
 54+ ),
5255 'jquery.suggestions' => new ResourceLoaderFileModule(
5356 array(
5457 'scripts' => 'resources/jquery/jquery.suggestions.js',
Index: trunk/extensions/Vector/modules/ext.vector.simpleSearch.js
@@ -1,6 +1,6 @@
22 /* JavaScript for SimpleSearch extension */
33
4 -// Disable mwsuggest.js's effect on searchInput
 4+// XXX: Disable mwsuggest.js's effect on searchInput
55 if ( typeof os_autoload_inputs !== 'undefined' && os_autoload_forms !== 'undefined' ) {
66 os_autoload_inputs = [];
77 os_autoload_forms = [];
@@ -8,6 +8,7 @@
99
1010 $( document ).ready( function() {
1111
 12+ // Compatibility map
1213 var map = {
1314 'browsers': {
1415 // Left-to-right languages
@@ -33,81 +34,56 @@
3435 return true;
3536 }
3637
37 - // Placeholder text
38 - // if the placeholder attribute is supported, use it
39 - if ( 'placeholder' in document.createElement( 'input' ) ) {
40 - $( 'div#simpleSearch > input#searchInput' )
41 - .attr( 'placeholder', mediaWiki.msg.get( 'vector-simplesearch-search' ) );
42 - } else {
43 - $( 'div#simpleSearch > input#searchInput' )
44 - .each( function() {
45 - var $input = $( this );
46 - $input
47 - .bind( 'blur', function() {
48 - if ( $input.val().length == 0 ) {
49 - $input
50 - .val( mediaWiki.msg.get( 'vector-simplesearch-search' ) )
51 - .addClass( 'placeholder' );
52 - }
53 - } )
54 - .bind( 'focus', function() {
55 - if ( $input.hasClass( 'placeholder' ) ) {
56 - $input.val( '' ).removeClass( 'placeholder' );
57 - }
58 - } )
59 - .parents( 'form' )
60 - .bind( 'submit', function() {
61 - $input.trigger( 'focus' );
62 - } );
63 - if ( $input.val() == '' ) {
64 - $input.trigger( 'blur' );
 38+ // Placeholder text for SimpleSearch box
 39+ $( 'div#simpleSearch > input#searchInput' ).placeholder( mediaWiki.msg.get( 'vector-simplesearch-search' ) );
 40+
 41+ // General suggestions functionality for all search boxes
 42+ $( '#searchInput, #searchInput2, #powerSearchText, #searchText' )
 43+ .suggestions( {
 44+ fetch: function( query ) {
 45+ var $this = $(this);
 46+ var request = $.ajax( {
 47+ url: wgScriptPath + '/api.php',
 48+ data: {
 49+ 'action': 'opensearch',
 50+ 'search': query,
 51+ 'namespace': 0,
 52+ 'suggest': ''
 53+ },
 54+ dataType: 'json',
 55+ success: function( data ) {
 56+ $this.suggestions( 'suggestions', data[1] );
 57+ }
 58+ });
 59+ $(this).data( 'request', request );
 60+ },
 61+ cancel: function () {
 62+ var request = $(this).data( 'request' );
 63+ // If the delay setting has caused the fetch to have not even happend yet, the request object will
 64+ // have never been set
 65+ if ( request && typeof request.abort == 'function' ) {
 66+ request.abort();
 67+ $(this).removeData( 'request' );
6568 }
66 - } );
67 - }
68 - $( '#searchInput, #searchInput2, #powerSearchText, #searchText' ).suggestions( {
69 - fetch: function( query ) {
70 - var $this = $(this);
71 - var request = $.ajax( {
72 - url: wgScriptPath + '/api.php',
73 - data: {
74 - 'action': 'opensearch',
75 - 'search': query,
76 - 'namespace': 0,
77 - 'suggest': ''
78 - },
79 - dataType: 'json',
80 - success: function( data ) {
81 - $this.suggestions( 'suggestions', data[1] );
 69+ },
 70+ result: {
 71+ select: function( $input ) {
 72+ $input.closest( 'form' ).submit();
8273 }
83 - });
84 - $(this).data( 'request', request );
85 - },
86 - cancel: function () {
87 - var request = $(this).data( 'request' );
88 - // If the delay setting has caused the fetch to have not even happend yet, the request object will
89 - // have never been set
90 - if ( request && typeof request.abort == 'function' ) {
91 - request.abort();
92 - $(this).removeData( 'request' );
93 - }
94 - },
95 - result: {
96 - select: function( $textbox ) {
97 - $textbox.closest( 'form' ).submit();
98 - }
99 - },
100 - delay: 120,
101 - positionFromLeft: $( 'body' ).is( '.rtl' ),
102 - highlightInput: true
103 - } )
 74+ },
 75+ delay: 120,
 76+ positionFromLeft: $( 'body' ).is( '.rtl' ),
 77+ highlightInput: true
 78+ } )
10479 .bind( 'paste cut', function( e ) {
10580 // make sure paste and cut events from the mouse trigger the keypress handler and cause the suggestions to update
10681 $( this ).trigger( 'keypress' );
10782 } );
 83+ // Special suggestions functionality for skin-provided search box
10884 $( '#searchInput' ).suggestions( {
10985 result: {
110 - select: function( $textbox ) {
111 - $textbox.closest( 'form' ).submit();
 86+ select: function( $input ) {
 87+ $input.closest( 'form' ).submit();
11288 }
11389 },
11490 special: {
@@ -130,11 +106,11 @@
131107 .autoEllipsis();
132108 }
133109 },
134 - select: function( $textbox ) {
135 - $textbox.closest( 'form' ).append(
 110+ select: function( $input ) {
 111+ $input.closest( 'form' ).append(
136112 $( '<input />' ).attr( { 'type': 'hidden', 'name': 'fulltext', 'value': 1 } )
137113 );
138 - $textbox.closest( 'form' ).submit();
 114+ $input.closest( 'form' ).submit();
139115 }
140116 },
141117 $region: $( '#simpleSearch' )
Index: trunk/extensions/Vector/Vector.hooks.php
@@ -64,6 +64,7 @@
6565 'jquery.client',
6666 'jquery.suggestions',
6767 'jquery.autoEllipsis',
 68+ 'jquery.placeholder',
6869 ),
6970 'group' => 'ext.vector',
7071 ),

Follow-up revisions

RevisionCommit summaryAuthorDate
r74380Improved on r74282 and r74333 in response to CR comments.tparscal19:05, 6 October 2010

Comments

#Comment by Catrope (talk | contribs)   18:30, 6 October 2010
+			$input = jQuery(this);

Use var $input.

#Comment by Trevor Parscal (WMF) (talk | contribs)   19:06, 6 October 2010

Yes, good point - fixed in r74380

Status & tagging log