Commit b4f540a6 authored by campaign's avatar campaign

Merge remote-tracking branch 'origin/dev' into dev

Conflicts:
	demo/dev.html
	src/core/minder.event.js
	src/core/utils.js
parents ee395869 89efd7bb
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</style> </style>
</head> </head>
<body style="background:#262626; margin:0; padding:0"> <body style="background:#262626; margin:0; padding:0">
<div id="kityminder" style="height:1000px;width:100%"></div> <div id="kityminder" style="height:1000px;width:100%"></div>
</body> </body>
<script> <script>
......
...@@ -17,8 +17,10 @@ kity.extendClass( Minder, { ...@@ -17,8 +17,10 @@ kity.extendClass( Minder, {
this._paper.on( 'click mousedown mouseup mousemove touchstart touchmove touchend', this._firePharse.bind( this ) ); this._paper.on( 'click mousedown mouseup mousemove touchstart touchmove touchend', this._firePharse.bind( this ) );
}, },
_bindKeyboardEvents: function () { _bindKeyboardEvents: function () {
//只能在这里做,要不无法触发 if ( ( navigator.userAgent.indexOf( 'iPhone' ) == -1 ) && ( navigator.userAgent.indexOf( 'iPod' ) == -1 ) && ( navigator.userAgent.indexOf( 'iPad' ) == -1 ) ) {
Utils.listen( document.body, 'keydown keyup keypress', this._firePharse.bind( this ) ); //只能在这里做,要不无法触发
Utils.listen( document.body, 'keydown keyup keypress', this._firePharse.bind( this ) );
}
}, },
_firePharse: function ( e ) { _firePharse: function ( e ) {
var beforeEvent, preEvent, executeEvent; var beforeEvent, preEvent, executeEvent;
......
KityMinder.registerModule( "LayoutModule", function () { KityMinder.registerModule( "LayoutModule", function () {
var defaultHeight = 35; var defaultHeight = 35;
var isOdd = function ( num ) {
return num % 2 !== 0;
};
//更新分支的高度信息 //更新分支的高度信息
var updateBranchHeight = function ( node, appendSide, root, isAdd, oldParent ) { var updateBranchHeight = function ( node, appendSide, root, isAdd, oldParent ) {
var siblings = ( function () { var siblings = ( function () {
...@@ -19,7 +16,6 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -19,7 +16,6 @@ KityMinder.registerModule( "LayoutModule", function () {
node.setData( "branchheight", defaultHeight + 10 ); node.setData( "branchheight", defaultHeight + 10 );
if ( isAdd ) { if ( isAdd ) {
var add = ( ( siblings.length === 1 && node.getParent() !== root ) ? 0 : ( defaultHeight + 10 ) ); var add = ( ( siblings.length === 1 && node.getParent() !== root ) ? 0 : ( defaultHeight + 10 ) );
console.log( add );
while ( parent || ( parent === root ) ) { while ( parent || ( parent === root ) ) {
var branchheight = parent.getData( appendSide + "Height" ) || parent.getData( "branchheight" ) || 0; var branchheight = parent.getData( appendSide + "Height" ) || parent.getData( "branchheight" ) || 0;
if ( parent === root ) { if ( parent === root ) {
...@@ -49,6 +45,7 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -49,6 +45,7 @@ KityMinder.registerModule( "LayoutModule", function () {
return true; return true;
} }
}; };
var reAnalyze = function ( km, layerArray, appendSide ) { var reAnalyze = function ( km, layerArray, appendSide ) {
for ( var lv = 0; lv < layerArray.length; lv++ ) { for ( var lv = 0; lv < layerArray.length; lv++ ) {
var lvData = layerArray[ lv ]; var lvData = layerArray[ lv ];
...@@ -63,10 +60,30 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -63,10 +60,30 @@ KityMinder.registerModule( "LayoutModule", function () {
var part2 = ( children[ j + 1 ] ? ( children[ j + 1 ].getData( "branchheight" ) - 10 ) / 2 : 0 ); var part2 = ( children[ j + 1 ] ? ( children[ j + 1 ].getData( "branchheight" ) - 10 ) / 2 : 0 );
sY += ( part1 + part2 ); sY += ( part1 + part2 );
} }
km.execCommand( "rendernode", children ); km.fire( "rendernode", {
nodes: children,
rerender: false
} );
} }
} }
}; };
var setX = function ( node ) {
var parent = node.getParent();
if ( !parent ) return false;
var parentX = parent.getData( "x" );
var parentWidth = parent.getRenderContainer().getWidth();
if ( parent.getData( "align" ) === "center" ) {
parentWidth = parentWidth / 2;
}
var side = node.getData( "appendside" );
if ( side === "left" ) {
node.setData( "x", parentX - parentWidth - 50 );
} else {
node.setData( "x", parentX + parentWidth + 50 );
}
};
var createChildNode = function ( km, parent, index ) { var createChildNode = function ( km, parent, index ) {
var root = km.getRoot(); var root = km.getRoot();
var appendSide = parent.getData( "appendside" ); var appendSide = parent.getData( "appendside" );
...@@ -76,18 +93,9 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -76,18 +93,9 @@ KityMinder.registerModule( "LayoutModule", function () {
_node.setData( "appendside", appendSide ); _node.setData( "appendside", appendSide );
if ( parent === root ) {
var childCount = parent.getChildren().length;
console.log( childCount );
if ( isOdd( parseInt( childCount / 5 ) ) ) {
root.setData( "appendside", "left" );
} else {
root.setData( "appendside", "right" );
}
}
var parentX = parent.getData( "x" ); var parentX = parent.getData( "x" );
var parentWidth = parent.getRenderContainer().getWidth(); var parentWidth = parent.getRenderContainer().getWidth();
if ( parent.getData( "align" ) === "center" ) parentWidth = parentWidth / 2;
switch ( appendSide ) { switch ( appendSide ) {
case "left": case "left":
...@@ -145,17 +153,40 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -145,17 +153,40 @@ KityMinder.registerModule( "LayoutModule", function () {
} }
layerData.splice( insertPos, 0, _node ); layerData.splice( insertPos, 0, _node );
if ( parent === root ) {
var leftCount = parent.getData( "layerleft" );
var rightCount = parent.getData( "layerright" );
leftCount = leftCount[ 1 ] ? leftCount[ 1 ].length : 0;
rightCount = rightCount[ 1 ] ? rightCount[ 1 ].length : 0;
if ( rightCount > leftCount && rightCount > 1 ) {
parent.setData( "appendside", "left" );
} else {
parent.setData( "appendside", "right" );
}
}
var reAnal = updateBranchHeight( _node, appendSide, root, true ); var reAnal = updateBranchHeight( _node, appendSide, root, true );
//判断是重绘全部还是只是添加节点 //判断是重绘全部还是只是添加节点
if ( reAnal ) { if ( reAnal ) {
reAnalyze( km, layerArray, appendSide ); reAnalyze( km, layerArray, appendSide );
} else { } else {
_node.setData( "y", _node.getParent().getData( "y" ) ); _node.setData( "y", _node.getParent().getData( "y" ) );
km.execCommand( "rendernode", _node ); km.fire( "rendernode", {
nodes: _node,
rerender: false
} );
} }
return _node; return _node;
}; };
var updateNode = function ( km, node ) {
km.fire( "updatenode", {
node: node,
rerender: true
} );
return node;
};
var CreateChildNodeCommand = kity.createClass( "CreateChildNodeCommand", ( function () { var CreateChildNodeCommand = kity.createClass( "CreateChildNodeCommand", ( function () {
return { return {
base: Command, base: Command,
...@@ -167,7 +198,11 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -167,7 +198,11 @@ KityMinder.registerModule( "LayoutModule", function () {
return { return {
base: Command, base: Command,
execute: function ( km, sibling ) { execute: function ( km, sibling ) {
var root = km.getRoot();
var parent = sibling.getParent(); var parent = sibling.getParent();
if ( parent === root ) {
parent.setData( "appendside", sibling.getData( "appendside" ) );
}
var index = sibling.getIndex() + 1; var index = sibling.getIndex() + 1;
if ( parent ) { if ( parent ) {
return createChildNode( km, parent, index ); return createChildNode( km, parent, index );
...@@ -200,24 +235,16 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -200,24 +235,16 @@ KityMinder.registerModule( "LayoutModule", function () {
} }
} }
var reAnal = updateBranchHeight( nodes[ i ], appendSide, root, false, parent ); var reAnal = updateBranchHeight( nodes[ i ], appendSide, root, false, parent );
console.log( layerArray );
if ( reAnal ) { if ( reAnal ) {
reAnalyze( km, layerArray, appendSide ); reAnalyze( km, layerArray, appendSide );
} }
if ( parent === root ) {
var childCount = parent.getChildren().length;
if ( isOdd( parseInt( childCount / 5 ) ) ) {
root.setData( "appendside", "left" );
} else {
root.setData( "appendside", "right" );
}
}
} }
} }
this.setContentChanged( true ); this.setContentChanged( true );
} }
}; };
} )() ); } )() );
return { return {
"commands": { "commands": {
"createchildnode": CreateChildNodeCommand, "createchildnode": CreateChildNodeCommand,
...@@ -226,7 +253,33 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -226,7 +253,33 @@ KityMinder.registerModule( "LayoutModule", function () {
}, },
"events": { "events": {
"contentupdate": function ( e ) {
var me = this;
updateNode( me, e.node );
},
"noderendercomplete": function ( e ) {
if ( !e.rerender ) return false;
var parent = e.node;
var nodes = [];
parent.preTraverse( function ( node ) {
var prt = node.getParent();
if ( !prt ) return false;
var parentWidth = prt.getData( "width" );
var parentX = prt.getData( "x" );
if ( parent.getData( "align" ) === "center" ) parentWidth = parentWidth / 2;
if ( parent.getData( "appendside" ) === "left" ) {
node.setData( "x", parentX - parentWidth - 50 );
} else {
node.setData( "x", parentX + parentWidth + 50 );
}
nodes.push( node );
} );
this.fire( "rendernode", {
nodes: nodes,
rerender: false
} );
}
} }
}; };
} ); } );
\ No newline at end of file
KityMinder.registerModule( "RenderModule", function () { KityMinder.registerModule( "RenderModule", function () {
var RenderNodeCommand = kity.createClass( "RenderNodeCommand", ( function () { var MinderNodeShape = kity.createClass( "MinderNodeShape", ( function () {
var MinderNodeShape = kity.createClass( "MinderNodeShape", ( function () { return {
return { constructor: function ( container ) {
constructor: function ( container ) { this.rect = new kity.Rect();
this.rect = new kity.Rect(); this.text = new kity.Text();
this.text = new kity.Text(); this.shape = new kity.Group();
this.shape = new kity.Group(); this.shape.addShapes( [ this.rect, this.text ] );
this.shape.addShapes( [ this.rect, this.text ] ); container.addShape( this.shape, "nodeShape" );
container.addShape( this.shape, "nodeShape" ); },
}, highlight: function () {
highlight: function () { this.rect.stroke( new kity.Pen( "white", 3 ) );
this.rect.stroke( new kity.Pen( "white", 3 ) ); },
}, unhighlight: function () {
unhighlight: function () { this.rect.stroke( this.NormalInfo );
this.rect.stroke( this.NormalInfo ); }
} };
}; } )() );
} )() ); var renderNode = function ( km, node ) {
var renderNode = function ( km, node ) { console.log( km, node );
var styledefault = { var styledefault = {
radius: 10, radius: 10,
fill: "yellow", fill: "yellow",
stroke: "orange", stroke: "orange",
color: "black", color: "black",
padding: [ 5, 10, 5, 10 ], padding: [ 5, 10, 5, 10 ],
fontSize: 20, fontSize: 20,
}; };
var kR = node.getRenderContainer(); var kR = node.getRenderContainer();
var nodeShape = kR.nodeShape = kR.nodeShape || new MinderNodeShape( kR ); var nodeShape = kR.nodeShape = kR.nodeShape || new MinderNodeShape( kR );
var nd = JSON.parse( JSON.stringify( styledefault ) ); var nd = JSON.parse( JSON.stringify( styledefault ) );
var nodeD = Utils.extend( nd, node.getData( "style" ) ); var nodeD = Utils.extend( nd, node.getData( "style" ) );
node.setData( "style", nodeD ); node.setData( "style", nodeD );
var _style = nodeD; var _style = nodeD;
nodeShape.text nodeShape.text
.setContent( node.getData( "text" ) || "Node" ) .setContent( node.getData( "text" ) || "Node" )
.setSize( nodeD.fontSize ) .setSize( nodeD.fontSize )
.fill( nodeD.color ); .fill( nodeD.color );
var txtWidth = nodeShape.text.getWidth(); var txtWidth = nodeShape.text.getWidth();
var txtHeight = nodeShape.text.getHeight(); var txtHeight = nodeShape.text.getHeight();
var _padding = _style.padding; var _padding = _style.padding;
var _rectWidth = txtWidth + _padding[ 1 ] + _padding[ 3 ]; var _rectWidth = txtWidth + _padding[ 1 ] + _padding[ 3 ];
var _rectHeight = txtHeight + _padding[ 0 ] + _padding[ 2 ]; var _rectHeight = txtHeight + _padding[ 0 ] + _padding[ 2 ];
nodeShape.text node.setData( "width", _rectWidth );
.setX( _padding[ 3 ] ).setY( _padding[ 0 ] + txtHeight ); node.setData( "height", _rectHeight );
nodeShape.text
.setX( _padding[ 3 ] ).setY( _padding[ 0 ] + txtHeight );
nodeShape.NormalInfo = new kity.Pen( _style.stroke, _style.strokeWidth ); nodeShape.NormalInfo = new kity.Pen( _style.stroke, _style.strokeWidth );
nodeShape.rect.setWidth( _rectWidth ).setHeight( _rectHeight ).stroke( nodeShape.NormalInfo ).fill( _style.fill ).setRadius( _style.radius ); nodeShape.rect.setWidth( _rectWidth ).setHeight( _rectHeight ).stroke( nodeShape.NormalInfo ).fill( _style.fill ).setRadius( _style.radius );
switch ( node.getData( "align" ) ) { switch ( node.getData( "align" ) ) {
case "center": case "center":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth / 2, node.getData( "y" ) - _rectHeight / 2 ) ); nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth / 2, node.getData( "y" ) - _rectHeight / 2 ) );
break; break;
case "right": case "right":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth, node.getData( "y" ) - _rectHeight / 2 ) ); nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth, node.getData( "y" ) - _rectHeight / 2 ) );
break; break;
default: default:
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ), node.getData( "y" ) - _rectHeight / 2 ) ); nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ), node.getData( "y" ) - _rectHeight / 2 ) );
break; break;
} }
if ( km.isNodeSelected( node ) ) { if ( km.isNodeSelected( node ) ) {
nodeShape.highlight(); nodeShape.highlight();
} }
}; };
var renderNodes = function ( km, node ) { var renderNodes = function ( km, node ) {
if ( node instanceof Array ) { if ( node instanceof Array ) {
if ( node.length === 0 ) return false; if ( node.length === 0 ) return false;
for ( var i = 0; i < node.length; i++ ) { for ( var i = 0; i < node.length; i++ ) {
renderNode( km, node[ i ] ); renderNode( km, node[ i ] );
}
} else {
renderNode( km, node );
} }
}; return false;
} else {
renderNode( km, node );
return true;
}
};
var RenderNodeCommand = kity.createClass( "RenderNodeCommand", ( function () {
return { return {
base: Command, base: Command,
execute: renderNodes execute: renderNodes
...@@ -95,6 +100,16 @@ KityMinder.registerModule( "RenderModule", function () { ...@@ -95,6 +100,16 @@ KityMinder.registerModule( "RenderModule", function () {
}, },
"keydown keyup": function ( e ) { "keydown keyup": function ( e ) {
},
"rendernode": function ( e ) {
var nodes = e.nodes;
var rerender = e.rerender;
renderNodes( this, nodes );
this.fire( "noderendercomplete", {
node: nodes,
rerender: rerender
} );
} }
} }
}; };
......
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