Commit 1a6a1dce authored by campaign's avatar campaign

utils 添加getNodeCommonAncestor

parent c8922806
...@@ -3,35 +3,30 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -3,35 +3,30 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
this.parent = null; this.parent = null;
this.children = []; this.children = [];
this.data = {}; this.data = {};
if ( Utils.isString( options ) ) { if ( utils.isString( options ) ) {
this.setData( 'text', options ); this.setData( 'text', options )
} else { } else {
this.setData( options ); this.setData( options );
} }
this.rc = new kity.Group(); this.rc = new kity.Group();
this.rc.minderNode = this; this.rc.minderNode = this;
}, },
setPoint: function ( x, y ) { setPoint:function(x,y){
if ( arguments.length === 0 ) { this.setData('point',{
this.setData( 'point', null ); x:x,y:y
} else { })
this.setData( 'point', {
x: x,
y: y
} );
}
}, },
getPoint: function () { getPoint:function(){
return this.getData( 'point' ); return this.getData('point')
}, },
setText: function ( text ) { setText: function ( text ) {
this.setData( 'text', text ); this.setData( 'text', text )
}, },
getText: function () { getText: function () {
return this.getData( 'text' ); return this.getData( 'text' )
}, },
isRoot: function () { isRoot: function () {
return this.getParent() === null ? true : false; return this.getParent() == null ? true : false;
}, },
getParent: function () { getParent: function () {
return this.parent; return this.parent;
...@@ -94,7 +89,17 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -94,7 +89,17 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
node.root = parent.root; node.root = parent.root;
this.children.splice( index, 0, node ); this.children.splice( index, 0, node );
// this.handelInsert( node );
}, },
//
// handelInsert: function ( node ) {
// var root = this.getRoot();
// if ( root.tnh ) {
// root.tnh.handelNodeInsert.call( root.tnh, node );
// }
// },
appendChild: function ( node ) { appendChild: function ( node ) {
return this.insertChild( node ); return this.insertChild( node );
}, },
...@@ -112,17 +117,25 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -112,17 +117,25 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
if ( index >= 0 ) { if ( index >= 0 ) {
removed = this.children.splice( index, 1 )[ 0 ]; removed = this.children.splice( index, 1 )[ 0 ];
removed.parent = null; removed.parent = null;
// this.handelRemove( removed );
} }
}, },
// handelRemove: function ( node ) {
// var root = this.getRoot();
// if ( root.tnh ) {
// root.tnh.handelNodeRemove.call( root.tnh, node );
// }
// },
getChild: function ( index ) { getChild: function ( index ) {
return this.children[ index ]; return this.children[ index ];
}, },
getFirstChild: function () { getFirstChild: function () {
return this.children[ 0 ]; return this.children[ 0 ]
}, },
getLastChild: function () { getLastChild: function () {
return this.children[ this.children.length - 1 ]; return this.children[ this.children.length - 1 ]
}, },
getData: function ( name ) { getData: function ( name ) {
if ( name === undefined ) { if ( name === undefined ) {
...@@ -132,17 +145,17 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -132,17 +145,17 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
}, },
setData: function ( name, value ) { setData: function ( name, value ) {
if ( name === undefined ) { if(name === undefined){
this.data = {}; this.data = {}
} else if ( Utils.isObject( name ) ) { }else if(utils.isObject(name)){
Utils.extend( this.data, name ); utils.extend(this.data,name)
} else { }else{
if ( value === undefined ) { if(value === undefined){
this.data[ name ] = null; this.data[name] = null;
delete this.data[ name ]; delete this.data[name]
} else { }else{
this.data[ name ] = value; this.data[name] = value;
} }
} }
}, },
...@@ -150,20 +163,7 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -150,20 +163,7 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
return this.rc; return this.rc;
}, },
getCommonAncestor: function ( node ) { getCommonAncestor: function ( node ) {
if ( this === node ) { return utils.getNodeCommonAncestor(this,node)
return this.parent;
}
if ( this.contains( node ) ) {
return this;
}
if ( node.contains( this ) ) {
return node;
}
var parent = this.parent;
while ( !parent.contains( node ) ) {
parent = parent.parent;
}
return parent;
}, },
contains: function ( node ) { contains: function ( node ) {
if ( this === node ) { if ( this === node ) {
...@@ -173,44 +173,44 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", { ...@@ -173,44 +173,44 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
return true; return true;
} }
var isContain = false; var isContain = false;
Utils.each( this.getChildren(), function ( i, n ) { utils.each( this.getChildren(), function ( i, n ) {
isContain = n.contains( node ); isContain = n.contains( node );
if ( isContain === true ) { if ( isContain === true ) {
return false; return false
} }
} ); } );
return isContain; return isContain;
}, },
clone: function () { clone:function(){
function cloneNode( parent, isClonedNode ) { function cloneNode(parent,isClonedNode){
var _tmp = new KM.MinderNode(); var _tmp = new KM.MinderNode();
_tmp.data = Utils.clonePlainObject( isClonedNode.getData() ); _tmp.data = utils.clonePlainObject(isClonedNode.getData());
_tmp.parent = parent; _tmp.parent = parent;
if ( parent ) { if(parent){
parent.children.push( _tmp ); parent.children.push(_tmp);
} }
for ( var i = 0, ci; ci = isClonedNode.children[ i++ ]; ) { for(var i= 0,ci;ci=isClonedNode.children[i++];){
cloneNode( _tmp, ci ); cloneNode(_tmp,ci);
} }
return _tmp; return _tmp;
} }
return function () { return function(){
return cloneNode( null, this ); return cloneNode(null,this);
} }
}(), }(),
equals: function ( node ) { equals:function(node){
if ( node.children.length != this.children.length ) { if(node.children.length != this.children.length){
return false; return false;
} }
if ( utils.compareObject( node.getData(), this.getData() ) === false ) { if(utils.compareObject(node.getData(),this.getData()) === false){
return false; return false;
} }
for ( var i = 0, ci; ci = this.children[ i++ ]; ) { for(var i= 0,ci;ci=this.children[i++];){
if ( ci.equals( node ) === false ) { if(ci.equals(node)===false){
return false; return false;
} }
} }
......
...@@ -169,6 +169,22 @@ var utils = Utils = KityMinder.Utils = { ...@@ -169,6 +169,22 @@ var utils = Utils = KityMinder.Utils = {
if (this.isArray(obj) || this.isString(obj)) return obj.length === 0; if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
for (var key in obj) if (obj.hasOwnProperty(key)) return false; for (var key in obj) if (obj.hasOwnProperty(key)) return false;
return true; return true;
},
getNodeCommonAncestor : function(nodeA,nodeB){
if ( nodeA === nodeB ) {
return nodeA.parent
}
if ( nodeA.contains( nodeB ) ) {
return this
}
if ( nodeB.contains( nodeA ) ) {
return nodeB
}
var parent = nodeA.parent;
while ( !parent.contains( nodeB ) ) {
parent = parent.parent;
}
return parent;
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment