wisemapping-open-source/mindplot/src/main/javascript/model/Mindmap.js

177 lines
5.4 KiB
JavaScript
Raw Normal View History

/*
2011-07-27 13:25:10 +02: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.
*/
mindplot.model.Mindmap = new Class({
2011-07-27 13:25:10 +02:00
initialize : function() {
this._branches = [];
this._description = null;
this._version = null;
this._relationships = [];
},
getCentralTopic : function() {
return this._branches[0];
},
getDescription : function() {
return this._description;
},
getId : function() {
return this._iconType;
},
setId : function(id) {
this._iconType = id;
},
getVersion : function() {
return this._version;
},
setVersion : function(version) {
this._version = version;
},
addBranch : function(nodeModel) {
$assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects');
2011-07-27 13:25:10 +02:00
if (this._branches.length == 0) {
$assert(nodeModel.getType() == mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE, "First element must be the central topic");
2011-07-27 13:25:10 +02:00
nodeModel.setPosition(0, 0);
} else {
$assert(nodeModel.getType() != mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE, "Mindmaps only have one cental topic");
2011-07-27 13:25:10 +02:00
}
this._branches.push(nodeModel);
},
getBranches : function() {
return this._branches;
},
getRelationships : function() {
return this._relationships;
},
connect : function(parent, child) {
// Child already has a parent ?
var branches = this.getBranches();
$assert(!child.getParent(), 'Child model seems to be already connected');
2011-07-27 13:25:10 +02:00
// Connect node...
parent._appendChild(child);
// Remove from the branch ...
branches.erase(child);
},
disconnect : function(child) {
var parent = child.getParent();
$assert(child, 'Child can not be null.');
$assert(parent, 'Child model seems to be already connected');
2011-07-27 13:25:10 +02:00
parent._removeChild(child);
var branches = this.getBranches();
branches.push(child);
},
hasAlreadyAdded : function(node) {
var result = false;
// Check in not connected nodes.
var branches = this._branches;
for (var i = 0; i < branches.length; i++) {
result = branches[i]._isChildNode(node);
if (result) {
break;
}
}
},
createNode : function(type, id) {
$assert(type, "node type can not be null");
2011-07-27 13:25:10 +02:00
return this._createNode(type, id);
},
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
_createNode : function(type, id) {
$assert(type, 'Node type must be specified.');
var result = new mindplot.model.NodeModel(type, this, id);
2011-07-27 13:25:10 +02:00
return result;
},
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
createRelationship : function(fromNode, toNode) {
$assert(fromNode, 'from node cannot be null');
$assert(toNode, 'to node cannot be null');
2009-06-07 20:59:43 +02:00
return new mindplot.model.RelationshipModel(fromNode, toNode);
2011-07-27 13:25:10 +02:00
},
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
addRelationship : function(relationship) {
this._relationships.push(relationship);
},
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
removeRelationship : function(relationship) {
this._relationships.erase(relationship);
},
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
inspect : function() {
var result = '';
result = '{ ';
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
var branches = this.getBranches();
for (var i = 0; i < branches.length; i++) {
var node = branches[i];
if (i != 0) {
result = result + ', ';
}
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
result = result + this._toString(node);
}
result = result + ' } ';
return result;
},
_toString : function(node) {
var result = node.inspect();
var children = node.getChildren();
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
for (var i = 0; i < children.length; i++) {
var child = children[i];
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
if (i == 0) {
result = result + '-> {';
} else {
result = result + ', ';
}
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
result = result + this._toString(child);
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
if (i == children.length - 1) {
result = result + '}';
}
}
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
return result;
2009-06-07 20:59:43 +02:00
}
}
2011-07-27 13:25:10 +02:00
);