wisemapping-frontend/packages/mindplot/src/components/CommandContext.ts

126 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-12-04 01:11:17 +01:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-12-04 01:11:17 +01: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-19 09:44:08 +01:00
import { $assert, $defined } from '@wisemapping/core-js';
2022-01-25 07:09:30 +01:00
import Point from '@wisemapping/web2d';
import { Designer } from '..';
2021-12-19 09:44:08 +01:00
import EventBus from './layout/EventBus';
2022-01-25 07:09:30 +01:00
import NodeModel from './model/NodeModel';
import RelationshipModel from './model/RelationshipModel';
import Relationship from './Relationship';
import Topic from './Topic';
2021-12-04 01:43:50 +01:00
2021-12-04 01:11:17 +01:00
class CommandContext {
2022-01-25 07:09:30 +01:00
private _designer: Designer;
constructor(designer: Designer) {
2021-12-04 02:03:49 +01:00
$assert(designer, 'designer can not be null');
this._designer = designer;
}
2022-01-25 07:09:30 +01:00
public get designer(): Designer {
return this._designer;
}
public set designer(value: Designer) {
this._designer = value;
}
findTopics(topicIds: number[]): Topic[] {
$assert($defined(topicIds), 'topicsIds can not be null');
const topicsIds = Array.isArray(topicIds) ? topicIds : [topicIds];
2021-12-04 02:03:49 +01:00
const designerTopics = this._designer.getModel().getTopics();
2021-12-14 18:06:09 +01:00
const result = designerTopics.filter((topic) => topicsIds.includes(topic.getId()));
2021-12-04 02:03:49 +01:00
if (result.length !== topicsIds.length) {
const ids = designerTopics.map((topic) => topic.getId());
$assert(
result.length === topicsIds.length,
`Could not find topic. Result:${result
} Filter Criteria:${topicsIds
} Current Topics: [${ids
}]`,
);
2021-12-04 01:11:17 +01:00
}
2021-12-04 02:03:49 +01:00
return result;
}
/** */
2022-01-25 07:09:30 +01:00
deleteTopic(topic: Topic) {
2021-12-04 02:03:49 +01:00
this._designer.removeTopic(topic);
}
/** */
2022-01-25 07:09:30 +01:00
createTopic(model: NodeModel) {
2021-12-04 02:03:49 +01:00
$assert(model, 'model can not be null');
2022-01-10 20:50:54 +01:00
return this._designer.nodeModelToTopic(model);
2021-12-04 02:03:49 +01:00
}
2022-01-25 07:09:30 +01:00
// /** */
// createModel() {
// const mindmap = this._designer.getMindmap();
// return mindmap.createNode('MainTopic');
// }
2021-12-04 02:03:49 +01:00
/** */
2022-01-25 07:09:30 +01:00
addTopic(topic: Topic) {
2021-12-04 02:03:49 +01:00
const mindmap = this._designer.getMindmap();
return mindmap.addBranch(topic.getModel());
}
/** */
2022-01-25 07:09:30 +01:00
connect(childTopic: Topic, parentTopic: Topic) {
childTopic.connectTo(parentTopic, this._designer.getWorkSpace());
2021-12-04 02:03:49 +01:00
}
/** */
2022-01-25 07:09:30 +01:00
disconnect(topic: Topic) {
topic.disconnect(this._designer.getWorkSpace());
2021-12-04 02:03:49 +01:00
}
/** */
2022-01-25 07:09:30 +01:00
addRelationship(model: RelationshipModel) {
2021-12-04 02:03:49 +01:00
$assert(model, 'model cannot be null');
return this._designer.addRelationship(model);
}
/** */
2022-01-25 07:09:30 +01:00
deleteRelationship(relationship: Relationship) {
2021-12-04 02:03:49 +01:00
this._designer.deleteRelationship(relationship);
}
/** */
2022-01-25 07:09:30 +01:00
findRelationships(relationshipIds: number[]) {
$assert($defined(relationshipIds), 'relId can not be null');
const relIds = Array.isArray(relationshipIds) ? relationshipIds : [relationshipIds];
2021-12-04 01:11:17 +01:00
2021-12-04 02:03:49 +01:00
const designerRel = this._designer.getModel().getRelationships();
2021-12-14 18:06:09 +01:00
return designerRel.filter((rel) => relIds.includes(rel.getId()));
2021-12-04 02:03:49 +01:00
}
/** */
2022-01-25 07:09:30 +01:00
moveTopic(topic: Topic, position: Point) {
2021-12-04 02:03:49 +01:00
$assert(topic, 'topic cannot be null');
$assert(position, 'position cannot be null');
2022-03-01 01:12:45 +01:00
EventBus.instance.fireEvent('topicMoved', {
2021-12-04 02:03:49 +01:00
node: topic.getModel(),
position,
});
}
2021-12-04 01:11:17 +01:00
}
export default CommandContext;