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

178 lines
4.3 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.
*/
2021-12-04 01:11:17 +01:00
import { $assert } from '@wisemapping/core-js';
2022-02-04 08:07:34 +01:00
import { ElementClass } from '@wisemapping/web2d';
import TopicConfig from './TopicConfig';
2022-02-04 08:07:34 +01:00
import NodeModel from './model/NodeModel';
import Workspace from './Workspace';
import DragTopic from './DragTopic';
2022-02-04 08:07:34 +01:00
import LayoutManager from './layout/LayoutManager';
import SizeType from './SizeType';
import PositionType from './PositionType';
2021-09-07 22:31:46 +02:00
2022-02-04 08:07:34 +01:00
abstract class NodeGraph {
private _mouseEvents: boolean;
private _options;
private _onFocus: boolean;
private _size: SizeType;
private _model: NodeModel;
private _elem2d: ElementClass;
constructor(nodeModel: NodeModel, options) {
2021-12-05 00:39:20 +01:00
$assert(nodeModel, 'model can not be null');
this._options = options;
this._mouseEvents = true;
this.setModel(nodeModel);
this._onFocus = false;
this._size = { width: 50, height: 20 };
}
2022-02-04 08:07:34 +01:00
isReadOnly(): boolean {
2021-12-05 00:39:20 +01:00
return this._options.readOnly;
}
2022-02-04 08:07:34 +01:00
getType(): string {
2021-12-05 00:39:20 +01:00
const model = this.getModel();
return model.getType();
}
2022-02-04 08:07:34 +01:00
setId(id: number) {
$assert(typeof id === 'number', `id is not a number:${id}`);
2021-12-05 00:39:20 +01:00
this.getModel().setId(id);
}
2021-09-07 22:31:46 +02:00
2022-02-04 08:07:34 +01:00
protected _set2DElement(elem2d: ElementClass) {
2021-12-05 00:39:20 +01:00
this._elem2d = elem2d;
}
2021-09-07 22:31:46 +02:00
2022-02-04 08:07:34 +01:00
get2DElement(): ElementClass {
2021-12-05 00:39:20 +01:00
$assert(this._elem2d, 'NodeGraph has not been initialized properly');
return this._elem2d;
}
2022-02-04 08:07:34 +01:00
abstract setPosition(point, fireEvent): void;
2021-12-05 00:39:20 +01:00
/** */
2022-02-04 08:07:34 +01:00
addEvent(type: string, listener) {
2021-12-05 00:39:20 +01:00
const elem = this.get2DElement();
elem.addEvent(type, listener);
}
/** */
removeEvent(type, listener) {
const elem = this.get2DElement();
elem.removeEvent(type, listener);
}
/** */
fireEvent(type, event) {
const elem = this.get2DElement();
elem.trigger(type, event);
}
/** */
setMouseEventsEnabled(isEnabled) {
this._mouseEvents = isEnabled;
}
/** */
isMouseEventsEnabled() {
return this._mouseEvents;
}
2022-02-04 08:07:34 +01:00
getSize(): SizeType {
2021-12-05 00:39:20 +01:00
return this._size;
}
setSize(size) {
this._size.width = parseInt(size.width, 10);
this._size.height = parseInt(size.height, 10);
}
2022-02-04 08:07:34 +01:00
getModel(): NodeModel {
2021-12-05 00:39:20 +01:00
$assert(this._model, 'Model has not been initialized yet');
return this._model;
}
2021-09-07 22:31:46 +02:00
2022-02-04 08:07:34 +01:00
setModel(model: NodeModel) {
2021-12-05 00:39:20 +01:00
$assert(model, 'Model can not be null');
this._model = model;
}
2022-02-04 08:07:34 +01:00
getId(): number {
2021-12-05 00:39:20 +01:00
return this._model.getId();
}
2022-02-04 08:07:34 +01:00
setOnFocus(focus: boolean) {
2021-12-05 00:39:20 +01:00
if (this._onFocus !== focus) {
this._onFocus = focus;
const outerShape = this.getOuterShape();
if (focus) {
outerShape.setFill(TopicConfig.OUTER_SHAPE_ATTRIBUTES_FOCUS.fillColor);
outerShape.setOpacity(1);
} else {
outerShape.setFill(TopicConfig.OUTER_SHAPE_ATTRIBUTES.fillColor);
outerShape.setOpacity(0);
2021-10-05 02:05:34 +02:00
}
2021-12-05 00:39:20 +01:00
this.setCursor('move');
// In any case, always try to hide the editor ...
this.closeEditors();
// Fire event ...
this.fireEvent(focus ? 'ontfocus' : 'ontblur', this);
}
}
2022-02-04 08:07:34 +01:00
abstract closeEditors(): void;
abstract setCursor(type: string): void;
abstract getOuterShape(): ElementClass;
isOnFocus(): boolean {
2021-12-05 00:39:20 +01:00
return this._onFocus;
}
2022-02-04 08:07:34 +01:00
dispose(workspace: Workspace) {
2021-12-05 00:39:20 +01:00
this.setOnFocus(false);
workspace.removeChild(this);
}
/** */
2022-02-04 08:07:34 +01:00
createDragNode(layoutManager: LayoutManager) {
2021-12-05 00:39:20 +01:00
const dragShape = this._buildDragShape();
return new DragTopic(dragShape, this, layoutManager);
}
2022-02-04 08:07:34 +01:00
abstract _buildDragShape();
2021-12-05 00:39:20 +01:00
2022-02-04 08:07:34 +01:00
getPosition(): PositionType {
2021-12-05 00:39:20 +01:00
const model = this.getModel();
return model.getPosition();
}
}
2021-07-16 16:41:58 +02:00
export default NodeGraph;