r92216 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92215‎ | r92216 | r92217 >
Date:23:17, 14 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Using events to update things, rather than manually synchronizing rendering
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Block.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Document.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Surface.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Document.js
@@ -4,6 +4,7 @@
55 * @returns {Document}
66 */
77 function Document( blocks ) {
 8+ EventEmitter.call( this );
89 this.blocks = [];
910 var i;
1011 for( i = 0; i < blocks.length; i++ ) {
@@ -39,6 +40,9 @@
4041 */
4142 Document.prototype.appendBlock = function( block ) {
4243 block.document = this;
 44+ block.on( 'update', function() {
 45+ block.document.emit( 'update' );
 46+ } );
4347 this.blocks.push( block );
4448 };
4549
@@ -49,6 +53,9 @@
5054 */
5155 Document.prototype.prependBlock = function( block ) {
5256 block.document = this;
 57+ block.on( 'update', function() {
 58+ block.document.emit( 'update' );
 59+ } );
5360 this.blocks.unshift( block );
5461 };
5562
@@ -60,6 +67,9 @@
6168 */
6269 Document.prototype.insertBlockBefore = function( block, before ) {
6370 block.document = this;
 71+ block.on( 'update', function() {
 72+ block.document.emit( 'update' );
 73+ } );
6474 if ( before ) {
6575 this.blocks.splice( before.getIndex(), 0, block );
6676 } else {
@@ -73,6 +83,9 @@
7484 */
7585 Document.prototype.insertBlockAfter = function( block, after ) {
7686 block.document = this;
 87+ block.on( 'update', function() {
 88+ block.document.emit( 'update' );
 89+ } );
7790 if ( after ) {
7891 this.blocks.splice( after.getIndex() + 1, 0, block );
7992 } else {
@@ -86,22 +99,23 @@
87100 * @param {Block} Block to remove
88101 */
89102 Document.prototype.removeBlock = function( block ) {
 103+ block.removeAllListeners( 'update' );
90104 this.blocks.splice( block.getIndex(), 1 );
91105 block.document = null;
92106 };
93107
94 -Document.prototype.renderBlocks = function( offset, callback ) {
 108+Document.prototype.renderBlocks = function( offset ) {
95109 // Remember width, to avoid updates when without width changes
96110 this.width = this.$.innerWidth();
97111 // Render blocks
98112 var i;
99113 for ( i = 0; i < this.blocks.length; i++ ) {
100114 this.$.append( this.blocks[i].$ );
101 - this.blocks[i].renderContent( offset, callback );
 115+ this.blocks[i].renderContent( offset );
102116 }
103117 };
104118
105 -Document.prototype.updateBlocks = function( offset, callback ) {
 119+Document.prototype.updateBlocks = function( offset ) {
106120 // Bypass rendering when width has not changed
107121 var width = this.$.innerWidth();
108122 if ( this.width === width ) {
@@ -111,6 +125,8 @@
112126 // Render blocks
113127 var doc;
114128 this.$.children( '.editSurface-block' ).each( function( i ) {
115 - $(this).data( 'block' ).renderContent( offset, callback );
 129+ $(this).data( 'block' ).renderContent( offset );
116130 } );
117131 };
 132+
 133+extend( Document, EventEmitter );
Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
@@ -9,6 +9,10 @@
1010 this.$ = $( '<div class="editSurface-block editSurface-paragraph"></div>' )
1111 .data( 'block', this );
1212 this.flow = new TextFlow( this.$, this.content );
 13+ var block = this;
 14+ this.flow.on( 'render', function() {
 15+ block.emit( 'update' );
 16+ } );
1317 }
1418
1519 ParagraphBlock.prototype.getLength = function() {
@@ -48,8 +52,8 @@
4953 *
5054 * @param $container {jQuery Selection} Container to render into
5155 */
52 -ParagraphBlock.prototype.renderContent = function( offset, callback ) {
53 - this.flow.render( offset, callback );
 56+ParagraphBlock.prototype.renderContent = function( offset ) {
 57+ this.flow.render( offset );
5458 };
5559
5660 /**
Index: trunk/parsers/wikidom/lib/es/es.Surface.js
@@ -9,7 +9,6 @@
1010
1111 this.$ = $container.addClass( 'editSurface' );
1212 this.doc = doc;
13 - this.rendered = false;
1413 this.location = null;
1514 this.selection = new Selection();
1615 this.mouseSelecting = false;
@@ -35,42 +34,44 @@
3635 this.$.append( this.cursor.$ );
3736
3837 // Hidden input
 38+ var $document = $(document);
3939 this.$input = $( '<input class="editSurface-input" />' )
4040 .prependTo( this.$ )
4141 .bind({
4242 'focus' : function() {
4343 $(document).bind({
44 - 'mousemove.es' : function(e) {
 44+ 'mousemove.editSurface' : function(e) {
4545 return surface.onMouseMove( e );
4646 },
47 - 'mouseup.es' : function(e) {
 47+ 'mouseup.editSurface' : function(e) {
4848 return surface.onMouseUp( e );
4949 },
50 - 'keydown.es' : function( e ) {
 50+ 'keydown.editSurface' : function( e ) {
5151 return surface.onKeyDown( e );
5252 },
53 - 'keyup.es' : function( e ) {
 53+ 'keyup.editSurface' : function( e ) {
5454 return surface.onKeyUp( e );
5555 },
5656 });
5757 },
5858 'blur': function( e ) {
59 - $(document).unbind('mousemove.es');
60 - $(document).unbind('mouseup.es');
61 - $(document).unbind('keydown.es');
62 - $(document).unbind('keyup.es');
 59+ $document.unbind('.editSurface');
6360 surface.cursor.hide();
6461 }
6562 });
66 -
 63+
6764 $(window).resize( function() {
68 - surface.render( 0, function() {
69 - surface.drawSelection();
70 - } );
 65+ document.updateBlocks();
7166 } );
7267
 68+ this.doc.on( 'update', function() {
 69+ surface.drawSelection();
 70+ // TODO: Update the cursor position
 71+ } );
 72+
7373 // First render
74 - this.render();
 74+ this.$.append( this.doc.$ );
 75+ this.doc.renderBlocks();
7576 }
7677
7778 Surface.prototype.getLocationFromEvent = function( e ) {
@@ -504,21 +505,6 @@
505506 };
506507
507508 /**
508 - * Updates the rendered view.
509 - *
510 - * @param offset Location: Where to start re-flowing from (optional)
511 - */
512 -Surface.prototype.render = function( offset, callback ) {
513 - if ( !this.rendered ) {
514 - this.rendered = true;
515 - this.$.append( this.doc.$ );
516 - this.doc.renderBlocks( offset, callback );
517 - } else {
518 - this.doc.updateBlocks( offset, callback );
519 - }
520 -};
521 -
522 -/**
523509 * Applies an annotation to a given selection.
524510 *
525511 * If a selection argument is not provided, the current selection will be annotated.
@@ -542,9 +528,6 @@
543529 if ( from.block === to.block ) {
544530 // Single block annotation
545531 from.block.annotateContent( method, annotation, from.offset, to.offset );
546 - from.block.renderContent( from.offset, function() {
547 - surface.drawSelection();
548 - } );
549532 } else {
550533 // Multiple block annotation
551534 for ( i = from.block.getIndex(), end = to.block.getIndex(); i <= end; i++ ) {
@@ -552,21 +535,12 @@
553536 if ( block === from.block ) {
554537 // From offset to length
555538 block.annotateContent( method, annotation, from.offset, block.getLength() );
556 - block.renderContent( from.offset, function() {
557 - surface.drawSelection();
558 - } );
559539 } else if ( block === to.block ) {
560540 // From 0 to offset
561541 block.annotateContent( method, annotation, 0, to.offset );
562 - block.renderContent( 0, function() {
563 - surface.drawSelection();
564 - } );
565542 } else {
566543 // Full coverage
567544 block.annotateContent( method, annotation, 0, block.getLength() );
568 - block.renderContent( 0, function() {
569 - surface.drawSelection();
570 - } );
571545 }
572546 }
573547 }
Index: trunk/parsers/wikidom/lib/es/es.Block.js
@@ -3,6 +3,7 @@
44 * @returns {Block}
55 */
66 function Block() {
 7+ EventEmitter.call( this );
78 this.document = null;
89 }
910
@@ -106,3 +107,5 @@
107108 Block.prototype.annotateContent = function( method, annotation, start, end ) {
108109 throw 'Block.annotateContent not implemented in this subclass.';
109110 };
 111+
 112+extend( Block, EventEmitter );
Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -5,6 +5,7 @@
66 * @returns {TextFlow}
77 */
88 function TextFlow( $container, content ) {
 9+ EventEmitter.call( this );
910 this.$ = $container;
1011 this.content = content || new Content();
1112 this.boundaries = [];
@@ -13,6 +14,22 @@
1415 this.boundaryTest = /([ \-\t\r\n\f])/g;
1516 this.widthCache = {};
1617 this.renderState = {};
 18+
 19+ var flow = this;
 20+ this.content.on( 'change', function() {
 21+ flow.scanBoundaries();
 22+ } );
 23+ this.content.on( 'insert', function( args ) {
 24+ flow.render( args.offset );
 25+ } );
 26+ this.content.on( 'remove', function( args ) {
 27+ flow.render( args.start );
 28+ } );
 29+ this.content.on( 'annotate', function( args ) {
 30+ flow.render( args.start );
 31+ } );
 32+ this.scanBoundaries();
 33+ this.render();
1734 }
1835
1936 /**
@@ -225,9 +242,7 @@
226243 .nextAll()
227244 .remove();
228245 rs.timeout = undefined;
229 - if ( $.isFunction( rs.callback ) ) {
230 - rs.callback();
231 - }
 246+ this.emit( 'render' );
232247 } else {
233248 rs.ruler.innerHTML = '';
234249 var flow = this;
@@ -249,7 +264,7 @@
250265 *
251266 * @param offset {Integer} Offset to re-render from, if possible (not yet implemented)
252267 */
253 -TextFlow.prototype.render = function( offset, callback ) {
 268+TextFlow.prototype.render = function( offset ) {
254269 var rs = this.renderState;
255270
256271 // Stop iterating from last render
@@ -261,8 +276,6 @@
262277
263278 this.widthCache = {};
264279
265 - this.scanBoundaries();
266 -
267280 /*
268281 * Container measurement
269282 *
@@ -293,7 +306,6 @@
294307 rs.wordCount = this.boundaries.length;
295308 rs.ruler = rs.$ruler.addClass('editSurface-ruler')[0];
296309 rs.iterationLimit = 3;
297 - rs.callback = callback;
298310
299311 this.renderIteration();
300312 };
@@ -424,3 +436,5 @@
425437 } while ( start < end );
426438 return { 'end': start, 'width': lineWidth };
427439 };
 440+
 441+extend( TextFlow, EventEmitter );

Status & tagging log