Index: trunk/parsers/wikidom/lib/es/es.ListBlockList.js |
— | — | @@ -1,76 +1,76 @@ |
2 | | -/**
|
3 | | - * es.ListBlockList
|
4 | | - */
|
5 | | -es.ListBlockList = function( style, items ) {
|
6 | | - // Inheritance
|
7 | | - es.EventEmitter.call( this );
|
8 | | -
|
9 | | - // Convert items to es.ListBlockItem objects
|
10 | | - var listItems = [],
|
11 | | - list = this;
|
12 | | - for ( var i = 0; i < items.length; i++ ) {
|
13 | | - listItems.push( new es.ListBlockItem( items[i].line, items[i].lists || [] ) );
|
14 | | - listItems[i].on( 'update', function() {
|
15 | | - list.emit( 'update' );
|
16 | | - } );
|
17 | | - }
|
18 | | -
|
19 | | - /*
|
20 | | - * Initialize container
|
21 | | - *
|
22 | | - * - Adds class to container: "editSurface-list"
|
23 | | - * - Sets .data( 'list', this )
|
24 | | - * - Adds this.items array
|
25 | | - */
|
26 | | - es.Container.call( this, 'list', 'items', listItems );
|
27 | | -
|
28 | | - this.style = style;
|
29 | | -}
|
30 | | -
|
31 | | -es.ListBlockList.prototype.getLength = function() {
|
32 | | - var length = 0;
|
33 | | - for ( var i = 0; i < this.items.length; i++ ) {
|
34 | | - length += this.items[i].getLength();
|
35 | | - }
|
36 | | - return length;
|
37 | | -};
|
38 | | -
|
39 | | -es.ListBlockList.prototype.getLocationFromOffset = function( offset ) {
|
40 | | - var itemOffset = 0,
|
41 | | - itemLength;
|
42 | | - for ( var i = 0; i < this.items.length; i++ ) {
|
43 | | - itemLength = this.items[i].getLength();
|
44 | | - if ( offset >= itemOffset && offset < itemOffset + itemLength ) {
|
45 | | - return this.items[i].getLocationFromOffset( offset - itemOffset );
|
46 | | - }
|
47 | | - itemOffset += itemLength;
|
48 | | - }
|
49 | | -};
|
50 | | -
|
51 | | -es.ListBlockList.prototype.getOffsetFromPosition = function( position ) {
|
52 | | - var itemOffset = null,
|
53 | | - globalOffset = null;
|
54 | | -
|
55 | | - for ( var i = 0; i < this.items.length; i++ ) {
|
56 | | - itemOffset = this.items[i].getOffsetFromPosition( position );
|
57 | | -
|
58 | | - if ( itemOffset !== null ) {
|
59 | | - return globalOffset + itemOffset;
|
60 | | - } else {
|
61 | | - globalOffset += this.items[i].getLength();
|
62 | | - }
|
63 | | - }
|
64 | | -};
|
65 | | -
|
66 | | -/**
|
67 | | - * Renders content into a container.
|
68 | | - */
|
69 | | -es.ListBlockList.prototype.renderContent = function( offset ) {
|
70 | | - // TODO: Abstract offset and use it when rendering
|
71 | | - for ( var i = 0; i < this.items.length; i++ ) {
|
72 | | - this.items[i].renderContent();
|
73 | | - }
|
74 | | -};
|
75 | | -
|
76 | | -es.extend( es.ListBlockList, es.EventEmitter );
|
| 2 | +/** |
| 3 | + * es.ListBlockList |
| 4 | + */ |
| 5 | +es.ListBlockList = function( style, items ) { |
| 6 | + // Inheritance |
| 7 | + es.EventEmitter.call( this ); |
| 8 | + |
| 9 | + // Convert items to es.ListBlockItem objects |
| 10 | + var listItems = [], |
| 11 | + list = this; |
| 12 | + for ( var i = 0; i < items.length; i++ ) { |
| 13 | + listItems.push( new es.ListBlockItem( items[i].line, items[i].lists || [] ) ); |
| 14 | + listItems[i].on( 'update', function() { |
| 15 | + list.emit( 'update' ); |
| 16 | + } ); |
| 17 | + } |
| 18 | + |
| 19 | + /* |
| 20 | + * Initialize container |
| 21 | + * |
| 22 | + * - Adds class to container: "editSurface-list" |
| 23 | + * - Sets .data( 'list', this ) |
| 24 | + * - Adds this.items array |
| 25 | + */ |
| 26 | + es.Container.call( this, 'list', 'items', listItems ); |
| 27 | + |
| 28 | + this.style = style; |
| 29 | +} |
| 30 | + |
| 31 | +es.ListBlockList.prototype.getLength = function() { |
| 32 | + var length = 0; |
| 33 | + for ( var i = 0; i < this.items.length; i++ ) { |
| 34 | + length += this.items[i].getLength(); |
| 35 | + } |
| 36 | + return length; |
| 37 | +}; |
| 38 | + |
| 39 | +es.ListBlockList.prototype.getLocationFromOffset = function( offset ) { |
| 40 | + var itemOffset = 0, |
| 41 | + itemLength; |
| 42 | + for ( var i = 0; i < this.items.length; i++ ) { |
| 43 | + itemLength = this.items[i].getLength(); |
| 44 | + if ( offset >= itemOffset && offset < itemOffset + itemLength ) { |
| 45 | + return this.items[i].getLocationFromOffset( offset - itemOffset ); |
| 46 | + } |
| 47 | + itemOffset += itemLength; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +es.ListBlockList.prototype.getOffsetFromPosition = function( position ) { |
| 52 | + var itemOffset = null, |
| 53 | + globalOffset = null; |
| 54 | + |
| 55 | + for ( var i = 0; i < this.items.length; i++ ) { |
| 56 | + itemOffset = this.items[i].getOffsetFromPosition( position ); |
| 57 | + |
| 58 | + if ( itemOffset !== null ) { |
| 59 | + return globalOffset + itemOffset; |
| 60 | + } else { |
| 61 | + globalOffset += this.items[i].getLength(); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Renders content into a container. |
| 68 | + */ |
| 69 | +es.ListBlockList.prototype.renderContent = function( offset ) { |
| 70 | + // TODO: Abstract offset and use it when rendering |
| 71 | + for ( var i = 0; i < this.items.length; i++ ) { |
| 72 | + this.items[i].renderContent(); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +es.extend( es.ListBlockList, es.EventEmitter ); |
77 | 77 | es.extend( es.ListBlockList, es.Container ); |
\ No newline at end of file |
Property changes on: trunk/parsers/wikidom/lib/es/es.ListBlockList.js |
___________________________________________________________________ |
Added: svn:eol-style |
78 | 78 | + native |
Index: trunk/parsers/wikidom/lib/es/es.ListBlockItem.js |
— | — | @@ -1,108 +1,108 @@ |
2 | | -/**
|
3 | | - * es.ListBlockItem
|
4 | | - */
|
5 | | -es.ListBlockItem = function( line, lists ) {
|
6 | | - // Inheritance
|
7 | | - es.EventEmitter.call( this );
|
8 | | -
|
9 | | - // Convert items to es.ListBlockItem objects
|
10 | | - var itemLists = [];
|
11 | | - for ( var i = 0; i < lists.length; i++ ) {
|
12 | | - itemLists.push( new es.ListBlockList( lists[i].style, lists[i].items || [] ) );
|
13 | | - }
|
14 | | - /*var contentLength = this.content.getLength(
|
15 | | - * Initialize container
|
16 | | - *
|
17 | | - * - Adds class to container: "editSurface-item"
|
18 | | - * - Sets .data( 'item', this )
|
19 | | - * - Adds this.lists array
|
20 | | - */
|
21 | | - es.Container.call( this, 'item', 'lists', itemLists );
|
22 | | -
|
23 | | - this.$line = $( '<div class="editSurface-list-line"></div>' ).prependTo( this.$ )
|
24 | | - this.$content = $( '<div class="editSurface-list-content"></div>' ).appendTo( this.$line );
|
25 | | -
|
26 | | - this.content = line ? es.Content.newFromLine( line ) : new es.Content();
|
27 | | - this.flow = new es.TextFlow( this.$content, this.content );
|
28 | | - var item = this;
|
29 | | - this.flow.on( 'render', function() {
|
30 | | - item.emit( 'update' );
|
31 | | - } );
|
32 | | -}
|
33 | | -
|
34 | | -/**
|
35 | | - * Gets the index of the item within it's list.
|
36 | | - *
|
37 | | - * @returns {Integer} Index of item
|
38 | | - */
|
39 | | -es.ListBlockItem.prototype.getIndex = function() {
|
40 | | - return this.list._list.indexOf( this );
|
41 | | -};
|
42 | | -
|
43 | | -es.ListBlockItem.prototype.getLength = function() {
|
44 | | - var length = this.content.getLength() + 1;
|
45 | | - for ( var i = 0; i < this.lists.length; i++ ) {
|
46 | | - length += this.lists[i].getLength();
|
47 | | - }
|
48 | | - return length;
|
49 | | -};
|
50 | | -
|
51 | | -es.ListBlockItem.prototype.getLocationFromOffset = function( offset ) {
|
52 | | - var contentLength = this.content.getLength() + 1;
|
53 | | -
|
54 | | - if ( offset < contentLength ) {
|
55 | | - return {
|
56 | | - 'item': this,
|
57 | | - 'offset': offset
|
58 | | - };
|
59 | | - }
|
60 | | -
|
61 | | - offset -= contentLength;
|
62 | | -
|
63 | | - var listOffset = 0,
|
64 | | - listLength;
|
65 | | - for ( var i = 0; i < this.lists.length; i++ ) {
|
66 | | - listLength = this.lists[i].getLength();
|
67 | | - if ( offset >= listOffset && offset < listOffset + listLength ) {
|
68 | | - return this.lists[i].getLocationFromOffset( offset - listOffset );
|
69 | | - }
|
70 | | - listOffset += listLength;
|
71 | | - }
|
72 | | -};
|
73 | | -
|
74 | | -es.ListBlockItem.prototype.getOffsetFromPosition = function( position ) {
|
75 | | - var itemOffset = this.$.offset(),
|
76 | | - itemHeight = this.$.height(),
|
77 | | - offset = null,
|
78 | | - globalOffset = null;
|
79 | | -
|
80 | | - if ( position.top >= itemOffset.top && position.top < itemOffset.top + itemHeight ) {
|
81 | | - if ( position.top < itemOffset.top + this.$line.height() ) {
|
82 | | - position.top -= itemOffset.top;
|
83 | | - position.left -= itemOffset.left;
|
84 | | - position.left += this.$.parent().offset().left - itemOffset.left;
|
85 | | - return globalOffset + this.flow.getOffset( position );
|
86 | | - }
|
87 | | -
|
88 | | - for ( var i = 0; i < this.lists.length; i++ ) {
|
89 | | - offset = this.lists[i].getOffsetFromPosition( position );
|
90 | | - if ( offset != null ) {
|
91 | | - return globalOffset + offset + this.content.getLength() + 1;
|
92 | | - } else {
|
93 | | - globalOffset += this.lists[i].getLength();
|
94 | | - }
|
95 | | - }
|
96 | | - }
|
97 | | - return null;
|
98 | | -};
|
99 | | -
|
100 | | -es.ListBlockItem.prototype.renderContent = function( offset ) {
|
101 | | - // TODO: Abstract offset and use it when rendering
|
102 | | - this.flow.render();
|
103 | | - for ( var i = 0; i < this.lists.length; i++ ) {
|
104 | | - this.lists[i].renderContent();
|
105 | | - }
|
106 | | -};
|
107 | | -
|
108 | | -es.extend( es.ListBlockItem, es.EventEmitter );
|
| 2 | +/** |
| 3 | + * es.ListBlockItem |
| 4 | + */ |
| 5 | +es.ListBlockItem = function( line, lists ) { |
| 6 | + // Inheritance |
| 7 | + es.EventEmitter.call( this ); |
| 8 | + |
| 9 | + // Convert items to es.ListBlockItem objects |
| 10 | + var itemLists = []; |
| 11 | + for ( var i = 0; i < lists.length; i++ ) { |
| 12 | + itemLists.push( new es.ListBlockList( lists[i].style, lists[i].items || [] ) ); |
| 13 | + } |
| 14 | + /*var contentLength = this.content.getLength( |
| 15 | + * Initialize container |
| 16 | + * |
| 17 | + * - Adds class to container: "editSurface-item" |
| 18 | + * - Sets .data( 'item', this ) |
| 19 | + * - Adds this.lists array |
| 20 | + */ |
| 21 | + es.Container.call( this, 'item', 'lists', itemLists ); |
| 22 | + |
| 23 | + this.$line = $( '<div class="editSurface-list-line"></div>' ).prependTo( this.$ ) |
| 24 | + this.$content = $( '<div class="editSurface-list-content"></div>' ).appendTo( this.$line ); |
| 25 | + |
| 26 | + this.content = line ? es.Content.newFromLine( line ) : new es.Content(); |
| 27 | + this.flow = new es.TextFlow( this.$content, this.content ); |
| 28 | + var item = this; |
| 29 | + this.flow.on( 'render', function() { |
| 30 | + item.emit( 'update' ); |
| 31 | + } ); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Gets the index of the item within it's list. |
| 36 | + * |
| 37 | + * @returns {Integer} Index of item |
| 38 | + */ |
| 39 | +es.ListBlockItem.prototype.getIndex = function() { |
| 40 | + return this.list._list.indexOf( this ); |
| 41 | +}; |
| 42 | + |
| 43 | +es.ListBlockItem.prototype.getLength = function() { |
| 44 | + var length = this.content.getLength() + 1; |
| 45 | + for ( var i = 0; i < this.lists.length; i++ ) { |
| 46 | + length += this.lists[i].getLength(); |
| 47 | + } |
| 48 | + return length; |
| 49 | +}; |
| 50 | + |
| 51 | +es.ListBlockItem.prototype.getLocationFromOffset = function( offset ) { |
| 52 | + var contentLength = this.content.getLength() + 1; |
| 53 | + |
| 54 | + if ( offset < contentLength ) { |
| 55 | + return { |
| 56 | + 'item': this, |
| 57 | + 'offset': offset |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + offset -= contentLength; |
| 62 | + |
| 63 | + var listOffset = 0, |
| 64 | + listLength; |
| 65 | + for ( var i = 0; i < this.lists.length; i++ ) { |
| 66 | + listLength = this.lists[i].getLength(); |
| 67 | + if ( offset >= listOffset && offset < listOffset + listLength ) { |
| 68 | + return this.lists[i].getLocationFromOffset( offset - listOffset ); |
| 69 | + } |
| 70 | + listOffset += listLength; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +es.ListBlockItem.prototype.getOffsetFromPosition = function( position ) { |
| 75 | + var itemOffset = this.$.offset(), |
| 76 | + itemHeight = this.$.height(), |
| 77 | + offset = null, |
| 78 | + globalOffset = null; |
| 79 | + |
| 80 | + if ( position.top >= itemOffset.top && position.top < itemOffset.top + itemHeight ) { |
| 81 | + if ( position.top < itemOffset.top + this.$line.height() ) { |
| 82 | + position.top -= itemOffset.top; |
| 83 | + position.left -= itemOffset.left; |
| 84 | + position.left += this.$.parent().offset().left - itemOffset.left; |
| 85 | + return globalOffset + this.flow.getOffset( position ); |
| 86 | + } |
| 87 | + |
| 88 | + for ( var i = 0; i < this.lists.length; i++ ) { |
| 89 | + offset = this.lists[i].getOffsetFromPosition( position ); |
| 90 | + if ( offset != null ) { |
| 91 | + return globalOffset + offset + this.content.getLength() + 1; |
| 92 | + } else { |
| 93 | + globalOffset += this.lists[i].getLength(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return null; |
| 98 | +}; |
| 99 | + |
| 100 | +es.ListBlockItem.prototype.renderContent = function( offset ) { |
| 101 | + // TODO: Abstract offset and use it when rendering |
| 102 | + this.flow.render(); |
| 103 | + for ( var i = 0; i < this.lists.length; i++ ) { |
| 104 | + this.lists[i].renderContent(); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +es.extend( es.ListBlockItem, es.EventEmitter ); |
109 | 109 | es.extend( es.ListBlockItem, es.Container ); |
\ No newline at end of file |
Property changes on: trunk/parsers/wikidom/lib/es/es.ListBlockItem.js |
___________________________________________________________________ |
Added: svn:eol-style |
110 | 110 | + native |
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
— | — | @@ -1,228 +1,228 @@ |
2 | | -/**
|
3 | | - * es.ListBlock
|
4 | | - *
|
5 | | - * @extends {es.Block}
|
6 | | - * @param style {String} Type of list, either "number" or "bullet"
|
7 | | - * @param items {Array} List of es.ListBlockItems to append initially to the root list
|
| 2 | +/** |
| 3 | + * es.ListBlock |
| 4 | + * |
| 5 | + * @extends {es.Block} |
| 6 | + * @param style {String} Type of list, either "number" or "bullet" |
| 7 | + * @param items {Array} List of es.ListBlockItems to append initially to the root list |
8 | 8 | * @return {es.ListBlock} |
9 | | - */
|
10 | | -es.ListBlock = function( style, items ) {
|
11 | | - es.Block.call( this );
|
12 | | - this.list = new es.ListBlockList( style, items );
|
13 | | - this.$ = this.list.$
|
14 | | - .addClass( 'editSurface-block' )
|
15 | | - .data( 'block', this );
|
16 | | - var block = this;
|
17 | | - this.list.on( 'update', function() {
|
18 | | - block.emit( 'update' );
|
19 | | - } );
|
20 | | -};
|
21 | | -
|
22 | | -/**
|
23 | | - * Creates a new list block object from Wikidom data.
|
24 | | - *
|
25 | | - * @param wikidomList {Object} Wikidom data to convert from
|
26 | | - */
|
27 | | -es.ListBlock.newFromWikidom = function( wikidomList ) {
|
28 | | - return new es.ListBlock( wikidomList.style, wikidomList.items );
|
29 | | -};
|
30 | | -
|
31 | | -/**
|
32 | | - * Gets the length of all block content.
|
33 | | - */
|
34 | | -es.ListBlock.prototype.getLength = function() {
|
35 | | - // Compensate for n+1 virtual position on the last item's content
|
36 | | - return this.list.getLength() - 1;
|
37 | | -};
|
38 | | -
|
39 | | -/**
|
40 | | - * Inserts content into a block at an offset.
|
41 | | - *
|
42 | | - * @param offset {Integer} Position to insert content at
|
43 | | - * @param content {Object} Content to insert
|
44 | | - */
|
45 | | -es.ListBlock.prototype.insertContent = function( offset, content ) {
|
46 | | - var location = this.list.getLocationFromOffset( offset );
|
47 | | - location.item.flow.content.insert( location.offset, content );
|
48 | | -};
|
49 | | -
|
50 | | -/**
|
51 | | - * Deletes content in a block within a range.
|
52 | | - *
|
53 | | - * @param offset {Integer} Offset to start removing content from
|
54 | | - * @param length {Integer} Offset to start removing content to
|
55 | | - */
|
56 | | -es.ListBlock.prototype.deleteContent = function( start, end ) {
|
57 | | - // Normalize start/end
|
58 | | - if ( end < start ) {
|
59 | | - var tmp = end;
|
60 | | - end = start;
|
61 | | - start = tmp;
|
62 | | - }
|
63 | | - var location = this.list.getLocationFromOffset( start );
|
64 | | - location.item.flow.content.remove( location.offset, location.offset + end - start );
|
65 | | -};
|
66 | | -
|
67 | | -/**
|
68 | | - * Applies an annotation to a given range.
|
69 | | - *
|
70 | | - * If a range arguments are not provided, all content will be annotated.
|
71 | | - *
|
72 | | - * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
|
73 | | - * @param annotation {Object} Annotation to apply
|
74 | | - * @param start {Integer} Offset to begin annotating from
|
75 | | - * @param end {Integer} Offset to stop annotating to
|
76 | | - */
|
77 | | -es.ListBlock.prototype.annotateContent = function( method, annotation, start, end ) {
|
78 | | - // TODO: Support annotating multiple items at once
|
79 | | - var location = this.list.getLocationFromOffset( start );
|
80 | | - location.item.content.annotate( method, annotation, location.offset, location.offset + end - start );
|
81 | | -};
|
82 | | -
|
83 | | -/**
|
84 | | - * Gets content within a range.
|
85 | | - *
|
86 | | - * @param start {Integer} Offset to get content from
|
87 | | - * @param end {Integer} Offset to get content to
|
88 | | - */
|
89 | | -es.ListBlock.prototype.getContent = function() {
|
90 | | - // TODO: Implement me!
|
91 | | - return new Content();
|
92 | | -};
|
93 | | -
|
94 | | -/**
|
95 | | - * Gets content as plain text within a range.
|
96 | | - *
|
97 | | - * @param start {Integer} Offset to start get text from
|
98 | | - * @param end {Integer} Offset to start get text to
|
99 | | - * @param render {Boolean} If annotations should have any influence on output
|
100 | | - */
|
101 | | -es.ListBlock.prototype.getText = function() {
|
102 | | - // TODO: Implement me!
|
103 | | - return '';
|
104 | | -};
|
105 | | -
|
106 | | -/**
|
107 | | - * Renders content into a container.
|
108 | | - */
|
109 | | -es.ListBlock.prototype.renderContent = function( offset ) {
|
110 | | - this.list.renderContent( offset );
|
111 | | -};
|
112 | | -
|
113 | | -/**
|
114 | | - * Gets the offset of a position.
|
115 | | - *
|
116 | | - * @param position {Integer} Offset to translate
|
117 | | - */
|
118 | | -es.ListBlock.prototype.getOffset = function( position ) {
|
119 | | - if ( position.top < 0 ) {
|
120 | | - return 0;
|
121 | | - } else if ( position.top >= this.$.height() ) {
|
122 | | - return this.getLength();
|
123 | | - }
|
124 | | - var blockOffset = this.$.offset();
|
125 | | - position.top += blockOffset.top;
|
126 | | - position.left += blockOffset.left;
|
127 | | - return this.list.getOffsetFromPosition( position );
|
128 | | -};
|
129 | | -
|
130 | | -/**
|
131 | | - * Gets the position of an offset.
|
132 | | - *
|
133 | | - * @param offset {Integer} Offset to translate
|
134 | | - */
|
135 | | -es.ListBlock.prototype.getPosition = function( offset ) {
|
136 | | - var location = this.list.getLocationFromOffset( offset )
|
137 | | - position = location.item.flow.getPosition( location.offset ),
|
138 | | - blockOffset = this.$.offset(),
|
139 | | - lineOffset = location.item.$line.find( '.editSurface-list-content' ).offset();
|
140 | | -
|
141 | | - position.top += lineOffset.top - blockOffset.top;
|
142 | | - position.left += lineOffset.left - blockOffset.left;
|
143 | | - position.bottom += lineOffset.top - blockOffset.top;
|
144 | | -
|
145 | | - this.traverseItems( function( item ) {
|
146 | | - if ( item === location.item ) {
|
147 | | - return false;
|
148 | | - }
|
149 | | - position.line += item.flow.lines.length;
|
150 | | - } );
|
151 | | - return position;
|
152 | | -};
|
153 | | -
|
154 | | -/**
|
155 | | - * Gets the start and end points of the word closest a given offset.
|
156 | | - *
|
157 | | - * @param offset {Integer} Offset to find word nearest to
|
158 | | - * @return {Object} Range object of boundaries
|
159 | | - */
|
160 | | -es.ListBlock.prototype.getWordBoundaries = function( offset ) {
|
161 | | - var location = this.list.getLocationFromOffset( offset );
|
162 | | - var boundaries = location.item.flow.content.getWordBoundaries( location.offset );
|
163 | | - boundaries.start += offset - location.offset;
|
164 | | - boundaries.end += offset - location.offset;
|
165 | | - return boundaries;
|
166 | | -};
|
167 | | -
|
168 | | -/**
|
169 | | - * Gets the start and end points of the section closest a given offset.
|
170 | | - *
|
171 | | - * @param offset {Integer} Offset to find section nearest to
|
172 | | - * @return {Object} Range object of boundaries
|
173 | | - */
|
174 | | -es.ListBlock.prototype.getSectionBoundaries = function( offset ) {
|
175 | | - var location = this.list.getLocationFromOffset( offset ),
|
176 | | - start = offset - location.offset;
|
177 | | - return new es.Range( start, start + location.item.content.getLength() );
|
178 | | -};
|
179 | | -
|
180 | | -/**
|
181 | | - * Iteratively execute a callback on each item in the list.
|
182 | | - *
|
183 | | - * Traversal is performed in a depth-first pattern, which is equivilant to a vertical scan of list
|
184 | | - * items. To stop traversal, return false within the callback function.
|
185 | | - *
|
186 | | - * @param callback {Function} Function to execute for each item, accepts an item and index argument
|
187 | | - * @return {Boolean} Whether all items were traversed, or traversal was cut short
|
188 | | - */
|
189 | | -es.ListBlock.prototype.traverseItems = function( callback ) {
|
190 | | - var stack = [{ 'list': this.list, 'index': 0 }],
|
191 | | - list,
|
192 | | - item,
|
193 | | - pop,
|
194 | | - parent,
|
195 | | - index = 0;
|
196 | | - while ( stack.length ) {
|
197 | | - iteration = stack[stack.length - 1];
|
198 | | - pop = true;
|
199 | | - while ( iteration.index < iteration.list.items.length ) {
|
200 | | - item = iteration.list.items[iteration.index++];
|
201 | | - if ( callback( item, index++ ) === false ) {
|
202 | | - return false;
|
203 | | - }
|
204 | | - if ( item.lists.length ) {
|
205 | | - parent = stack.length;
|
206 | | - for ( var i = 0; i < item.lists.length; i++ ) {
|
207 | | - stack.push( { 'list': item.lists[i], 'index': 0, 'parent': parent } );
|
208 | | - }
|
209 | | - pop = false;
|
210 | | - break;
|
211 | | - }
|
212 | | - }
|
213 | | - if ( pop ) {
|
214 | | - if ( iteration.parent ) {
|
215 | | - stack = stack.slice( 0, iteration.parent );
|
216 | | - } else {
|
217 | | - stack.pop();
|
218 | | - }
|
219 | | - }
|
220 | | - }
|
221 | | - return true;
|
222 | | -};
|
223 | | -
|
224 | | -/**
|
225 | | - * Extend es.Block to support list block creation with es.Block.newFromWikidom
|
226 | | - */
|
227 | | -es.Block.models.list = es.ListBlock;
|
228 | | -
|
229 | | -es.extend( es.ListBlock, es.Block );
|
| 9 | + */ |
| 10 | +es.ListBlock = function( style, items ) { |
| 11 | + es.Block.call( this ); |
| 12 | + this.list = new es.ListBlockList( style, items ); |
| 13 | + this.$ = this.list.$ |
| 14 | + .addClass( 'editSurface-block' ) |
| 15 | + .data( 'block', this ); |
| 16 | + var block = this; |
| 17 | + this.list.on( 'update', function() { |
| 18 | + block.emit( 'update' ); |
| 19 | + } ); |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Creates a new list block object from Wikidom data. |
| 24 | + * |
| 25 | + * @param wikidomList {Object} Wikidom data to convert from |
| 26 | + */ |
| 27 | +es.ListBlock.newFromWikidom = function( wikidomList ) { |
| 28 | + return new es.ListBlock( wikidomList.style, wikidomList.items ); |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * Gets the length of all block content. |
| 33 | + */ |
| 34 | +es.ListBlock.prototype.getLength = function() { |
| 35 | + // Compensate for n+1 virtual position on the last item's content |
| 36 | + return this.list.getLength() - 1; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Inserts content into a block at an offset. |
| 41 | + * |
| 42 | + * @param offset {Integer} Position to insert content at |
| 43 | + * @param content {Object} Content to insert |
| 44 | + */ |
| 45 | +es.ListBlock.prototype.insertContent = function( offset, content ) { |
| 46 | + var location = this.list.getLocationFromOffset( offset ); |
| 47 | + location.item.flow.content.insert( location.offset, content ); |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Deletes content in a block within a range. |
| 52 | + * |
| 53 | + * @param offset {Integer} Offset to start removing content from |
| 54 | + * @param length {Integer} Offset to start removing content to |
| 55 | + */ |
| 56 | +es.ListBlock.prototype.deleteContent = function( start, end ) { |
| 57 | + // Normalize start/end |
| 58 | + if ( end < start ) { |
| 59 | + var tmp = end; |
| 60 | + end = start; |
| 61 | + start = tmp; |
| 62 | + } |
| 63 | + var location = this.list.getLocationFromOffset( start ); |
| 64 | + location.item.flow.content.remove( location.offset, location.offset + end - start ); |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Applies an annotation to a given range. |
| 69 | + * |
| 70 | + * If a range arguments are not provided, all content will be annotated. |
| 71 | + * |
| 72 | + * @param method {String} Way to apply annotation ("toggle", "add" or "remove") |
| 73 | + * @param annotation {Object} Annotation to apply |
| 74 | + * @param start {Integer} Offset to begin annotating from |
| 75 | + * @param end {Integer} Offset to stop annotating to |
| 76 | + */ |
| 77 | +es.ListBlock.prototype.annotateContent = function( method, annotation, start, end ) { |
| 78 | + // TODO: Support annotating multiple items at once |
| 79 | + var location = this.list.getLocationFromOffset( start ); |
| 80 | + location.item.content.annotate( method, annotation, location.offset, location.offset + end - start ); |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Gets content within a range. |
| 85 | + * |
| 86 | + * @param start {Integer} Offset to get content from |
| 87 | + * @param end {Integer} Offset to get content to |
| 88 | + */ |
| 89 | +es.ListBlock.prototype.getContent = function() { |
| 90 | + // TODO: Implement me! |
| 91 | + return new Content(); |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Gets content as plain text within a range. |
| 96 | + * |
| 97 | + * @param start {Integer} Offset to start get text from |
| 98 | + * @param end {Integer} Offset to start get text to |
| 99 | + * @param render {Boolean} If annotations should have any influence on output |
| 100 | + */ |
| 101 | +es.ListBlock.prototype.getText = function() { |
| 102 | + // TODO: Implement me! |
| 103 | + return ''; |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * Renders content into a container. |
| 108 | + */ |
| 109 | +es.ListBlock.prototype.renderContent = function( offset ) { |
| 110 | + this.list.renderContent( offset ); |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * Gets the offset of a position. |
| 115 | + * |
| 116 | + * @param position {Integer} Offset to translate |
| 117 | + */ |
| 118 | +es.ListBlock.prototype.getOffset = function( position ) { |
| 119 | + if ( position.top < 0 ) { |
| 120 | + return 0; |
| 121 | + } else if ( position.top >= this.$.height() ) { |
| 122 | + return this.getLength(); |
| 123 | + } |
| 124 | + var blockOffset = this.$.offset(); |
| 125 | + position.top += blockOffset.top; |
| 126 | + position.left += blockOffset.left; |
| 127 | + return this.list.getOffsetFromPosition( position ); |
| 128 | +}; |
| 129 | + |
| 130 | +/** |
| 131 | + * Gets the position of an offset. |
| 132 | + * |
| 133 | + * @param offset {Integer} Offset to translate |
| 134 | + */ |
| 135 | +es.ListBlock.prototype.getPosition = function( offset ) { |
| 136 | + var location = this.list.getLocationFromOffset( offset ) |
| 137 | + position = location.item.flow.getPosition( location.offset ), |
| 138 | + blockOffset = this.$.offset(), |
| 139 | + lineOffset = location.item.$line.find( '.editSurface-list-content' ).offset(); |
| 140 | + |
| 141 | + position.top += lineOffset.top - blockOffset.top; |
| 142 | + position.left += lineOffset.left - blockOffset.left; |
| 143 | + position.bottom += lineOffset.top - blockOffset.top; |
| 144 | + |
| 145 | + this.traverseItems( function( item ) { |
| 146 | + if ( item === location.item ) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + position.line += item.flow.lines.length; |
| 150 | + } ); |
| 151 | + return position; |
| 152 | +}; |
| 153 | + |
| 154 | +/** |
| 155 | + * Gets the start and end points of the word closest a given offset. |
| 156 | + * |
| 157 | + * @param offset {Integer} Offset to find word nearest to |
| 158 | + * @return {Object} Range object of boundaries |
| 159 | + */ |
| 160 | +es.ListBlock.prototype.getWordBoundaries = function( offset ) { |
| 161 | + var location = this.list.getLocationFromOffset( offset ); |
| 162 | + var boundaries = location.item.flow.content.getWordBoundaries( location.offset ); |
| 163 | + boundaries.start += offset - location.offset; |
| 164 | + boundaries.end += offset - location.offset; |
| 165 | + return boundaries; |
| 166 | +}; |
| 167 | + |
| 168 | +/** |
| 169 | + * Gets the start and end points of the section closest a given offset. |
| 170 | + * |
| 171 | + * @param offset {Integer} Offset to find section nearest to |
| 172 | + * @return {Object} Range object of boundaries |
| 173 | + */ |
| 174 | +es.ListBlock.prototype.getSectionBoundaries = function( offset ) { |
| 175 | + var location = this.list.getLocationFromOffset( offset ), |
| 176 | + start = offset - location.offset; |
| 177 | + return new es.Range( start, start + location.item.content.getLength() ); |
| 178 | +}; |
| 179 | + |
| 180 | +/** |
| 181 | + * Iteratively execute a callback on each item in the list. |
| 182 | + * |
| 183 | + * Traversal is performed in a depth-first pattern, which is equivilant to a vertical scan of list |
| 184 | + * items. To stop traversal, return false within the callback function. |
| 185 | + * |
| 186 | + * @param callback {Function} Function to execute for each item, accepts an item and index argument |
| 187 | + * @return {Boolean} Whether all items were traversed, or traversal was cut short |
| 188 | + */ |
| 189 | +es.ListBlock.prototype.traverseItems = function( callback ) { |
| 190 | + var stack = [{ 'list': this.list, 'index': 0 }], |
| 191 | + list, |
| 192 | + item, |
| 193 | + pop, |
| 194 | + parent, |
| 195 | + index = 0; |
| 196 | + while ( stack.length ) { |
| 197 | + iteration = stack[stack.length - 1]; |
| 198 | + pop = true; |
| 199 | + while ( iteration.index < iteration.list.items.length ) { |
| 200 | + item = iteration.list.items[iteration.index++]; |
| 201 | + if ( callback( item, index++ ) === false ) { |
| 202 | + return false; |
| 203 | + } |
| 204 | + if ( item.lists.length ) { |
| 205 | + parent = stack.length; |
| 206 | + for ( var i = 0; i < item.lists.length; i++ ) { |
| 207 | + stack.push( { 'list': item.lists[i], 'index': 0, 'parent': parent } ); |
| 208 | + } |
| 209 | + pop = false; |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + if ( pop ) { |
| 214 | + if ( iteration.parent ) { |
| 215 | + stack = stack.slice( 0, iteration.parent ); |
| 216 | + } else { |
| 217 | + stack.pop(); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + return true; |
| 222 | +}; |
| 223 | + |
| 224 | +/** |
| 225 | + * Extend es.Block to support list block creation with es.Block.newFromWikidom |
| 226 | + */ |
| 227 | +es.Block.models.list = es.ListBlock; |
| 228 | + |
| 229 | +es.extend( es.ListBlock, es.Block ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
___________________________________________________________________ |
Added: svn:eol-style |
230 | 230 | + native |
Property changes on: trunk/parsers/wikidom/demos/es/images/link.png |
___________________________________________________________________ |
Modified: svn:mime-type |
231 | 231 | - application/octet-stream |
232 | 232 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/super.png |
___________________________________________________________________ |
Modified: svn:mime-type |
233 | 233 | - application/octet-stream |
234 | 234 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/bold.png |
___________________________________________________________________ |
Modified: svn:mime-type |
235 | 235 | - application/octet-stream |
236 | 236 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/big.png |
___________________________________________________________________ |
Modified: svn:mime-type |
237 | 237 | - application/octet-stream |
238 | 238 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/italic.png |
___________________________________________________________________ |
Modified: svn:mime-type |
239 | 239 | - application/octet-stream |
240 | 240 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/clear.png |
___________________________________________________________________ |
Modified: svn:mime-type |
241 | 241 | - application/octet-stream |
242 | 242 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/bullet.png |
___________________________________________________________________ |
Modified: svn:mime-type |
243 | 243 | - application/octet-stream |
244 | 244 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/small.png |
___________________________________________________________________ |
Modified: svn:mime-type |
245 | 245 | - application/octet-stream |
246 | 246 | + image/png |
Property changes on: trunk/parsers/wikidom/demos/es/images/sub.png |
___________________________________________________________________ |
Modified: svn:mime-type |
247 | 247 | - application/octet-stream |
248 | 248 | + image/png |