Index: trunk/parsers/wikidom/lib/es/es.Content.Selection.js |
— | — | @@ -0,0 +1,142 @@ |
| 2 | +/** |
| 3 | + * Creates a selection of content objects. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @extends {es.EventEmitter} |
| 8 | + * @param contents {Array} List of content objects to append |
| 9 | + * @property contents {Array} List of content objects in selection |
| 10 | + */ |
| 11 | +es.Content.Selection = function( contents ) { |
| 12 | + es.EventEmitter.call( this ); |
| 13 | + this.contents = contents || []; |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Gets the first content object in the selection. |
| 18 | + * |
| 19 | + * @method |
| 20 | + * @returns {es.Content} First child object |
| 21 | + */ |
| 22 | +es.Content.Selection.prototype.first = function() { |
| 23 | + return this.contents.length ? this.contents[0] : null; |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Gets the last content object in the selection. |
| 28 | + * |
| 29 | + * @method |
| 30 | + * @returns {es.Content} Last child object |
| 31 | + */ |
| 32 | +es.Content.Selection.prototype.last = function() { |
| 33 | + return this.contents.length ? this.contents[this.contents.length - 1] : null; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Iterates over content objects, executing a callback for each. |
| 38 | + * |
| 39 | + * Returning false in the callback will stop iteration. |
| 40 | + * |
| 41 | + * @method |
| 42 | + * @param callback {Function} Function to call on each content object which takes content and index |
| 43 | + * arguments |
| 44 | + */ |
| 45 | +es.Content.Selection.prototype.each = function( callback ) { |
| 46 | + for ( var i = 0; i < this.contents.length; i++ ) { |
| 47 | + if ( !callback( this.contents[i], i ) ) { |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Adds a content object to the end of the selection. |
| 55 | + * |
| 56 | + * @method |
| 57 | + * @param content {es.Content} Content to append |
| 58 | + * @emits "update" |
| 59 | + */ |
| 60 | +es.Content.Selection.prototype.append = function( content ) { |
| 61 | + var selection = this; |
| 62 | + content.on( 'update', function() { |
| 63 | + selection.emit( 'update' ); |
| 64 | + } ); |
| 65 | + this.contents.push( content ); |
| 66 | + this.emit( 'update' ); |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Adds a content object to the beginning of the selection. |
| 71 | + * |
| 72 | + * @method |
| 73 | + * @param content {es.Content} Content to prepend |
| 74 | + * @emits "update" |
| 75 | + */ |
| 76 | +es.Content.Selection.prototype.prepend = function( content ) { |
| 77 | + var selection = this; |
| 78 | + content.on( 'update', function() { |
| 79 | + selection.emit( 'update' ); |
| 80 | + } ); |
| 81 | + this.contents.unshift( content ); |
| 82 | + this.emit( 'update' ); |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * Adds a content object to the selection after an existing one. |
| 87 | + * |
| 88 | + * @method |
| 89 | + * @param content {es.Content} Content to insert |
| 90 | + * @param before {es.Content} Content to insert before, if null content will be inserted at the end |
| 91 | + * @emits "update" |
| 92 | + */ |
| 93 | +es.Content.Selection.prototype.insertBefore = function( content, before ) { |
| 94 | + var selection = this; |
| 95 | + content.on( 'update', function() { |
| 96 | + selection.emit( 'update' ); |
| 97 | + } ); |
| 98 | + if ( before ) { |
| 99 | + this.contents.splice( before.getIndex(), 0, content ); |
| 100 | + } else { |
| 101 | + this.contents.push( content ); |
| 102 | + } |
| 103 | + this.emit( 'update' ); |
| 104 | +}; |
| 105 | +/** |
| 106 | + * Adds a content object to the selection after an existing one. |
| 107 | + * |
| 108 | + * @method |
| 109 | + * @param content {Object} Content to insert |
| 110 | + * @param after {Object} Content to insert after, if null content will be inserted at the end |
| 111 | + * @emits "update" |
| 112 | + */ |
| 113 | +es.Content.Selection.prototype.insertAfter = function( content, after ) { |
| 114 | + var selection = this; |
| 115 | + content.on( 'update', function() { |
| 116 | + selection.emit( 'update' ); |
| 117 | + } ); |
| 118 | + if ( after ) { |
| 119 | + this.contents.splice( after.getIndex() + 1, 0, content ); |
| 120 | + } else { |
| 121 | + this.contents.push( content ); |
| 122 | + } |
| 123 | + this.emit( 'update' ); |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Removes a content object from the selection. |
| 128 | + * |
| 129 | + * Also detaches item's Element object to the DOM and removes all listeners its "update" events. |
| 130 | + * |
| 131 | + * @method |
| 132 | + * @param content {Object} Content to remove |
| 133 | + * @emits "update" |
| 134 | + */ |
| 135 | +es.Content.Selection.prototype.remove = function( content ) { |
| 136 | + content.removeAllListeners( 'update' ); |
| 137 | + this.contents.splice( content.getIndex(), 1 ); |
| 138 | + this.emit( 'update' ); |
| 139 | +}; |
| 140 | + |
| 141 | +/* Inheritance */ |
| 142 | + |
| 143 | +es.extend( es.Content.Selection, es.EventEmitter ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.Content.Selection.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 144 | + native |
Added: svn:mime-type |
2 | 145 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -12,9 +12,10 @@ |
13 | 13 | * @param data {Array} List of plain or annotated characters |
14 | 14 | * @property data {Array} List of plain or annotated characters |
15 | 15 | */ |
16 | | -es.Content = function( data ) { |
| 16 | +es.Content = function( data, attributes ) { |
17 | 17 | es.EventEmitter.call( this ); |
18 | 18 | this.data = data || []; |
| 19 | + this.attributes = attributes || {}; |
19 | 20 | }; |
20 | 21 | |
21 | 22 | /* Static Members */ |
— | — | @@ -291,6 +292,38 @@ |
292 | 293 | /* Methods */ |
293 | 294 | |
294 | 295 | /** |
| 296 | + * Gets the length of the content data. |
| 297 | + * |
| 298 | + * @method |
| 299 | + * @returns {Integer} Length of content data |
| 300 | + */ |
| 301 | +es.Content.prototype.getLength = function() { |
| 302 | + return this.data.length; |
| 303 | +}; |
| 304 | + |
| 305 | +/** |
| 306 | + * Gets the value of an attribute. |
| 307 | + * |
| 308 | + * @method |
| 309 | + * @param name {String} Name of attribute to get value for |
| 310 | + * @returns {Mixed} Value of attribute, or undefined if attribute does not exist |
| 311 | + */ |
| 312 | +es.Content.prototype.getAttribute = function( name ) { |
| 313 | + return this.attributes[name]; |
| 314 | +}; |
| 315 | + |
| 316 | +/** |
| 317 | + * Sets the value of an attribute. |
| 318 | + * |
| 319 | + * @method |
| 320 | + * @param name {String} Name of attribute to set value for |
| 321 | + * @param value {Mixed} Value to set attribute to |
| 322 | + */ |
| 323 | +es.Content.prototype.setAttribute = function( name, value ) { |
| 324 | + this.attributes[name] = value; |
| 325 | +}; |
| 326 | + |
| 327 | +/** |
295 | 328 | * Gets plain text version of the content within a specific range. |
296 | 329 | * |
297 | 330 | * Range arguments (start and end) are clamped if out of range. |
— | — | @@ -391,16 +424,6 @@ |
392 | 425 | }; |
393 | 426 | |
394 | 427 | /** |
395 | | - * Gets the length of the content data. |
396 | | - * |
397 | | - * @method |
398 | | - * @returns {Integer} Length of content data |
399 | | - */ |
400 | | -es.Content.prototype.getLength = function() { |
401 | | - return this.data.length; |
402 | | -}; |
403 | | - |
404 | | -/** |
405 | 428 | * Gets a list of indexes within the content data which use a given annotation. |
406 | 429 | * |
407 | 430 | * Strict coverage may be used to compare not only annotation types, but also their data. Since new |
Index: trunk/parsers/wikidom/lib/es/es.Container.js |
— | — | @@ -1,6 +1,8 @@ |
2 | 2 | /** |
3 | 3 | * Generic synchronized Object/Element container. |
4 | 4 | * |
| 5 | + * Child objects must extend es.EventEmitter. |
| 6 | + * |
5 | 7 | * @class |
6 | 8 | * @constructor |
7 | 9 | * @extends {es.EventEmitter} |
— | — | @@ -59,12 +61,28 @@ |
60 | 62 | }; |
61 | 63 | |
62 | 64 | /** |
| 65 | + * Iterates over items, executing a callback for each. |
| 66 | + * |
| 67 | + * Returning false in the callback will stop iteration. |
| 68 | + * |
| 69 | + * @method |
| 70 | + * @param callback {Function} Function to call on each item which takes item and index arguments |
| 71 | + */ |
| 72 | +es.Container.prototype.each = function( callback ) { |
| 73 | + for ( var i = 0; i < this._list.length; i++ ) { |
| 74 | + if ( !callback( this._list[i], i ) ) { |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
63 | 81 | * Adds an item to the end of the container. |
64 | 82 | * |
65 | 83 | * Also inserts item's Element object to the DOM and adds a listener to its "update" events. |
66 | 84 | * |
67 | 85 | * @method |
68 | | - * @param {Object} Item to append |
| 86 | + * @param item {Object} Item to append |
69 | 87 | * @emits "update" |
70 | 88 | */ |
71 | 89 | es.Container.prototype.append = function( item ) { |
— | — | @@ -84,7 +102,7 @@ |
85 | 103 | * Also inserts item's Element object to the DOM and adds a listener to its "update" events. |
86 | 104 | * |
87 | 105 | * @method |
88 | | - * @param {Object} Item to prepend |
| 106 | + * @param item {Object} Item to prepend |
89 | 107 | * @emits "update" |
90 | 108 | */ |
91 | 109 | es.Container.prototype.prepend = function( item ) { |
— | — | @@ -130,7 +148,7 @@ |
131 | 149 | * |
132 | 150 | * @method |
133 | 151 | * @param item {Object} Item to insert |
134 | | - * @param after {Object} Item to insert after, if null then item will be inserted at the end |
| 152 | + * @param after {Object} Item to insert after, if null item will be inserted at the end |
135 | 153 | * @emits "update" |
136 | 154 | */ |
137 | 155 | es.Container.prototype.insertAfter = function( item, after ) { |
— | — | @@ -155,7 +173,7 @@ |
156 | 174 | * Also detaches item's Element object to the DOM and removes all listeners its "update" events. |
157 | 175 | * |
158 | 176 | * @method |
159 | | - * @param {Object} Item to remove |
| 177 | + * @param item {Object} Item to remove |
160 | 178 | * @emits "update" |
161 | 179 | */ |
162 | 180 | es.Container.prototype.remove = function( item ) { |