Commit acb7dc6c authored by campaign's avatar campaign

by zhanyi

parent b4f540a6
......@@ -84,7 +84,7 @@ kity.extendClass( Minder, {
result = command.execute.apply( command, [ me ].concat( cmdArgs ) );
this._fire( new MinderEvent( name.toLowerCase(), eventParams, false ) );
this._fire( new MinderEvent( "command", eventParams, false ) );
}
......
var ConnectModule = KityMinder.registerModule( "ConnectModule", function () {
var ConnectBezier = kity.createClass( "ConnectBezier", ( function () {
function mid( a, b ) {
return ( a + b ) / 2;
}
var ConnectBezier = kity.createClass( "ConnectBezier", ( 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 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 {
base: kity.Bezier,
constructor: function ( start, end ) {
this.callBase();
this.setStartSnaper( start );
this.setEndSnaper( end );
this.init();
this.updateConnection();
},
init: function () {
this.addPoint( this.startBesierPoint = new kity.BezierPoint() );
this.addPoint( this.endBesierPoint = new kity.BezierPoint() );
},
bindSnaper: function ( snaper ) {
var me = this;
snaper.on( 'shapeupdate', function () {
me.updateConnection();
} );
},
setStartSnaper: function ( snaper ) {
this.start = snaper;
this.bindSnaper( snaper );
},
setEndSnaper: function ( snaper ) {
this.end = snaper;
this.bindSnaper( snaper );
},
isReady: function () {
return !!( this.start && this.end );
},
calcEndPoints: function () {
var startEnds = getSnapPoints( this.start ),
endEnds = getSnapPoints( this.end );
var nearStart, nearEnd, minDistance = Number.MAX_VALUE;
var i, j, startEnd, endEnd, distance;
function fillNormal( snapPoint ) {
if ( snapPoint.normal ) {
return;
}
snapPoint.normal = DIR_NORMALS[ snapPoint.type ] || DIR_NORMALS.left;
}
return {
base: kity.Bezier,
constructor: function ( start, end ) {
this.callBase();
this.setStartSnaper( start );
this.setEndSnaper( end );
this.init();
this.updateConnection();
},
init: function () {
this.addPoint( this.startBesierPoint = new kity.BezierPoint() );
this.addPoint( this.endBesierPoint = new kity.BezierPoint() );
},
bindSnaper: function ( snaper ) {
var me = this;
snaper.on( 'shapeupdate', function () {
me.updateConnection();
} );
},
setStartSnaper: function ( snaper ) {
this.start = snaper;
this.bindSnaper( snaper );
},
setEndSnaper: function ( snaper ) {
this.end = snaper;
this.bindSnaper( snaper );
},
isReady: function () {
return !!( this.start && this.end );
},
calcEndPoints: function () {
var startEnds = getSnapPoints( this.start ),
endEnds = getSnapPoints( this.end );
var nearStart, nearEnd, minDistance = Number.MAX_VALUE;
var i, j, startEnd, endEnd, distance;
// 寻找最近的粘附点
// 暴力解法:可优化但不必要,因为点集不会很大
for ( i = 0; i < startEnds.length; i++ ) {
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 {
start: nearStart,
end: nearEnd
};
},
updateConnection: function () {
if ( !this.isReady() ) {
return false;
}
var endPoints = this.calcEndPoints(),
startEnd = endPoints.start,
endEnd = endPoints.end;
// 寻找最近的粘附点
// 暴力解法:可优化但不必要,因为点集不会很大
for ( i = 0; i < startEnds.length; i++ ) {
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 {
start: nearStart,
end: nearEnd
};
},
updateConnection: function () {
if ( !this.isReady() ) {
return false;
}
var endPoints = this.calcEndPoints(),
startEnd = endPoints.start,
endEnd = endPoints.end;
fillNormal( startEnd );
fillNormal( endEnd );
fillNormal( startEnd );
fillNormal( endEnd );
var pointVector = kity.Vector.fromPoints( startEnd, 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 );
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 );
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.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 );
}
};
} )() );
return {
"events": {
"rendernode": function ( e ) {
var command = e;
var minder = this;
switch ( command.commandName ) {
case "rendernode":
( function () {
var node = command.commandArgs[ 0 ];
if ( !( node instanceof Array ) ) {
node = [ node ];
}
for ( var i = 0; i < node.length; i++ ) {
var curnode = node[ i ];
if ( !curnode.getParent() ) {
return false;
} else {
var parent = curnode.getParent();
var connectExist = curnode._connect;
if ( connectExist ) {
connectExist.updateConnection();
} else {
var _connect = new ConnectBezier( parent.getRenderContainer(), curnode.getRenderContainer() );
var nodeD = curnode.getData( "style" );
_connect.stroke( new kity.Pen( nodeD.stroke, nodeD.strokeWidth ) );
curnode._connect = _connect;
minder.getRenderContainer().addShape( _connect );
}
}
}
} )();
break;
case "removenode":
( function () {
var nodes = command.commandArgs[ 0 ];
if ( ( nodes instanceof Array ) === false ) {
nodes = [ nodes ];
}
this.endBesierPoint.setVertex( endEnd.x, endEnd.y );
this.endBesierPoint.setBackward( backward.x, backward.y );
}
};
} )() );
return {
"events": {
"command": function ( e ) {
var command = e;
var minder = this;
switch ( command.commandName ) {
case "rendernode":
( function () {
var node = command.commandArgs[ 0 ];
if ( !( node instanceof Array ) ) {
node = [ node ];
}
for ( var i = 0; i < node.length; i++ ) {
var curnode = node[ i ];
if ( !curnode.getParent() ) {
return false;
} else {
var parent = curnode.getParent();
var connectExist = curnode._connect;
if ( connectExist ) {
connectExist.updateConnection();
} else {
var _connect = new ConnectBezier( parent.getRenderContainer(), curnode.getRenderContainer() );
var nodeD = curnode.getData( "style" );
_connect.stroke( new kity.Pen( nodeD.stroke, nodeD.strokeWidth ) );
curnode._connect = _connect;
minder.getRenderContainer().addShape( _connect );
}
}
}
} )();
break;
case "removenode":
( function () {
var nodes = command.commandArgs[ 0 ];
if ( ( nodes instanceof Array ) === false ) {
nodes = [ nodes ];
}
function removeConnect( node ) {
var connect = node._connect;
if ( connect && connect.remove ) {
connect.remove();
}
}
for ( var i = 0; i < nodes.length; i++ ) {
nodes[ i ].traverse( removeConnect );
}
} )();
}
}
}
};
function removeConnect( node ) {
var connect = node._connect;
if ( connect && connect.remove ) {
connect.remove();
}
}
for ( var i = 0; i < nodes.length; i++ ) {
nodes[ i ].traverse( removeConnect );
}
} )();
}
}
}
};
} );
\ No newline at end of file
......@@ -25,11 +25,13 @@ KityMinder.registerModule( "TextEditModule", function () {
receiver.setCursor(cursor)
.setKityMinder(this)
.setMinderNode(e.getTargetNode())
.setTextShape(targetShape)
.setCursorHeight()
.setCurrentIndex(position)
.updateCursor()
.setRange(range)
.setRange(range);
}
},
......
......@@ -52,12 +52,23 @@ Minder.Receiver = kity.createClass('Receiver',{
paper.addShape(this.textShape);
return this;
},
setKityMinder:function(km){
this.km = km;
return this;
},
setMinderNode:function(node){
this.minderNode = node;
return this;
},
keyboardEvents : function(e){
clearTimeout(this.timer);
var me = this;
switch(e.type){
case 'keyup':
this.textShape.setContent((this.container.textContent || this.container.innerText).replace(/\u200b/g,''));
var text = (this.container.textContent || this.container.innerText).replace(/\u200b/g,'');
this.textShape.setContent(text);
this.minderNode.setData('text',text);
this.km.execCommand('renderNode',this.minderNode);
this.updateTextData();
this.updateCursor();
this.timer = setTimeout(function(){
......
......@@ -60,14 +60,12 @@ KityMinder.registerModule( "LayoutModule", function () {
var part2 = ( children[ j + 1 ] ? ( children[ j + 1 ].getData( "branchheight" ) - 10 ) / 2 : 0 );
sY += ( part1 + part2 );
}
km.fire( "rendernode", {
nodes: children,
rerender: false
} );
km.execCommand( "rendernode", children );
}
}
};
var setX = function ( node ) {
var parent = node.getParent();
if ( !parent ) return false;
......@@ -172,10 +170,7 @@ KityMinder.registerModule( "LayoutModule", function () {
reAnalyze( km, layerArray, appendSide );
} else {
_node.setData( "y", _node.getParent().getData( "y" ) );
km.fire( "rendernode", {
nodes: _node,
rerender: false
} );
km.execCommand( "rendernode", _node );
}
return _node;
};
......@@ -253,33 +248,7 @@ KityMinder.registerModule( "LayoutModule", function () {
},
"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 () {
var MinderNodeShape = kity.createClass( "MinderNodeShape", ( function () {
return {
constructor: function ( container ) {
this.rect = new kity.Rect();
this.text = new kity.Text();
this.shape = new kity.Group();
this.shape.addShapes( [ this.rect, this.text ] );
container.addShape( this.shape, "nodeShape" );
},
highlight: function () {
this.rect.stroke( new kity.Pen( "white", 3 ) );
},
unhighlight: function () {
this.rect.stroke( this.NormalInfo );
}
};
} )() );
var renderNode = function ( km, node ) {
console.log( km, node );
var styledefault = {
radius: 10,
fill: "yellow",
stroke: "orange",
color: "black",
padding: [ 5, 10, 5, 10 ],
fontSize: 20,
};
var kR = node.getRenderContainer();
var nodeShape = kR.nodeShape = kR.nodeShape || new MinderNodeShape( kR );
var nd = JSON.parse( JSON.stringify( styledefault ) );
var nodeD = Utils.extend( nd, node.getData( "style" ) );
node.setData( "style", nodeD );
var _style = nodeD;
nodeShape.text
.setContent( node.getData( "text" ) || "Node" )
.setSize( nodeD.fontSize )
.fill( nodeD.color );
var txtWidth = nodeShape.text.getWidth();
var txtHeight = nodeShape.text.getHeight();
var _padding = _style.padding;
var RenderNodeCommand = kity.createClass( "RenderNodeCommand", ( function () {
var MinderNodeShape = kity.createClass( "MinderNodeShape", ( function () {
return {
constructor: function ( container ) {
this.rect = new kity.Rect();
this.text = new kity.Text();
this.shape = new kity.Group();
this.shape.addShapes( [ this.rect, this.text ] );
container.addShape( this.shape, "nodeShape" );
},
highlight: function () {
this.rect.stroke( new kity.Pen( "white", 3 ) );
},
unhighlight: function () {
this.rect.stroke( this.NormalInfo );
}
};
} )() );
var renderNode = function ( km, node ) {
var _rectWidth = txtWidth + _padding[ 1 ] + _padding[ 3 ];
var _rectHeight = txtHeight + _padding[ 0 ] + _padding[ 2 ];
node.setData( "width", _rectWidth );
node.setData( "height", _rectHeight );
nodeShape.text
.setX( _padding[ 3 ] ).setY( _padding[ 0 ] + txtHeight );
var styledefault = {
radius: 10,
fill: "yellow",
stroke: "orange",
color: "black",
padding: [ 5, 10, 5, 10 ],
fontSize: 20,
};
var kR = node.getRenderContainer();
var nodeShape = kR.nodeShape = kR.nodeShape || new MinderNodeShape( kR );
var nd = JSON.parse( JSON.stringify( styledefault ) );
var nodeD = Utils.extend( nd, node.getData( "style" ) );
node.setData( "style", nodeD );
var _style = nodeD;
nodeShape.text
.setContent( node.getData( "text" ) || "Node" )
.setSize( nodeD.fontSize )
.fill( nodeD.color );
var txtWidth = nodeShape.text.getWidth();
var txtHeight = nodeShape.text.getHeight();
var _padding = _style.padding;
nodeShape.NormalInfo = new kity.Pen( _style.stroke, _style.strokeWidth );
nodeShape.rect.setWidth( _rectWidth ).setHeight( _rectHeight ).stroke( nodeShape.NormalInfo ).fill( _style.fill ).setRadius( _style.radius );
switch ( node.getData( "align" ) ) {
case "center":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth / 2, node.getData( "y" ) - _rectHeight / 2 ) );
break;
case "right":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth, node.getData( "y" ) - _rectHeight / 2 ) );
break;
default:
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ), node.getData( "y" ) - _rectHeight / 2 ) );
break;
}
var _rectWidth = txtWidth + _padding[ 1 ] + _padding[ 3 ];
var _rectHeight = txtHeight + _padding[ 0 ] + _padding[ 2 ];
nodeShape.text
.setX( _padding[ 3 ] ).setY( _padding[ 0 ] + txtHeight );
if ( km.isNodeSelected( node ) ) {
nodeShape.highlight();
}
};
var renderNodes = function ( km, node ) {
if ( node instanceof Array ) {
if ( node.length === 0 ) return false;
for ( var i = 0; i < node.length; i++ ) {
renderNode( km, node[ i ] );
nodeShape.NormalInfo = new kity.Pen( _style.stroke, _style.strokeWidth );
nodeShape.rect.setWidth( _rectWidth ).setHeight( _rectHeight ).stroke( nodeShape.NormalInfo ).fill( _style.fill ).setRadius( _style.radius );
switch ( node.getData( "align" ) ) {
case "center":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth / 2, node.getData( "y" ) - _rectHeight / 2 ) );
break;
case "right":
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ) - _rectWidth, node.getData( "y" ) - _rectHeight / 2 ) );
break;
default:
nodeShape.shape.setTransform( new kity.Matrix().translate( node.getData( "x" ), node.getData( "y" ) - _rectHeight / 2 ) );
break;
}
return false;
} else {
renderNode( km, node );
return true;
}
};
var RenderNodeCommand = kity.createClass( "RenderNodeCommand", ( function () {
if ( km.isNodeSelected( node ) ) {
nodeShape.highlight();
}
};
var renderNodes = function ( km, node ) {
if ( node instanceof Array ) {
if ( node.length === 0 ) return false;
for ( var i = 0; i < node.length; i++ ) {
renderNode( km, node[ i ] );
}
} else {
renderNode( km, node );
}
};
return {
base: Command,
execute: renderNodes
......@@ -100,16 +96,6 @@ KityMinder.registerModule( "RenderModule", function () {
},
"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