Index: trunk/parsers/wikidom/lib/es/es.ListBlockItem.js |
— | — | @@ -1,5 +1,6 @@ |
2 | 2 | /** |
3 | 3 | * Creates a list block item. |
| 4 | + * Levels are 0-indexed. |
4 | 5 | * |
5 | 6 | * @class |
6 | 7 | * @constructor |
— | — | @@ -8,17 +9,17 @@ |
9 | 10 | * @param style {String} |
10 | 11 | * @param level {Integer} |
11 | 12 | */ |
12 | | -es.ListBlockItem = function( content, style, level ) { |
| 13 | +es.ListBlockItem = function( content, styles ) { |
13 | 14 | es.EventEmitter.call( this ); |
14 | | - |
| 15 | + this.styles = styles || ['bullet']; |
15 | 16 | this.content = content || new es.Content(); |
16 | 17 | this.$icon = $( '<div class="editSurface-listItem-icon"></div>' ); |
17 | 18 | this.$content = $( '<div class="editSurface-listItem-content"></div>' ); |
18 | 19 | this.$ = $( '<div class="editSurface-listItem"></div>' ) |
19 | 20 | .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] ); |
23 | 24 | this.flow = new es.ContentFlow( this.$content, this.content ); |
24 | 25 | // Listen to render events and trigger update event upstream |
25 | 26 | var listBlockItem = this; |
— | — | @@ -33,18 +34,45 @@ |
34 | 35 | this.$icon.text( number + '.' ); |
35 | 36 | }; |
36 | 37 | |
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] ); |
41 | 56 | }; |
42 | 57 | |
| 58 | +es.ListBlockItem.prototype.getLevel = function() { |
| 59 | + return this.styles.length - 1; |
| 60 | +}; |
| 61 | + |
43 | 62 | 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] ); |
47 | 67 | }; |
48 | 68 | |
| 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 | + |
49 | 77 | es.ListBlockItem.prototype.renderContent = function( offset ) { |
50 | 78 | this.flow.render(); |
51 | 79 | }; |
Index: trunk/parsers/wikidom/lib/es/es.ListBlockList.js |
— | — | @@ -21,30 +21,30 @@ |
22 | 22 | * @returns {es.ListBlockList} List block list |
23 | 23 | */ |
24 | 24 | 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 ); |
27 | 28 | return new es.ListBlockList( items ); |
28 | 29 | }; |
29 | 30 | |
30 | | -es.ListBlockList.flattenList = function( wikidomList, items, level ) { |
| 31 | +es.ListBlockList.flattenList = function( wikidomList, items, styles ) { |
| 32 | + styles.push( wikidomList.style ); |
31 | 33 | for ( var i = 0; i < wikidomList.items.length; i++ ) { |
32 | 34 | if ( typeof wikidomList.items[i].line !== 'undefined' ) { |
33 | 35 | items.push( |
34 | 36 | new es.ListBlockItem( |
35 | 37 | es.Content.newFromWikiDomLine( wikidomList.items[i].line ), |
36 | | - wikidomList.style, |
37 | | - level |
| 38 | + styles.slice(0) |
38 | 39 | ) |
39 | 40 | ); |
40 | 41 | } |
41 | 42 | if ( wikidomList.items[i].lists ) { |
42 | | - level++; |
43 | 43 | 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 ); |
45 | 45 | } |
46 | | - level--; |
47 | 46 | } |
48 | 47 | } |
| 48 | + styles.pop(); |
49 | 49 | }; |
50 | 50 | |
51 | 51 | /* Public Methods */ |
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
— | — | @@ -53,10 +53,10 @@ |
54 | 54 | levels = []; |
55 | 55 | |
56 | 56 | for ( var i = 0; i < this.list.items.length; i++ ) { |
57 | | - itemLevel = this.list.items[i].level; |
| 57 | + itemLevel = this.list.items[i].getLevel(); |
58 | 58 | levels = levels.slice(0, itemLevel + 1); |
59 | 59 | |
60 | | - if ( this.list.items[i].style === 'number' ) { |
| 60 | + if ( this.list.items[i].getStyle() === 'number' ) { |
61 | 61 | if ( !levels[itemLevel] ) { |
62 | 62 | levels[itemLevel] = 0; |
63 | 63 | } |
— | — | @@ -356,75 +356,7 @@ |
357 | 357 | }; |
358 | 358 | |
359 | 359 | es.ListBlock.prototype.getWikiDom = function() { |
360 | | - var previousStyle, |
361 | | - stack = [], |
362 | | - previousLevel = -1, |
363 | | - items = this.list.items; |
364 | 360 | |
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]; |
429 | 361 | }; |
430 | 362 | |
431 | 363 | /* Registration */ |