r57248 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57247‎ | r57248 | r57249 >
Date:20:23, 1 October 2009
Author:catrope
Status:ok
Tags:
Comment:
UsabilityInitiative: Some performance improvements for NTOC and autoEllipse
Modified paths:
  • /trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php (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.autoEllipse.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.toc.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -57,26 +57,26 @@
5858 'base_sets' => array(
5959 'raw' => array(
6060 array( 'src' => 'js/plugins/jquery.async.js', 'version' => 3 ),
61 - array( 'src' => 'js/plugins/jquery.autoEllipse.js', 'version' => 2 ),
 61+ array( 'src' => 'js/plugins/jquery.autoEllipse.js', 'version' => 3 ),
6262 array( 'src' => 'js/plugins/jquery.browser.js', 'version' => 3 ),
6363 array( 'src' => 'js/plugins/jquery.cookie.js', 'version' => 3 ),
6464 array( 'src' => 'js/plugins/jquery.delayedBind.js', 'version' => 1 ),
6565 array( 'src' => 'js/plugins/jquery.namespaceSelect.js', 'version' => 1 ),
6666 array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 4 ),
6767 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 14 ),
68 - array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 8 ),
 68+ array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 9 ),
6969 array( 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js', 'version' => 11 ),
7070 array( 'src' => 'js/plugins/jquery.wikiEditor.dialogs.js', 'version' => 4 ),
71 - array( 'src' => 'js/plugins/jquery.wikiEditor.toc.js', 'version' => 15 ),
 71+ array( 'src' => 'js/plugins/jquery.wikiEditor.toc.js', 'version' => 16 ),
7272 // FIXME: jQuery UI doesn't belong here, should move to no_js2
7373 // once we figure out how to do jQuery UI properly in JS2
7474 array( 'src' => 'js/js2/jquery-ui-1.7.2.js', 'version' => '1.7.2y' ),
7575 ),
7676 'combined' => array(
77 - array( 'src' => 'js/plugins.combined.js', 'version' => 36 ),
 77+ array( 'src' => 'js/plugins.combined.js', 'version' => 37 ),
7878 ),
7979 'minified' => array(
80 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 36 ),
 80+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 37 ),
8181 ),
8282 ),
8383 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.autoEllipse.js
@@ -15,13 +15,24 @@
1616 if ( $text.outerWidth() > $(this).innerWidth() ) {
1717 switch ( options.position ) {
1818 case 'right':
19 - var l = text.length;
20 - while ( $text.outerWidth() > $(this).innerWidth() && l > 0 ) {
21 - $text.text( text.substr( 0, l ) + '...' );
22 - l--;
23 - }
 19+ // Use binary search-like technique for
 20+ // efficiency
 21+ var l = 0, r = text.length;
 22+ var ow, iw;
 23+ do {
 24+ var m = Math.ceil( ( l + r ) / 2 );
 25+ $text.text( text.substr( 0, m ) + '...' );
 26+ ow = $text.outerWidth();
 27+ iw = $(this).innerWidth();
 28+ if ( ow > iw )
 29+ // Text is too long
 30+ r = m - 1;
 31+ else
 32+ l = m;
 33+ } while ( l < r );
2434 break;
2535 case 'center':
 36+ // TODO: Use binary search like for 'right'
2637 var i = [Math.round( text.length / 2 ), Math.round( text.length / 2 )];
2738 var side = 1; // Begin with making the end shorter
2839 while ( $text.outerWidth() > ( $(this).innerWidth() ) && i[0] > 0 ) {
@@ -39,6 +50,7 @@
4051 }
4152 break;
4253 case 'left':
 54+ // TODO: Use binary search like for 'right'
4355 var r = 0;
4456 while ( $text.outerWidth() > $(this).innerWidth() && r < text.length ) {
4557 $text.text( '...' + text.substr( r ) );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -70,7 +70,7 @@
7171 .height( 0 )
7272 .width( 0 )
7373 .insertBefore( $.wikiEditor.instances[0] );
74 - var textarea = $( '<textarea></textarea' )
 74+ var textarea = $( '<textarea />' )
7575 .height( 0 )
7676 .appendTo( div )
7777 .val( "foo\r\nbar" );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.toc.js
@@ -23,10 +23,9 @@
2424 if ( '$toc' in context.modules ) {
2525 return;
2626 }
27 - context.modules.$toc = $( '<div></div>' )
 27+ context.modules.$toc = $( '<div />' )
2828 .addClass( 'wikiEditor-ui-toc' )
2929 .attr( 'id', 'wikiEditor-ui-toc' );
30 - $.wikiEditor.modules.toc.fn.build( context, config );
3130 context.$ui.find( '.wikiEditor-ui-bottom' )
3231 .append( context.modules.$toc );
3332 context.modules.$toc.height(
@@ -35,12 +34,13 @@
3635 // Make some css modifications to make room for the toc on the right...
3736 // Perhaps this could be configurable?
3837 context.modules.$toc
39 - .css( 'width', '12em' )
40 - .css( 'marginTop', -( context.$ui.find( '.wikiEditor-ui-bottom' ).height() ) );
 38+ .css( { 'width': '12em',
 39+ 'marginTop': -( context.$ui.find( '.wikiEditor-ui-bottom' ).height() )
 40+ } );
4141 context.$ui.find( '.wikiEditor-ui-text' )
4242 .css( ( $( 'body.rtl' ).size() ? 'marginLeft' : 'marginRight' ), '12em' );
4343 // Add the TOC to the document
44 - $.wikiEditor.modules.toc.fn.build( context );
 44+ $.wikiEditor.modules.toc.fn.build( context, config );
4545 context.$textarea
4646 .delayedBind( 1000, 'keyup encapsulateSelection change',
4747 function( event ) {
@@ -146,11 +146,11 @@
147147 * @param {Object} structure Structured outline
148148 */
149149 function buildList( structure ) {
150 - var list = $( '<ul></ul>' );
 150+ var list = $( '<ul />' );
151151 for ( i in structure ) {
152 - var item = $( '<li></li>' )
 152+ var item = $( '<li />' )
153153 .append(
154 - $( '<a></a>' )
 154+ $( '<a />' )
155155 .attr( 'href', '#' )
156156 .addClass( 'section-' + structure[i].index )
157157 .data( 'textbox', context.$textarea )
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -93,13 +93,24 @@
9494 if ( $text.outerWidth() > $(this).innerWidth() ) {
9595 switch ( options.position ) {
9696 case 'right':
97 - var l = text.length;
98 - while ( $text.outerWidth() > $(this).innerWidth() && l > 0 ) {
99 - $text.text( text.substr( 0, l ) + '...' );
100 - l--;
101 - }
 97+ // Use binary search-like technique for
 98+ // efficiency
 99+ var l = 0, r = text.length;
 100+ var ow, iw;
 101+ do {
 102+ var m = Math.ceil( ( l + r ) / 2 );
 103+ $text.text( text.substr( 0, m ) + '...' );
 104+ ow = $text.outerWidth();
 105+ iw = $(this).innerWidth();
 106+ if ( ow > iw )
 107+ // Text is too long
 108+ r = m - 1;
 109+ else
 110+ l = m;
 111+ } while ( l < r );
102112 break;
103113 case 'center':
 114+ // TODO: Use binary search like for 'right'
104115 var i = [Math.round( text.length / 2 ), Math.round( text.length / 2 )];
105116 var side = 1; // Begin with making the end shorter
106117 while ( $text.outerWidth() > ( $(this).innerWidth() ) && i[0] > 0 ) {
@@ -117,6 +128,7 @@
118129 }
119130 break;
120131 case 'left':
 132+ // TODO: Use binary search like for 'right'
121133 var r = 0;
122134 while ( $text.outerWidth() > $(this).innerWidth() && r < text.length ) {
123135 $text.text( '...' + text.substr( r ) );
@@ -1191,7 +1203,7 @@
11921204 .height( 0 )
11931205 .width( 0 )
11941206 .insertBefore( $.wikiEditor.instances[0] );
1195 - var textarea = $( '<textarea></textarea' )
 1207+ var textarea = $( '<textarea />' )
11961208 .height( 0 )
11971209 .appendTo( div )
11981210 .val( "foo\r\nbar" );
@@ -2032,10 +2044,9 @@
20332045 if ( '$toc' in context.modules ) {
20342046 return;
20352047 }
2036 - context.modules.$toc = $( '<div></div>' )
 2048+ context.modules.$toc = $( '<div />' )
20372049 .addClass( 'wikiEditor-ui-toc' )
20382050 .attr( 'id', 'wikiEditor-ui-toc' );
2039 - $.wikiEditor.modules.toc.fn.build( context, config );
20402051 context.$ui.find( '.wikiEditor-ui-bottom' )
20412052 .append( context.modules.$toc );
20422053 context.modules.$toc.height(
@@ -2044,12 +2055,13 @@
20452056 // Make some css modifications to make room for the toc on the right...
20462057 // Perhaps this could be configurable?
20472058 context.modules.$toc
2048 - .css( 'width', '12em' )
2049 - .css( 'marginTop', -( context.$ui.find( '.wikiEditor-ui-bottom' ).height() ) );
 2059+ .css( { 'width': '12em',
 2060+ 'marginTop': -( context.$ui.find( '.wikiEditor-ui-bottom' ).height() )
 2061+ } );
20502062 context.$ui.find( '.wikiEditor-ui-text' )
20512063 .css( ( $( 'body.rtl' ).size() ? 'marginLeft' : 'marginRight' ), '12em' );
20522064 // Add the TOC to the document
2053 - $.wikiEditor.modules.toc.fn.build( context );
 2065+ $.wikiEditor.modules.toc.fn.build( context, config );
20542066 context.$textarea
20552067 .delayedBind( 1000, 'keyup encapsulateSelection change',
20562068 function( event ) {
@@ -2155,11 +2167,11 @@
21562168 * @param {Object} structure Structured outline
21572169 */
21582170 function buildList( structure ) {
2159 - var list = $( '<ul></ul>' );
 2171+ var list = $( '<ul />' );
21602172 for ( i in structure ) {
2161 - var item = $( '<li></li>' )
 2173+ var item = $( '<li />' )
21622174 .append(
2163 - $( '<a></a>' )
 2175+ $( '<a />' )
21642176 .attr( 'href', '#' )
21652177 .addClass( 'section-' + structure[i].index )
21662178 .data( 'textbox', context.$textarea )
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -11,8 +11,9 @@
1212 {var i=0,l=array.length,loop=opts.loop||function(){};$.whileAsync($.extend(opts,{test:function(){return i<l;},loop:function()
1313 {var val=array[i];return loop.call(val,i++,val);}}));}
1414 $.fn.eachAsync=function(opts)
15 -{$.eachAsync(this,opts);return this;}})(jQuery);(function($){$.fn.autoEllipse=function(options){$(this).each(function(){options=$.extend({'position':'center','tooltip':false},options);var text=$(this).text();var $text=$('<span />').text(text).css('whiteSpace','nowrap');$(this).empty().append($text);if($text.outerWidth()>$(this).innerWidth()){switch(options.position){case'right':var l=text.length;while($text.outerWidth()>$(this).innerWidth()&&l>0){$text.text(text.substr(0,l)+'...');l--;}
16 -break;case'center':var i=[Math.round(text.length/2),Math.round(text.length/2)];var side=1;while($text.outerWidth()>($(this).innerWidth())&&i[0]>0){$text.text(text.substr(0,i[0])+'...'+text.substr(i[1]));if(side==0){i[0]--;side=1;}else{i[1]++;side=0;}}
 15+{$.eachAsync(this,opts);return this;}})(jQuery);(function($){$.fn.autoEllipse=function(options){$(this).each(function(){options=$.extend({'position':'center','tooltip':false},options);var text=$(this).text();var $text=$('<span />').text(text).css('whiteSpace','nowrap');$(this).empty().append($text);if($text.outerWidth()>$(this).innerWidth()){switch(options.position){case'right':var l=0,r=text.length;var ow,iw;do{var m=Math.ceil((l+r)/2);$text.text(text.substr(0,m)+'...');ow=$text.outerWidth();iw=$(this).innerWidth();if(ow>iw)
 16+r=m-1;else
 17+l=m;}while(l<r);break;case'center':var i=[Math.round(text.length/2),Math.round(text.length/2)];var side=1;while($text.outerWidth()>($(this).innerWidth())&&i[0]>0){$text.text(text.substr(0,i[0])+'...'+text.substr(i[1]));if(side==0){i[0]--;side=1;}else{i[1]++;side=0;}}
1718 break;case'left':var r=0;while($text.outerWidth()>$(this).innerWidth()&&r<text.length){$text.text('...'+text.substr(r));r++;}
1819 break;}
1920 if(options.tooltip)
@@ -78,7 +79,7 @@
7980 $(this).scrollTop(scroll);}else if(document.selection&&document.selection.createRange){var range=document.selection.createRange();var pos=$(this).getCaretPosition();var oldScrollTop=this.scrollTop;range.moveToElementText(this);range.collapse();range.move('character',pos+1);range.select();if(this.scrollTop!=oldScrollTop)
8081 this.scrollTop+=range.offsetTop;else if(force){range.move('character',-1);range.select();}}
8182 $(this).trigger('scrollToPosition');});}});})(jQuery);(function($){$.wikiEditor={'modules':{},'instances':[],'supportedBrowsers':{'ltr':{'msie':7,'firefox':2,'opera':9,'safari':3,'chrome':1,'camino':1},'rtl':{'msie':8,'firefox':2,'opera':9,'safari':3,'chrome':1,'camino':1}},imgPath:wgScriptPath+'/extensions/UsabilityInitiative/images/wikiEditor/'};$.wikiEditor.isSupportKnown=function(){return(function(supportedBrowsers){return $.browser.name in supportedBrowsers;})($.wikiEditor.supportedBrowsers[$('body.rtl').size()?'rtl':'ltr']);};$.wikiEditor.isSupported=function(){return(function(supportedBrowsers){return $.browser.name in supportedBrowsers&&$.browser.versionNumber>=supportedBrowsers[$.browser.name];})($.wikiEditor.supportedBrowsers[$('body.rtl').size()?'rtl':'ltr']);};$.wikiEditor.autoMsg=function(object,property){if(typeof property=='object'){for(i in property){if(property[i]in object||property[i]+'Msg'in object){property=property[i];break;}}}
82 -if(property in object){return object[property];}else if(property+'Msg'in object){return gM(object[property+'Msg']);}else{return'';}};$.wikiEditor.fixOperaBrokenness=function(s){if(typeof $.isOperaBroken=='undefined'&&$.wikiEditor.instances.length>0){var div=$('<div />').height(0).width(0).insertBefore($.wikiEditor.instances[0]);var textarea=$('<textarea></textarea').height(0).appendTo(div).val("foo\r\nbar");var index=textarea.val().indexOf('bar');textarea.select();textarea.setSelection(index,index+3);textarea.encapsulateSelection('','BAR','',false,true);if(textarea.val().substr(-1)=='R')
 83+if(property in object){return object[property];}else if(property+'Msg'in object){return gM(object[property+'Msg']);}else{return'';}};$.wikiEditor.fixOperaBrokenness=function(s){if(typeof $.isOperaBroken=='undefined'&&$.wikiEditor.instances.length>0){var div=$('<div />').height(0).width(0).insertBefore($.wikiEditor.instances[0]);var textarea=$('<textarea />').height(0).appendTo(div).val("foo\r\nbar");var index=textarea.val().indexOf('bar');textarea.select();textarea.setSelection(index,index+3);textarea.encapsulateSelection('','BAR','',false,true);if(textarea.val().substr(-1)=='R')
8384 $.isOperaBroken=false;else
8485 $.isOperaBroken=true;div.remove();}
8586 if($.isOperaBroken)
@@ -137,14 +138,14 @@
138139 return $section;},updateBookletSelection:function(context,id,$pages,$index){var cookie='wikiEditor-'+context.instance+'-booklet-'+id+'-page';var selected=$.cookie(cookie);var $selectedIndex=$index.find('*[rel='+selected+']');if($selectedIndex.size()==0){selected=$index.children().eq(0).attr('rel');$.cookie(cookie,selected);}
139140 $pages.children().hide();$pages.find('*[rel='+selected+']').show();$index.children().removeClass('current');$selectedIndex.addClass('current');},build:function(context,config){var $tabs=$('<div />').addClass('tabs').appendTo(context.modules.$toolbar);var $sections=$('<div />').addClass('sections').appendTo(context.modules.$toolbar);context.modules.$toolbar.append($('<div />').css('clear','both'));var sectionQueue=[];for(section in config){if(section=='main'){context.modules.$toolbar.prepend($.wikiEditor.modules.toolbar.fn.buildSection(context,section,config[section]));}else{sectionQueue.push({'$sections':$sections,'context':context,'id':section,'config':config[section]});$tabs.append($.wikiEditor.modules.toolbar.fn.buildTab(context,section,config[section]));}}
140141 $.eachAsync(sectionQueue,{'bulk':0,'end':function(){$('body').css('position','static');$('body').css('position','relative');},'loop':function(i,s){s.$sections.append($.wikiEditor.modules.toolbar.fn.buildSection(s.context,s.id,s.config));}});}}};})(jQuery);(function($){$.wikiEditor.modules.toc={api:{},fn:{create:function(context,config){if('$toc'in context.modules){return;}
141 -context.modules.$toc=$('<div></div>').addClass('wikiEditor-ui-toc').attr('id','wikiEditor-ui-toc');$.wikiEditor.modules.toc.fn.build(context,config);context.$ui.find('.wikiEditor-ui-bottom').append(context.modules.$toc);context.modules.$toc.height(context.$ui.find('.wikiEditor-ui-bottom').height());context.modules.$toc.css('width','12em').css('marginTop',-(context.$ui.find('.wikiEditor-ui-bottom').height()));context.$ui.find('.wikiEditor-ui-text').css(($('body.rtl').size()?'marginLeft':'marginRight'),'12em');$.wikiEditor.modules.toc.fn.build(context);context.$textarea.delayedBind(1000,'keyup encapsulateSelection change',function(event){var context=$(this).data('wikiEditor-context');$(this).eachAsync({bulk:0,loop:function(){$.wikiEditor.modules.toc.fn.build(context);$.wikiEditor.modules.toc.fn.update(context);}});}).bind('mouseup scrollToPosition focus keyup encapsulateSelection change',function(event){var context=$(this).data('wikiEditor-context');$(this).eachAsync({bulk:0,loop:function(){$.wikiEditor.modules.toc.fn.update(context);}});}).blur(function(){var context=$(this).data('wikiEditor-context');$.wikiEditor.modules.toc.fn.unhighlight(context);});},unhighlight:function(context){context.modules.$toc.find('a').removeClass('currentSelection');},update:function(context){$.wikiEditor.modules.toc.fn.unhighlight(context);var position=context.$textarea.getCaretPosition();var section=0;if(context.data.outline.length>0){if(!(position<context.data.outline[0].position-1)){while(section<context.data.outline.length&&context.data.outline[section].position-1<position){section++;}
 142+context.modules.$toc=$('<div />').addClass('wikiEditor-ui-toc').attr('id','wikiEditor-ui-toc');context.$ui.find('.wikiEditor-ui-bottom').append(context.modules.$toc);context.modules.$toc.height(context.$ui.find('.wikiEditor-ui-bottom').height());context.modules.$toc.css({'width':'12em','marginTop':-(context.$ui.find('.wikiEditor-ui-bottom').height())});context.$ui.find('.wikiEditor-ui-text').css(($('body.rtl').size()?'marginLeft':'marginRight'),'12em');$.wikiEditor.modules.toc.fn.build(context,config);context.$textarea.delayedBind(1000,'keyup encapsulateSelection change',function(event){var context=$(this).data('wikiEditor-context');$(this).eachAsync({bulk:0,loop:function(){$.wikiEditor.modules.toc.fn.build(context);$.wikiEditor.modules.toc.fn.update(context);}});}).bind('mouseup scrollToPosition focus keyup encapsulateSelection change',function(event){var context=$(this).data('wikiEditor-context');$(this).eachAsync({bulk:0,loop:function(){$.wikiEditor.modules.toc.fn.update(context);}});}).blur(function(){var context=$(this).data('wikiEditor-context');$.wikiEditor.modules.toc.fn.unhighlight(context);});},unhighlight:function(context){context.modules.$toc.find('a').removeClass('currentSelection');},update:function(context){$.wikiEditor.modules.toc.fn.unhighlight(context);var position=context.$textarea.getCaretPosition();var section=0;if(context.data.outline.length>0){if(!(position<context.data.outline[0].position-1)){while(section<context.data.outline.length&&context.data.outline[section].position-1<position){section++;}
142143 section=Math.max(0,section);}
143144 var sectionLink=context.modules.$toc.find('a.section-'+section);sectionLink.addClass('currentSelection');var relTop=sectionLink.offset().top-context.modules.$toc.offset().top;var scrollTop=context.modules.$toc.scrollTop();var divHeight=context.modules.$toc.height();var sectionHeight=sectionLink.height();if(relTop<0)
144145 context.modules.$toc.scrollTop(scrollTop+relTop);else if(relTop+sectionHeight>divHeight)
145146 context.modules.$toc.scrollTop(scrollTop+relTop+sectionHeight-divHeight);}},build:function(context){function buildStructure(outline,offset,level){if(offset==undefined)offset=0;if(level==undefined)level=1;var sections=[];for(var i=offset;i<outline.length;i++){if(outline[i].nLevel==level){var sub=buildStructure(outline,i+1,level+1);if(sub.length){outline[i].sections=sub;}
146147 sections[sections.length]=outline[i];}else if(outline[i].nLevel<level){break;}}
147148 return sections;}
148 -function buildList(structure){var list=$('<ul></ul>');for(i in structure){var item=$('<li></li>').append($('<a></a>').attr('href','#').addClass('section-'+structure[i].index).data('textbox',context.$textarea).data('position',structure[i].position).click(function(event){$(this).data('textbox').setSelection($(this).data('position')).scrollToCaretPosition(true);event.preventDefault();}).text(structure[i].text));if(structure[i].sections!==undefined){item.append(buildList(structure[i].sections));}
 149+function buildList(structure){var list=$('<ul />');for(i in structure){var item=$('<li />').append($('<a />').attr('href','#').addClass('section-'+structure[i].index).data('textbox',context.$textarea).data('position',structure[i].position).click(function(event){$(this).data('textbox').setSelection($(this).data('position')).scrollToCaretPosition(true);event.preventDefault();}).text(structure[i].text));if(structure[i].sections!==undefined){item.append(buildList(structure[i].sections));}
149150 list.append(item);}
150151 return list;}
151152 var outline=[];var wikitext='\n'+$.wikiEditor.fixOperaBrokenness(context.$textarea.val())+'\n';var headings=wikitext.match(/^={1,6}.+={1,6}\s*$/gm);var offset=0;headings=$.makeArray(headings);for(var h=0;h<headings.length;h++){text=headings[h];var position=wikitext.indexOf(text,offset);if(position>offset){offset=position+1;}else if(position==-1){continue;}

Follow-up revisions

RevisionCommit summaryAuthorDate
r57719wmf-deployment: Merge babaco fixes from trunk...catrope20:20, 14 October 2009

Status & tagging log