r66658 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66657‎ | r66658 | r66659 >
Date:21:18, 19 May 2010
Author:adam
Status:deferred (Comments)
Tags:
Comment:
Adding match highlighting to the suggestions plugin
Modified paths:
  • /trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/css/combined.css (modified) (history)
  • /trunk/extensions/UsabilityInitiative/css/combined.min.css (modified) (history)
  • /trunk/extensions/UsabilityInitiative/css/suggestions.css (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.suggestions.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/css/suggestions.css
@@ -39,8 +39,9 @@
4040 margin: 0;
4141 width: 100%;
4242 }
43 -.suggestions-result span {
 43+.suggestions-result div {
4444 line-height: 1.5em;
 45+ display: inline;
4546 padding: 0.25em 0.25em;
4647 }
4748 .suggestions-result-current {
Index: trunk/extensions/UsabilityInitiative/css/combined.css
@@ -39,8 +39,9 @@
4040 margin: 0;
4141 width: 100%;
4242 }
43 -.suggestions-result span {
 43+.suggestions-result div {
4444 line-height: 1.5em;
 45+ display: inline;
4546 padding: 0.25em 0.25em;
4647 }
4748 .suggestions-result-current {
Index: trunk/extensions/UsabilityInitiative/css/combined.min.css
@@ -37,8 +37,9 @@
3838 margin:0;
3939 width:100%;
4040 }
41 -.suggestions-result span{
 41+.suggestions-result div{
4242 line-height:1.5em;
 43+display:inline;
4344 padding:0.25em 0.25em;
4445 }
4546 .suggestions-result-current{
Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -18,7 +18,7 @@
1919 private static $styleFiles = array(
2020 'base_sets' => array(
2121 'raw' => array(
22 - array( 'src' => 'css/suggestions.css', 'version' => 13 ),
 22+ array( 'src' => 'css/suggestions.css', 'version' => 14 ),
2323 array( 'src' => 'css/vector.collapsibleNav.css', 'version' => 9 ),
2424 array( 'src' => 'css/vector.footerCleanup.css', 'version' => 2 ),
2525 array( 'src' => 'css/wikiEditor.css', 'version' => 13 ),
@@ -29,11 +29,11 @@
3030 array( 'src' => 'css/vector/jquery-ui-1.7.2.css', 'version' => '1.7.2y' ),
3131 ),
3232 'combined' => array(
33 - array( 'src' => 'css/combined.css', 'version' => 93 ),
 33+ array( 'src' => 'css/combined.css', 'version' => 94 ),
3434 array( 'src' => 'css/vector/jquery-ui-1.7.2.css', 'version' => '1.7.2y' ),
3535 ),
3636 'minified' => array(
37 - array( 'src' => 'css/combined.min.css', 'version' => 93 ),
 37+ array( 'src' => 'css/combined.min.css', 'version' => 94 ),
3838 array( 'src' => 'css/vector/jquery-ui-1.7.2.css', 'version' => '1.7.2y' ),
3939 ),
4040 )
@@ -72,7 +72,7 @@
7373 array( 'src' => 'js/plugins/jquery.delayedBind.js', 'version' => 1 ),
7474 array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 18 ),
7575 array( 'src' => 'js/plugins/jquery.expandableField.js', 'version' => 15 ),
76 - array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 18 ),
 76+ array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 19 ),
7777 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 33 ),
7878 array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 187 ),
7979 array( 'src' => 'js/plugins/jquery.wikiEditor.highlight.js', 'version' => 53 ),
@@ -85,10 +85,10 @@
8686 array( 'src' => 'js/plugins/jquery.wikiEditor.publish.js', 'version' => 5 ),
8787 ),
8888 'combined' => array(
89 - array( 'src' => 'js/plugins.combined.js', 'version' => 402 ),
 89+ array( 'src' => 'js/plugins.combined.js', 'version' => 403 ),
9090 ),
9191 'minified' => array(
92 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 402 ),
 92+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 403 ),
9393 ),
9494 ),
9595 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.suggestions.js
@@ -38,6 +38,8 @@
3939 * Type: Number, Range: 1 - infinity, Default: 3
4040 * positionFromLeft: Whether to position the suggestion box with the left attribute or the right
4141 * Type: Boolean, Default: true
 42+ * highlightInput: Highlights the matched text in the suggestions
 43+ * Type: Boolean, Default: true
4244 */
4345 ( function( $ ) {
4446
@@ -146,6 +148,14 @@
147149 var expWidth = -1;
148150 var $autoEllipseMe = $( [] );
149151 for ( var i = 0; i < context.config.suggestions.length; i++ ) {
 152+ var text = context.config.suggestions[i];
 153+ if( context.config.highlightInput ) {
 154+ var text = '<strong>'
 155+ + text.substr( 0, context.data.prevText.length)
 156+ + '</strong>'
 157+ + text.substr( context.data.prevText.length, text.length );
 158+ }
 159+
150160 var $result = $( '<div />' )
151161 .addClass( 'suggestions-result' )
152162 .attr( 'rel', i )
@@ -159,12 +169,12 @@
160170
161171 // Allow custom rendering
162172 if ( typeof context.config.result.render == 'function' ) {
163 - context.config.result.render.call( $result, context.config.suggestions[i] );
 173+ context.config.result.render.call( $result, text );
164174 } else {
165175 // Add <span> with text
166 - $result.append( $( '<span />' )
 176+ $result.append( $( '<div />' )
167177 .css( 'whiteSpace', 'nowrap' )
168 - .text( context.config.suggestions[i] )
 178+ .html( text )
169179 );
170180
171181 // Widen results box if needed
@@ -197,6 +207,7 @@
198208 break;
199209 case 'submitOnClick':
200210 case 'positionFromLeft':
 211+ case 'highlightInput':
201212 context.config[property] = value ? true : false;
202213 break;
203214 }
@@ -348,7 +359,8 @@
349360 'delay': 120,
350361 'submitOnClick': false,
351362 'maxExpandFactor': 3,
352 - 'positionFromLeft': true
 363+ 'positionFromLeft': true,
 364+ 'highlightInput': true
353365 }
354366 };
355367 }
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -5795,6 +5795,8 @@
57965796 * Type: Number, Range: 1 - infinity, Default: 3
57975797 * positionFromLeft: Whether to position the suggestion box with the left attribute or the right
57985798 * Type: Boolean, Default: true
 5799+ * highlightInput: Highlights the matched text in the suggestions
 5800+ * Type: Boolean, Default: true
57995801 */
58005802 ( function( $ ) {
58015803
@@ -5903,6 +5905,14 @@
59045906 var expWidth = -1;
59055907 var $autoEllipseMe = $( [] );
59065908 for ( var i = 0; i < context.config.suggestions.length; i++ ) {
 5909+ var text = context.config.suggestions[i];
 5910+ if( context.config.highlightInput ) {
 5911+ var text = '<strong>'
 5912+ + text.substr( 0, context.data.prevText.length)
 5913+ + '</strong>'
 5914+ + text.substr( context.data.prevText.length, text.length );
 5915+ }
 5916+
59075917 var $result = $( '<div />' )
59085918 .addClass( 'suggestions-result' )
59095919 .attr( 'rel', i )
@@ -5916,12 +5926,12 @@
59175927
59185928 // Allow custom rendering
59195929 if ( typeof context.config.result.render == 'function' ) {
5920 - context.config.result.render.call( $result, context.config.suggestions[i] );
 5930+ context.config.result.render.call( $result, text );
59215931 } else {
59225932 // Add <span> with text
5923 - $result.append( $( '<span />' )
 5933+ $result.append( $( '<div />' )
59245934 .css( 'whiteSpace', 'nowrap' )
5925 - .text( context.config.suggestions[i] )
 5935+ .html( text )
59265936 );
59275937
59285938 // Widen results box if needed
@@ -5954,6 +5964,7 @@
59555965 break;
59565966 case 'submitOnClick':
59575967 case 'positionFromLeft':
 5968+ case 'highlightInput':
59585969 context.config[property] = value ? true : false;
59595970 break;
59605971 }
@@ -6105,7 +6116,8 @@
61066117 'delay': 120,
61076118 'submitOnClick': false,
61086119 'maxExpandFactor': 3,
6109 - 'positionFromLeft': true
 6120+ 'positionFromLeft': true,
 6121+ 'highlightInput': true
61106122 }
61116123 };
61126124 }
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -397,11 +397,15 @@
398398 if(delayed){context.data.timerID=setTimeout(maybeFetch,context.config.delay);}else{maybeFetch();}
399399 $.suggestions.special(context);},special:function(context){if(typeof context.config.special.render=='function'){setTimeout(function(){$special=context.data.$container.find('.suggestions-special');context.config.special.render.call($special,context.data.$textbox.val());},1);}},configure:function(context,property,value){switch(property){case'fetch':case'cancel':case'special':case'result':case'$region':context.config[property]=value;break;case'suggestions':context.config[property]=value;if(typeof context.data!=='undefined'){if(context.data.$textbox.val().length==0){context.data.$container.hide();}else{context.data.$container.show();var newCSS={'top':context.config.$region.offset().top+context.config.$region.outerHeight(),'bottom':'auto','width':context.config.$region.outerWidth(),'height':'auto'}
400400 if(context.config.positionFromLeft){newCSS['left']=context.config.$region.offset().left;newCSS['right']='auto';}else{newCSS['left']='auto';newCSS['right']=$('body').width()-(context.config.$region.offset().left+context.config.$region.outerWidth());}
401 -context.data.$container.css(newCSS);var $results=context.data.$container.children('.suggestions-results');$results.empty();var expWidth=-1;var $autoEllipseMe=$([]);for(var i=0;i<context.config.suggestions.length;i++){var $result=$('<div />').addClass('suggestions-result').attr('rel',i).data('text',context.config.suggestions[i]).mouseover(function(e){$.suggestions.highlight(context,$(this).closest('.suggestions-results div'),false);}).appendTo($results);if(typeof context.config.result.render=='function'){context.config.result.render.call($result,context.config.suggestions[i]);}else{$result.append($('<span />').css('whiteSpace','nowrap').text(context.config.suggestions[i]));var $span=$result.children('span');if($span.outerWidth()>$result.width()&&$span.outerWidth()>expWidth){expWidth=$span.outerWidth();}
 401+context.data.$container.css(newCSS);var $results=context.data.$container.children('.suggestions-results');$results.empty();var expWidth=-1;var $autoEllipseMe=$([]);for(var i=0;i<context.config.suggestions.length;i++){var text=context.config.suggestions[i];if(context.config.highlightInput){var text='<strong>'
 402++text.substr(0,context.data.prevText.length)
 403++'</strong>'
 404++text.substr(context.data.prevText.length,text.length);}
 405+var $result=$('<div />').addClass('suggestions-result').attr('rel',i).data('text',context.config.suggestions[i]).mouseover(function(e){$.suggestions.highlight(context,$(this).closest('.suggestions-results div'),false);}).appendTo($results);if(typeof context.config.result.render=='function'){context.config.result.render.call($result,text);}else{$result.append($('<div />').css('whiteSpace','nowrap').html(text));var $span=$result.children('span');if($span.outerWidth()>$result.width()&&$span.outerWidth()>expWidth){expWidth=$span.outerWidth();}
402406 $autoEllipseMe=$autoEllipseMe.add($result);}}
403407 if(expWidth>context.data.$container.width()){var maxWidth=context.config.maxExpandFactor*context.data.$textbox.width();context.data.$container.width(Math.min(expWidth,maxWidth));}
404408 $autoEllipseMe.autoEllipsis({hasSpan:true,tooltip:true});}}
405 -break;case'maxRows':context.config[property]=Math.max(1,Math.min(100,value));break;case'delay':context.config[property]=Math.max(0,Math.min(1200,value));break;case'maxExpandFactor':context.config[property]=Math.max(1,value);break;case'submitOnClick':case'positionFromLeft':context.config[property]=value?true:false;break;}},highlight:function(context,result,updateTextbox){var selected=context.data.$container.find('.suggestions-result-current');if(!result.get||selected.get(0)!=result.get(0)){if(result=='prev'){if(selected.is('.suggestions-special')){result=context.data.$container.find('.suggestions-results div:last')}else{result=selected.prev();if(selected.length==0){if(context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}else{result=context.data.$container.find('.suggestions-results div:last');}}}}else if(result=='next'){if(selected.length==0){result=context.data.$container.find('.suggestions-results div:first');if(result.length==0&&context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}}else{result=selected.next();if(selected.is('.suggestions-special')){result=$([]);}else if(result.length==0&&context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}}}
 409+break;case'maxRows':context.config[property]=Math.max(1,Math.min(100,value));break;case'delay':context.config[property]=Math.max(0,Math.min(1200,value));break;case'maxExpandFactor':context.config[property]=Math.max(1,value);break;case'submitOnClick':case'positionFromLeft':case'highlightInput':context.config[property]=value?true:false;break;}},highlight:function(context,result,updateTextbox){var selected=context.data.$container.find('.suggestions-result-current');if(!result.get||selected.get(0)!=result.get(0)){if(result=='prev'){if(selected.is('.suggestions-special')){result=context.data.$container.find('.suggestions-results div:last')}else{result=selected.prev();if(selected.length==0){if(context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}else{result=context.data.$container.find('.suggestions-results div:last');}}}}else if(result=='next'){if(selected.length==0){result=context.data.$container.find('.suggestions-results div:first');if(result.length==0&&context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}}else{result=selected.next();if(selected.is('.suggestions-special')){result=$([]);}else if(result.length==0&&context.data.$container.find('.suggestions-special').html()!=""){result=context.data.$container.find('.suggestions-special');}}}
406410 selected.removeClass('suggestions-result-current');result.addClass('suggestions-result-current');}
407411 if(updateTextbox){if(result.length==0){$.suggestions.restore(context);}else{context.data.$textbox.val(result.data('text'));context.data.$textbox.change();}
408412 context.data.$textbox.trigger('change');}
@@ -409,7 +413,7 @@
410414 preventDefault=true;break;case 38:if(wasVisible){$.suggestions.highlight(context,'prev',false);}
411415 preventDefault=wasVisible;break;case 27:context.data.$container.hide();$.suggestions.restore(context);$.suggestions.cancel(context);context.data.$textbox.trigger('change');preventDefault=wasVisible;break;case 13:context.data.$container.hide();preventDefault=wasVisible;selected=context.data.$container.find('.suggestions-result-current');if(selected.size()==0){$.suggestions.cancel(context);context.config.$region.closest('form').submit();}else if(selected.is('.suggestions-special')){if(typeof context.config.special.select=='function'){context.config.special.select.call(selected,context.data.$textbox);}}else{if(typeof context.config.result.select=='function'){$.suggestions.highlight(context,selected,true);context.config.result.select.call(selected,context.data.$textbox);}else{$.suggestions.highlight(context,selected,true);}}
412416 break;default:$.suggestions.update(context,true);break;}
413 -if(preventDefault){e.preventDefault();e.stopImmediatePropagation();}}};$.fn.suggestions=function(){var returnValue=null;var args=arguments;$(this).each(function(){var context=$(this).data('suggestions-context');if(typeof context=='undefined'||context==null){context={config:{'fetch':function(){},'cancel':function(){},'special':{},'result':{},'$region':$(this),'suggestions':[],'maxRows':7,'delay':120,'submitOnClick':false,'maxExpandFactor':3,'positionFromLeft':true}};}
 417+if(preventDefault){e.preventDefault();e.stopImmediatePropagation();}}};$.fn.suggestions=function(){var returnValue=null;var args=arguments;$(this).each(function(){var context=$(this).data('suggestions-context');if(typeof context=='undefined'||context==null){context={config:{'fetch':function(){},'cancel':function(){},'special':{},'result':{},'$region':$(this),'suggestions':[],'maxRows':7,'delay':120,'submitOnClick':false,'maxExpandFactor':3,'positionFromLeft':true,'highlightInput':true}};}
414418 if(args.length>0){if(typeof args[0]=='object'){for(var key in args[0]){$.suggestions.configure(context,key,args[0][key]);}}else if(typeof args[0]=='string'){if(args.length>1){$.suggestions.configure(context,args[0],args[1]);}else if(returnValue==null){returnValue=(args[0]in context.config?undefined:context.config[args[0]]);}}}
415419 if(typeof context.data=='undefined'){context.data={'timerID':null,'prevText':null,'visibleResults':0,'mouseDownOn':$([]),'$textbox':$(this)};var newCSS={'top':Math.round(context.data.$textbox.offset().top+context.data.$textbox.outerHeight()),'width':context.data.$textbox.outerWidth(),'display':'none'}
416420 if(context.config.positionFromLeft){newCSS['left']=context.config.$region.offset().left;newCSS['right']='auto';}else{newCSS['left']='auto';newCSS['right']=$('body').width()-(context.config.$region.offset().left+context.config.$region.outerWidth());}

Follow-up revisions

RevisionCommit summaryAuthorDate
r66662Follow up to r66658. Not sure if this fixes everything but i can not figure o...adam22:23, 19 May 2010
r67741UsabilityInitiative: Temporarily revert r66658, r66662, r66690 so UsabilityIn...catrope18:42, 9 June 2010

Comments

#Comment by Adammiller~mediawikiwiki (talk | contribs)   21:27, 19 May 2010

Broke auto elipsing and auto expansion with the switch to a div.

#Comment by Catrope (talk | contribs)   21:32, 19 May 2010
+							var text = context.config.suggestions[i];
+							if( context.config.highlightInput ) {
+								var text = '<strong>' 
+									+ text.substr( 0, context.data.prevText.length) 
+									+ '</strong>'
+									+  text.substr( context.data.prevText.length, text.length );
+							}

Don't do this, use .text() properly: consider what would happen if text were to contain HTML tags (yes, this is legal).

Also, you shouldn't try to declare the text variable twice.

Why did you use a div, is it because you can't put a strong in a span? In that case, maybe try to put styled spans in the span?

#Comment by Adammiller~mediawikiwiki (talk | contribs)   14:32, 20 July 2010

No longer a problem. Reverted to the previous way it was working ( using spans and .text() ) and approached the problem a different way.


Status & tagging log