http://www.mediawiki.org/wiki/Special:Code/MediaWiki/98382
Revision: 98382 Author: catrope Date: 2011-09-28 23:05:58 +0000 (Wed, 28 Sep 2011) Log Message: ----------- Implement es.DocumentModel.newFromPlainObject and fix the es.DocumentModel constructor Modified Paths: -------------- trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js Modified: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js =================================================================== --- trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js 2011-09-28 22:52:32 UTC (rev 98381) +++ trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js 2011-09-28 23:05:58 UTC (rev 98382) @@ -9,8 +9,7 @@ * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData() */ es.DocumentModel = function( data ) { - var data = $.isArray( data ) ? data : []; - return $.extend( data, this ); + this.data = $.isArray( data ) ? data : []; }; /* Static Methods */ @@ -126,10 +125,6 @@ /* * SCRATCH CODE * -es.DocumentModel.newFromPlainObject = function( obj ) { - -}; - es.DocumentModel.prototype.toPlainObject = function() { }; @@ -173,6 +168,36 @@ }; */ +es.DocumentModel.newFromPlainObject = function( obj ) { + /* + * Flatten a node and its children into a data array, recursively. + * + * @param obj {Object} A plain node object //TODO where do we document this whole structure? + * @return {Array} Array to append the flattened version of obj to + */ + function flattenNode( obj ) { + var i, data = []; + // Open element + // TODO do we need to copy the attributes object or can we use a reference? + data.push( { 'type': obj.type, 'attributes': obj.attributes, 'node': null } ); + if ( obj.content !== undefined ) { + // Add content + data = data.concat( es.ContentModel.newFromPlainObject( obj.content ).data ); + } else { + // Add children. Only do this if there is no content property + for ( i = 0; i < obj.children.length; i++ ) { + //TODO figure out if all this concatting is inefficent. I think it is + data = data.concat( flattenNode( obj.children[i] ) ); + } + } + // Close element // TODO do we need attributes here or not? + data.push( { 'type': '/' + obj.type, 'node': null } ); + return data; + } + + return new es.DocumentModel( flattenNode( obj ) ); +}; + /* * Example of content data * _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs