r105022 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105021‎ | r105022 | r105023 >
Date:23:01, 2 December 2011
Author:inez
Status:deferred
Tags:
Comment:
Optimization for toolbarView. Count data for all toolbars (currently two) once, and do it not more often than 50ms.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/views/es.ToolbarView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -30,6 +30,7 @@
3131 this.insertionAnnotations = [];
3232 this.updateSelectionTimeout = undefined;
3333 this.emitUpdateTimeout = undefined;
 34+ this.emitCursorTimeout = undefined;
3435
3536 // Interaction states
3637
@@ -145,6 +146,42 @@
146147 };
147148
148149 /* Methods */
 150+es.SurfaceView.prototype.emitCursor = function() {
 151+ if ( this.emitCursorTimeout ) {
 152+ clearTimeout( this.emitCursorTimeout );
 153+ }
 154+ var _this = this;
 155+ this.emitCursorTimeout = setTimeout( function() {
 156+ var annotations,
 157+ nodes = [],
 158+ model = _this.documentView.model;
 159+
 160+ if( _this.currentSelection.from === _this.currentSelection.to ) {
 161+ var insertionAnnotations = _this.getInsertionAnnotations();
 162+ annotations = {
 163+ 'full': insertionAnnotations,
 164+ 'partial': [],
 165+ 'all': insertionAnnotations
 166+ };
 167+ nodes.push( model.getNodeFromOffset( _this.currentSelection.from ) );
 168+ } else {
 169+ annotations = model.getAnnotationsFromRange( _this.currentSelection );
 170+ var startNode = model.getNodeFromOffset( _this.currentSelection.start ),
 171+ endNode = model.getNodeFromOffset( _this.currentSelection.end );
 172+ if ( startNode === endNode ) {
 173+ nodes.push( startNode );
 174+ } else {
 175+ model.traverseLeafNodes( function( node ) {
 176+ nodes.push( node );
 177+ if( node === endNode ) {
 178+ return false;
 179+ }
 180+ }, startNode );
 181+ }
 182+ }
 183+ _this.emit( 'cursor', annotations, nodes );
 184+ }, 50 );
 185+};
149186
150187 es.SurfaceView.prototype.getInsertionAnnotations = function() {
151188 return this.insertionAnnotations;
@@ -152,7 +189,7 @@
153190
154191 es.SurfaceView.prototype.addInsertionAnnotation = function( annotation ) {
155192 this.insertionAnnotations.push( annotation );
156 - this.emit( 'cursor' );
 193+ this.emitCursor();
157194 };
158195
159196 es.SurfaceView.prototype.loadInsertionAnnotations = function( annotation ) {
@@ -165,7 +202,7 @@
166203 i--;
167204 }
168205 }
169 - this.emit( 'cursor' );
 206+ this.emitCursor();
170207 };
171208
172209 es.SurfaceView.prototype.removeInsertionAnnotation = function( annotation ) {
@@ -173,12 +210,12 @@
174211 if ( index !== -1 ) {
175212 this.insertionAnnotations.splice( index, 1 );
176213 }
177 - this.emit( 'cursor' );
 214+ this.emitCursor();
178215 };
179216
180217 es.SurfaceView.prototype.clearInsertionAnnotations = function() {
181218 this.insertionAnnotations = [];
182 - this.emit( 'cursor' );
 219+ this.emitCursor();
183220 };
184221
185222 es.SurfaceView.prototype.getModel = function() {
Index: trunk/extensions/VisualEditor/modules/es/views/es.ToolbarView.js
@@ -40,12 +40,12 @@
4141 }
4242 }
4343 } );
44 - this.surfaceView.model.on( 'select', function() {
45 - _this.updateState();
 44+
 45+ this.surfaceView.on( 'cursor', function( annotations, nodes ) {
 46+ for( var i = 0; i < _this.tools.length; i++ ) {
 47+ _this.tools[i].updateState( annotations, nodes );
 48+ }
4649 } );
47 - this.surfaceView.on( 'cursor', function() {
48 - _this.updateState();
49 - } );
5050
5151 this.config = config || [
5252 { 'name': 'history', 'items' : ['undo', 'redo'] },
@@ -56,40 +56,6 @@
5757 this.setup();
5858 };
5959
60 -es.ToolbarView.prototype.updateState = function() {
61 - var selection = this.surfaceView.currentSelection,
62 - annotations,
63 - nodes = [];
64 -
65 - if( selection.from === selection.to ) {
66 - var insertionAnnotations = this.surfaceView.getInsertionAnnotations();
67 - annotations = {
68 - 'full': insertionAnnotations,
69 - 'partial': [],
70 - 'all': insertionAnnotations
71 - };
72 - nodes.push( this.surfaceView.documentView.model.getNodeFromOffset( selection.from ) );
73 - } else {
74 - annotations = this.surfaceView.documentView.model.getAnnotationsFromRange( selection );
75 - var startNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.start ),
76 - endNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.end );
77 - if ( startNode === endNode ) {
78 - nodes.push( startNode );
79 - } else {
80 - this.surfaceView.documentView.model.traverseLeafNodes( function( node ) {
81 - nodes.push( node );
82 - if( node === endNode ) {
83 - return false;
84 - }
85 - }, startNode );
86 - }
87 - }
88 -
89 - for( var i = 0; i < this.tools.length; i++ ) {
90 - this.tools[i].updateState( annotations, nodes );
91 - }
92 -};
93 -
9460 es.ToolbarView.prototype.setup = function() {
9561 for ( var i = 0; i < this.config.length; i++ ) {
9662 var $group = $( '<div>' )

Status & tagging log