r95289 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95288‎ | r95289 | r95290 >
Date:00:11, 23 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Make ListBlockItem keep whole path of styles, so for instance: #*# == [number,bullet,number] instead of just number.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlockItem.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlockList.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ListBlockItem.js
@@ -1,5 +1,6 @@
22 /**
33 * Creates a list block item.
 4+ * Levels are 0-indexed.
45 *
56 * @class
67 * @constructor
@@ -8,17 +9,17 @@
910 * @param style {String}
1011 * @param level {Integer}
1112 */
12 -es.ListBlockItem = function( content, style, level ) {
 13+es.ListBlockItem = function( content, styles ) {
1314 es.EventEmitter.call( this );
14 -
 15+ this.styles = styles || ['bullet'];
1516 this.content = content || new es.Content();
1617 this.$icon = $( '<div class="editSurface-listItem-icon"></div>' );
1718 this.$content = $( '<div class="editSurface-listItem-content"></div>' );
1819 this.$ = $( '<div class="editSurface-listItem"></div>' )
1920 .append( this.$icon )
20 - .append( this.$content );
21 - this.setStyle( style );
22 - this.setLevel( level );
 21+ .append( this.$content )
 22+ .addClass( 'editSurface-listItem-level' + ( this.styles.length - 1 ) )
 23+ .addClass( 'editSurface-listItem-' + this.styles[this.styles.length - 1] );
2324 this.flow = new es.ContentFlow( this.$content, this.content );
2425 // Listen to render events and trigger update event upstream
2526 var listBlockItem = this;
@@ -33,18 +34,45 @@
3435 this.$icon.text( number + '.' );
3536 };
3637
37 -es.ListBlockItem.prototype.setLevel = function( level ) {
38 - this.$.removeClass( 'editSurface-listItem-level' + this.level );
39 - this.level = level;
40 - this.$.addClass( 'editSurface-listItem-level' + this.level );
 38+es.ListBlockItem.prototype.setLevel = function( level, style ) {
 39+ this.$.removeClass( 'editSurface-listItem-level' + ( this.styles.length - 1 ) );
 40+ this.$.removeClass( 'editSurface-listItem-' + this.styles[this.styles.length - 1] );
 41+ if ( level <= this.styles.length - 1 ) {
 42+ this.styles = this.styles.slice( 0, level + 1 );
 43+ if ( typeof style !== 'undefined' ) {
 44+ this.styles[this.styles.length - 1] = style;
 45+ }
 46+ } else {
 47+ if ( typeof style === 'undefined' ) {
 48+ style = this.styles[this.styles.length - 1];
 49+ }
 50+ while ( this.styles.length - 1 < level ) {
 51+ this.styles.push( style );
 52+ }
 53+ }
 54+ this.$.addClass( 'editSurface-listItem-level' + ( this.styles.length - 1 ) );
 55+ this.$.addClass( 'editSurface-listItem-' + this.styles[this.styles.length - 1] );
4156 };
4257
 58+es.ListBlockItem.prototype.getLevel = function() {
 59+ return this.styles.length - 1;
 60+};
 61+
4362 es.ListBlockItem.prototype.setStyle = function( style ) {
44 - this.$.removeClass( 'editSurface-listItem-' + this.style );
45 - this.style = style;
46 - this.$.addClass( 'editSurface-listItem-' + this.style );
 63+ this.$.removeClass( 'editSurface-listItem-' + this.styles[this.styles.length - 1] );
 64+ this.styles.pop();
 65+ this.styles.push( style );
 66+ this.$.addClass( 'editSurface-listItem-' + this.styles[this.styles.length - 1] );
4767 };
4868
 69+es.ListBlockItem.prototype.getStyle = function( level ) {
 70+ if ( typeof level === 'undefined') {
 71+ return this.styles[this.styles.length - 1];
 72+ } else {
 73+ return this.styles[level];
 74+ }
 75+};
 76+
4977 es.ListBlockItem.prototype.renderContent = function( offset ) {
5078 this.flow.render();
5179 };
Index: trunk/parsers/wikidom/lib/es/es.ListBlockList.js
@@ -21,30 +21,30 @@
2222 * @returns {es.ListBlockList} List block list
2323 */
2424 es.ListBlockList.newFromWikiDomList = function( wikidomList ) {
25 - var items = [];
26 - es.ListBlockList.flattenList( wikidomList, items, 0 );
 25+ var items = [],
 26+ styles = [];
 27+ es.ListBlockList.flattenList( wikidomList, items, styles );
2728 return new es.ListBlockList( items );
2829 };
2930
30 -es.ListBlockList.flattenList = function( wikidomList, items, level ) {
 31+es.ListBlockList.flattenList = function( wikidomList, items, styles ) {
 32+ styles.push( wikidomList.style );
3133 for ( var i = 0; i < wikidomList.items.length; i++ ) {
3234 if ( typeof wikidomList.items[i].line !== 'undefined' ) {
3335 items.push(
3436 new es.ListBlockItem(
3537 es.Content.newFromWikiDomLine( wikidomList.items[i].line ),
36 - wikidomList.style,
37 - level
 38+ styles.slice(0)
3839 )
3940 );
4041 }
4142 if ( wikidomList.items[i].lists ) {
42 - level++;
4343 for ( var j = 0; j < wikidomList.items[i].lists.length; j++ ) {
44 - es.ListBlockList.flattenList( wikidomList.items[i].lists[j], items, level );
 44+ es.ListBlockList.flattenList( wikidomList.items[i].lists[j], items, styles );
4545 }
46 - level--;
4746 }
4847 }
 48+ styles.pop();
4949 };
5050
5151 /* Public Methods */
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -53,10 +53,10 @@
5454 levels = [];
5555
5656 for ( var i = 0; i < this.list.items.length; i++ ) {
57 - itemLevel = this.list.items[i].level;
 57+ itemLevel = this.list.items[i].getLevel();
5858 levels = levels.slice(0, itemLevel + 1);
5959
60 - if ( this.list.items[i].style === 'number' ) {
 60+ if ( this.list.items[i].getStyle() === 'number' ) {
6161 if ( !levels[itemLevel] ) {
6262 levels[itemLevel] = 0;
6363 }
@@ -356,75 +356,7 @@
357357 };
358358
359359 es.ListBlock.prototype.getWikiDom = function() {
360 - var previousStyle,
361 - stack = [],
362 - previousLevel = -1,
363 - items = this.list.items;
364360
365 - for( var i = 0; i < items.length; i++) {
366 - var item = items[i];
367 -
368 - if ( item.level === previousLevel ) {
369 - if ( item.style !== previousStyle ) {
370 - if ( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
371 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = [];
372 - }
373 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
374 - stack.push( { 'style' : item.style, 'items' : [] } );
375 - previousStyle = item.style;
376 - }
377 - }
378 -
379 - /**
380 - * Cases:
381 - *
382 - * # item 1
383 - * ## item 2 (item)
384 - *
385 - * or
386 - *
387 - * # item 1
388 - * #### item 2 (item)
389 - *
390 - * and first item in list
391 - *
392 - * # item 1 (item)
393 - *
394 - * or
395 - *
396 - * #### item 1 (item)
397 - */
398 - if( item.level > previousLevel ) {
399 - for( var j = previousLevel; j < item.level; j++ ) {
400 - stack.push( { 'style' : item.style, 'items' : [] } );
401 - }
402 - previousLevel = item.level;
403 - }
404 -
405 - if ( item.level < previousLevel ) {
406 - for ( var j = previousLevel; j > item.level; j-- ) {
407 - if( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
408 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = []
409 - }
410 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
411 - previousLevel = item.level;
412 - }
413 - }
414 -
415 - if(item.level === previousLevel) {
416 - if( item.content.getLength() > 0 ) {
417 - stack[stack.length - 1].items.push( { 'line' : item.content.getWikiDomLines()[0] } );
418 - }
419 - previousStyle = item.style;
420 - }
421 - }
422 -
423 - if ( stack.length !== 1 ) {
424 - throw 'At this point stack should contain exactly one element.';
425 - }
426 -
427 - stack[0].type = 'list';
428 - return stack[0];
429361 };
430362
431363 /* Registration */

Status & tagging log