Commit cb3b550b authored by Akikonata's avatar Akikonata

test

parent 6a811fe9
Subproject commit 682bc4b4200af7a7c4997758307bc9b1b6e4e291 Subproject commit 8b33f729a570f3501769d16ad4c329c0e7f4e08d
...@@ -419,9 +419,9 @@ KityMinder.registerModule( "LayoutDefault", function () { ...@@ -419,9 +419,9 @@ KityMinder.registerModule( "LayoutDefault", function () {
drawNode( node ); drawNode( node );
}, },
initStyle: function () { initStyle: function () {
minder.getRenderContainer().clear();
//绘制root并且调整到正确位置 //绘制root并且调整到正确位置
var _root = this.getRoot(); var _root = this.getRoot();
minder.getRenderContainer().clear();
minder.handelNodeInsert( _root ); minder.handelNodeInsert( _root );
var rc = new RootShape( _root ); var rc = new RootShape( _root );
translateNode( _root ); translateNode( _root );
......
...@@ -3,282 +3,383 @@ KityMinder.registerModule( "LayoutGreen", function () { ...@@ -3,282 +3,383 @@ KityMinder.registerModule( "LayoutGreen", function () {
var minderWidth = _target.clientWidth; var minderWidth = _target.clientWidth;
var minderHeight = _target.clientHeight; var minderHeight = _target.clientHeight;
var minder = this; var minder = this;
var ConnectBezier = kity.createClass( "ConnectBezier", ( function () { var ShIcon = kity.createClass( "DefaultshIcon", ( function () {
function mid( a, b ) {
return ( a + b ) / 2;
}
function getSnapPoints( snaper ) {
if ( snaper.getSnapPoints ) {
return snaper.getSnapPoints();
}
var box = snaper.getRenderBox();
var x1 = box.x,
x2 = box.x + box.width,
y1 = box.y,
y2 = box.y + box.height,
xm = mid( x1, x2 ),
ym = mid( y1, y2 );
return [ {
x: xm,
y: y1,
type: 'top'
}, // top
{
x: x2,
y: ym,
type: 'right'
}, // right
{
x: xm,
y: y2,
type: 'bottom'
}, // bottom
{
x: x1,
y: ym,
type: 'left'
} // left
];
}
var DIR_NORMALS = {
top: new kity.Vector( 0, -1 ),
left: new kity.Vector( -1, 0 ),
bottom: new kity.Vector( 0, 1 ),
right: new kity.Vector( 1, 0 )
};
function fillNormal( snapPoint ) {
if ( snapPoint.normal ) {
return;
}
snapPoint.normal = DIR_NORMALS[ snapPoint.type ] || DIR_NORMALS.left;
}
return { return {
base: kity.Bezier, constructor: function ( node ) {
constructor: function ( start, end ) { this._show = false;
this.callBase(); this._node = node;
this.setStartSnaper( start ); var iconShape = this.shape = new kity.Group();
this.setEndSnaper( end ); iconShape.class = "shicon";
this.init(); iconShape.icon = this;
this.updateConnection(); var circle = this._circle = new kity.Circle().fill( "white" ).stroke( "gray" ).setRadius( 5 );
}, var plus = this._plus = new kity.Path();
init: function () { plus.getDrawer()
this.addPoint( this.startBesierPoint = new kity.BezierPoint() ); .moveTo( -3, 0 )
this.addPoint( this.endBesierPoint = new kity.BezierPoint() ); .lineTo( 3, 0 )
}, .moveTo( 0, -3 )
bindSnaper: function ( snaper ) { .lineTo( 0, 3 );
var me = this; plus.stroke( "gray" );
snaper.on( 'shapeupdate', function () { var dec = this._dec = new kity.Path();
me.updateConnection(); dec.getDrawer()
} ); .moveTo( -3, 0 )
}, .lineTo( 3, 0 );
setStartSnaper: function ( snaper ) { dec.stroke( "gray" );
this.start = snaper; minder.getRenderContainer().addShape( iconShape );
this.bindSnaper( snaper ); iconShape.addShapes( [ circle, plus, dec ] );
}, node.setData( "shicon", this );
setEndSnaper: function ( snaper ) { this.update();
this.end = snaper; this.switchState();
this.bindSnaper( snaper );
},
isReady: function () {
return !!( this.start && this.end );
}, },
calcEndPoints: function () { switchState: function () {
var startEnds = getSnapPoints( this.start ), if ( !this._show ) {
endEnds = getSnapPoints( this.end ); this._plus.setOpacity( 0 );
var nearStart, nearEnd, minDistance = Number.MAX_VALUE; this._dec.setOpacity( 1 );
var i, j, startEnd, endEnd, distance; this._show = true;
} else {
// 寻找最近的粘附点 this._plus.setOpacity( 1 );
// 暴力解法:可优化但不必要,因为点集不会很大 this._dec.setOpacity( 0 );
for ( i = 0; i < startEnds.length; i++ ) { this._show = false;
for ( j = 0; j < endEnds.length; j++ ) {
distance = Math.abs( startEnds[ i ].x - endEnds[ j ].x ) + Math.abs( startEnds[ i ].y - endEnds[ j ].y ) * 0.5; //Vector.fromPoints( startEnds[i], endEnds[j] ).length();
if ( distance < minDistance ) {
minDistance = distance;
nearStart = startEnds[ i ];
nearEnd = endEnds[ j ];
}
}
} }
return { return this._show;
start: nearStart,
end: nearEnd
};
}, },
updateConnection: function () { update: function () {
if ( !this.isReady() ) { var node = this._node;
return false; var Layout = node.getData( "layout" );
var nodeShape = node.getRenderContainer();
var nodeX, nodeY = ( node.getType() === "main" ? Layout.y : ( Layout.y + nodeShape.getHeight() / 2 - 5 ) );
if ( Layout.appendside === "left" ) {
nodeX = nodeShape.getRenderBox().closurePoints[ 1 ].x - 6;
} else {
nodeX = nodeShape.getRenderBox().closurePoints[ 0 ].x + 6;
} }
var endPoints = this.calcEndPoints(), this.shape.setTransform( new kity.Matrix().translate( nodeX, nodeY ) );
startEnd = endPoints.start, },
endEnd = endPoints.end; remove: function () {
this.shape.remove();
fillNormal( startEnd );
fillNormal( endEnd );
var pointVector = kity.Vector.fromPoints( startEnd, endEnd );
var forward = kity.Vector.projection( pointVector, startEnd.normal );
var backward = kity.Vector.projection( kity.Vector.reverse( pointVector ), endEnd.normal );
forward = kity.Vector.multipy( forward, 0.5 );
forward = kity.Vector.add( startEnd, forward );
backward = kity.Vector.multipy( backward, 0.5 );
backward = kity.Vector.add( endEnd, backward );
this.startBesierPoint.setVertex( startEnd.x, startEnd.y );
this.startBesierPoint.setForward( forward.x, forward.y );
this.endBesierPoint.setVertex( endEnd.x, endEnd.y );
this.endBesierPoint.setBackward( backward.x, backward.y );
} }
}; };
} )() ); } )() );
var nodeDefautStyle = { //主分支
radius: 10, var MainBranch = kity.createClass( "DefaultMainBranch", ( function () {
fill: "beige",
stroke: "orange",
strokeWidth: 1,
color: "black",
padding: [ 5, 10, 5, 10 ],
fontSize: 20,
margin: [ 50, 5, 0, 5 ]
};
var MinderNodeShape = kity.createClass( "MinderNodeShape", ( function () {
return { return {
constructor: function ( node ) { constructor: function ( node ) {
var txt = this._txt = new kity.Text();
var rect = this._rect = new kity.Rect();
this._node = node; this._node = node;
var container = node.getRenderContainer(); var container = node.getRenderContainer();
var txt = this._txt = new kity.Text();
var rect = this._rect = new kity.Rect();
var shicon = this._shicon = new ShIcon( node );
container.addShapes( [ rect, txt ] ); container.addShapes( [ rect, txt ] );
var Layout = node.getData( "layout" ); var connect = this._connect = new kity.Group();
Layout.shape = this; var bezier = connect.bezier = new kity.Bezier();
var ND = JSON.parse( JSON.stringify( nodeDefautStyle ) ); var circle = connect.circle = new kity.Circle();
Layout.style = Utils.extend( ND, Layout.style ); connect.addShapes( [ bezier, circle ] );
var _style = Layout.style; minder.getRenderContainer().addShape( connect ).bringTop( minder.getRoot().getRenderContainer() );
txt.setContent( node.getData( "text" ) || "新建节点" ).setSize( _style.fontSize ).fill( _style.color ); var Layout = {
var _txtHeight = txt.getHeight(); radius: 0,
txt.translate( _style.padding[ 3 ], _txtHeight + _style.padding[ 0 ] ); fill: "white",
color: "black",
padding: [ 5.5, 20, 5.5, 20 ],
fontSize: 20,
margin: [ 0, 10, 30, 50 ],
shape: this,
align: ( "leftright" ).replace( node.getData( "layout" ).appendside, "" ),
appendside: node.getData( "layout" ).appendside
};
node.setData( "layout", Layout );
txt.translate( Layout.padding[ 3 ], Layout.padding[ 0 ] + 15 );
this.update(); this.update();
}, },
update: function () { update: function () {
var node = this._node;
var txt = this._txt; var txt = this._txt;
var rect = this._rect; var rect = this._rect;
var _style = node.getData( "layout" ).style; var node = this._node;
txt.setContent( node.getData( "text" ) || "新建节点" ).setSize( _style.fontSize ).fill( _style.color ); var Layout = node.getData( "layout" );
txt.setContent( node.getData( "text" ) ).fill( Layout.color );
var _txtWidth = txt.getWidth();
var _txtHeight = txt.getHeight(); var _txtHeight = txt.getHeight();
var _rectWidth = _style.padding[ 1 ] + _style.padding[ 3 ] + txt.getWidth(); var _rectWidth = _txtWidth + Layout.padding[ 1 ] + Layout.padding[ 3 ];
var _rectHeight = _style.padding[ 0 ] + _style.padding[ 2 ] + _txtHeight; var _rectHeight = _txtHeight + Layout.padding[ 0 ] + Layout.padding[ 2 ];
rect.fill( _style.fill ).stroke( _style.stroke ).setRadius( _style.radius ).setWidth( _rectWidth ).setHeight( _rectHeight ); rect.setWidth( _rectWidth ).setHeight( _rectHeight ).fill( node.getData( "highlight" ) ? "chocolate" : Layout.fill ).setRadius( Layout.radius );
if ( node.getData( "highlight" ) ) { this.updateConnect();
rect.stroke( new kity.Pen( "white", 3 ) ); this.updateShIcon();
},
updateConnect: function () {
var rootX = minder.getRoot().getData( "layout" ).x;
var rootY = minder.getRoot().getData( "layout" ).y;
var connect = this._connect;
var node = this._node;
var Layout = node.getData( "layout" );
var parent = node.getParent();
var nodeShape = node.getRenderContainer();
var nodeClosurePoints = nodeShape.getRenderBox().closurePoints;
var sPos;
var endPos;
if ( Layout.appendside === "left" ) {
sPos = new kity.BezierPoint( rootX - 30, nodeClosurePoints[ 2 ].y + nodeShape.getHeight() / 2 );
endPos = new kity.BezierPoint( nodeClosurePoints[ 2 ].x, nodeClosurePoints[ 2 ].y + nodeShape.getHeight() / 2 );
} else { } else {
rect.stroke( new kity.Pen( _style.stroke, _style.strokeWidth ) ); sPos = new kity.BezierPoint( rootX + 30, nodeClosurePoints[ 3 ].y + nodeShape.getHeight() / 2 );
endPos = new kity.BezierPoint( nodeClosurePoints[ 3 ].x, nodeClosurePoints[ 3 ].y + nodeShape.getHeight() / 2 );
} }
var sPosV = sPos.getVertex();
var endPosV = endPos.getVertex();
sPos.setVertex( rootX, rootY );
connect.bezier.setPoints( [ sPos, endPos ] ).stroke( "white" );
connect.circle.setCenter( endPosV.x + ( Layout.appendside === "left" ? 3 : -3 ), endPosV.y ).fill( "white" ).stroke( "gray" ).setRadius( 2 );
},
updateShIcon: function () {
this._shicon.update();
},
clear: function () {
this._node.getRenderContainer().clear();
this._connect.remove();
this._shicon.remove();
} }
}; };
} )() ); } )() );
//更新连线 //子分支
var updateConnect = function ( minder, node, action ) { var SubBranch = kity.createClass( "DefaultSubBranch", ( function () {
return {
constructor: function ( node ) {
this._node = node;
var container = node.getRenderContainer();
var txt = this._txt = new kity.Text();
var underline = this._underline = new kity.Path();
var shicon = this._shicon = new ShIcon( node );
var highlightshape = this._highlightshape = new kity.Rect();
container.addShapes( [ highlightshape, underline, txt ] );
var connect = this._connect = new kity.Path();
minder.getRenderContainer().addShape( connect ).bringTop( minder.getRoot().getRenderContainer() );
var Layout = {
stroke: new kity.Pen( "white", 1 ).setLineCap( "round" ).setLineJoin( "round" ),
color: "white",
padding: [ 5, 10, 5.5, 10 ],
fontSize: 12,
margin: [ 0, 10, 20, 5 ],
shape: this,
align: ( "leftright" ).replace( node.getData( "layout" ).appendside, "" ),
appendside: node.getData( "layout" ).appendside
};
node.setData( "layout", Layout );
txt.translate( Layout.padding[ 3 ], Layout.padding[ 0 ] + 10 );
highlightshape.fill( "chocolate" ).translate( -1, 0 );
this.update();
},
update: function () {
var node = this._node;
var Layout = node.getData( "layout" );
var underline = this._underline;
var highlightshape = this._highlightshape;
var txt = this._txt;
txt.setContent( node.getData( "text" ) ).fill( Layout.color ).setSize( Layout.fontSize );
var _txtWidth = txt.getWidth();
var _txtHeight = txt.getHeight();
var sY = Layout.padding[ 0 ] + _txtHeight / 2;
underline.getDrawer()
.clear()
.moveTo( 0, _txtHeight + Layout.padding[ 2 ] + Layout.padding[ 0 ] )
.lineTo( _txtWidth + Layout.padding[ 1 ] + Layout.padding[ 3 ], _txtHeight + Layout.padding[ 2 ] + Layout.padding[ 0 ] );
underline.stroke( Layout.stroke );
highlightshape
.setWidth( _txtWidth + Layout.padding[ 1 ] + Layout.padding[ 3 ] )
.setHeight( _txtHeight + Layout.padding[ 0 ] + Layout.padding[ 2 ] )
.setOpacity( node.getData( "highlight" ) ? 1 : 0 );
this.updateConnect();
this.updateShIcon();
},
updateConnect: function () {
var connect = this._connect;
var node = this._node;
var parentShape = node.getParent().getRenderContainer();
var parentBox = parentShape.getRenderBox();
var parentLayout = node.getParent().getData( "layout" );
var Layout = node.getData( "layout" ); var Layout = node.getData( "layout" );
var _style = Layout.style; var Shape = node.getRenderContainer();
if ( !node.getParent() ) return false; var sX, sY = parentLayout.y;
var start = node.getParent().getRenderContainer(); var nodeX, nodeY = Shape.getRenderBox().closurePoints[ 1 ].y;
var end = node.getRenderContainer(); if ( Layout.appendside === "left" ) {
var _connect = Layout.connect; sX = parentBox.closurePoints[ 1 ].x - parentLayout.margin[ 1 ];
if ( action === "remove" ) { nodeX = Shape.getRenderBox().closurePoints[ 0 ].x;
_connect.remove(); connect.getDrawer()
.clear()
.moveTo( sX, sY )
.lineTo( sX, nodeY > sY ? ( nodeY - Layout.margin[ 3 ] ) : ( nodeY + Layout.margin[ 3 ] ) );
if ( nodeY > sY ) connect.getDrawer().carcTo( Layout.margin[ 3 ], nodeX, nodeY, 0, 1 );
else connect.getDrawer().carcTo( Layout.margin[ 3 ], nodeX, nodeY );
connect.stroke( Layout.stroke );
} else { } else {
if ( _connect ) _connect.updateConnection(); sX = parentBox.closurePoints[ 0 ].x + parentLayout.margin[ 1 ];
else { nodeX = Shape.getRenderBox().closurePoints[ 1 ].x + 1;
_connect = new ConnectBezier( start, end ); connect.getDrawer()
Layout.connect = _connect; .clear()
minder.getRenderContainer().addShape( _connect ); .moveTo( sX, sY )
_connect.stroke( _style.stroke ); .lineTo( sX, nodeY > sY ? ( nodeY - Layout.margin[ 3 ] ) : ( nodeY + Layout.margin[ 3 ] ) );
if ( nodeY > sY ) connect.getDrawer().carcTo( Layout.margin[ 3 ], nodeX, nodeY );
else connect.getDrawer().carcTo( Layout.margin[ 3 ], nodeX, nodeY, 0, 1 );
connect.stroke( Layout.stroke );
} }
},
updateShIcon: function () {
this._shicon.update();
},
clear: function () {
this._node.getRenderContainer().clear();
this._connect.remove();
this._shicon.remove();
} }
}; };
//求并集 } )() );
var uSet = function ( a, b ) { //根节点
for ( var i = 0; i < a.length; i++ ) { var RootShape = kity.createClass( "DefaultRootShape", ( function () {
var idx = b.indexOf( a[ i ] ); return {
if ( idx !== -1 ) { constructor: function ( node ) {
b.splice( idx, 1 ); var container = node.getRenderContainer();
} var txt = this._txt = new kity.Text();
var rect = this._rect = new kity.Rect();
container.addShapes( [ rect, txt ] );
this._node = node;
var Layout = {
shape: this,
x: minderWidth / 2,
y: minderHeight / 2,
align: "center",
appendside: node.getData( "layout" ).appendside || "right",
leftList: [],
rightList: [],
color: "white",
fontSize: 20,
fill: "cadetblue",
stroke: null,
padding: [ 10.5, 10, 10.5, 10 ],
radius: 15,
margin: [ 0, 0, 0, 0 ]
};
node.setData( "layout", Layout );
node.setData( "text", "Minder Root" );
txt.translate( Layout.padding[ 3 ], Layout.padding[ 0 ] + 15 );
this.update();
},
update: function () {
var txt = this._txt;
var rect = this._rect;
var connect = this._connect;
var node = this._node;
var Layout = node.getData( "layout" );
txt.setContent( node.getData( "text" ) ).fill( Layout.color );
var _txtWidth = txt.getWidth();
var _txtHeight = txt.getHeight();
var _rectWidth = _txtWidth + Layout.padding[ 1 ] + Layout.padding[ 3 ];
var _rectHeight = _txtHeight + Layout.padding[ 0 ] + Layout.padding[ 2 ];
rect.setWidth( _rectWidth ).setHeight( _rectHeight ).fill( node.getData( "highlight" ) ? "chocolate" : Layout.fill ).setRadius( Layout.radius );
},
clear: function () {
this._node.getRenderContainer().clear();
} }
return a.concat( b );
}; };
} )() );
//流程:绘制->计算Y坐标->计算X坐标->translate //流程:绘制->计算Y坐标->计算X坐标->translate
//绘制node //绘制node
var drawNode = function ( node ) { var drawNode = function ( node ) {
var container = node.getRenderContainer();
var shape = node.getData( "layout" ).shape; var shape = node.getData( "layout" ).shape;
if ( !shape ) new MinderNodeShape( node ); shape.update();
else shape.update();
updateConnect( minder, node );
}; };
//以某个节点为seed对整体高度进行更改计算 //以某个节点为seed对整体高度进行更改计算
var updateLayoutVertical = function ( node ) { var updateLayoutVertical = function ( node, parent, action ) {
var Layout = node.getData( "layout" ); var effectSet = [ node ]; //用于返回受影响的节点集
var parent = node.getParent(); if ( !parent ) {
var parentLayout = parent.getData( "layout" );
var effectSet = [];
var parentHeight = parent.getRenderContainer().getHeight();
var _style = Layout.style;
var parentY = parentLayout.y;
var marginTop = _style.margin[ 0 ];
var marginBottom = _style.margin[ 2 ];
Layout.y = parentY + parentHeight / 2 + marginTop + marginBottom;
effectSet.push( node );
return effectSet; return effectSet;
}
var Layout = node.getData( "layout" );
var marginTop = Layout.margin[ 0 ],
marginBottom = Layout.margin[ 2 ];
var appendside = Layout.appendside;
var branchheight = Layout.branchheight || node.getRenderContainer().getHeight() + marginTop + marginBottom;
var countY = function ( node, appendside ) {
var nodeLayout = node.getData( "layout" );
var centerY = nodeLayout.y;
var nodeBranchHeight = nodeLayout[ appendside + "Height" ] || nodeLayout.branchheight;
var nodeChildren = nodeLayout[ appendside + "List" ] || node.getChildren();
var sY = centerY - nodeBranchHeight / 2;
if ( nodeChildren.length === 1 ) {
var childrenLayout = nodeChildren[ 0 ].getData( "layout" );
childrenLayout.y = centerY;
} else {
for ( var i = 0; i < nodeChildren.length; i++ ) {
var childrenLayout1 = nodeChildren[ i ].getData( "layout" );
if ( !childrenLayout1.shape ) break;
var childBranchHeight = childrenLayout1.branchheight;
childrenLayout1.y = sY + marginTop + childBranchHeight / 2;
sY += childBranchHeight;
}
}
return nodeChildren;
}; };
Layout.branchheight = branchheight;
//以某个节点为seed对水平方向进行调整 var parentLayout = parent.getData( "layout" );
var updateLayoutHorizon = function ( node, parent ) { var siblings = parentLayout[ appendside + "List" ] || parent.getChildren();
var effectSet = []; var getChildHeight = function ( node, appendside ) {
var Layout = node.getData( "layout" );
var _style = Layout.style;
var marginLeft = _style.margin[ 3 ];
var marginRight = _style.margin[ 1 ];
var nodeWidth = node.getRenderContainer().getWidth();
Layout.branchwidth = nodeWidth + marginLeft + marginRight;
var prt = parent;
while ( prt ) {
var children = prt.getChildren();
var parentWidth = prt.getRenderContainer().getWidth() + marginLeft + marginRight;
var sum = 0; var sum = 0;
var nodeLayout = node.getData( "layout" );
var children = nodeLayout[ appendside + "List" ] || node.getChildren();
for ( var i = 0; i < children.length; i++ ) { for ( var i = 0; i < children.length; i++ ) {
sum += children[ i ].getData( "layout" ).branchwidth; var childLayout = children[ i ].getData( "layout" );
if ( childLayout.shape ) sum += childLayout.branchheight;
} }
return sum;
};
var prt = parent;
do {
var minH = prt.getRenderContainer().getHeight() + marginTop + marginBottom;
var childH = getChildHeight( prt, appendside );
var branchH = ( minH > childH ? minH : childH );
var prtLayout = prt.getData( "layout" ); var prtLayout = prt.getData( "layout" );
prtLayout.branchwidth = ( sum > parentWidth ? sum : parentWidth );
prtLayout.childrenwidth = sum;
prt = prt.getParent();
}
if ( prt.getParent() ) {
prtLayout.branchheight = branchH + prtLayout.margin[ 0 ] + prtLayout.margin[ 2 ];
} else {
prtLayout[ appendside + "Height" ] = branchH + prtLayout.margin[ 0 ] + prtLayout.margin[ 2 ];
}
prt = prt.getParent();
} while ( prt );
//遍历
var _buffer = [ minder.getRoot() ]; var _buffer = [ minder.getRoot() ];
while ( _buffer.length !== 0 ) { while ( _buffer.length !== 0 ) {
var childrenC = _buffer[ 0 ].getChildren(); _buffer = _buffer.concat( countY( _buffer[ 0 ], appendside ) );
var parentLayout = _buffer[ 0 ].getData( "layout" );
var parentX = parentLayout.x;
var parentChildrenWidth = parentLayout.childrenwidth;
var sX = parentX - parentChildrenWidth / 2;
for ( var j = 0; j < childrenC.length; j++ ) {
var layoutC = childrenC[ j ].getData( "layout" );
layoutC.x = sX + layoutC.branchwidth / 2;
sX += layoutC.branchwidth;
}
_buffer = _buffer.concat( childrenC );
effectSet.push( _buffer[ 0 ] ); effectSet.push( _buffer[ 0 ] );
_buffer.shift(); _buffer.shift();
} }
return effectSet; return effectSet;
}; };
//以某个节点为seed对水平方向进行调整(包括调整子树)
var updateLayoutHorizon = function ( node ) {
var nodeLayout = node.getData( "layout" );
var effectSet = [ node ]; //返回受影响(即需要进行下一步translate的节点)
var parent = node.getParent();
var appendside = nodeLayout.appendside;
var selfWidth = node.getRenderContainer().getWidth();
if ( parent ) {
var parentLayout = parent.getData( "layout" );
var parentWidth = parent.getRenderContainer().getWidth();
if ( parentLayout.align === "center" ) parentWidth = parentWidth / 2;
var parentX = parentLayout.x;
switch ( appendside ) {
case "left":
nodeLayout.x = parentX - parentWidth - parentLayout.margin[ 1 ] - nodeLayout.margin[ 3 ];
break;
case "right":
nodeLayout.x = parentX + parentWidth + parentLayout.margin[ 1 ] + nodeLayout.margin[ 3 ];
break;
default:
break;
}
}
return effectSet;
};
//调整node的位置 //调整node的位置
var translateNode = function ( node ) { var translateNode = function ( node ) {
var Layout = node.getData( "layout" ); var Layout = node.getData( "layout" );
...@@ -297,6 +398,21 @@ KityMinder.registerModule( "LayoutGreen", function () { ...@@ -297,6 +398,21 @@ KityMinder.registerModule( "LayoutGreen", function () {
nodeShape.setTransform( new kity.Matrix().translate( Layout.x, Layout.y - _rectHeight / 2 ) ); nodeShape.setTransform( new kity.Matrix().translate( Layout.x, Layout.y - _rectHeight / 2 ) );
break; break;
} }
if ( Layout.shape ) {
if ( Layout.shape.updateConnect ) Layout.shape.updateConnect();
if ( Layout.shape.updateShIcon ) Layout.shape.updateShIcon();
}
};
//求并集
var uSet = function ( a, b ) {
for ( var i = 0; i < a.length; i++ ) {
var idx = b.indexOf( a[ i ] );
if ( idx !== -1 ) {
b.splice( idx, 1 );
}
}
return a.concat( b );
}; };
var _style = { var _style = {
renderNode: function ( node ) { renderNode: function ( node ) {
...@@ -305,79 +421,170 @@ KityMinder.registerModule( "LayoutGreen", function () { ...@@ -305,79 +421,170 @@ KityMinder.registerModule( "LayoutGreen", function () {
initStyle: function () { initStyle: function () {
//绘制root并且调整到正确位置 //绘制root并且调整到正确位置
var _root = this.getRoot(); var _root = this.getRoot();
this.getRenderContainer().clear().addShape( _root.getRenderContainer().clear() ); minder.getRenderContainer().clear();
var minder = this; minder.handelNodeInsert( _root );
_root.setData( "text", _root.getData( "text" ) || "I am the root" ); var rc = new RootShape( _root );
_root.setData( "layout", {} );
var Layout = _root.getData( "layout" );
Layout.style = {
radius: 20,
fill: "darkgreen",
stroke: "orange",
color: "black",
padding: [ 10, 10, 10, 10 ],
fontSize: 30,
margin: [ 0, 5, 0, 5 ]
};
Layout.x = minderWidth / 2;
Layout.y = 50;
Layout.align = "center";
drawNode( _root );
translateNode( _root ); translateNode( _root );
var box = _root.getRenderContainer().getRenderBox();
_root.setPoint( box.x, box.y );
var _buffer = _root.getChildren(); var _buffer = _root.getChildren();
_root.children = []; var Layout = _root.getData( "layout" );
//根据保存的xy值初始化左右子树
if ( _buffer.length !== 0 ) {
for ( var i = 0; i < _buffer.length; i++ ) {
var point = _buffer[ i ].getPoint();
if ( point && point.x && point.y ) {
if ( point.x > Layout.x ) {
Layout.rightList.push( _buffer[ i ] );
} else {
Layout.leftList.push( _buffer[ i ] );
}
} else {
break;
}
}
}
//如果是从其他style切过来的,需要重新布局
while ( _buffer.length !== 0 ) { while ( _buffer.length !== 0 ) {
var parent = _buffer[ 0 ].getParent();
_buffer = _buffer.concat( _buffer[ 0 ].getChildren() ); _buffer = _buffer.concat( _buffer[ 0 ].getChildren() );
var prt = _buffer[ 0 ].getParent();
_buffer[ 0 ].clearLayout();
_buffer[ 0 ].children = []; _buffer[ 0 ].children = [];
_buffer[ 0 ].setData( "layout", {} ); this.appendChildNode( prt, _buffer[ 0 ] );
this.appendChildNode( parent, _buffer[ 0 ] );
_buffer.shift(); _buffer.shift();
} }
}, },
appendChildNode: function ( parent, node, index ) { appendChildNode: function ( parent, node, index ) {
node.setData( "layout", {} ); minder.handelNodeInsert( node );
var Layout = node.getData( "layout" ); var _root = this.getRoot();
var parentLayout = parent.getData( "layout" ); var childbranch;
var minder = this; if ( !node.getData( "layout" ) ) node.setData( "layout", {} );
if ( parent.getChildren().indexOf( node ) === -1 ) { if ( parent.getChildren().indexOf( node ) === -1 ) {
if ( !index ) parent.appendChild( node ); if ( !index ) parent.appendChild( node );
else parent.insertChild( node, index ); else parent.insertChild( node, index );
} }
minder.handelNodeInsert( node ); var parentLayout = parent.getData( "layout" );
drawNode( node ); var Layout = node.getData( "layout" );
Layout.align = "center"; if ( parent.getType() === "root" ) {
//调整影响到的节点位置 node.setType( "main" );
var set1 = updateLayoutVertical( node ); var leftList = parentLayout.leftList;
var set2 = updateLayoutHorizon( node, parent ); var rightList = parentLayout.rightList;
var sibling = parent.getChildren();
var aside = Layout.appendside;
if ( !aside ) {
if ( rightList.length > 1 && rightList.length > leftList.length ) {
aside = "left";
} else {
aside = "right";
}
Layout.appendside = aside;
}
if ( leftList.indexOf( node ) !== -1 ) {
Layout.appendside = "left";
} else if ( rightList.indexOf( node ) !== -1 ) {
Layout.appendside = "right";
} else {
parentLayout.appendside = aside;
parentLayout[ aside + "List" ].push( node );
}
childbranch = new MainBranch( node );
} else {
node.setType( "sub" );
Layout.appendside = parentLayout.appendside;
childbranch = new SubBranch( node );
}
var set1 = updateLayoutVertical( node, parent, "append" );
var set2 = updateLayoutHorizon( node );
var set = uSet( set1, set2 ); var set = uSet( set1, set2 );
for ( var i = 0; i < set.length; i++ ) { for ( var i = 0; i < set.length; i++ ) {
translateNode( set[ i ] ); translateNode( set[ i ] );
var box = set[ i ].getRenderContainer().getRenderBox();
set[ i ].setPoint( box.x, box.y );
} }
//var shicon = new ShIcon( node );
}, },
appendSiblingNode: function ( sibling, node ) { appendSiblingNode: function ( sibling, node ) {
var siblingLayout = sibling.getData( "layout" );
if ( !node.getData( "layout" ) ) {
node.setData( "layout", {} );
}
var Layout = node.getData( "layout" );
var parent = sibling.getParent(); var parent = sibling.getParent();
var index = sibling.getIndex() + 1; var index = sibling.getIndex() + 1;
var appendside = siblingLayout.appendside;
Layout.appendside = appendside;
this.appendChildNode( parent, node, index ); this.appendChildNode( parent, node, index );
}, },
removeNode: function ( nodes ) { removeNode: function ( nodes ) {
var root = this.getRoot(); var root = this.getRoot();
var minder = this;
for ( var i = 0; i < nodes.length; i++ ) { for ( var i = 0; i < nodes.length; i++ ) {
var parent = nodes[ i ].getParent(); var parent = nodes[ i ].getParent();
if ( parent ) { if ( parent ) {
nodes[ i ].getRenderContainer().remove(); var _buffer = [ nodes[ i ] ];
updateConnect( minder, nodes[ i ], "remove" ); var parentLayout = parent.getData( "layout" );
while ( _buffer.length !== 0 ) {
_buffer = _buffer.concat( _buffer[ 0 ].getChildren() );
_buffer[ 0 ].getData( "layout" ).shape.clear();
_buffer[ 0 ].getRenderContainer().remove();
var prt = _buffer[ 0 ].getParent();
prt.removeChild( _buffer[ 0 ] );
_buffer.shift();
}
if ( parent === root ) {
var Layout = nodes[ i ].getData( "layout" );
var appendside = Layout.appendside;
var sideList = parentLayout[ appendside + "List" ];
var idx = sideList.indexOf( nodes[ i ] );
sideList.splice( idx, 1 );
}
parent.removeChild( nodes[ i ] ); parent.removeChild( nodes[ i ] );
updateLayoutHorizon( parent, nodes[ i ] ); var set = updateLayoutVertical( nodes[ i ], parent, "remove" );
for ( var j = 0; j < set.length; j++ ) {
translateNode( set[ j ] );
}
} }
} }
},
updateLayout: function ( node ) {
drawNode( node );
} }
}; };
this.addLayoutStyle( "green", _style ); this.addLayoutStyle( "green", _style );
return {}; return {
};
// return {
// "events": {
// "click": function ( e ) {
// var ico = e.kityEvent.targetShape.container;
// if ( ico.class === "shicon" ) {
// var isShow = ico.icon.switchState();
// var node = ico.icon._node;
// var _buffer;
// if ( isShow ) {
// _buffer = node.getChildren();
// while ( _buffer.length !== 0 ) {
// minder.appendChildNode( _buffer[ 0 ].getParent(), _buffer[ 0 ] );
// _buffer = _buffer.concat( _buffer[ 0 ].getChildren() );
// _buffer.shift();
// }
// } else {
// var Layout = node.getData( "layout" );
// var marginTop = Layout.margin[ 0 ];
// var marginBottom = Layout.margin[ 2 ];
// Layout.branchheight = node.getRenderContainer().getHeight() + marginTop + marginBottom;
// _buffer = node.getChildren();
// while ( _buffer.length !== 0 ) {
// try {
// _buffer[ 0 ].getData( "layout" ).shape.clear();
// } catch ( error ) {}
// _buffer = _buffer.concat( _buffer[ 0 ].getChildren() );
// _buffer.shift();
// }
// var set = updateLayoutVertical( node, node.getParent(), "append" );
// for ( var i = 0; i < set.length; i++ ) {
// translateNode( set[ i ] );
// }
// }
// }
// }
// }
// };
} ); } );
\ No newline at end of file
...@@ -70,11 +70,9 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -70,11 +70,9 @@ KityMinder.registerModule( "LayoutModule", function () {
} ); } );
var switchLayout = function ( km, style ) { var switchLayout = function ( km, style ) {
var _root = km.getRoot(); var _root = km.getRoot();
km.getRenderContainer().clear();
_root.preTraverse( function ( n ) { _root.preTraverse( function ( n ) {
n.clearLayout(); n.clearLayout();
n.setPoint(); n.setPoint();
n.getRenderContainer().clear();
} ); } );
km.setCurrentStyle( style ); km.setCurrentStyle( style );
km.initStyle(); km.initStyle();
......
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