wisemapping-open-source/mindplot/src/main/javascript/layoutManagers/FreeMindLayoutManager.js

208 lines
8.7 KiB
JavaScript
Raw Normal View History

2011-03-24 19:00:51 +01:00
mindplot.layoutManagers.FreeMindLayoutManager = mindplot.layoutManagers.BaseLayoutManager.extend({
options:{
},
initialize:function(designer, options){
this.parent(designer, options);
},
2011-04-08 16:31:40 +02:00
_nodeConnectEvent:function(targetNode, node){
if(!this._isCentralTopic(node)){
this.parent(targetNode, node);
}
},
2011-03-24 19:00:51 +01:00
registerListenersOnNode : function(topic)
{
var id = topic.getId();
// Register node listeners ...
var designer = this.getDesigner();
topic.addEventListener('onfocus', function(event)
{
designer.onObjectFocusEvent.attempt([topic, event], designer);
});
// Add drag behaviour ...
if (topic.getType() != mindplot.NodeModel.CENTRAL_TOPIC_TYPE)
{
topic.addEventListener("mousedown",this._mousedownListener.bindWithEvent(this,[topic]));
}
2011-04-08 16:31:40 +02:00
// Register editor events ...
if (!this.getDesigner()._viewMode)
2011-03-24 19:00:51 +01:00
{
2011-04-08 16:31:40 +02:00
this.getDesigner()._editor.listenEventOnNode(topic, 'dblclick', true);
}
2011-03-24 19:00:51 +01:00
},
_mousedownListener:function(event,topic){
var workSpace = this._designer.getWorkSpace();
if (workSpace.isWorkspaceEventsEnabled())
{
// Disable double drag...
workSpace.enableWorkspaceEvents(false);
var id = topic.getId();
2011-04-08 16:31:40 +02:00
this._command = new mindplot.commands.freeMind.DragTopicCommand();
this._modifiedTopics = new Hash();
2011-03-24 19:00:51 +01:00
var topics = this.getDesigner()._getTopics();
// Disable all mouse events.
for (var i = 0; i < topics.length; i++)
{
topics[i].setMouseEventsEnabled(false);
}
var ev = new Event(event);
var screen = workSpace.getScreenManager();
// Set initial position.
// var mousePos = screen.getWorkspaceMousePosition(event);
2011-04-08 16:31:40 +02:00
this._isMovingNode=false;
2011-03-24 19:00:51 +01:00
// Register mouse move listener ...
this._mouseMoveListenerInstance = this._mouseMoveListener.bindWithEvent(this,[topic]);
screen.addEventListener('mousemove', this._mouseMoveListenerInstance);
// Register mouse up listeners ...
this._mouseUpListenerInstance = this._mouseUpListener.bindWithEvent(this,[topic]);
screen.addEventListener('mouseup', this._mouseUpListenerInstance);
// Change cursor.
window.document.body.style.cursor = 'move';
}
},
_mouseMoveListener:function(event, node){
2011-04-08 16:31:40 +02:00
if(!this._isMovingNode){
this._isMovingNode=true;
var screen = this._designer.getWorkSpace().getScreenManager();
var nodePos = node.getPosition().clone();
var pos = screen.getWorkspaceMousePosition(event);
pos.x = Math.round(pos.x);
pos.y = Math.round(pos.y);
//If still in same side
if(Math.sign(nodePos.x)==Math.sign(pos.x) || (Math.sign(nodePos.x)!=Math.sign(pos.x) && !this._isCentralTopic(node.getParent()))){
var x = nodePos.x - pos.x;
var y = nodePos.y - pos.y;
var delta = new core.Point(Math.round(x), Math.round(y));
var board = this.getTopicBoardForTopic(node.getParent());
board.updateEntry(node, delta, this._modifiedTopics);
} else {
var parentBoard = this.getTopicBoardForTopic(node.getParent());
var entryObj = parentBoard.findNodeEntryIndex(node);
var entry = entryObj.table[entryObj.index];
//.removeTopicFromBoard(node,this._modifiedTopics);
parentBoard._removeEntry(node, entryObj.table, entryObj.index, this._modifiedTopics);
this._changeChildrenSide(node, pos, this._modifiedTopics);
node.setPosition(pos.clone(), false);
if(this._modifiedTopics.set){
var key = node.getId();
if(this._modifiedTopics.hasKey(key)){
nodePos = this._modifiedTopics.get(key).originalPos;
}
this._modifiedTopics.set(key,{originalPos:nodePos, newPos:pos});
}
entryObj = parentBoard.findNewNodeEntryIndex(entry);
parentBoard._addEntry(entry, entryObj.table, entryObj.index);
parentBoard._updateTable(entryObj.index, entryObj.table, this._modifiedTopics, true);
//this.getTopicBoardForTopic(node.getParent()).addBranch(node,this._modifiedTopics);
}
this._isMovingNode=false;
}
2011-03-24 19:00:51 +01:00
event.preventDefault();
},
2011-04-08 16:31:40 +02:00
_changeChildrenSide:function(node, newPos, modifiedTopics){
var children = node._getChildren();
if(children.length>0){
var refPos = node.getPosition();
for( var i = 0 ; i< children.length ; i++){
var child = children[i];
this._changeChildrenSide(child);
var childPos = child.getPosition();
var oldPos=childPos.clone();
childPos.x = newPos.x +(childPos.x - refPos.x)*-1;
childPos.y = newPos.y +(childPos.y - refPos.y);
child.setPosition(childPos, false);
if(modifiedTopics.set){
var key = node.getId();
if(modifiedTopics.hasKey(key)){
childPos = this._modifiedTopics.get(key).originalPos;
}
this._modifiedTopics.set(key,{originalPos:oldPos, newPos:childPos});
}
}
}
},
2011-03-24 19:00:51 +01:00
_mouseUpListener:function(event, node){
var id = node.getId();
2011-04-08 16:31:40 +02:00
2011-03-24 19:00:51 +01:00
var screen = this._designer.getWorkSpace().getScreenManager();
// Remove all the events.
screen.removeEventListener('mousemove', this._mouseMoveListenerInstance);
screen.removeEventListener('mouseup', this._mouseUpListenerInstance);
delete this._mouseMoveListenerInstance;
delete this._mouseUpListenerInstance;
var topics = this.getDesigner()._getTopics();
// Disable all mouse events.
for (var i = 0; i < topics.length; i++)
{
topics[i].setMouseEventsEnabled(true);
}
// Change the cursor to the default.
window.document.body.style.cursor = 'default';
this._designer.getWorkSpace().enableWorkspaceEvents(true);
2011-04-08 16:31:40 +02:00
this._command.setModifiedTopics(this._modifiedTopics);
var actionRunner = mindplot.DesignerActionRunner.getInstance();
actionRunner.execute(this._command);
this._command=null;
this._modifiedTopics=null;
2011-03-24 19:00:51 +01:00
// var topicId = draggedTopic.getId();
// var command = new mindplot.commands.DragTopicCommand(topicId);
},
getClassName:function(){
return mindplot.layoutManagers.FreeMindLayoutManager.NAME;
},
_createMainTopicBoard:function(node){
return new mindplot.layoutManagers.boards.freeMindBoards.MainTopicBoard(node, this);
},
_createCentralTopicBoard:function(node){
return new mindplot.layoutManagers.boards.freeMindBoards.CentralTopicBoard(node, this);
2011-04-08 16:31:40 +02:00
},
_updateParentBoard:function(node, modifiedTopics){
var parent = node.getParent();
if(!this._isCentralTopic(parent)){
var parentBoard = this.getTopicBoardForTopic(parent.getParent());
var result = parentBoard.findNodeEntryIndex(parent);
var parentEntry = result.table[result.index];
var board = this.getTopicBoardForTopic(parent);
var table = board._getTableForNode(null);
if(table.length>0){
var firstChild = table[0];
var marginTop = parentEntry.getPosition()-(firstChild.getPosition()-firstChild.getTotalMarginTop());
parentBoard.setNodeChildrenMarginTop(parentEntry,marginTop);
var lastChild = table[table.length-1];
var marginBottom = (lastChild.getPosition()+lastChild.getTotalMarginBottom())-parentEntry.getPosition();
parentBoard.setNodeChildrenMarginBottom(parentEntry,marginBottom);
parentBoard._updateTable(result.index, result.table, modifiedTopics, false);
}
this._updateParentBoard(parent, modifiedTopics);
}
},
_updateChildrenBoards:function(node, delta, modifiedTopics){
var board = this.getTopicBoardForTopic(node);
var topics = board._getTableForNode(null);
for(var i=0; i<topics.length; i++){
board._updateEntryPos(topics[i],delta, modifiedTopics, false);
}
2011-03-24 19:00:51 +01:00
}
});
mindplot.layoutManagers.FreeMindLayoutManager.NAME ="FreeMindLayoutManager";