2021-07-16 16:41:58 +02:00
|
|
|
/*
|
2021-12-25 23:39:34 +01:00
|
|
|
* Copyright [2021] [wisemapping]
|
2021-07-16 16:41:58 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-12-04 01:11:17 +01:00
|
|
|
import { $assert, $defined } from '@wisemapping/core-js';
|
2021-12-02 01:41:56 +01:00
|
|
|
import IMindmap from './IMindmap';
|
2022-01-02 23:01:50 +01:00
|
|
|
import INodeModel, { NodeModelType } from './INodeModel';
|
2021-12-02 01:41:56 +01:00
|
|
|
import NodeModel from './NodeModel';
|
|
|
|
import RelationshipModel from './RelationshipModel';
|
|
|
|
import ModelCodeName from '../persistence/ModelCodeName';
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-12-03 19:58:25 +01:00
|
|
|
class Mindmap extends IMindmap {
|
2022-01-03 06:04:37 +01:00
|
|
|
private _description: string;
|
2022-01-13 17:13:05 +01:00
|
|
|
|
|
|
|
private _version: string;
|
|
|
|
|
2022-01-03 06:04:37 +01:00
|
|
|
private _id: string;
|
2022-01-13 17:13:05 +01:00
|
|
|
|
|
|
|
private _branches: Array<NodeModel>;
|
|
|
|
|
|
|
|
private _relationships: Array<RelationshipModel>;
|
2021-12-29 19:35:58 +01:00
|
|
|
|
|
|
|
constructor(id: string, version: string = ModelCodeName.TANGO) {
|
2021-12-03 19:58:25 +01:00
|
|
|
super();
|
2021-10-05 02:05:34 +02:00
|
|
|
$assert(id, 'Id can not be null');
|
2021-12-29 19:35:58 +01:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
this._branches = [];
|
2022-01-26 07:33:39 +01:00
|
|
|
this._description = '';
|
2021-10-05 02:05:34 +02:00
|
|
|
this._relationships = [];
|
2021-12-29 19:35:58 +01:00
|
|
|
this._version = version;
|
2021-10-05 02:05:34 +02:00
|
|
|
this._id = id;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
getDescription(): string {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._description;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
setDescription(value: string) {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._description = value;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
getId(): string {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._id;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
setId(id: string) {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._id = id;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
getVersion(): string {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._version;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/** */
|
2021-12-29 19:35:58 +01:00
|
|
|
setVersion(version: string): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._version = version;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2022-01-26 07:33:39 +01:00
|
|
|
addBranch(nodeModel: INodeModel): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
$assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects');
|
|
|
|
const branches = this.getBranches();
|
2021-12-03 19:58:25 +01:00
|
|
|
if (branches.length === 0) {
|
2022-01-02 23:01:50 +01:00
|
|
|
$assert(nodeModel.getType() === 'CentralTopic', 'First element must be the central topic');
|
2021-10-05 02:05:34 +02:00
|
|
|
nodeModel.setPosition(0, 0);
|
|
|
|
} else {
|
2022-01-02 23:01:50 +01:00
|
|
|
$assert(nodeModel.getType() !== 'CentralTopic', 'Mindmaps only have one cental topic');
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 07:33:39 +01:00
|
|
|
this._branches.push(nodeModel as NodeModel);
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
/**
|
2021-07-16 16:41:58 +02:00
|
|
|
* @param nodeModel
|
|
|
|
*/
|
2022-01-31 01:05:22 +01:00
|
|
|
removeBranch(nodeModel: INodeModel): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
$assert(nodeModel && nodeModel.isNodeModel(), 'Remove node must be invoked with model objects');
|
2021-12-14 18:06:09 +01:00
|
|
|
this._branches = this._branches.filter((b) => b !== nodeModel);
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-26 07:33:39 +01:00
|
|
|
getBranches():NodeModel[] {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._branches;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-29 19:35:58 +01:00
|
|
|
getRelationships(): Array<RelationshipModel> {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._relationships;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-02 23:01:50 +01:00
|
|
|
hasAlreadyAdded(node: NodeModel): boolean {
|
2021-10-05 02:05:34 +02:00
|
|
|
let result = false;
|
|
|
|
|
|
|
|
// Check in not connected nodes.
|
|
|
|
const branches = this._branches;
|
|
|
|
for (let i = 0; i < branches.length; i++) {
|
2022-01-02 23:01:50 +01:00
|
|
|
result = branches[i].isChildNode(node);
|
2021-10-05 02:05:34 +02:00
|
|
|
if (result) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 23:01:50 +01:00
|
|
|
return result;
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-26 07:33:39 +01:00
|
|
|
createNode(type: NodeModelType = 'MainTopic', id?: number):NodeModel {
|
2021-10-05 02:05:34 +02:00
|
|
|
return new NodeModel(type, this, id);
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-01-26 07:33:39 +01:00
|
|
|
createRelationship(sourceNodeId: number, targetNodeId: number): RelationshipModel {
|
2021-10-05 02:05:34 +02:00
|
|
|
$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-12-03 19:58:25 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
/**
|
2021-12-16 02:59:51 +01:00
|
|
|
* @param relationship
|
|
|
|
*/
|
2021-12-29 19:35:58 +01:00
|
|
|
addRelationship(relationship: RelationshipModel) {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._relationships.push(relationship);
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
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-12-29 19:35:58 +01:00
|
|
|
deleteRelationship(relationship: RelationshipModel) {
|
2021-12-29 19:10:35 +01:00
|
|
|
this._relationships = this._relationships.filter((r) => r !== relationship);
|
2021-12-03 19:58:25 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-01-24 20:24:16 +01:00
|
|
|
findNodeById(id: number) {
|
2022-01-26 07:33:39 +01:00
|
|
|
let result;
|
2021-10-05 02:05:34 +02:00
|
|
|
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-12-03 19:58:25 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-12-29 19:35:58 +01:00
|
|
|
static buildEmpty = (mapId: string) => {
|
|
|
|
const result = new Mindmap(mapId);
|
2022-01-02 23:01:50 +01:00
|
|
|
const node = result.createNode('CentralTopic', 0);
|
2021-12-29 19:35:58 +01:00
|
|
|
result.addBranch(node);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
|
|
|
export default Mindmap;
|