Index: trunk/parsers/wikidom/lib/hype/es.DocumentModel.js |
— | — | @@ -1,11 +1,73 @@ |
2 | 2 | /** |
| 3 | + * Creates an es.DocumentModel object. |
| 4 | + * |
| 5 | + * es.DocumentModel objects extend the native Array object, so it's contents are directly accessible |
| 6 | + * through the typical methods. |
| 7 | + * |
3 | 8 | * @class |
4 | 9 | * @constructor |
| 10 | + * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData() |
5 | 11 | */ |
6 | | -es.DocumentModel = function() { |
7 | | - this.data = []; |
| 12 | +es.DocumentModel = function( data ) { |
| 13 | + var data = $.isArray( data ) ? data : []; |
| 14 | + return $.extend( data, this ); |
8 | 15 | }; |
9 | 16 | |
| 17 | +/* Static Methods */ |
| 18 | + |
| 19 | +es.DocumentModel.isContent = function( content ) { |
| 20 | + return typeof content === 'string' || $.isArray( content ); |
| 21 | +}; |
| 22 | + |
| 23 | +es.DocumentModel.isElement = function( content ) { |
| 24 | + return content.type !== undefined; |
| 25 | +}; |
| 26 | + |
| 27 | +/* Methods */ |
| 28 | + |
| 29 | +es.DocumentModel.prototype.findElement = function( node, root, callback ) { |
| 30 | + for ( var i = 0; i < this.data.length; i++ ) { |
| 31 | + if ( es.DocumentModel.isElement( this.data[i] ) ) { |
| 32 | + if ( content.node === node ) { |
| 33 | + return callback( i ); |
| 34 | + } |
| 35 | + // If we are looking for a root node, we can skip over the contents of this one |
| 36 | + if ( root ) { |
| 37 | + i += node.getContentLength() + 2; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return null; |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * Gets the element object of a node. |
| 46 | + * |
| 47 | + * @method |
| 48 | + * @param {es.DocumentModelNode} node Reference to node object to get element object for |
| 49 | + * @param {Boolean} root Whether to only scan root nodes |
| 50 | + * @returns {Object|null} Element object |
| 51 | + */ |
| 52 | +es.DocumentModel.prototype.getElement = function( node, root ) { |
| 53 | + return this.findNode( node, root, function( index ) { |
| 54 | + return this.data[index]; |
| 55 | + } ); |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * Gets the content data of a node. |
| 60 | + * |
| 61 | + * @method |
| 62 | + * @param {es.DocumentModelNode} node Reference to node object to get content data for |
| 63 | + * @param {Boolean} root Whether to only scan root nodes |
| 64 | + * @returns {Array|null} List of content and elements inside node or null if node is not found |
| 65 | + */ |
| 66 | +es.DocumentModel.prototype.getContent = function( node, root ) { |
| 67 | + return this.findNode( node, root, function( index ) { |
| 68 | + return this.data.slice( index + 1, index + node.getContentLength() ); |
| 69 | + } ); |
| 70 | +}; |
| 71 | + |
10 | 72 | /* |
11 | 73 | // High-level operations |
12 | 74 | |
— | — | @@ -39,7 +101,7 @@ |
40 | 102 | */ |
41 | 103 | |
42 | 104 | // Low-level operations |
43 | | - |
| 105 | +/* |
44 | 106 | es.DocumentModel.newFromPlainObject = function( obj ) { |
45 | 107 | |
46 | 108 | }; |
— | — | @@ -85,6 +147,7 @@ |
86 | 148 | } |
87 | 149 | } |
88 | 150 | }; |
| 151 | +*/ |
89 | 152 | |
90 | 153 | /* |
91 | 154 | * Example of content data |
— | — | @@ -96,7 +159,7 @@ |
97 | 160 | */ |
98 | 161 | var data = [ |
99 | 162 | // 0 - Beginning of paragraph |
100 | | - { 'type': 'paragraph' }, |
| 163 | + { 'type': 'paragraph', 'node': {} }, |
101 | 164 | // 1 - Plain content |
102 | 165 | 'a', |
103 | 166 | // 2 - Annotated content |
— | — | @@ -104,53 +167,53 @@ |
105 | 168 | // 3 - Annotated content |
106 | 169 | ['c', { 'type': 'italic' }], |
107 | 170 | // 4 - End of paragraph |
108 | | - { 'type': '/paragraph' } |
| 171 | + { 'type': '/paragraph', 'node': {} } |
109 | 172 | // 5 - Beginning of table |
110 | | - { 'type': 'table' }, |
| 173 | + { 'type': 'table', 'node': {} }, |
111 | 174 | // 6 - Beginning of row |
112 | | - { 'type': 'row' }, |
| 175 | + { 'type': 'row', 'node': {} }, |
113 | 176 | // 7 - Beginning of cell |
114 | | - { 'type': 'cell' }, |
| 177 | + { 'type': 'cell', 'node': {} }, |
115 | 178 | // 8 - Beginning of paragraph |
116 | | - { 'type': 'paragraph' }, |
| 179 | + { 'type': 'paragraph', 'node': {} }, |
117 | 180 | // 9 - Plain content |
118 | 181 | 'a', |
119 | 182 | // 10 - End of paragraph |
120 | | - { 'type': '/paragraph' }, |
| 183 | + { 'type': '/paragraph', 'node': {} }, |
121 | 184 | // 11 - Beginning of list |
122 | | - { 'type': 'list' }, |
| 185 | + { 'type': 'list', 'node': {} }, |
123 | 186 | // 12 - Beginning of bullet list item |
124 | | - { 'type': 'item', 'styles': ['bullet'] }, |
| 187 | + { 'type': 'item', 'styles': ['bullet'], 'node': {} }, |
125 | 188 | // 13 - Plain content |
126 | 189 | 'a', |
127 | 190 | // 14 - End of item |
128 | | - { 'type': '/item' }, |
| 191 | + { 'type': '/item', 'node': {} }, |
129 | 192 | // 15 - Beginning of nested bullet list item |
130 | | - { 'type': 'item', 'styles': ['bullet', 'bullet'] }, |
| 193 | + { 'type': 'item', 'styles': ['bullet', 'bullet'], 'node': {} }, |
131 | 194 | // 16 - Plain content |
132 | 195 | 'b', |
133 | 196 | // 17 - End of item |
134 | | - { 'type': '/item' }, |
| 197 | + { 'type': '/item', 'node': {} }, |
135 | 198 | // 18 - Beginning of numbered list item |
136 | | - { 'type': 'item', 'styles': ['number'] }, |
| 199 | + { 'type': 'item', 'styles': ['number'], 'node': {} }, |
137 | 200 | // 19 - Plain content |
138 | 201 | 'c', |
139 | 202 | // 20 - End of item |
140 | | - { 'type': '/item' }, |
| 203 | + { 'type': '/item', 'node': {} }, |
141 | 204 | // 21 - End of list |
142 | | - { 'type': '/list' }, |
| 205 | + { 'type': '/list', 'node': {} }, |
143 | 206 | // 22 - End of cell |
144 | | - { 'type': '/cell' } |
| 207 | + { 'type': '/cell', 'node': {} } |
145 | 208 | // 23 - End of row |
146 | | - { 'type': '/row' } |
| 209 | + { 'type': '/row', 'node': {} } |
147 | 210 | // 24 - End of table |
148 | | - { 'type': '/table' } |
| 211 | + { 'type': '/table', 'node': {} } |
149 | 212 | // 25 - Beginning of paragraph |
150 | | - { 'type': 'paragraph' }, |
| 213 | + { 'type': 'paragraph', 'node': {} }, |
151 | 214 | // 26 - Plain content |
152 | 215 | 'a' |
153 | 216 | // 27 - End of paragraph |
154 | | - { 'type': '/paragraph' }, |
| 217 | + { 'type': '/paragraph', 'node': {} }, |
155 | 218 | ]; |
156 | 219 | |
157 | 220 | /* |
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js |
— | — | @@ -0,0 +1,45 @@ |
| 2 | +/** |
| 3 | + * Creates an es.DocumentModelNode object. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @param {es.DocumentModel} documentModel Document model this node is a part of |
| 8 | + * @param {Integer} contentLength Length of contents |
| 9 | + * @property {es.DocumentModel} documentModel Document model this node is a part of |
| 10 | + * @property {Integer} contentLength Length of contents |
| 11 | + */ |
| 12 | +es.DocumentModelNode = function( documentModel, contentLength ) { |
| 13 | + // Inheritance |
| 14 | + es.ModelList.call( this ); |
| 15 | + es.ModelItem.call( this ); |
| 16 | + // Properties |
| 17 | + this.documentModel = documentModel; |
| 18 | + this.contentLength = contentLength || 0; |
| 19 | + // Events |
| 20 | + this.on( 'update', function() { |
| 21 | + // Recalculate length when child nodes change |
| 22 | + this.contentLength = 0; |
| 23 | + for ( var i = 0; i < this.length, i++ ) { |
| 24 | + this.contentLength += this[i].getElementLength(); |
| 25 | + } |
| 26 | + } ); |
| 27 | +}; |
| 28 | + |
| 29 | +/* Methods */ |
| 30 | + |
| 31 | +es.DocumentModelNode.getContentLength = function() { |
| 32 | + return this.contentLength; |
| 33 | +}; |
| 34 | + |
| 35 | +es.DocumentModelNode.getElementLength = function() { |
| 36 | + return this.contentLength + 2; |
| 37 | +}; |
| 38 | + |
| 39 | +es.DocumentModelNode.getContent = function( range ) { |
| 40 | + return this.documentModel.getContent( this ); |
| 41 | +}; |
| 42 | + |
| 43 | +/* Inheritance */ |
| 44 | + |
| 45 | +es.extend( es.DocumentModelNode, es.ModelList ); |
| 46 | +es.extend( es.DocumentModelNode, es.ModelItem ); |