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

228 lines
7.7 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.NodeModel = new Class({
2011-09-08 14:54:55 +02:00
Extends: mindplot.model.INodeModel,
2011-07-27 13:25:10 +02:00
initialize:function(type, mindmap, id) {
$assert(type, 'Node type can not be null');
$assert(mindmap, 'mindmap can not be null');
2011-09-08 14:54:55 +02:00
this._properties = {};
this.parent(mindmap);
this.setId(id);
this.setType(type);
this.areChildrenShrinked(false);
this.setSize(50, 20);
2011-07-27 13:25:10 +02:00
this._children = [];
this._icons = [];
this._links = [];
this._notes = [];
},
2011-09-08 14:54:55 +02:00
putProperty : function(key, value) {
$defined(key, 'key can not be null');
this._properties[key] = value;
2011-07-27 13:25:10 +02:00
},
2011-09-09 07:22:44 +02:00
2011-09-10 06:17:48 +02:00
getProperties: function() {
return this._properties;
2011-09-09 07:22:44 +02:00
},
2011-09-08 14:54:55 +02:00
getProperty : function(key) {
$defined(key, 'key can not be null');
var result = this._properties[key];
return !$defined(result) ? null : result;
2011-07-27 13:25:10 +02:00
},
2011-09-08 14:54:55 +02:00
clone : function() {
var result = new mindplot.model.NodeModel(this.getType(), this._mindmap);
result._children = this._children.each(function(node) {
var cnode = node.clone();
cnode._parent = result;
return cnode;
});
2011-07-27 13:25:10 +02:00
2011-09-11 05:07:43 +02:00
result._properties = Object.clone(this._properties);
2011-09-08 14:54:55 +02:00
result._icons = this._icons.clone();
result._links = this._links.clone();
result._notes = this._notes.clone();
return result;
2011-07-27 13:25:10 +02:00
},
2011-09-10 06:17:48 +02:00
addChildren : function() {
2011-09-10 02:53:41 +02:00
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object');
2011-09-10 06:17:48 +02:00
this._children.push(child);
child._parent = this;
2011-09-10 02:53:41 +02:00
},
2011-07-27 13:25:10 +02:00
createLink : function(url) {
$assert(url, 'Link URL must be specified.');
return new mindplot.model.LinkModel(url, this);
2011-07-27 13:25:10 +02:00
},
addLink : function(link) {
$assert(link && link.isLinkModel(), 'Only LinkModel can be appended to Mindmap object as links');
2011-07-27 13:25:10 +02:00
this._links.push(link);
},
_removeLink : function(link) {
$assert(link && link.isLinkModel(), 'Only LinkModel can be appended to Mindmap object as links');
2011-07-27 13:25:10 +02:00
this._links.erase(link);
},
createNote : function(text) {
$assert(text != null, 'note text must be specified.');
return new mindplot.model.NoteModel(text, this);
2011-07-27 13:25:10 +02:00
},
addNote : function(note) {
$assert(note && note.isNoteModel(), 'Only NoteModel can be appended to Mindmap object as links');
2011-07-27 13:25:10 +02:00
this._notes.push(note);
},
removeNote : function(note) {
$assert(note && note.isNoteModel(), 'Only NoteModel can be appended to Mindmap object as links');
2011-07-27 13:25:10 +02:00
this._notes.erase(note);
},
createIcon : function(iconType) {
$assert(iconType, 'IconType must be specified.');
return new mindplot.model.IconModel(iconType, this);
2011-07-27 13:25:10 +02:00
},
addIcon : function(icon) {
$assert(icon && icon.isIconModel(), 'Only IconModel can be appended to Mindmap object as icons');
2011-07-27 13:25:10 +02:00
this._icons.push(icon);
},
removeIcon : function(icon) {
$assert(icon && icon.isIconModel(), 'Only IconModel can be appended to Mindmap object as icons');
2011-07-27 13:25:10 +02:00
this._icons.erase(icon);
},
removeLastIcon : function() {
this._icons.pop();
},
2011-09-10 02:53:41 +02:00
appendChild : function(child) {
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object');
2011-07-27 13:25:10 +02:00
this._children.push(child);
child._parent = this;
},
2011-09-10 02:53:41 +02:00
removeChild : function(child) {
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object.');
2011-07-27 13:25:10 +02:00
this._children.erase(child);
child._parent = null;
},
getChildren : function() {
return this._children;
},
getIcons : function() {
return this._icons;
},
getLinks : function() {
return this._links;
},
getNotes : function() {
return this._notes;
},
getParent : function() {
return this._parent;
},
setParent : function(parent) {
$assert(parent != this, 'The same node can not be parent and child if itself.');
2011-07-27 13:25:10 +02:00
this._parent = parent;
},
canBeConnected : function(sourceModel, sourcePosition, targetTopicHeight) {
$assert(sourceModel != this, 'The same node can not be parent and child if itself.');
$assert(sourcePosition, 'childPosition can not be null.');
2011-07-27 19:53:32 +02:00
$assert(targetTopicHeight, 'childrenWidth can not be null.');
2011-07-27 13:25:10 +02:00
// Only can be connected if the node is in the left or rigth.
var targetModel = this;
var mindmap = targetModel.getMindmap();
var targetPosition = targetModel.getPosition();
var result = false;
2011-09-08 14:54:55 +02:00
if (sourceModel.getType() == mindplot.model.INodeModel.MAIN_TOPIC_TYPE) {
2011-07-27 13:25:10 +02:00
// Finally, check current node ubication.
var targetTopicSize = targetModel.getSize();
var yDistance = Math.abs(sourcePosition.y - targetPosition.y);
var gap = 35 + targetTopicHeight / 2;
if (targetModel.getChildren().length > 0) {
gap += Math.abs(targetPosition.y - targetModel.getChildren()[0].getPosition().y);
}
if (yDistance <= gap) {
// Circular connection ?
if (!sourceModel._isChildNode(this)) {
var toleranceDistance = (targetTopicSize.width / 2) + targetTopicHeight;
var xDistance = sourcePosition.x - targetPosition.x;
var isTargetAtRightFromCentral = targetPosition.x >= 0;
2009-06-07 20:59:43 +02:00
2011-07-27 13:25:10 +02:00
if (isTargetAtRightFromCentral) {
2011-09-08 14:54:55 +02:00
if (xDistance >= -targetTopicSize.width / 2 && xDistance <= mindplot.model.INodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
2011-07-27 13:25:10 +02:00
result = true;
}
} else {
2011-09-08 14:54:55 +02:00
if (xDistance <= targetTopicSize.width / 2 && Math.abs(xDistance) <= mindplot.model.INodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
2011-07-27 13:25:10 +02:00
result = true;
}
2009-06-07 20:59:43 +02:00
}
}
}
2011-07-27 13:25:10 +02:00
} else {
throw "No implemented yet";
2009-06-07 20:59:43 +02:00
}
2011-07-27 13:25:10 +02:00
return result;
},
_isChildNode : function(node) {
var result = false;
if (node == this) {
result = true;
} else {
var children = this.getChildren();
for (var i = 0; i < children.length; i++) {
var child = children[i];
result = child._isChildNode(node);
if (result) {
break;
}
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-09-08 14:54:55 +02:00
inspect : function() {
2011-07-27 13:25:10 +02:00
return '(type:' + this.getType() + ' , id: ' + this.getId() + ')';
}
2011-09-08 14:54:55 +02:00
});