wisemapping-frontend/packages/mindplot/storybook/src/stories/Connection.ts

155 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-02-17 22:25:57 +01:00
import $ from 'jquery';
import { Mindmap, Topic } from '../../../src';
import NodeModel from '../../../src/components/model/NodeModel';
import CentralTopic from '../../../src/components/CentralTopic';
import Canvas from '../../../src/components/Canvas';
import ScreenManager from '../../../src/components/ScreenManager';
import TopicEventDispatcher from '../../../src/components/TopicEventDispatcher';
import ThemeType from '../../../src/components/model/ThemeType';
import MainTopic from '../../../src/components/MainTopic';
import EventBusDispatcher from '../../../src/components/layout/EventBusDispatcher';
import LayoutManager from '../../../src/components/layout/LayoutManager';
import ChangeEvent from '../../../src/components/layout/ChangeEvent';
2023-03-01 01:54:30 +01:00
import LayoutEventBus from '../../../src/components/layout/LayoutEventBus';
import { $assert } from '@wisemapping/core-js';
2023-02-17 22:25:57 +01:00
const registerRefreshHook = (topics: Topic[]) => {
// Trigger a redraw after the node is added ...
if (globalThis.observer) {
globalThis.observer.disconnect();
}
globalThis.observer = new MutationObserver(() => {
// Relayout...
topics.forEach((t) => t.redraw());
2023-03-01 01:54:30 +01:00
LayoutEventBus.fireEvent('forceLayout');
2023-02-17 22:25:57 +01:00
});
globalThis.observer.observe(document.getElementById('root')!, { childList: true });
};
export type TopicArgs = {
readOnly?: boolean;
theme?: ThemeType;
};
const createConnection = ({ theme = undefined, readOnly = true }: TopicArgs) => {
// Build basic container ...
const divElem = document.createElement('div');
const jqueryDiv = $(divElem);
jqueryDiv.css({
height: '600px',
width: '800px',
backgroundColor: 'gray',
});
// Initialize designer helpers ...
const screenManager = new ScreenManager(divElem);
const canvas = new Canvas(screenManager, 0.7, readOnly);
TopicEventDispatcher.configure(readOnly);
// Register event propagation ..
const mindmap = new Mindmap();
const central = new NodeModel('CentralTopic', mindmap);
central.setText('Central Topic');
mindmap.addBranch(central);
// Add Children ...
const child1 = new NodeModel('MainTopic', mindmap);
child1.setOrder(0);
2023-03-01 01:54:30 +01:00
child1.setText('Child1: This is child one !\nwith other line');
2023-02-17 22:25:57 +01:00
child1.setPosition(100, 100);
const child2 = new NodeModel('MainTopic', mindmap);
child2.setOrder(1);
child2.setPosition(100, -100);
2023-03-01 01:54:30 +01:00
child2.setText('Child2');
2023-02-17 22:25:57 +01:00
const child3 = new NodeModel('MainTopic', mindmap);
2023-03-01 01:54:30 +01:00
child3.setOrder(2);
2023-02-17 22:25:57 +01:00
child3.setPosition(-100, 100);
2023-03-01 01:54:30 +01:00
child3.setText('Child3');
2023-02-17 22:25:57 +01:00
const child4 = new NodeModel('MainTopic', mindmap);
2023-03-01 01:54:30 +01:00
child4.setOrder(3);
2023-02-17 22:25:57 +01:00
child4.setPosition(-100, -100);
2023-03-01 01:54:30 +01:00
child3.setText('Child4');
2023-02-17 22:25:57 +01:00
const subchild1 = new NodeModel('MainTopic', mindmap);
subchild1.setOrder(0);
subchild1.setPosition(300, 80);
const subchild2 = new NodeModel('MainTopic', mindmap);
subchild2.setOrder(1);
subchild2.setPosition(300, 120);
// Theme ...
if (theme) {
mindmap.setTheme(theme);
}
// Create and add to canvas..
const centralTopic = new CentralTopic(central, { readOnly });
const child1Topic = new MainTopic(child1, { readOnly });
const child2Topic = new MainTopic(child2, { readOnly });
const child3Topic = new MainTopic(child3, { readOnly });
const child4Topic = new MainTopic(child4, { readOnly });
const subchild1Topic = new MainTopic(subchild1, { readOnly });
const subchild2Topic = new MainTopic(subchild2, { readOnly });
2023-03-01 01:54:30 +01:00
2023-02-17 22:25:57 +01:00
const topics = [
child1Topic,
child2Topic,
child3Topic,
child4Topic,
centralTopic,
subchild1Topic,
subchild2Topic,
];
// Configure event dispatcher ...
const dispatcher = new EventBusDispatcher();
const size = { width: 25, height: 25 };
const layoutManager = new LayoutManager(mindmap.getCentralTopic().getId(), size);
dispatcher.setLayoutManager(layoutManager);
2023-03-01 01:54:30 +01:00
2023-02-17 22:25:57 +01:00
layoutManager.addEvent('change', (event: ChangeEvent) => {
const id = event.getId();
2023-03-01 01:54:30 +01:00
const ids = topics.filter((t) => t.getModel().getId() === id);
$assert(ids.length === 1, `Unexpeted number of elements ${String(ids)} - ${id}`);
const topic = ids[0];
2023-02-17 22:25:57 +01:00
topic.setPosition(event.getPosition());
topic.setOrder(event.getOrder());
});
// Connect nodes ...
2023-03-01 01:54:30 +01:00
canvas.append(centralTopic);
2023-02-17 22:25:57 +01:00
child1Topic.connectTo(centralTopic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(child1Topic);
2023-02-17 22:25:57 +01:00
child2Topic.connectTo(centralTopic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(child2Topic);
2023-02-17 22:25:57 +01:00
child3Topic.connectTo(centralTopic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(child3Topic);
2023-02-17 22:25:57 +01:00
child4Topic.connectTo(centralTopic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(child4Topic);
2023-02-17 22:25:57 +01:00
subchild1Topic.connectTo(child1Topic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(subchild1Topic);
2023-02-17 22:25:57 +01:00
subchild2Topic.connectTo(child1Topic, canvas);
2023-03-01 01:54:30 +01:00
canvas.append(subchild2Topic);
2023-02-17 22:25:57 +01:00
// Register refresh hook ..
registerRefreshHook(topics);
return divElem;
};
export default createConnection;