r94297 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94296‎ | r94297 | r94298 >
Date:23:09, 11 August 2011
Author:tparscal
Status:deferred
Tags:
Comment:
* Added each method for iterating over es.Container objects
* Fixed some documentation for es.Container
* Added new es.Content.Selection object, which contains es.Content objects
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Container.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.Selection.js (added) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)

Diff [purge]

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
1144 + native
Added: svn:mime-type
2145 + text/plain
Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -12,9 +12,10 @@
1313 * @param data {Array} List of plain or annotated characters
1414 * @property data {Array} List of plain or annotated characters
1515 */
16 -es.Content = function( data ) {
 16+es.Content = function( data, attributes ) {
1717 es.EventEmitter.call( this );
1818 this.data = data || [];
 19+ this.attributes = attributes || {};
1920 };
2021
2122 /* Static Members */
@@ -291,6 +292,38 @@
292293 /* Methods */
293294
294295 /**
 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+/**
295328 * Gets plain text version of the content within a specific range.
296329 *
297330 * Range arguments (start and end) are clamped if out of range.
@@ -391,16 +424,6 @@
392425 };
393426
394427 /**
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 -/**
405428 * Gets a list of indexes within the content data which use a given annotation.
406429 *
407430 * 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 @@
22 /**
33 * Generic synchronized Object/Element container.
44 *
 5+ * Child objects must extend es.EventEmitter.
 6+ *
57 * @class
68 * @constructor
79 * @extends {es.EventEmitter}
@@ -59,12 +61,28 @@
6062 };
6163
6264 /**
 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+/**
6381 * Adds an item to the end of the container.
6482 *
6583 * Also inserts item's Element object to the DOM and adds a listener to its "update" events.
6684 *
6785 * @method
68 - * @param {Object} Item to append
 86+ * @param item {Object} Item to append
6987 * @emits "update"
7088 */
7189 es.Container.prototype.append = function( item ) {
@@ -84,7 +102,7 @@
85103 * Also inserts item's Element object to the DOM and adds a listener to its "update" events.
86104 *
87105 * @method
88 - * @param {Object} Item to prepend
 106+ * @param item {Object} Item to prepend
89107 * @emits "update"
90108 */
91109 es.Container.prototype.prepend = function( item ) {
@@ -130,7 +148,7 @@
131149 *
132150 * @method
133151 * @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
135153 * @emits "update"
136154 */
137155 es.Container.prototype.insertAfter = function( item, after ) {
@@ -155,7 +173,7 @@
156174 * Also detaches item's Element object to the DOM and removes all listeners its "update" events.
157175 *
158176 * @method
159 - * @param {Object} Item to remove
 177+ * @param item {Object} Item to remove
160178 * @emits "update"
161179 */
162180 es.Container.prototype.remove = function( item ) {

Status & tagging log