r93119 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93118‎ | r93119 | r93120 >
Date:21:15, 25 July 2011
Author:inez
Status:deferred
Tags:
Comment:
Split classes into separated classes and implement recursive methods to get offset based on position
Modified paths:
  • /trunk/parsers/wikidom/demos/es/index.html (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlockItem.js (added) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlockList.js (added) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ListBlockList.js
@@ -0,0 +1,72 @@
 2+/**
 3+ * es.ListBlockList
 4+ */
 5+es.ListBlockList = function( style, items ) {
 6+ // Convert items to es.ListBlockItem objects
 7+ var listItems = [];
 8+ for ( var i = 0; i < items.length; i++ ) {
 9+ listItems.push( new es.ListBlockItem( items[i].line, items[i].lists || [] ) );
 10+ }
 11+
 12+ /*
 13+ * Initialize container
 14+ *
 15+ * - Adds class to container: "editSurface-list"
 16+ * - Sets .data( 'list', this )
 17+ * - Adds this.items array
 18+ */
 19+ es.Container.call( this, 'list', 'items', listItems );
 20+
 21+ this.style = style;
 22+}
 23+
 24+es.ListBlockList.prototype.getLength = function() {
 25+ var length = 0;
 26+ for ( var i = 0; i < this.items.length; i++ ) {
 27+ length += this.items[i].getLength();
 28+ }
 29+ return length;
 30+};
 31+
 32+es.ListBlockList.prototype.getLocationFromOffset = function( offset ) {
 33+ var itemOffset = 0,
 34+ itemLength;
 35+ for ( var i = 0; i < this.items.length; i++ ) {
 36+ itemLength = this.items[i].getLength();
 37+ if ( offset >= itemOffset && offset < itemOffset + itemLength ) {
 38+ var location = this.items[i].getLocationFromOffset( offset - itemOffset );
 39+ return {
 40+ 'item': location.item,
 41+ 'offset': location.offset
 42+ }
 43+ }
 44+ itemOffset += itemLength;
 45+ }
 46+};
 47+
 48+es.ListBlockList.prototype.getOffsetFromPosition = function( position ) {
 49+ var itemOffset = null,
 50+ globalOffset = null;
 51+
 52+ for ( var i = 0; i < this.items.length; i++ ) {
 53+ itemOffset = this.items[i].getOffsetFromPosition( position );
 54+
 55+ if ( itemOffset !== null ) {
 56+ return globalOffset + itemOffset;
 57+ } else {
 58+ globalOffset += this.items[i].getLength();
 59+ }
 60+ }
 61+};
 62+
 63+/**
 64+ * Renders content into a container.
 65+ */
 66+es.ListBlockList.prototype.renderContent = function( offset ) {
 67+ // TODO: Abstract offset and use it when rendering
 68+ for ( var i = 0; i < this.items.length; i++ ) {
 69+ this.items[i].renderContent();
 70+ }
 71+};
 72+
 73+es.extend( es.ListBlockList, es.Container );
\ No newline at end of file
Index: trunk/parsers/wikidom/lib/es/es.ListBlockItem.js
@@ -0,0 +1,94 @@
 2+/**
 3+ * es.ListBlockItem
 4+ */
 5+es.ListBlockItem = function( line, lists ) {
 6+ // Convert items to es.ListBlockItem objects
 7+ var itemLists = [];
 8+ for ( var i = 0; i < lists.length; i++ ) {
 9+ itemLists.push( new es.ListBlockList( lists[i].style, lists[i].items || [] ) );
 10+ }
 11+ /*
 12+ * Initialize container
 13+ *
 14+ * - Adds class to container: "editSurface-item"
 15+ * - Sets .data( 'item', this )
 16+ * - Adds this.lists array
 17+ */
 18+ es.Container.call( this, 'item', 'lists', itemLists );
 19+
 20+ this.$line = $( '<div class="editSurface-list-line"></div>' ).prependTo( this.$ )
 21+ this.$content = $( '<div class="editSurface-list-content"></div>' ).appendTo( this.$line );
 22+
 23+ this.content = line ? es.Content.newFromLine( line ) : new es.Content();
 24+ this.flow = new es.TextFlow( this.$content, this.content );
 25+ var item = this;
 26+ this.flow.on( 'render', function() {
 27+ item.emit( 'update' );
 28+ } );
 29+}
 30+
 31+es.ListBlockItem.prototype.getLength = function() {
 32+ var length = this.content.getLength();
 33+ for ( var i = 0; i < this.lists.length; i++ ) {
 34+ length += this.lists[i].getLength();
 35+ }
 36+ return length;
 37+};
 38+
 39+es.ListBlockItem.prototype.getLocationFromOffset = function( offset ) {
 40+ var contentLength = this.content.getLength();
 41+
 42+ if ( offset < contentLength ) {
 43+ return {
 44+ 'item': this,
 45+ 'offset': offset
 46+ };
 47+ }
 48+
 49+ offset -= contentLength;
 50+
 51+ var listOffset = 0,
 52+ listLength;
 53+ for ( var i = 0; i < this.lists.length; i++ ) {
 54+ listLength = this.lists[i].getLength();
 55+ if ( offset >= listOffset && offset < listOffset + listLength ) {
 56+ return this.lists[i].getLocationFromOffset( offset - listOffset );
 57+ }
 58+ listOffset += listLength;
 59+ }
 60+};
 61+
 62+es.ListBlockItem.prototype.getOffsetFromPosition = function( position ) {
 63+ var itemOffset = this.$.offset(),
 64+ itemHeight = this.$.height(),
 65+ offset = null,
 66+ globalOffset = null;
 67+
 68+ if ( position.top > itemOffset.top && position.top < itemOffset.top + itemHeight ) {
 69+ if ( position.top < itemOffset.top + this.$line.height() ) {
 70+ position.top -= itemOffset.top;
 71+ position.left -= itemOffset.left;
 72+ position.left += this.$.parent().offset().left - itemOffset.left;
 73+ return globalOffset + this.flow.getOffset( position );
 74+ }
 75+
 76+ for ( var i = 0; i < this.lists.length; i++ ) {
 77+ offset = this.lists[i].getOffsetFromPosition( position );
 78+ if ( offset != null ) {
 79+ return globalOffset + offset + this.content.getLength();
 80+ } else {
 81+ globalOffset += this.lists[i].getLength();
 82+ }
 83+ }
 84+ }
 85+};
 86+
 87+es.ListBlockItem.prototype.renderContent = function( offset ) {
 88+ // TODO: Abstract offset and use it when rendering
 89+ this.flow.render();
 90+ for ( var i = 0; i < this.lists.length; i++ ) {
 91+ this.lists[i].renderContent();
 92+ }
 93+};
 94+
 95+es.extend( es.ListBlockItem, es.Container );
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -1,139 +1,4 @@
22 /**
3 - * es.ListBlockList
4 - */
5 -es.ListBlockList = function( style, items ) {
6 - // Convert items to es.ListBlockItem objects
7 - var listItems = [];
8 - for ( var i = 0; i < items.length; i++ ) {
9 - listItems.push( new es.ListBlockItem( items[i].line, items[i].lists || [] ) );
10 - }
11 -
12 - /*
13 - * Initialize container
14 - *
15 - * - Adds class to container: "editSurface-list"
16 - * - Sets .data( 'list', this )
17 - * - Adds this.items array
18 - */
19 - es.Container.call( this, 'list', 'items', listItems );
20 -
21 - this.style = style;
22 -}
23 -
24 -es.ListBlockList.prototype.getLength = function() {
25 - var length = 0;
26 - for ( var i = 0; i < this.items.length; i++ ) {
27 - length += this.items[i].getLength();
28 - }
29 - return length;
30 -};
31 -
32 -es.ListBlockList.prototype.getLocationFromOffset = function( offset ) {
33 -
34 - var itemOffset = 0,
35 - itemLength;
36 -
37 - for ( var i = 0; i < this.items.length; i++ ) {
38 -
39 - itemLength = this.items[i].getLength();
40 -
41 - if ( offset >= itemOffset && offset < itemOffset + itemLength ) {
42 - var location = this.items[i].getLocationFromOffset( offset - itemOffset );
43 - return {
44 - 'item': location.item,
45 - 'offset': location.offset
46 - }
47 - }
48 -
49 - itemOffset += itemLength;
50 - }
51 -};
52 -
53 -/**
54 - * Renders content into a container.
55 - */
56 -es.ListBlockList.prototype.renderContent = function( offset ) {
57 - // TODO: Abstract offset and use it when rendering
58 - for ( var i = 0; i < this.items.length; i++ ) {
59 - this.items[i].renderContent();
60 - }
61 -};
62 -
63 -es.extend( es.ListBlockList, es.Container );
64 -
65 -/**
66 - * es.ListBlockItem
67 - */
68 -es.ListBlockItem = function( line, lists ) {
69 - // Convert items to es.ListBlockItem objects
70 - var itemLists = [];
71 - for ( var i = 0; i < lists.length; i++ ) {
72 - itemLists.push( new es.ListBlockList( lists[i].style, lists[i].items || [] ) );
73 - }
74 - /*
75 - * Initialize container
76 - *
77 - * - Adds class to container: "editSurface-item"
78 - * - Sets .data( 'item', this )
79 - * - Adds this.lists array
80 - */
81 - es.Container.call( this, 'item', 'lists', itemLists );
82 -
83 - this.$line = $( '<div class="editSurface-list-line"></div>' ).prependTo( this.$ )
84 - this.$content = $( '<div class="editSurface-list-content"></div>' ).appendTo( this.$line );
85 -
86 - this.content = line ? es.Content.newFromLine( line ) : new es.Content();
87 - this.flow = new es.TextFlow( this.$content, this.content );
88 - var item = this;
89 - this.flow.on( 'render', function() {
90 - item.emit( 'update' );
91 - } );
92 -}
93 -
94 -es.ListBlockItem.prototype.getLength = function() {
95 - var length = this.content.getLength();
96 - for ( var i = 0; i < this.lists.length; i++ ) {
97 - length += this.lists[i].getLength();
98 - }
99 - return length;
100 -};
101 -
102 -es.ListBlockItem.prototype.getLocationFromOffset = function( offset ) {
103 - var contentLength = this.content.getLength();
104 -
105 - if ( offset < contentLength ) {
106 - return {
107 - 'item': this,
108 - 'offset': offset
109 - };
110 - }
111 -
112 - offset -= contentLength;
113 -
114 - var listOffset = 0,
115 - listLength;
116 -
117 - for ( var i = 0; i < this.lists.length; i++ ) {
118 - listLength = this.lists[i].getLength();
119 -
120 - if ( offset >= listOffset && offset < listOffset + listLength ) {
121 - return this.lists[i].getLocationFromOffset( offset - listOffset );
122 - }
123 - listOffset += listLength;
124 - }
125 -};
126 -
127 -es.ListBlockItem.prototype.renderContent = function( offset ) {
128 - // TODO: Abstract offset and use it when rendering
129 - this.flow.render();
130 - for ( var i = 0; i < this.lists.length; i++ ) {
131 - this.lists[i].renderContent();
132 - }
133 -};
134 -
135 -es.extend( es.ListBlockItem, es.Container );
136 -
137 -/**
1383 * es.ListBlock
1394 */
1405 es.ListBlock = function( style, items ) {
@@ -148,6 +13,10 @@
14914 return new es.ListBlock( wikidomList.style, wikidomList.items );
15015 };
15116
 17+es.ListBlock.prototype.getText = function() {
 18+ return '';
 19+};
 20+
15221 es.ListBlock.prototype.getLength = function() {
15322 return this.list.getLength();
15423 };
@@ -165,38 +34,18 @@
16635 blockOffset = this.$.offset(),
16736 lineOffset = location.item.$line.find( '.editSurface-list-content' ).offset();
16837
169 -
170 -
17138 position.top += lineOffset.top - blockOffset.top;
17239 position.left += lineOffset.left - blockOffset.left;
17340 position.bottom += lineOffset.top - blockOffset.top;
174 -
 41+
17542 return position;
17643 };
17744
17845 es.ListBlock.prototype.getOffset = function( position ) {
179 - var $lines = this.$.find( '.editSurface-line' ),
180 - $line,
181 - blockOffset = this.$.offset();
182 - var length = 0;
183 - $lines.each( function() {
184 - if ( position.top <= ( $(this).offset().top - blockOffset.top ) ) {
185 - return false;
186 - }
187 - length += $(this).closest( '.editSurface-item' ).data('item').content.getLength();
188 - $line = $(this);
189 - } );
190 -
191 - var item = $line.closest( '.editSurface-item' ).data( 'item' ),
192 - lineOffset = $line.parent().offset();
193 - length -= item.content.getLength();
194 -
195 -
196 - position.top -= lineOffset.top - blockOffset.top;
197 - position.left -= lineOffset.left - blockOffset.left;
198 - position.bottom -= lineOffset.top - blockOffset.top;
199 -
200 - return length + item.flow.getOffset( position );
 46+ var blockOffset = this.$.offset();
 47+ position.top += blockOffset.top;
 48+ position.left += blockOffset.left;
 49+ return this.list.getOffsetFromPosition( position );
20150 };
20251
20352 es.Block.models['list'] = es.ListBlock;
Index: trunk/parsers/wikidom/demos/es/index.html
@@ -61,6 +61,8 @@
6262 <script type="text/javascript" src="../../lib/es/es.Surface.js"></script>
6363 <script type="text/javascript" src="../../lib/es/es.TextFlow.js"></script>
6464 <script type="text/javascript" src="../../lib/es/es.ParagraphBlock.js"></script>
 65+ <script type="text/javascript" src="../../lib/es/es.ListBlockList.js"></script>
 66+ <script type="text/javascript" src="../../lib/es/es.ListBlockItem.js"></script>
6567 <script type="text/javascript" src="../../lib/es/es.ListBlock.js"></script>
6668 <script type="text/javascript" src="../../lib/es/es.Cursor.js"></script>
6769
@@ -148,7 +150,7 @@
149151 ]
150152 }
151153 },
152 - { 'line': { 'text': 'Server: Secure, fast and powerful, Ubuntu Server is transforming IT environments worldwide. Realise the full potential of your infrastructure with a reliable, easy-to-integrate technology platform.' } },
 154+ { 'line': { 'text': 'Server: Secure, fast and powerful, Ubuntu Server is transforming IT environments worldwide. Realise the full potential of your infrastructure with a reliable, easy-to-integrate technology platform. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. Lorem ipsum.. ' } },
153155 { 'line': { 'text': 'Cloud: Ubuntu cloud computing puts you in control of your IT infrastructure. It helps you access computing power as and when you need it so you can meet user demand more effectively.' } }
154156 ]
155157 }

Status & tagging log