From 4359c8741d4bb5c28ffcb6b1fb60a9c84817a925 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Wed, 29 Dec 2021 10:08:45 -0800 Subject: [PATCH] Migrate mindplot to typescript --- .../model/{IMindmap.js => IMindmap.ts} | 78 +++++-------------- .../model/{Mindmap.js => Mindmap.ts} | 68 ++++++++-------- .../test/unit/TxTExportTestSuite.test.ts | 2 +- 3 files changed, 55 insertions(+), 93 deletions(-) rename packages/mindplot/src/components/model/{IMindmap.js => IMindmap.ts} (66%) rename packages/mindplot/src/components/model/{Mindmap.js => Mindmap.ts} (75%) diff --git a/packages/mindplot/src/components/model/IMindmap.js b/packages/mindplot/src/components/model/IMindmap.ts similarity index 66% rename from packages/mindplot/src/components/model/IMindmap.js rename to packages/mindplot/src/components/model/IMindmap.ts index f71f7865..3533554d 100644 --- a/packages/mindplot/src/components/model/IMindmap.js +++ b/packages/mindplot/src/components/model/IMindmap.ts @@ -18,68 +18,35 @@ * limitations under the License. */ import { $assert } from '@wisemapping/core-js'; +import NodeModel from './NodeModel'; +import RelationshipModel from './RelationshipModel'; -class IMindmap { - getCentralTopic() { +abstract class IMindmap { + getCentralTopic(): NodeModel { return this.getBranches()[0]; } - /** @abstract */ - getDescription() { - throw new Error('Unsupported operation'); - } + abstract getDescription(): string; - /** @abstract */ - setDescription(value) { - throw new Error('Unsupported operation'); - } + abstract setDescription(value: string); - /** @abstract */ - getId() { - throw new Error('Unsupported operation'); - } + abstract getId(): string - /** @abstract */ - setId(id) { - throw new Error('Unsupported operation'); - } + abstract setId(id: string); - /** @abstract */ - getVersion() { - throw new Error('Unsupported operation'); - } + abstract getVersion(): string; - /** @abstract */ - setVersion(version) { - throw new Error('Unsupported operation'); - } + abstract setVersion(version: string): void; - /** @abstract */ - addBranch(nodeModel) { - throw new Error('Unsupported operation'); - } + abstract addBranch(nodeModel: NodeModel): void; - /** @abstract */ - getBranches() { - throw new Error('Unsupported operation'); - } + abstract getBranches(): Array; - /** @abstract */ - removeBranch(node) { - throw new Error('Unsupported operation'); - } + abstract removeBranch(node: NodeModel): void; - /** @abstract */ - getRelationships() { - throw new Error('Unsupported operation'); - } + abstract getRelationships(): Array; - /** - * @param parent - * @param child - * @throws will throw an error if child already has a connection to a parent node - */ - connect(parent, child) { + connect(parent: NodeModel, child: NodeModel): void { // Child already has a parent ? $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's parent cannot be found */ - disconnect(child) { + disconnect(child: NodeModel): void { const parent = child.getParent(); $assert(child, 'Child can not be null.'); $assert(parent, 'Child model seems to be already connected'); @@ -114,18 +81,11 @@ class IMindmap { throw new Error('Unsupported operation'); } - /** @abstract */ - createRelationship(fromNode, toNode) { - throw new Error('Unsupported operation'); - } + abstract createRelationship(fromNode: NodeModel, toNode: NodeModel): void; - /** @abstract */ - addRelationship(rel) { - throw new Error('Unsupported operation'); - } + abstract addRelationship(rel: RelationshipModel): void; - /** @abstract */ - deleteRelationship(relationship) { + deleteRelationship(relationship: RelationshipModel): void { throw new Error('Unsupported operation'); } diff --git a/packages/mindplot/src/components/model/Mindmap.js b/packages/mindplot/src/components/model/Mindmap.ts similarity index 75% rename from packages/mindplot/src/components/model/Mindmap.js rename to packages/mindplot/src/components/model/Mindmap.ts index 053d43ad..c509486e 100644 --- a/packages/mindplot/src/components/model/Mindmap.js +++ b/packages/mindplot/src/components/model/Mindmap.ts @@ -23,43 +23,51 @@ import RelationshipModel from './RelationshipModel'; import ModelCodeName from '../persistence/ModelCodeName'; class Mindmap extends IMindmap { - constructor(id, version) { + _description: string; + _version: string; + _id: string; + _branches: Array; + _relationships: Array; + + constructor(id: string, version: string = ModelCodeName.TANGO) { super(); $assert(id, 'Id can not be null'); + $assert($defined(version), 'Version can not be null'); + this._branches = []; this._description = null; this._relationships = []; - this._version = $defined(version) ? version : ModelCodeName.TANGO; + this._version = version; this._id = id; } /** */ - getDescription() { + getDescription(): string { return this._description; } /** */ - setDescription(value) { + setDescription(value: string) { this._description = value; } /** */ - getId() { + getId(): string { return this._id; } /** */ - setId(id) { + setId(id: string) { this._id = id; } /** */ - getVersion() { + getVersion(): string { return this._version; } /** */ - setVersion(version) { + setVersion(version: string): void { 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 */ - addBranch(nodeModel) { + addBranch(nodeModel: NodeModel): void { $assert(nodeModel && nodeModel.isNodeModel(), 'Add node must be invoked with model objects'); const branches = this.getBranches(); if (branches.length === 0) { @@ -84,18 +92,16 @@ class Mindmap extends IMindmap { /** * @param nodeModel */ - removeBranch(nodeModel) { + removeBranch(nodeModel: NodeModel): void { $assert(nodeModel && nodeModel.isNodeModel(), 'Remove node must be invoked with model objects'); this._branches = this._branches.filter((b) => b !== nodeModel); } - /** */ getBranches() { return this._branches; } - /** */ - getRelationships() { + getRelationships(): Array { return this._relationships; } @@ -103,7 +109,7 @@ class Mindmap extends IMindmap { * @param node * @return {Boolean} true if node already exists */ - hasAlreadyAdded(node) { + hasAlreadyAdded(node: any) { let result = false; // Check in not connected nodes. @@ -121,7 +127,7 @@ class Mindmap extends IMindmap { * @param id * @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); } @@ -132,7 +138,7 @@ class Mindmap extends IMindmap { * @throws will throw an error if target node is null or undefined * @return the relationship model created */ - createRelationship(sourceNodeId, targetNodeId) { + createRelationship(sourceNodeId: any, targetNodeId: any) { $assert($defined(sourceNodeId), 'from node cannot be null'); $assert($defined(targetNodeId), 'to node cannot be null'); @@ -142,18 +148,18 @@ class Mindmap extends IMindmap { /** * @param relationship */ - addRelationship(relationship) { + addRelationship(relationship: RelationshipModel) { this._relationships.push(relationship); } /** - * @param relationship - */ - deleteRelationship(relationship) { - this._relationships = this._branches.filter((r) => r !== relationship); + * @param relationship + */ + deleteRelationship(relationship: RelationshipModel) { + this._relationships = this._relationships.filter((r) => r !== relationship); } - findNodeById(id) { + findNodeById(id: any) { let result = null; for (let i = 0; i < this._branches.length; i++) { const branch = this._branches[i]; @@ -164,17 +170,13 @@ class Mindmap extends IMindmap { } return result; } + + static buildEmpty = (mapId: string) => { + const result = new Mindmap(mapId); + const node = result.createNode(INodeModel.CENTRAL_TOPIC_TYPE, 0); + result.addBranch(node); + return result; + }; } -/** - * @param mapId - * @return an empty mindmap with central topic only - */ -Mindmap.buildEmpty = (mapId) => { - const result = new Mindmap(mapId); - const node = result.createNode(INodeModel.CENTRAL_TOPIC_TYPE, 0); - result.addBranch(node); - return result; -}; - export default Mindmap; diff --git a/packages/mindplot/test/unit/TxTExportTestSuite.test.ts b/packages/mindplot/test/unit/TxTExportTestSuite.test.ts index 1a17bfe5..4f48ef01 100644 --- a/packages/mindplot/test/unit/TxTExportTestSuite.test.ts +++ b/packages/mindplot/test/unit/TxTExportTestSuite.test.ts @@ -2,7 +2,7 @@ import TxtExporter from '../../src/components/export/TxtExporter'; import Mindmap from '../../src/components/model/Mindmap'; test('adds 1 + 2 to equal 3', () => { - const m = new Mindmap(1); + const m = new Mindmap("some map"); const exporter = new TxtExporter(); console.log(exporter.export(m));