http://www.mediawiki.org/wiki/Special:Code/MediaWiki/97856

Revision: 97856
Author:   inez
Date:     2011-09-22 21:30:14 +0000 (Thu, 22 Sep 2011)
Log Message:
-----------
Basic implementation of getOffsetFromPosition in many places.
Switch from having document inside a table cell to just having list of blocks.

Modified Paths:
--------------
    trunk/parsers/wikidom/demos/synth/es.js
    trunk/parsers/wikidom/demos/synth/index.html
    trunk/parsers/wikidom/lib/synth/bases/es.ViewList.js
    trunk/parsers/wikidom/lib/synth/models/es.TableBlockCellModel.js
    trunk/parsers/wikidom/lib/synth/views/es.BlockView.js
    trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js
    trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js
    trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js
    trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js
    trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js
    trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js
    trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js

Added Paths:
-----------
    trunk/parsers/wikidom/lib/synth/bases/es.Position.js

Modified: trunk/parsers/wikidom/demos/synth/es.js
===================================================================
--- trunk/parsers/wikidom/demos/synth/es.js     2011-09-22 21:27:04 UTC (rev 
97855)
+++ trunk/parsers/wikidom/demos/synth/es.js     2011-09-22 21:30:14 UTC (rev 
97856)
@@ -46,25 +46,21 @@
                                        'cells': [
                                                {
                                                        'attributes': { 
'style': 'border: solid 1px;' },
-                                                       'document': {
-                                                               'blocks': [
-                                                                       {
-                                                                               
'type': 'paragraph',
-                                                                               
'content': { 'text': 'row 1 & cell 1' }
-                                                                       }
-                                                               ]
-                                                       }
+                                                       'blocks': [
+                                                               {
+                                                                       'type': 
'paragraph',
+                                                                       
'content': { 'text': 'row 1 & cell 1' }
+                                                               }
+                                                       ]
                                                },
                                                {
                                                        'attributes': { 
'style': 'border: solid 1px;' },
-                                                       'document': {
-                                                               'blocks': [
-                                                                       {
-                                                                               
'type': 'paragraph',
-                                                                               
'content': { 'text': 'row 1 & cell 2' }
-                                                                       }
-                                                               ]
-                                                       }
+                                                       'blocks': [
+                                                               {
+                                                                       'type': 
'paragraph',
+                                                                       
'content': { 'text': 'row 1 & cell 2' }
+                                                               }
+                                                       ]
                                                }
                                        ]
                                },
@@ -72,86 +68,82 @@
                                        'cells': [
                                                {
                                                        'attributes': { 
'style': 'border: solid 1px;' },
-                                                       'document': {
-                                                               'blocks': [
-                                                                       {
-                                                                               
'type': 'paragraph',
-                                                                               
'content': { 'text': 'row 2 & cell 1' }
-                                                                       }
-                                                               ]
-                                                       }
+                                                       'blocks': [
+                                                               {
+                                                                       'type': 
'paragraph',
+                                                                       
'content': { 'text': 'row 2 & cell 1' }
+                                                               }
+                                                       ]
                                                },
                                                {
                                                        'attributes': { 
'style': 'border: solid 1px;' },
-                                                       'document': {
-                                                               'blocks': [
-                                                                       {
-                                                                               
'type': 'paragraph',
-                                                                               
'content': { 'text': 'row 2 & cell 2' }
-                                                                       },
-                                                                       {
-                                                                               
'type': 'list',
-                                                                               
'style': 'number',
-                                                                               
'items': [
-                                                                               
        {
-                                                                               
                'content': { 'text': 'First item' },
-                                                                               
                'lists': [
+                                                       'blocks': [
+                                                               {
+                                                                       'type': 
'paragraph',
+                                                                       
'content': { 'text': 'row 2 & cell 2' }
+                                                               },
+                                                               {
+                                                                       'type': 
'list',
+                                                                       
'style': 'number',
+                                                                       
'items': [
+                                                                               
{
+                                                                               
        'content': { 'text': 'First item' },
+                                                                               
        'lists': [
+                                                                               
                {
+                                                                               
                        'style': 'bullet',
+                                                                               
                        'items': [
+                                                                               
                                { 'content': { 'text': 'First subitem' } },
+                                                                               
                                { 'content': { 'text': 'Second subitem' } }
+                                                                               
                        ]
+                                                                               
                }
+                                                                               
        ]
+                                                                               
},
+                                                                               
{
+                                                                               
        'content': {
+                                                                               
                'text': 'Second item',
+                                                                               
                'annotations': [
                                                                                
                        {
-                                                                               
                                'style': 'bullet',
-                                                                               
                                'items': [
-                                                                               
                                        { 'content': { 'text': 'First subitem' 
} },
-                                                                               
                                        { 'content': { 'text': 'Second subitem' 
} }
-                                                                               
                                ]
+                                                                               
                                'type': 'italic',
+                                                                               
                                'range': {
+                                                                               
                                        'start': 0,
+                                                                               
                                        'end': 6
+                                                                               
                                }
                                                                                
                        }
                                                                                
                ]
-                                                                               
        },
-                                                                               
        {
-                                                                               
                'content': {
-                                                                               
                        'text': 'Second item',
-                                                                               
                        'annotations': [
-                                                                               
                                {
-                                                                               
                                        'type': 'italic',
-                                                                               
                                        'range': {
-                                                                               
                                                'start': 0,
-                                                                               
                                                'end': 6
-                                                                               
                                        }
+                                                                               
        }
+                                                                               
},
+                                                                               
{
+                                                                               
        'content': {
+                                                                               
                'text': 'Third item',
+                                                                               
                'annotations': [
+                                                                               
                        {
+                                                                               
                                'type': 'bold',
+                                                                               
                                'range': {
+                                                                               
                                        'start': 0,
+                                                                               
                                        'end': 5
                                                                                
                                }
-                                                                               
                        ]
-                                                                               
                }
-                                                                               
        },
-                                                                               
        {
-                                                                               
                'content': {
-                                                                               
                        'text': 'Third item',
-                                                                               
                        'annotations': [
-                                                                               
                                {
-                                                                               
                                        'type': 'bold',
-                                                                               
                                        'range': {
-                                                                               
                                                'start': 0,
-                                                                               
                                                'end': 5
-                                                                               
                                        }
-                                                                               
                                }
-                                                                               
                        ]
-                                                                               
                }
-                                                                               
        },
-                                                                               
        {
-                                                                               
                'content': {
-                                                                               
                        'text': 'Fourth item',
-                                                                               
                        'annotations': [
-                                                                               
                                {
-                                                                               
                                        'type': 'ilink',
-                                                                               
                                        'range': {
-                                                                               
                                                'start': 7,
-                                                                               
                                                'end': 11
-                                                                               
                                        },
-                                                                               
                                        'data': { 'title': 'User:JohnDoe' }
-                                                                               
                                }
-                                                                               
                        ]
-                                                                               
                }
+                                                                               
                        }
+                                                                               
                ]
                                                                                
        }
-                                                                               
]
-                                                                       }       
                                                                
-                                                               ]
-                                                       }
+                                                                               
},
+                                                                               
{
+                                                                               
        'content': {
+                                                                               
                'text': 'Fourth item',
+                                                                               
                'annotations': [
+                                                                               
                        {
+                                                                               
                                'type': 'ilink',
+                                                                               
                                'range': {
+                                                                               
                                        'start': 7,
+                                                                               
                                        'end': 11
+                                                                               
                                },
+                                                                               
                                'data': { 'title': 'User:JohnDoe' }
+                                                                               
                        }
+                                                                               
                ]
+                                                                               
        }
+                                                                               
}
+                                                                       ]
+                                                               }               
                                                        
+                                                       ]
                                                }
                                        ]
                                }

Modified: trunk/parsers/wikidom/demos/synth/index.html
===================================================================
--- trunk/parsers/wikidom/demos/synth/index.html        2011-09-22 21:27:04 UTC 
(rev 97855)
+++ trunk/parsers/wikidom/demos/synth/index.html        2011-09-22 21:30:14 UTC 
(rev 97856)
@@ -58,7 +58,8 @@
                <script src="../../lib/synth/es.Range.js"></script>
                <script src="../../lib/synth/es.Html.js"></script>
                <script src="../../lib/synth/es.Selection.js"></script>
-               
+               <script src="../../lib/synth/es.Position.js"></script>
+
                <!-- Bases -->
                <script src="../../lib/synth/bases/es.EventEmitter.js"></script>
                <script 
src="../../lib/synth/bases/es.AggregateArray.js"></script>

Added: trunk/parsers/wikidom/lib/synth/bases/es.Position.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/bases/es.Position.js                        
        (rev 0)
+++ trunk/parsers/wikidom/lib/synth/bases/es.Position.js        2011-09-22 
21:30:14 UTC (rev 97856)
@@ -0,0 +1,4 @@
+es.Position = function( left, top ) {
+       this.left = left || 0;
+       this.top = top || 0;
+};
\ No newline at end of file

Modified: trunk/parsers/wikidom/lib/synth/bases/es.ViewList.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/bases/es.ViewList.js        2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/bases/es.ViewList.js        2011-09-22 
21:30:14 UTC (rev 97856)
@@ -44,6 +44,7 @@
        
        this.model.on( 'prepend', function( itemModel ) {
                var itemView = list.recycleItemView( itemModel, true );
+               itemView.list = list;
                itemView.on( 'update', list.relayUpdate );
                list.items.unshift( itemView );
                list.$.prepend( itemView.$ );
@@ -52,6 +53,7 @@
        } );
        this.model.on( 'append', function( itemModel ) {
                var itemView = list.recycleItemView( itemModel, true );
+               itemView.list = list;
                itemView.on( 'update', list.relayUpdate );
                list.items.push( itemView );
                list.$.append( itemView.$ );
@@ -61,6 +63,7 @@
        this.model.on( 'insertBefore', function( itemModel, beforeModel ) {
                var beforeView = list.lookupItemView( beforeModel ),
                        itemView = list.recycleItemView( itemModel, true );
+               itemView.list = list;
                itemView.on( 'update', list.relayUpdate );
                if ( beforeView ) {
                        list.items.splice( list.items.indexOf( beforeView ), 0, 
itemView );
@@ -75,6 +78,7 @@
        this.model.on( 'insertAfter', function( itemModel, afterModel ) {
                var afterView = list.lookupItemView( afterModel ),
                        itemView = list.recycleItemView( itemModel, true );
+               itemView.list = list;
                itemView.on( 'update', list.relayUpdate );
                if ( afterView ) {
                        list.items.splice( list.items.indexOf( afterView ) + 1, 
0, itemView );
@@ -88,6 +92,7 @@
        } );
        this.model.on( 'remove', function( itemModel ) {
                var itemView = list.recycleItemView( itemModel );
+               itemView.list = null;
                itemView.removeListener( 'update', list.relayUpdate );
                list.emit( 'remove', itemView );
                list.emit( 'update' );
@@ -97,6 +102,7 @@
        var itemModels = this.model.all();
        for ( var i = 0; i < itemModels.length; i++ ) {
                var itemView = itemModels[i].createView();
+               itemView.list = list;
                itemView.on( 'update', this.relayUpdate );
                this.items.push( itemView );
                this.$.append( itemView.$ );

Modified: trunk/parsers/wikidom/lib/synth/models/es.TableBlockCellModel.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/models/es.TableBlockCellModel.js    
2011-09-22 21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/models/es.TableBlockCellModel.js    
2011-09-22 21:30:14 UTC (rev 97856)
@@ -8,9 +8,14 @@
  * @property documentModel {es.DocumentModel}
  * @property attributes {Object}
  */
-es.TableBlockCellModel = function( documentModel, attributes ) {
+es.TableBlockCellModel = function( blocks, attributes ) {
        es.ModelListItem.call( this );
-       this.documentModel = documentModel || null;
+       es.ModelList.call( this );
+       if ( $.isArray( blocks ) ) {
+               for ( var i = 0; i < blocks.length; i++ ) {
+                       this.append( blocks[i] );
+               }
+       }
        this.attributes = attributes || {};
 };
 
@@ -23,10 +28,12 @@
  */
 es.TableBlockCellModel.newFromPlainObject = function( obj ) {
        return new es.TableBlockCellModel(
-               // Cells - if given, convert plain document object to 
es.DocumentModel objects
-               !$.isPlainObject( obj.document ) ? null : 
es.DocumentModel.newFromPlainObject( obj.document ),
+               // Blocks - if given, convert all plain "block" objects to 
es.WikiDom* objects
+               !$.isArray( obj.blocks ) ? [] : $.map( obj.blocks, function( 
block ) {
+                       return es.BlockModel.newFromPlainObject( block );
+               } ),
                // Attributes - if given, make a deep copy of attributes
-               !$.isPlainObject( obj.attributes ) ? {} : $.extend( true, {}, 
obj.attributes )
+               !$.isPlainObject( obj.attributes ) ? {} : $.extend( true, {}, 
obj.attributes )          
        );
 };
 
@@ -102,4 +109,5 @@
 
 /* Inheritance */
 
-es.extend( es.TableBlockCellModel, es.ModelListItem );
\ No newline at end of file
+es.extend( es.TableBlockCellModel, es.ModelListItem );
+es.extend( es.TableBlockCellModel, es.ModelList );
\ No newline at end of file

Modified: trunk/parsers/wikidom/lib/synth/views/es.BlockView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.BlockView.js       2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.BlockView.js       2011-09-22 
21:30:14 UTC (rev 97856)
@@ -10,6 +10,8 @@
  */
 es.BlockView = function( blockModel, $element ) {
        es.ViewListItem.call( this, blockModel, $element );
+       this.$.addClass( 'editSurface-block' );
+       this.$.data( 'block', this );
 };
 
 /**

Modified: trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js    2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.DocumentView.js    2011-09-22 
21:30:14 UTC (rev 97856)
@@ -12,6 +12,22 @@
 
 /* Methods */
 
+es.DocumentView.prototype.getOffsetFromPosition = function( position ) {
+       if ( this.items.length === 0 ) {
+               return 0;
+       }
+       
+       var blockView = this.items[0];
+       for ( var i = 0; i < this.items.length; i++ ) {
+               if ( this.items[i].$.offset().top >= position.top ) {
+                       break;
+               }
+               blockView = this.items[i];
+       }
+
+       return blockView.getOffsetFromPosition( position );
+};
+
 /**
  * Render content.
  * 

Modified: trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js   2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.ListBlockView.js   2011-09-22 
21:30:14 UTC (rev 97856)
@@ -18,6 +18,26 @@
 
 /* Methods */
 
+es.ListBlockView.prototype.getOffsetFromPosition = function( position ) {
+       var contentOffset;
+       var itemHeight;
+       for ( var i = 0; i < this.items.length; i++ ) {
+               contentOffset = this.items[i].$content.offset();
+               if ( position.top >= contentOffset.top ) {
+                       itemHeight = this.items[i].$.height();
+                       if ( position.top < contentOffset.top + itemHeight ) {
+                               position.left -= contentOffset.left;
+                               position.top -= contentOffset.top;
+                               return this.items[i].contentView.getOffset( 
position );
+                       }
+               }
+       }
+       
+       while(!documentView.list) {
+               
+       }
+};
+
 /**
  * Render content.
  * 

Modified: trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js      
2011-09-22 21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.ParagraphBlockView.js      
2011-09-22 21:30:14 UTC (rev 97856)
@@ -7,9 +7,7 @@
  */
 es.ParagraphBlockView = function( model ) {
        es.BlockView.call( this, model );
-       
        this.$.addClass( 'editSurface-paragraphBlock' );
-       
        this.contentView = new es.ContentView( this.$, this.model.content );
        var view = this;
        this.contentView.on( 'update', function() {
@@ -19,6 +17,13 @@
 
 /* Methods */
 
+es.ParagraphBlockView.prototype.getOffsetFromPosition = function( position ) {
+       var blockPosition = this.$.offset();
+       position.left -= blockPosition.left;
+       position.top -= blockPosition.top;
+       return this.contentView.getOffset( position );
+};
+
 /**
  * Render content.
  * 

Modified: trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js     2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.SurfaceView.js     2011-09-22 
21:30:14 UTC (rev 97856)
@@ -235,7 +235,9 @@
 };
 
 es.SurfaceView.prototype.onMouseDown = function( e ) {
-       // TODO: Respond to mouse down event, moving cursor and possibly 
beginning selection painting
+       var position = es.Position.newFromEventPagePosition( e );
+       var offset = this.documentView.getOffsetFromPosition( position );
+       console.log( offset );
        return false;
 };
 

Modified: trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js      
2011-09-22 21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.TableBlockCellView.js      
2011-09-22 21:30:14 UTC (rev 97856)
@@ -6,21 +6,38 @@
  * @constructor
  */
 es.TableBlockCellView = function( model ) {
-       es.ViewListItem.call( this, model, $( '<td>' ) );
-       this.documentView = new es.DocumentView( this.model.documentModel );
-       this.$.append( this.documentView.$ );
+       es.ViewList.call( this, model, $( '<td>' ) );
+       es.ViewListItem.call( this, model, this.$ );
        this.$.attr( this.model.attributes );
 };
 
 /* Methods */
 
+es.TableBlockCellView.prototype.getOffsetFromPosition = function( position ) {
+       var blockOffset;
+       var itemHeight;
+       
+       for ( var i = 0; i < this.items.length; i++ ) {
+               blockOffset = this.items[i].$.offset();
+               if ( position.top >= blockOffset.top ) {
+                       itemHeight = this.items[i].$.height();
+                       if ( position.top < blockOffset.top + itemHeight ) {
+                               return this.items[i].getOffsetFromPosition( 
position );
+                       }
+               }
+       }
+};
+
+
 /**
  * Render content.
  * 
  * @method
  */
 es.TableBlockCellView.prototype.renderContent = function() {
-       this.documentView.renderContent();
+       for ( var i = 0; i < this.items.length; i++ ) {
+               this.items[i].renderContent();
+       }
 };
 
 /**
@@ -79,3 +96,4 @@
 /* Inheritance */
 
 es.extend( es.TableBlockCellView, es.ViewListItem );
+es.extend( es.TableBlockCellView, es.ViewList );
\ No newline at end of file

Modified: trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js       
2011-09-22 21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.TableBlockRowView.js       
2011-09-22 21:30:14 UTC (rev 97856)
@@ -14,6 +14,21 @@
 
 /* Methods */
 
+es.TableBlockRowView.prototype.getOffsetFromPosition = function( position ) {
+       var cellOffset;
+       var itemWidth;
+       
+       for ( var i = 0; i < this.items.length; i++ ) {
+               cellOffset = this.items[i].$.offset();
+               if ( position.left >= cellOffset.left ) {
+                       itemWidth = this.items[i].$.width();
+                       if ( position.left < cellOffset.left + itemWidth ) {
+                               return this.items[i].getOffsetFromPosition( 
position );
+                       }
+               }
+       }
+};
+
 /**
  * Render content.
  * 

Modified: trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js
===================================================================
--- trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js  2011-09-22 
21:27:04 UTC (rev 97855)
+++ trunk/parsers/wikidom/lib/synth/views/es.TableBlockView.js  2011-09-22 
21:30:14 UTC (rev 97856)
@@ -15,6 +15,21 @@
 
 /* Methods */
 
+es.TableBlockView.prototype.getOffsetFromPosition = function( position ) {
+       var rowOffset;
+       var itemHeight;
+
+       for ( var i = 0; i < this.items.length; i++ ) {
+               rowOffset = this.items[i].$.offset();
+               if ( position.top >= rowOffset.top ) {
+                       itemHeight = this.items[i].$.height();
+                       if ( position.top < rowOffset.top + itemHeight ) {
+                               return this.items[i].getOffsetFromPosition( 
position );
+                       }
+               }
+       }
+};
+
 /**
  * Render content.
  * 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to