Migrate mindplot to typescript

This commit is contained in:
Paulo Gustavo Veiga 2021-12-29 10:08:45 -08:00
parent 29f3e41fa3
commit 4359c8741d
3 changed files with 55 additions and 93 deletions

View File

@ -18,68 +18,35 @@
* limitations under the License. * limitations under the License.
*/ */
import { $assert } from '@wisemapping/core-js'; import { $assert } from '@wisemapping/core-js';
import NodeModel from './NodeModel';
import RelationshipModel from './RelationshipModel';
class IMindmap { abstract class IMindmap {
getCentralTopic() { getCentralTopic(): NodeModel {
return this.getBranches()[0]; return this.getBranches()[0];
} }
/** @abstract */ abstract getDescription(): string;
getDescription() {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract setDescription(value: string);
setDescription(value) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract getId(): string
getId() {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract setId(id: string);
setId(id) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract getVersion(): string;
getVersion() {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract setVersion(version: string): void;
setVersion(version) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract addBranch(nodeModel: NodeModel): void;
addBranch(nodeModel) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract getBranches(): Array<NodeModel>;
getBranches() {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract removeBranch(node: NodeModel): void;
removeBranch(node) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract getRelationships(): Array<RelationshipModel>;
getRelationships() {
throw new Error('Unsupported operation');
}
/** connect(parent: NodeModel, child: NodeModel): void {
* @param parent
* @param child
* @throws will throw an error if child already has a connection to a parent node
*/
connect(parent, child) {
// Child already has a parent ? // Child already has a parent ?
$assert(!child.getParent(), 'Child model seems to be already connected'); $assert(!child.getParent(), 'Child model seems to be already connected');
@ -95,7 +62,7 @@ class IMindmap {
* @throws will throw an error if child is null or undefined * @throws will throw an error if child is null or undefined
* @throws will throw an error if child's parent cannot be found * @throws will throw an error if child's parent cannot be found
*/ */
disconnect(child) { disconnect(child: NodeModel): void {
const parent = child.getParent(); const parent = child.getParent();
$assert(child, 'Child can not be null.'); $assert(child, 'Child can not be null.');
$assert(parent, 'Child model seems to be already connected'); $assert(parent, 'Child model seems to be already connected');
@ -114,18 +81,11 @@ class IMindmap {
throw new Error('Unsupported operation'); throw new Error('Unsupported operation');
} }
/** @abstract */ abstract createRelationship(fromNode: NodeModel, toNode: NodeModel): void;
createRelationship(fromNode, toNode) {
throw new Error('Unsupported operation');
}
/** @abstract */ abstract addRelationship(rel: RelationshipModel): void;
addRelationship(rel) {
throw new Error('Unsupported operation');
}
/** @abstract */ deleteRelationship(relationship: RelationshipModel): void {
deleteRelationship(relationship) {
throw new Error('Unsupported operation'); throw new Error('Unsupported operation');
} }

View File

@ -23,43 +23,51 @@ import RelationshipModel from './RelationshipModel';
import ModelCodeName from '../persistence/ModelCodeName'; import ModelCodeName from '../persistence/ModelCodeName';
class Mindmap extends IMindmap { class Mindmap extends IMindmap {
constructor(id, version) { _description: string;
_version: string;
_id: string;
_branches: Array<NodeModel>;
_relationships: Array<RelationshipModel>;
constructor(id: string, version: string = ModelCodeName.TANGO) {
super(); super();
$assert(id, 'Id can not be null'); $assert(id, 'Id can not be null');
$assert($defined(version), 'Version can not be null');
this._branches = []; this._branches = [];
this._description = null; this._description = null;
this._relationships = []; this._relationships = [];
this._version = $defined(version) ? version : ModelCodeName.TANGO; this._version = version;
this._id = id; this._id = id;
} }
/** */ /** */
getDescription() { getDescription(): string {
return this._description; return this._description;
} }
/** */ /** */
setDescription(value) { setDescription(value: string) {
this._description = value; this._description = value;
} }
/** */ /** */
getId() { getId(): string {
return this._id; return this._id;
} }
/** */ /** */
setId(id) { setId(id: string) {
this._id = id; this._id = id;
} }
/** */ /** */
getVersion() { getVersion(): string {
return this._version; return this._version;
} }
/** */ /** */
setVersion(version) { setVersion(version: string): void {
this._version = version; this._version = version;
} }
@ -68,7 +76,7 @@ class Mindmap extends IMindmap {
* @throws will throw an error if nodeModel is null, undefined or not a node model object * @throws will throw an error if nodeModel is null, undefined or not a node model object
* @throws will throw an error if * @throws will throw an error if
*/ */
addBranch(nodeModel) { addBranch(nodeModel: NodeModel): void {
$assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects'); $assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects');
const branches = this.getBranches(); const branches = this.getBranches();
if (branches.length === 0) { if (branches.length === 0) {
@ -84,18 +92,16 @@ class Mindmap extends IMindmap {
/** /**
* @param nodeModel * @param nodeModel
*/ */
removeBranch(nodeModel) { removeBranch(nodeModel: NodeModel): void {
$assert(nodeModel && nodeModel.isNodeModel(), 'Remove node must be invoked with model objects'); $assert(nodeModel && nodeModel.isNodeModel(), 'Remove node must be invoked with model objects');
this._branches = this._branches.filter((b) => b !== nodeModel); this._branches = this._branches.filter((b) => b !== nodeModel);
} }
/** */
getBranches() { getBranches() {
return this._branches; return this._branches;
} }
/** */ getRelationships(): Array<RelationshipModel> {
getRelationships() {
return this._relationships; return this._relationships;
} }
@ -103,7 +109,7 @@ class Mindmap extends IMindmap {
* @param node * @param node
* @return {Boolean} true if node already exists * @return {Boolean} true if node already exists
*/ */
hasAlreadyAdded(node) { hasAlreadyAdded(node: any) {
let result = false; let result = false;
// Check in not connected nodes. // Check in not connected nodes.
@ -121,7 +127,7 @@ class Mindmap extends IMindmap {
* @param id * @param id
* @return the node model created * @return the node model created
*/ */
createNode(type = INodeModel.MAIN_TOPIC_TYPE, id) { createNode(type = INodeModel.MAIN_TOPIC_TYPE, id: number) {
return new NodeModel(type, this, id); return new NodeModel(type, this, id);
} }
@ -132,7 +138,7 @@ class Mindmap extends IMindmap {
* @throws will throw an error if target node is null or undefined * @throws will throw an error if target node is null or undefined
* @return the relationship model created * @return the relationship model created
*/ */
createRelationship(sourceNodeId, targetNodeId) { createRelationship(sourceNodeId: any, targetNodeId: any) {
$assert($defined(sourceNodeId), 'from node cannot be null'); $assert($defined(sourceNodeId), 'from node cannot be null');
$assert($defined(targetNodeId), 'to node cannot be null'); $assert($defined(targetNodeId), 'to node cannot be null');
@ -142,18 +148,18 @@ class Mindmap extends IMindmap {
/** /**
* @param relationship * @param relationship
*/ */
addRelationship(relationship) { addRelationship(relationship: RelationshipModel) {
this._relationships.push(relationship); this._relationships.push(relationship);
} }
/** /**
* @param relationship * @param relationship
*/ */
deleteRelationship(relationship) { deleteRelationship(relationship: RelationshipModel) {
this._relationships = this._branches.filter((r) => r !== relationship); this._relationships = this._relationships.filter((r) => r !== relationship);
} }
findNodeById(id) { findNodeById(id: any) {
let result = null; let result = null;
for (let i = 0; i < this._branches.length; i++) { for (let i = 0; i < this._branches.length; i++) {
const branch = this._branches[i]; const branch = this._branches[i];
@ -164,17 +170,13 @@ class Mindmap extends IMindmap {
} }
return result; return result;
} }
}
/** static buildEmpty = (mapId: string) => {
* @param mapId
* @return an empty mindmap with central topic only
*/
Mindmap.buildEmpty = (mapId) => {
const result = new Mindmap(mapId); const result = new Mindmap(mapId);
const node = result.createNode(INodeModel.CENTRAL_TOPIC_TYPE, 0); const node = result.createNode(INodeModel.CENTRAL_TOPIC_TYPE, 0);
result.addBranch(node); result.addBranch(node);
return result; return result;
}; };
}
export default Mindmap; export default Mindmap;

View File

@ -2,7 +2,7 @@ import TxtExporter from '../../src/components/export/TxtExporter';
import Mindmap from '../../src/components/model/Mindmap'; import Mindmap from '../../src/components/model/Mindmap';
test('adds 1 + 2 to equal 3', () => { test('adds 1 + 2 to equal 3', () => {
const m = new Mindmap(1); const m = new Mindmap("some map");
const exporter = new TxtExporter(); const exporter = new TxtExporter();
console.log(exporter.export(m)); console.log(exporter.export(m));