http://www.mediawiki.org/wiki/Special:Code/MediaWiki/94297

Revision: 94297
Author:   tparscal
Date:     2011-08-11 23:09:16 +0000 (Thu, 11 Aug 2011)
Log Message:
-----------
* 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
    trunk/parsers/wikidom/lib/es/es.Content.js

Added Paths:
-----------
    trunk/parsers/wikidom/lib/es/es.Content.Selection.js

Modified: trunk/parsers/wikidom/lib/es/es.Container.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Container.js        2011-08-11 22:42:28 UTC 
(rev 94296)
+++ trunk/parsers/wikidom/lib/es/es.Container.js        2011-08-11 23:09:16 UTC 
(rev 94297)
@@ -1,6 +1,8 @@
 /**
  * Generic synchronized Object/Element container.
  * 
+ * Child objects must extend es.EventEmitter.
+ * 
  * @class
  * @constructor
  * @extends {es.EventEmitter}
@@ -59,12 +61,28 @@
 };
 
 /**
+ * Iterates over items, executing a callback for each.
+ * 
+ * Returning false in the callback will stop iteration.
+ * 
+ * @method
+ * @param callback {Function} Function to call on each item which takes item 
and index arguments
+ */
+es.Container.prototype.each = function( callback ) {
+       for ( var i = 0; i < this._list.length; i++ ) {
+               if ( !callback( this._list[i], i ) ) {
+                       break;
+               }
+       }
+};
+
+/**
  * Adds an item to the end of the container.
  * 
  * Also inserts item's Element object to the DOM and adds a listener to its 
"update" events.
  * 
  * @method
- * @param {Object} Item to append
+ * @param item {Object} Item to append
  * @emits "update"
  */
 es.Container.prototype.append = function( item ) {
@@ -84,7 +102,7 @@
  * Also inserts item's Element object to the DOM and adds a listener to its 
"update" events.
  * 
  * @method
- * @param {Object} Item to prepend
+ * @param item {Object} Item to prepend
  * @emits "update"
  */
 es.Container.prototype.prepend = function( item ) {
@@ -130,7 +148,7 @@
  * 
  * @method
  * @param item {Object} Item to insert
- * @param after {Object} Item to insert after, if null then item will be 
inserted at the end
+ * @param after {Object} Item to insert after, if null item will be inserted 
at the end
  * @emits "update"
  */
 es.Container.prototype.insertAfter = function( item, after ) {
@@ -155,7 +173,7 @@
  * Also detaches item's Element object to the DOM and removes all listeners 
its "update" events.
  * 
  * @method
- * @param {Object} Item to remove
+ * @param item {Object} Item to remove
  * @emits "update"
  */
 es.Container.prototype.remove = function( item ) {

Added: trunk/parsers/wikidom/lib/es/es.Content.Selection.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Content.Selection.js                        
        (rev 0)
+++ trunk/parsers/wikidom/lib/es/es.Content.Selection.js        2011-08-11 
23:09:16 UTC (rev 94297)
@@ -0,0 +1,142 @@
+/**
+ * Creates a selection of content objects.
+ * 
+ * @class
+ * @constructor
+ * @extends {es.EventEmitter}
+ * @param contents {Array} List of content objects to append
+ * @property contents {Array} List of content objects in selection
+ */
+es.Content.Selection = function( contents ) {
+       es.EventEmitter.call( this );
+       this.contents = contents || [];
+};
+
+/**
+ * Gets the first content object in the selection.
+ * 
+ * @method
+ * @returns {es.Content} First child object
+ */
+es.Content.Selection.prototype.first = function() {
+       return this.contents.length ? this.contents[0] : null;
+};
+
+/**
+ * Gets the last content object in the selection.
+ * 
+ * @method
+ * @returns {es.Content} Last child object
+ */
+es.Content.Selection.prototype.last = function() {
+       return this.contents.length ? this.contents[this.contents.length - 1] : 
null;
+};
+
+/**
+ * Iterates over content objects, executing a callback for each.
+ * 
+ * Returning false in the callback will stop iteration.
+ * 
+ * @method
+ * @param callback {Function} Function to call on each content object which 
takes content and index
+ * arguments
+ */
+es.Content.Selection.prototype.each = function( callback ) {
+       for ( var i = 0; i < this.contents.length; i++ ) {
+               if ( !callback( this.contents[i], i ) ) {
+                       break;
+               }
+       }
+};
+
+/**
+ * Adds a content object to the end of the selection.
+ * 
+ * @method
+ * @param content {es.Content} Content to append
+ * @emits "update"
+ */
+es.Content.Selection.prototype.append = function( content ) {
+       var selection = this;
+       content.on( 'update', function() {
+               selection.emit( 'update' );
+       } );
+       this.contents.push( content );
+       this.emit( 'update' );
+};
+
+/**
+ * Adds a content object to the beginning of the selection.
+ * 
+ * @method
+ * @param content {es.Content} Content to prepend
+ * @emits "update"
+ */
+es.Content.Selection.prototype.prepend = function( content ) {
+       var selection = this;
+       content.on( 'update', function() {
+               selection.emit( 'update' );
+       } );
+       this.contents.unshift( content );
+       this.emit( 'update' );
+};
+
+/**
+ * Adds a content object to the selection after an existing one.
+ * 
+ * @method
+ * @param content {es.Content} Content to insert
+ * @param before {es.Content} Content to insert before, if null content will 
be inserted at the end
+ * @emits "update"
+ */
+es.Content.Selection.prototype.insertBefore = function( content, before ) {
+       var selection = this;
+       content.on( 'update', function() {
+               selection.emit( 'update' );
+       } );
+       if ( before ) {
+               this.contents.splice( before.getIndex(), 0, content );
+       } else {
+               this.contents.push( content );
+       }
+       this.emit( 'update' );
+};
+/**
+ * Adds a content object to the selection after an existing one.
+ * 
+ * @method
+ * @param content {Object} Content to insert
+ * @param after {Object} Content to insert after, if null content will be 
inserted at the end
+ * @emits "update"
+ */
+es.Content.Selection.prototype.insertAfter = function( content, after ) {
+       var selection = this;
+       content.on( 'update', function() {
+               selection.emit( 'update' );
+       } );
+       if ( after ) {
+               this.contents.splice( after.getIndex() + 1, 0, content );
+       } else {
+               this.contents.push( content );
+       }
+       this.emit( 'update' );
+};
+
+/**
+ * Removes a content object from the selection.
+ * 
+ * Also detaches item's Element object to the DOM and removes all listeners 
its "update" events.
+ * 
+ * @method
+ * @param content {Object} Content to remove
+ * @emits "update"
+ */
+es.Content.Selection.prototype.remove = function( content ) {
+       content.removeAllListeners( 'update' );
+       this.contents.splice( content.getIndex(), 1 );
+       this.emit( 'update' );
+};
+
+/* Inheritance */
+
+es.extend( es.Content.Selection, es.EventEmitter );


Property changes on: trunk/parsers/wikidom/lib/es/es.Content.Selection.js
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Modified: trunk/parsers/wikidom/lib/es/es.Content.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Content.js  2011-08-11 22:42:28 UTC (rev 
94296)
+++ trunk/parsers/wikidom/lib/es/es.Content.js  2011-08-11 23:09:16 UTC (rev 
94297)
@@ -12,9 +12,10 @@
  * @param data {Array} List of plain or annotated characters
  * @property data {Array} List of plain or annotated characters
  */
-es.Content = function( data ) {
+es.Content = function( data, attributes ) {
        es.EventEmitter.call( this );
        this.data = data || [];
+       this.attributes = attributes || {};
 };
 
 /* Static Members */
@@ -291,6 +292,38 @@
 /* Methods */
 
 /**
+ * Gets the length of the content data.
+ * 
+ * @method
+ * @returns {Integer} Length of content data
+ */
+es.Content.prototype.getLength = function() {
+       return this.data.length; 
+};
+
+/**
+ * Gets the value of an attribute.
+ * 
+ * @method
+ * @param name {String} Name of attribute to get value for
+ * @returns {Mixed} Value of attribute, or undefined if attribute does not 
exist
+ */
+es.Content.prototype.getAttribute = function( name ) {
+       return this.attributes[name];
+};
+
+/**
+ * Sets the value of an attribute.
+ * 
+ * @method
+ * @param name {String} Name of attribute to set value for
+ * @param value {Mixed} Value to set attribute to
+ */
+es.Content.prototype.setAttribute = function( name, value ) {
+       this.attributes[name] = value;
+};
+
+/**
  * Gets plain text version of the content within a specific range.
  * 
  * Range arguments (start and end) are clamped if out of range.
@@ -391,16 +424,6 @@
 };
 
 /**
- * Gets the length of the content data.
- * 
- * @method
- * @returns {Integer} Length of content data
- */
-es.Content.prototype.getLength = function() {
-       return this.data.length; 
-};
-
-/**
  * Gets a list of indexes within the content data which use a given annotation.
  * 
  * Strict coverage may be used to compare not only annotation types, but also 
their data. Since new


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to