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

149 lines
3.9 KiB
TypeScript
Raw Normal View History

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.
*/
2022-01-26 07:33:39 +01:00
import { $assert } from '@wisemapping/core-js';
import CentralTopic from './CentralTopic';
import { DesignerOptions } from './DesignerOptionsBuilder';
import Events from './Events';
2022-01-09 00:22:59 +01:00
import Relationship from './Relationship';
import Topic from './Topic';
2021-12-05 18:25:16 +01:00
import { $notify } from './widget/ToolbarNotifier';
2021-07-16 16:41:58 +02:00
2021-12-04 01:11:17 +01:00
class DesignerModel extends Events {
2022-01-26 07:33:39 +01:00
private _zoom: number;
2022-01-26 07:33:39 +01:00
private _topics: Topic[];
2022-01-26 07:33:39 +01:00
private _relationships: Relationship[];
2022-01-09 00:22:59 +01:00
constructor(options: DesignerOptions) {
2021-12-04 01:11:17 +01:00
super();
2021-10-05 02:05:34 +02:00
this._zoom = options.zoom;
this._topics = [];
this._relationships = [];
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-26 07:33:39 +01:00
getZoom(): number {
2021-10-05 02:05:34 +02:00
return this._zoom;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-26 07:33:39 +01:00
setZoom(zoom: number): void {
2021-10-05 02:05:34 +02:00
this._zoom = zoom;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 02:08:26 +01:00
getTopics(): Topic[] {
2021-10-05 02:05:34 +02:00
return this._topics;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 02:08:26 +01:00
getRelationships(): Relationship[] {
2021-10-05 02:05:34 +02:00
return this._relationships;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
getCentralTopic(): CentralTopic {
2021-10-05 02:05:34 +02:00
const topics = this.getTopics();
2022-01-26 07:33:39 +01:00
return topics[0] as unknown as CentralTopic;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 00:22:59 +01:00
filterSelectedTopics(): Topic[] {
2022-01-26 07:33:39 +01:00
const result: Topic[] = [];
2021-10-05 02:05:34 +02:00
for (let i = 0; i < this._topics.length; i++) {
if (this._topics[i].isOnFocus()) {
result.push(this._topics[i]);
}
}
return result;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 02:08:26 +01:00
filterSelectedRelationships(): Relationship[] {
2022-01-26 07:33:39 +01:00
const result:Relationship[] = [];
2021-10-05 02:05:34 +02:00
for (let i = 0; i < this._relationships.length; i++) {
if (this._relationships[i].isOnFocus()) {
result.push(this._relationships[i]);
}
}
return result;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-09 02:08:26 +01:00
getEntities(): (Relationship | Topic)[] {
2022-01-26 07:33:39 +01:00
let result:(Relationship|Topic)[] = [];
result = result.concat(this._topics);
2021-12-14 18:06:09 +01:00
result = result.concat(this._relationships);
2021-10-05 02:05:34 +02:00
return result;
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-26 07:33:39 +01:00
removeTopic(topic: Topic): void {
2021-10-05 02:05:34 +02:00
$assert(topic, 'topic can not be null');
2021-12-14 18:06:09 +01:00
this._topics = this._topics.filter((t) => t !== topic);
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-26 07:33:39 +01:00
removeRelationship(rel: Relationship): void {
2021-10-05 02:05:34 +02:00
$assert(rel, 'rel can not be null');
2021-12-14 18:06:09 +01:00
this._relationships = this._relationships.filter((r) => r !== rel);
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-09 02:08:26 +01:00
addTopic(topic: Topic): void {
2021-10-05 02:05:34 +02:00
$assert(topic, 'topic can not be null');
$assert(typeof topic.getId() === 'number', `id is not a number:${topic.getId()}`);
this._topics.push(topic);
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-09 02:08:26 +01:00
addRelationship(rel: Relationship): void {
2021-10-05 02:05:34 +02:00
$assert(rel, 'rel can not be null');
this._relationships.push(rel);
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-26 07:33:39 +01:00
filterTopicsIds(validate?: (topic: Topic) => boolean, errorMsg?: string): number[] {
const result: number[] = [];
2021-10-05 02:05:34 +02:00
const topics = this.filterSelectedTopics();
let isValid = true;
topics.forEach((topic) => {
2022-01-26 07:33:39 +01:00
if (validate) {
2022-01-09 00:22:59 +01:00
isValid = validate(topic);
2021-10-05 02:05:34 +02:00
}
// Add node only if it's valid.
if (isValid) {
2022-01-09 00:22:59 +01:00
result.push(topic.getId());
2021-10-05 02:05:34 +02:00
} else {
$notify(errorMsg);
}
2022-01-09 00:22:59 +01:00
});
2021-10-05 02:05:34 +02:00
return result;
2021-12-04 01:11:17 +01:00
}
2021-10-05 02:05:34 +02:00
2022-01-26 07:33:39 +01:00
selectedTopic(): Topic | undefined {
2021-10-05 02:05:34 +02:00
const topics = this.filterSelectedTopics();
2022-01-26 07:33:39 +01:00
return (topics.length > 0) ? topics[0] : undefined;
2021-12-04 01:11:17 +01:00
}
2021-07-16 16:41:58 +02:00
2022-01-26 07:33:39 +01:00
findTopicById(id: number): Topic | undefined {
let result: Topic | undefined;
2021-10-05 02:05:34 +02:00
for (let i = 0; i < this._topics.length; i++) {
const topic = this._topics[i];
2021-12-04 01:11:17 +01:00
if (topic.getId() === id) {
2021-10-05 02:05:34 +02:00
result = topic;
break;
}
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
return result;
2021-12-04 01:11:17 +01:00
}
}
2021-07-16 16:41:58 +02:00
export default DesignerModel;