wisemapping-open-source/mindplot/src/main/javascript/layout/LayoutManager.js

196 lines
6.2 KiB
JavaScript
Raw Normal View History

2012-01-10 03:50:52 +01:00
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2012-01-14 18:20:59 +01:00
mindplot.layout.LayoutManager = new Class({
Extends: Events,
initialize: function(rootNodeId, rootSize) {
$assert($defined(rootNodeId), "rootNodeId can not be null");
$assert(rootSize, "rootSize can not be null");
2012-01-15 19:41:03 +01:00
var position = position || {x:0, y:0};
2012-01-14 18:20:59 +01:00
this._treeSet = new mindplot.layout.RootedTreeSet();
this._layout = new mindplot.layout.OriginalLayout(this._treeSet);
2012-01-15 19:41:03 +01:00
var rootNode = this._layout.createNode(rootNodeId, rootSize, position, 'root');
this._treeSet.setRoot(rootNode);
this._events = [];
},
updateNodeSize: function(id, size) {
$assert($defined(id), "id can not be null");
var node = this._treeSet.find(id);
node.setSize(size);
},
updateShrinkState: function(id, value) {
$assert($defined(id), "id can not be null");
2012-01-17 04:26:29 +01:00
$assert($defined(value), "value can not be null");
var node = this._treeSet.find(id);
node.setShrunken(value);
2012-01-17 04:26:29 +01:00
return this;
},
2012-01-05 21:34:14 +01:00
find: function(id) {
2012-01-02 21:10:14 +01:00
return this._treeSet.find(id);
},
2012-01-18 05:26:39 +01:00
moveNode: function(id, position) {
$assert($defined(id), "id cannot be null");
$assert($defined(position), "position cannot be null");
$assert($defined(position.x), "x can not be null");
$assert($defined(position.y), "y can not be null");
2012-01-17 04:26:29 +01:00
var node = this._treeSet.find(id);
node.setFree(true);
2012-01-17 04:51:13 +01:00
node.setFreeDisplacement({x:position.x - node.getPosition().x, y:position.y - node.getPosition().y});
2012-01-05 21:34:14 +01:00
},
connectNode: function(parentId, childId, order) {
$assert($defined(parentId), "parentId cannot be null");
$assert($defined(childId), "childId cannot be null");
$assert($defined(order), "order cannot be null");
this._layout.connectNode(parentId, childId, order);
return this;
},
2011-12-12 03:47:01 +01:00
disconnectNode: function(id) {
$assert($defined(id), "id can not be null");
this._layout.disconnectNode(id);
return this;
},
addNode:function(id, size, position) {
$assert($defined(id), "id can not be null");
var result = this._layout.createNode(id, size, position, 'topic');
this._treeSet.add(result);
return this;
},
2011-12-12 03:47:01 +01:00
removeNode: function(id) {
$assert($defined(id), "id can not be null");
var node = this._treeSet.find(id);
// Is It connected ?
if (this._treeSet.getParent(node)) {
this.disconnectNode(id);
}
// Remove the all the branch ...
this._treeSet.remove(id);
return this;
2011-12-12 03:47:01 +01:00
},
2012-01-18 20:07:40 +01:00
predict: function(parentId, position, free) {
$assert($defined(parentId), "parentId can not be null");
var parent = this._treeSet.find(parentId);
var sorter = parent.getSorter();
2012-01-18 20:07:40 +01:00
if (free) {
$assert($defined(position), "position cannot be null for predict in free positioning");
2012-01-26 17:07:45 +01:00
//TODO(gb): check this. Should direction be obtained by the sorter?
2012-01-18 20:07:40 +01:00
var rootNode = this._treeSet.getRootNode(parent);
var direction = parent.getPosition().x > rootNode.getPosition().x ? 1 : -1;
var xPos = direction > 0 ?
(position.x >= parent.getPosition().x ? position.x : parent.getPosition().x) :
(position.x <= parent.getPosition().x ? position.x : parent.getPosition().x);
return {order:0, position:{x: xPos, y:position.y}};
}
2012-01-14 13:58:06 +01:00
var result = sorter.predict(parent, this._treeSet, position);
return {order:result[0],position:result[1]};
},
dump: function() {
console.log(this._treeSet.dump());
},
plot: function(containerId, size) {
$assert(containerId, "containerId cannot be null");
2012-01-05 21:34:14 +01:00
size = size || {width:200,height:200};
var squaresize = 10;
2012-01-05 21:34:14 +01:00
var canvas = Raphael(containerId, size.width, size.height);
canvas.drawGrid(0, 0, size.width, size.height, size.width / squaresize, size.height / squaresize);
2011-12-30 19:28:03 +01:00
this._treeSet.plot(canvas);
2012-01-12 23:58:18 +01:00
return canvas;
2011-12-30 19:28:03 +01:00
},
layout: function(fireEvents) {
// File repositioning ...
this._layout.layout();
// Collect changes ...
this._collectChanges();
2011-12-12 03:47:01 +01:00
if (!$(fireEvents) || fireEvents) {
this._flushEvents();
}
return this;
},
2011-12-12 03:47:01 +01:00
_flushEvents: function() {
this._events.forEach(function(event) {
this.fireEvent('change', event);
}, this);
this._events = [];
},
_collectChanges: function(nodes) {
if (!nodes)
nodes = this._treeSet.getTreeRoots();
nodes.forEach(function(node) {
if (node.hasOrderChanged() || node.hasPositionChanged()) {
// Find or create a event ...
var id = node.getId();
var event = this._events.some(function(event) {
return event.id == id;
});
if (!event) {
2012-01-14 18:20:59 +01:00
event = new mindplot.layout.ChangeEvent(id);
}
// Update nodes ...
2012-01-12 04:49:18 +01:00
event.setOrder(node.getOrder());
event.setPosition(node.getPosition());
2012-01-12 04:49:18 +01:00
node.resetPositionState();
node.resetOrderState();
2012-01-17 21:45:51 +01:00
node.resetFreeState();
this._events.push(event);
}
this._collectChanges(this._treeSet.getChildren(node));
}, this);
}
});