Refactor and create INode and IMindmap

This commit is contained in:
Paulo Veiga 2011-09-08 10:02:55 -03:00
parent 71b761081f
commit 21f848e3df
2 changed files with 54 additions and 428 deletions

View File

@ -16,92 +16,75 @@
* limitations under the License.
*/
mindplot.collaboration.framework.brix.model.Mindmap = new Class({
Extends:mindplot.model.Mindmap,
initialize:function(brixModel, brixFramework){
this.parent();
Extends:mindplot.model.IMindmap,
initialize:function(brixModel, brixFramework) {
this._brixModel = brixModel;
this._brixFramework = brixFramework;
if(!$defined(this._brixModel)){
if (!$defined(this._brixModel)) {
this._brixModel = this._createBrixModel();
}else{
} else {
var branches = this._brixModel.get("branches");
for(var i=0; i<branches.size(); i++){
for (var i = 0; i < branches.size(); i++) {
var node = branches.get(i);
var nodeModel = new mindplot.collaboration.framework.brix.model.NodeModel(node, this._brixFramework, null, this);
this.addBranch(nodeModel, false);
}
}
},
_createBrixModel:function(){
_createBrixModel:function() {
var model = this._brixFramework.getBrixModel().create("Map");
var branches = this._brixFramework.getBrixModel().create("List");
model.put("branches",branches);
model.put("branches", branches);
this._brixFramework.addMindmap(model);
return model;
},
getBrixModel:function(){
getBrixModel:function() {
return this._brixModel;
},
setId : function(id) {
this._iconType = id;
},
setVersion : function(version) {
this._version = version;
},
addBranch : function(nodeModel, addToModel) {
this.parent(nodeModel);
if($defined(addToModel) && addToModel){
var branches = this._brixModel.get("branches");
branches.add(nodeModel.getBrixModel());
getBranches : function() {
var result = [];
var branches = this._brixModel.get("branches");
for (var i = 0; i < branches.size(); i++) {
result.push();
}
},
addBranch : function(nodeModel) {
$assert(nodeModel, "nodeModel can not be null");
var branches = this._brixModel.get("branches");
branches.add(nodeModel.getBrixModel());
},
removeBranch : function(nodeModel) {
$assert(nodeModel, "nodeModel can not be null");
var branches = this._brixModel.get("branches");
branches.remove(nodeModel.getBrixModel());
},
connect : function(parent, child) {
this.parent(parent, child);
// Remove from the branch ...
var branches = this._brixModel.get("branches");
var childIndex = null;
for(var i = 0; i<branches.size(); i++){
if(branches.get(i)==child.getBrixModel()){
for (var i = 0; i < branches.size(); i++) {
if (branches.get(i) == child.getBrixModel()) {
childIndex = i;
break;
}
}
if(childIndex!=null){
if (childIndex != null) {
branches.remove(childIndex);
}
},
disconnect : function(child) {
var parent = child.getParent();
$assert(child, 'Child can not be null.');
$assert(parent, 'Child model seems to be already connected');
parent._removeChild(child);
var branches = this.getBranches();
branches.push(child);
},
_createNode : function(type, id) {
$assert(type, 'Node type must be specified.');
var result = new mindplot.collaboration.framework.brix.model.NodeModel(null, this._brixFramework, type, this, id);
return result;
},
createRelationship : function(fromNode, toNode) {
$assert(fromNode, 'from node cannot be null');
$assert(toNode, 'to node cannot be null');
return new mindplot.model.RelationshipModel(fromNode, toNode);
},
addRelationship : function(relationship) {
this._relationships.push(relationship);
},
removeRelationship : function(relationship) {
this._relationships.erase(relationship);
createNode : function(type, id) {
return mindplot.collaboration.framework.brix.model.NodeModel.create(this._brixFramework, type, id, this);
}
}
);

View File

@ -17,70 +17,18 @@
*/
mindplot.collaboration.framework.brix.model.NodeModel = new Class({
Extends: mindplot.model.NodeModel,
Attributes: ['text','fontSize','fontFamily','fontStyle','fontColor','fontWeight','borderColor','backgroundColor','shapeType'],
initialize : function(brixModel, brixFramework, type, mindmap, id) {
// Inject property getters and setters ...
this._injectSetAndGet();
Extends: mindplot.model.INodeModel,
initialize : function(brixModel, brixFramework, mindmap) {
$assert(brixModel, "brixModel can not null");
$assert(brixFramework, "brixFramework can not null");
this.parent(mindmap);
this._brixModel = brixModel;
this._brixFramework = brixFramework;
if ($defined(this._brixModel)) {
type = this._brixModel.get("type");
id = this._brixModel.get("id");
}
this.parent(type, mindmap, id);
if (!$defined(this._brixModel)) {
this._brixModel = this._createBrixModel();
} else {
// Call all the required setters for simple properties ...
var keys = mindplot.collaboration.framework.brix.model.NodeModel.prototype.Attributes;
keys.forEach(function(key) {
// Call setters ...
var funName = 'set' + key.capitalize();
var value = this._brixModel.get(key);
if (value != null) {
this[funName](value, false);
}
}.bind(this));
var position = this._brixModel.get("position");
this.setPosition(position.get("x"), position.get("y"), false);
var children = this._brixModel.get("children");
for (var i = 0; i < children.size(); i++) {
var bChild = children.get(i);
var child = new mindplot.collaboration.framework.brix.model.NodeModel(bChild, this._brixFramework, null, mindmap);
this._appendChild(child, false);
}
}
this._injectSetAndGet();
this._addBrixListeners();
},
_injectSetAndGet : function() {
var keys = mindplot.collaboration.framework.brix.model.NodeModel.prototype.Attributes;
keys.forEach(function(key) {
// Create setters ...
var setterName = 'set' + key.capitalize();
this[setterName] = function(value, updateModel) {
console.log('Calling setter for:' + setterName);
if (!$defined(updateModel) || updateModel) {
this._brixModel.put(key, value);
}
};
// Create getters ...
var getterName = 'get' + key.capitalize();
this[getterName] = function() {
return this._brixModel.get(key);
};
}.bind(this));
},
_addBrixListeners:function() {
// Register listener for properties changes ....
this._brixModel.addListener("valueChanged", function(event) {
@ -110,325 +58,20 @@ mindplot.collaboration.framework.brix.model.NodeModel = new Class({
var y = position.get("y");
model.setPosition(x, y);
this._brixFramework.getActionDispatcher().addTopic(model, this.getId(), true);
}
,
},
_createBrixModel:function() {
var model = this._brixFramework.getBrixModel().create("Map");
model.put("type", this._type);
model.put("id", this._id);
model.put("text", this._type == mindplot.model.NodeModel.CENTRAL_TOPIC_TYPE ? "Central Topic" : "Main Topic");
var position = this._brixFramework.getBrixModel().create("Map");
position.put("x", 0);
position.put("y", 0);
model.put("position", position);
var children = this._brixFramework.getBrixModel().create("List");
model.put("children", children);
return model;
}
,
getBrixModel:function() {
return this._brixModel;
}
,
clone : function() {
var result = new mindplot.model.NodeModel(this._type, this._mindmap);
result._order = this._order;
result._type = this._type;
result._children = this._children.map(function(item, index) {
var model = item.clone();
model._parent = result;
return model;
});
result._icons = this._icons;
result._links = this._links;
result._notes = this._notes;
result._size = this._size;
result._position = this._position;
result._id = this._id;
result._mindmap = this._mindmap;
result._text = this._text;
result._shapeType = this._shapeType;
result._fontFamily = this._fontFamily;
result._fontSize = this._fontSize;
result._fontStyle = this._fontStyle;
result._fontWeight = this._fontWeight;
result._fontColor = this._fontColor;
result._borderColor = this._borderColor;
result._backgroundColor = this._backgroundColor;
result._areChildrenShrinked = this._areChildrenShrinked;
return result;
},
areChildrenShrinked : function() {
return this._areChildrenShrinked;
},
setChildrenShrinked : function(value) {
this._areChildrenShrinked = value;
},
getId : function() {
return this._id;
},
setId : function(id) {
this._id = id;
if (mindplot.model.NodeModel._uuid < id) {
mindplot.model.NodeModel._uuid = id;
}
},
getType : function() {
return this._type;
},
isNodeModel : function() {
return true;
},
isConnected : function() {
return this._parent != null;
},
createLink : function(url) {
$assert(url, 'Link URL must be specified.');
return new mindplot.model.LinkModel(url, this);
},
addLink : function(link) {
$assert(link && link.isLinkModel(), 'Only LinkModel can be appended to Mindmap object as links');
this._links.push(link);
},
_removeLink : function(link) {
$assert(link && link.isLinkModel(), 'Only LinkModel can be appended to Mindmap object as links');
this._links.erase(link);
},
createNote : function(text) {
$assert(text != null, 'note text must be specified.');
return new mindplot.model.NoteModel(text, this);
},
addNote : function(note) {
$assert(note && note.isNoteModel(), 'Only NoteModel can be appended to Mindmap object as links');
this._notes.push(note);
},
removeNote : function(note) {
$assert(note && note.isNoteModel(), 'Only NoteModel can be appended to Mindmap object as links');
this._notes.erase(note);
},
createIcon : function(iconType) {
$assert(iconType, 'IconType must be specified.');
return new mindplot.model.IconModel(iconType, this);
},
addIcon : function(icon) {
$assert(icon && icon.isIconModel(), 'Only IconModel can be appended to Mindmap object as icons');
this._icons.push(icon);
}
,
removeIcon : function(icon) {
$assert(icon && icon.isIconModel(), 'Only IconModel can be appended to Mindmap object as icons');
this._icons.erase(icon);
}
,
removeLastIcon : function() {
this._icons.pop();
}
,
_appendChild : function(child, updateModel) {
this.parent(child);
if (!$defined(updateModel) || ($defined(updateModel) && updateModel)) {
this.getBrixModel().get("children").add(child.getBrixModel());
}
}
,
_removeChild : function(child) {
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object.');
this._children.erase(child);
child._parent = null;
}
,
setPosition : function(x, y, updateModel) {
this.parent(x, y);
if (!$defined(updateModel) || ($defined(updateModel) && updateModel)) {
var position = this.getBrixModel().get("position");
position.put("x", this._position.x);
position.put("y", this._position.y);
}
}
,
setFinalPosition : function(x, y) {
$assert(x, "x coordinate must be defined");
$assert(y, "y coordinate must be defined");
if (!$defined(this._finalPosition)) {
this._finalPosition = new core.Point();
}
this._finalPosition.x = parseInt(x);
this._finalPosition.y = parseInt(y);
}
,
getFinalPosition : function() {
return this._finalPosition;
},
setSize : function(width, height) {
this._size.width = width;
this._size.height = height;
},
getSize : function() {
return {width:this._size.width,height:this._size.height};
},
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;
},
getMindmap : function() {
return this._mindmap;
},
setParent : function(parent) {
$assert(parent != this, 'The same node can not be parent and child if itself.');
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.');
$assert(targetTopicHeight, 'childrenWidth can not be null.');
// 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;
if (sourceModel.getType() == mindplot.model.NodeModel.MAIN_TOPIC_TYPE) {
// 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;
if (isTargetAtRightFromCentral) {
if (xDistance >= -targetTopicSize.width / 2 && xDistance <= mindplot.model.NodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
result = true;
}
} else {
if (xDistance <= targetTopicSize.width / 2 && Math.abs(xDistance) <= mindplot.model.NodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE / 2 + (targetTopicSize.width / 2)) {
result = true;
}
}
}
}
} else {
throw "No implemented yet";
}
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;
}
}
}
return result;
}
,
disconnect : function() {
var mindmap = this.getMindmap();
mindmap.disconnect(this);
}
,
getOrder : function() {
return this._order;
},
setOrder : function(value) {
this._order = value;
},
deleteNode : function() {
var mindmap = this._mindmap;
// if it has children nodes, Their must be disconnected.
var lenght = this._children;
for (var i = 0; i < lenght; i++) {
var child = this._children[i];
mindmap.disconnect(child);
}
var parent = this._parent;
if ($defined(parent)) {
// if it is connected, I must remove it from the parent..
mindmap.disconnect(this);
}
// It's an isolated node. It must be a hole branch ...
var branches = mindmap.getBranches();
branches.erase(this);
}
,
inspect : function() {
return '(type:' + this.getType() + ' , id: ' + this.getId() + ')';
}
});
mindplot.collaboration.framework.brix.model.NodeModel.create = function(brixFramework, type, id, mindmap) {
var brixModel = brixFramework.getBrixModel().create("Map");
brixModel.put("type", type);
brixModel.put("id", this._id); // @todo...
var children = brixFramework.getBrixModel().create("List");
brixModel.put("children", children);
return new mindplot.collaboration.framework.brix.model.NodeModel(brixFramework, brixModel, mindmap);
};