Permalink
Browse files

moved from folder to submodule

  • Loading branch information...
0 parents commit 0d3524eb043515faf2f4ce8491bcda5069257c2e @xavierm02 xavierm02 committed Jun 1, 2012
Showing with 139 additions and 0 deletions.
  1. +11 −0 XJSNode.css
  2. +113 −0 XJSNode.js
  3. +13 −0 XJSToken.js
  4. +2 −0 index.html
@@ -0,0 +1,11 @@
+.XJSNode {
+ margin: 3px;
+ padding: 3px;
+ display: inline-block;
+ border: 1px solid black;
+ background-color: white;
+}
+
+.XJSNode:hover {
+ background-color: rgba( 160, 160, 160, 0.1 );
+}
@@ -0,0 +1,113 @@
+function XJSNode( value ) {
+ this.childNodes = [ ];
+ if ( arguments.length > 0 ) {
+ this.value = value;
+ }
+}
+Object.defineProperties( XJSNode.prototype, {
+ appendChild: {
+ value: function ( ) {
+ return this.appendChildren( Array.toArray( arguments ) );
+ }
+ },
+ appendChildren: {
+ value: function ( childNodes ) {
+ this.childNodes = this.childNodes.concat( childNodes );
+ return this;
+ }
+ },
+ depth: {
+ get: function ( ) {
+ var previousNodes = [ ];
+ var currentNodes = [ this ];
+ var nextNodes = [ ];
+ var node;
+ var depth = 0;
+ var containsCircularReference;
+ while ( true ) {
+ containsCircularReference = currentNodes.some( function ( node ) {
+ if ( previousNodes.contains( node ) ) {
+ return true;
+ } else {
+ nextNodes = nextNodes.concat( node.childNodes );
+ }
+ } );
+ if ( containsCircularReference ) {
+ return Infinity;
+ } else {
+ if ( nextNodes.length === 0 ) {
+ return depth;
+ } else {
+ previousNodes = previousNodes.concat( currentNodes );
+ currentNodes = nextNodes;
+ nextNodes = [ ];
+ ++depth;
+ }
+ }
+ }
+ }
+ },
+ isLeaf: {
+ get: function ( ) {
+ return this.childNodes.length === 0;
+ }
+ },
+ flatten: {
+ value: function ( ) {
+ var childNodes = this.childNodes;
+ switch ( childNodes.length ) {
+ case 0:
+ return this;
+ break;
+ case 1:
+ return childNodes[ 0 ].flatten( );
+ break;
+ default:
+ this.childNodes = childNodes.map( function ( childNode ) {
+ return childNode.flatten( );
+ } );
+ return this;
+ }
+ }
+ },
+ leaves: {
+ get: function ( ) {
+ return this.childNodes.map( function ( childNode ) {
+ return childNode.isLeaf ? childNode : childNode.leaves;
+ } ).reduce( function ( childNodes1, childNodes2 ) {
+ return childNodes1.concat( childNodes2 );
+ }, [ ] );
+ }
+ },
+ toHTML: {
+ value: function ( ) {
+ var container = document.createElement( 'div' );
+ container.title = this.value;
+ container.className = 'XJSNode';
+ if ( this.isLeaf ) {
+ container.appendChild( document.createTextNode( this.value ) )
+ } else {
+ this.childNodes.forEach( function ( childNode ) {
+ container.appendChild( childNode.toHTML( ) );
+ } );
+ }
+ return container;
+ }
+ },
+ toString: {
+ value: function ( ) {
+ var string = '';
+ function walk( node, tabs ) {
+ if ( node.hasOwnProperty( 'value' ) ) {
+ string += tabs + node.value + '\r\n';
+ }
+ tabs += ' ';
+ node.childNodes.map( function ( childNode ) {
+ walk( childNode, tabs );
+ } )
+ }
+ walk( this, '' );
+ return string;
+ }
+ }
+} );
@@ -0,0 +1,13 @@
+function XJSToken( ) {
+ XJSNode.apply( this, arguments );
+}
+XJSToken.inherits( XJSNode );
+Object.defineProperties( XJSToken.prototype, {
+ source: {
+ get: function ( ) {
+ return this.leaves.map( function ( leaf ) {
+ return leaf.value;
+ } ).join( '' );
+ }
+ }
+} );
@@ -0,0 +1,2 @@
+<script src="XJSTREE.js"></script>
+<script src="C:\Users\xavierm02\workspace\xjslib.github.com\src\XJSLib.js"></script>

0 comments on commit 0d3524e

Please sign in to comment.