Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php |
— | — | @@ -83,10 +83,10 @@ |
84 | 84 | array( 'src' => 'js/thirdparty/contentCollector.js', 'version' => 2 ), |
85 | 85 | ), |
86 | 86 | 'combined' => array( |
87 | | - array( 'src' => 'js/plugins.combined.js', 'version' => 452 ), |
| 87 | + array( 'src' => 'js/plugins.combined.js', 'version' => 453 ), |
88 | 88 | ), |
89 | 89 | 'minified' => array( |
90 | | - array( 'src' => 'js/plugins.combined.min.js', 'version' => 470 ), |
| 90 | + array( 'src' => 'js/plugins.combined.min.js', 'version' => 471 ), |
91 | 91 | ), |
92 | 92 | ), |
93 | 93 | ); |
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js |
— | — | @@ -961,6 +961,68 @@ |
962 | 962 | |
963 | 963 | } )( jQuery ); |
964 | 964 | /** |
| 965 | + * Plugin that highlights matched word partials in a given element |
| 966 | + * TODO: add a function for restoring the previous text |
| 967 | + * TODO: accept mappings for converting shortcuts like WP: to Wikipedia: |
| 968 | + */ |
| 969 | +( function( $ ) { |
| 970 | + |
| 971 | +$.highlightText = { |
| 972 | + |
| 973 | + // Split our pattern string at spaces and run our highlight function on the results |
| 974 | + splitAndHighlight: function( node, pat ) { |
| 975 | + var patArray = pat.split(" "); |
| 976 | + for ( var i = 0; i < patArray.length; i++ ) { |
| 977 | + if ( patArray[i].length == 0 ) continue; |
| 978 | + $.highlightText.innerHighlight( node, patArray[i] ); |
| 979 | + } |
| 980 | + return node; |
| 981 | + }, |
| 982 | + // scans a node looking for the pattern and wraps a span around each match |
| 983 | + innerHighlight: function( node, pat ) { |
| 984 | + // if this is a text node |
| 985 | + if ( node.nodeType == 3 ) { |
| 986 | + // TODO - need to be smarter about the character matching here. |
| 987 | + // non latin characters can make regex think a new word has begun. |
| 988 | + // look for an occurence of our pattern and store the starting position |
| 989 | + var pos = node.data.search( new RegExp( "\\b" + RegExp.escape( pat ), "i" ) ); |
| 990 | + if ( pos >= 0 ) { |
| 991 | + // create the span wrapper for the matched text |
| 992 | + var spannode = document.createElement( 'span' ); |
| 993 | + spannode.className = 'highlight'; |
| 994 | + // shave off the characters preceding the matched text |
| 995 | + var middlebit = node.splitText( pos ); |
| 996 | + // shave off any unmatched text off the end |
| 997 | + middlebit.splitText( pat.length ); |
| 998 | + // clone for appending to our span |
| 999 | + var middleclone = middlebit.cloneNode( true ); |
| 1000 | + // append the matched text node to the span |
| 1001 | + spannode.appendChild( middleclone ); |
| 1002 | + // replace the matched node, with our span-wrapped clone of the matched node |
| 1003 | + middlebit.parentNode.replaceChild( spannode, middlebit ); |
| 1004 | + } |
| 1005 | + // if this is an element with childnodes, and not a script, style or an element we created |
| 1006 | + } else if ( node.nodeType == 1 && node.childNodes && !/(script|style)/i.test( node.tagName ) |
| 1007 | + && !( node.tagName.toLowerCase() == 'span' && node.className.match( /\bhighlight/ ) ) ) { |
| 1008 | + for ( var i = 0; i < node.childNodes.length; ++i ) { |
| 1009 | + // call the highlight function for each child node |
| 1010 | + $.highlightText.innerHighlight( node.childNodes[i], pat ); |
| 1011 | + } |
| 1012 | + } |
| 1013 | + } |
| 1014 | +}; |
| 1015 | + |
| 1016 | +$.fn.highlightText = function( matchString ) { |
| 1017 | + return $( this ).each( function() { |
| 1018 | + var $this = $( this ); |
| 1019 | + $this.data( 'highlightText', { originalText: $this.text() } ); |
| 1020 | + $.highlightText.splitAndHighlight( this, matchString ); |
| 1021 | + } ); |
| 1022 | +}; |
| 1023 | + |
| 1024 | +} )( jQuery ); |
| 1025 | + |
| 1026 | +/** |
965 | 1027 | * This plugin provides a generic way to add suggestions to a text box. |
966 | 1028 | * |
967 | 1029 | * Usage: |
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js |
— | — | @@ -58,7 +58,8 @@ |
59 | 59 | clearTimeout(timerID);});},delayedBindUnbind:function(timeout,event,callback){var encEvent=encodeEvent(event);return this.each(function(){$(this).unbind('_delayedBind-'+encEvent+'-'+timeout,callback);});}});})(jQuery);(function($){$.expandableField={expandField:function(e,context){context.config.beforeExpand.call(context.data.$field,context);context.data.$field.animate({'width':context.data.expandedWidth},'fast',function(){context.config.afterExpand.call(this,context);});},condenseField:function(e,context){context.config.beforeCondense.call(context.data.$field,context);context.data.$field.animate({'width':context.data.condensedWidth},'fast',function(){context.config.afterCondense.call(this,context);});},configure:function(context,property,value){switch(property){default:context.config[property]=value;break;}}};$.fn.expandableField=function(){var returnValue=null;var args=arguments;$(this).each(function(){var context=$(this).data('expandableField-context');if(context==null){context={config:{'beforeCondense':function(context){},'beforeExpand':function(context){},'afterCondense':function(context){},'afterExpand':function(context){},'expandToLeft':true}};} |
60 | 60 | if(args.length>0){if(typeof args[0]=='object'){for(var key in args[0]){$.expandableField.configure(context,key,args[0][key]);}}else if(typeof args[0]=='string'){if(args.length>1){$.expandableField.configure(context,args[0],args[1]);}else if(returnValue==null){returnValue=(args[0]in context.config?undefined:context.config[args[0]]);}}} |
61 | 61 | if(typeof context.data=='undefined'){context.data={'condensedWidth':$(this).width(),'expandedWidth':$(this).width()*2,'$field':$(this)};$(this).addClass('expandableField').focus(function(e){$.expandableField.expandField(e,context);}).delayedBind(250,'blur',function(e){$.expandableField.condenseField(e,context);});} |
62 | | -$(this).data('expandableField-context',context);});return returnValue!==null?returnValue:$(this);};})(jQuery);(function($){$.suggestions={cancel:function(context){if(context.data.timerID!=null){clearTimeout(context.data.timerID);} |
| 62 | +$(this).data('expandableField-context',context);});return returnValue!==null?returnValue:$(this);};})(jQuery);(function($){$.highlightText={splitAndHighlight:function(node,pat){var patArray=pat.split(" ");for(var i=0;i<patArray.length;i++){if(patArray[i].length==0)continue;$.highlightText.innerHighlight(node,patArray[i]);} |
| 63 | +return node;},innerHighlight:function(node,pat){if(node.nodeType==3){var pos=node.data.search(new RegExp("\\b"+RegExp.escape(pat),"i"));if(pos>=0){var spannode=document.createElement('span');spannode.className='highlight';var middlebit=node.splitText(pos);middlebit.splitText(pat.length);var middleclone=middlebit.cloneNode(true);spannode.appendChild(middleclone);middlebit.parentNode.replaceChild(spannode,middlebit);}}else if(node.nodeType==1&&node.childNodes&&!/(script|style)/i.test(node.tagName)&&!(node.tagName.toLowerCase()=='span'&&node.className.match(/\bhighlight/))){for(var i=0;i<node.childNodes.length;++i){$.highlightText.innerHighlight(node.childNodes[i],pat);}}}};$.fn.highlightText=function(matchString){return $(this).each(function(){var $this=$(this);$this.data('highlightText',{originalText:$this.text()});$.highlightText.splitAndHighlight(this,matchString);});};})(jQuery);(function($){$.suggestions={cancel:function(context){if(context.data.timerID!=null){clearTimeout(context.data.timerID);} |
63 | 64 | if(typeof context.config.cancel=='function'){context.config.cancel.call(context.data.$textbox);}},restore:function(context){context.data.$textbox.val(context.data.prevText);},update:function(context,delayed){function maybeFetch(){if(context.data.$textbox.val()!==context.data.prevText){context.data.prevText=context.data.$textbox.val();if(typeof context.config.fetch=='function'){context.config.fetch.call(context.data.$textbox,context.data.$textbox.val());}}} |
64 | 65 | if(context.data.timerID!=null){clearTimeout(context.data.timerID);} |
65 | 66 | if(delayed){context.data.timerID=setTimeout(maybeFetch,context.config.delay);}else{maybeFetch();} |
Index: trunk/extensions/UsabilityInitiative/Makefile |
— | — | @@ -31,6 +31,7 @@ |
32 | 32 | js/plugins/jquery.cookie.js\ |
33 | 33 | js/plugins/jquery.delayedBind.js\ |
34 | 34 | js/plugins/jquery.expandableField.js\ |
| 35 | + js/plugins/jquery.highlightText.js\ |
35 | 36 | js/plugins/jquery.suggestions.js\ |
36 | 37 | js/plugins/jquery.textSelection.js\ |
37 | 38 | js/plugins/jquery.wikiEditor.js\ |