wisemapping-frontend/packages/mindplot/lib/components/model/Mindmap.js

191 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
* Copyright [2015] [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.
*/
const IMindmap = require('./IMindmap').default;
const INodeModel = require('./INodeModel').default;
const NodeModel = require('./NodeModel').default;
const RelationshipModel = require('./RelationshipModel').default;
2021-10-05 02:05:34 +02:00
const ModelCodeName = require('../persistence/ModelCodeName').default;
2021-07-16 16:41:58 +02:00
const Mindmap = new Class(/** @lends Mindmap */{
2021-10-05 02:05:34 +02:00
Extends: IMindmap,
/**
2021-07-16 16:41:58 +02:00
* @constructs
* @param id
* @param version
* @extends mindplot.model.IMindmap
*/
2021-10-05 02:05:34 +02:00
initialize(id, version) {
$assert(id, 'Id can not be null');
this._branches = [];
this._description = null;
this._relationships = [];
this._version = $defined(version) ? version : ModelCodeName.TANGO;
this._id = id;
},
/** */
getDescription() {
return this._description;
},
/** */
setDescription(value) {
this._description = value;
},
/** */
getId() {
return this._id;
},
/** */
setId(id) {
this._id = id;
},
/** */
getVersion() {
return this._version;
},
/** */
setVersion(version) {
this._version = version;
},
/**
2021-07-16 16:41:58 +02:00
* @param {mindplot.model.NodeModel} nodeModel
* @throws will throw an error if nodeModel is null, undefined or not a node model object
2021-10-05 02:05:34 +02:00
* @throws will throw an error if
2021-07-16 16:41:58 +02:00
*/
2021-10-05 02:05:34 +02:00
addBranch(nodeModel) {
$assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects');
const branches = this.getBranches();
if (branches.length == 0) {
$assert(nodeModel.getType() == INodeModel.CENTRAL_TOPIC_TYPE, 'First element must be the central topic');
nodeModel.setPosition(0, 0);
} else {
$assert(nodeModel.getType() != INodeModel.CENTRAL_TOPIC_TYPE, 'Mindmaps only have one cental topic');
}
this._branches.push(nodeModel);
},
/**
2021-07-16 16:41:58 +02:00
* @param nodeModel
*/
2021-10-05 02:05:34 +02:00
removeBranch(nodeModel) {
$assert(nodeModel && nodeModel.isNodeModel(), 'Remove node must be invoked with model objects');
return this._branches.erase(nodeModel);
},
/** */
getBranches() {
return this._branches;
},
/** */
getRelationships() {
return this._relationships;
},
/**
2021-07-16 16:41:58 +02:00
* @param node
* @return {Boolean} true if node already exists
*/
2021-10-05 02:05:34 +02:00
hasAlreadyAdded(node) {
let result = false;
// Check in not connected nodes.
const branches = this._branches;
for (let i = 0; i < branches.length; i++) {
result = branches[i]._isChildNode(node);
if (result) {
break;
}
}
},
/**
2021-07-16 16:41:58 +02:00
* @param type
* @param id
* @return the node model created
*/
2021-10-05 02:05:34 +02:00
createNode(type, id) {
type = !$defined(type) ? INodeModel.MAIN_TOPIC_TYPE : type;
return new NodeModel(type, this, id);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param sourceNodeId
* @param targetNodeId
* @throws will throw an error if source node is null or undefined
* @throws will throw an error if target node is null or undefined
* @return the relationship model created
*/
2021-10-05 02:05:34 +02:00
createRelationship(sourceNodeId, targetNodeId) {
$assert($defined(sourceNodeId), 'from node cannot be null');
$assert($defined(targetNodeId), 'to node cannot be null');
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
return new RelationshipModel(sourceNodeId, targetNodeId);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param relationship
*/
2021-10-05 02:05:34 +02:00
addRelationship(relationship) {
this._relationships.push(relationship);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param relationship
*/
2021-10-05 02:05:34 +02:00
deleteRelationship(relationship) {
this._relationships.erase(relationship);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param id
* @return the node with the respective id or null if not in the mindmap
*/
2021-10-05 02:05:34 +02:00
findNodeById(id) {
let result = null;
for (let i = 0; i < this._branches.length; i++) {
const branch = this._branches[i];
result = branch.findNodeById(id);
if (result) {
break;
}
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
return result;
},
});
2021-07-16 16:41:58 +02:00
/**
* @param mapId
* @return an empty mindmap with central topic only
*/
Mindmap.buildEmpty = function (mapId) {
2021-10-05 02:05:34 +02:00
const result = new Mindmap(mapId);
const node = result.createNode(INodeModel.CENTRAL_TOPIC_TYPE, 0);
result.addBranch(node);
return result;
2021-07-16 16:41:58 +02:00
};
export default Mindmap;