2022-08-30 15:36:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-present The maxGraph project Contributors
|
|
|
|
Copyright (c) 2006-2020, JGraph Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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-09-17 02:30:23 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
|
|
|
RubberBandHandler,
|
|
|
|
SelectionHandler,
|
|
|
|
PopupMenuHandler,
|
|
|
|
} from '@maxgraph/core';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Layouts/Groups',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-24 12:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
const container = document.createElement('div');
|
|
|
|
container.style.position = 'relative';
|
|
|
|
container.style.overflow = 'hidden';
|
|
|
|
container.style.width = `${args.width}px`;
|
|
|
|
container.style.height = `${args.height}px`;
|
|
|
|
container.style.background = 'url(/images/grid.gif)';
|
|
|
|
container.style.cursor = 'default';
|
|
|
|
|
|
|
|
// Overrides check for valid roots
|
2021-08-30 14:20:26 +00:00
|
|
|
Graph.prototype.isValidRoot = function () {
|
2021-04-24 12:30:27 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't clear selection if multiple cells selected
|
2022-01-08 01:49:35 +00:00
|
|
|
const graphHandlerMouseDown = SelectionHandler.prototype.mouseDown;
|
|
|
|
SelectionHandler.prototype.mouseDown = function (sender, me) {
|
2021-04-24 12:30:27 +00:00
|
|
|
graphHandlerMouseDown.apply(this, arguments);
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
if (this.graph.isCellSelected(me.getCell()) && this.graph.getSelectionCount() > 1) {
|
2021-04-24 12:30:27 +00:00
|
|
|
this.delayedSelection = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Selects descendants before children selection mode
|
|
|
|
const graphHandlerGetInitialCellForEvent =
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler.prototype.getInitialCellForEvent;
|
|
|
|
SelectionHandler.prototype.getInitialCellForEvent = function (me) {
|
2022-09-17 02:30:23 +00:00
|
|
|
const psel = this.graph.getSelectionCell()
|
|
|
|
? this.graph.getSelectionCell().getParent()
|
|
|
|
: null;
|
2021-04-24 12:30:27 +00:00
|
|
|
let cell = graphHandlerGetInitialCellForEvent.apply(this, arguments);
|
2021-04-25 03:39:40 +00:00
|
|
|
let parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (psel == null || (psel != cell && psel != parent)) {
|
|
|
|
while (
|
|
|
|
!this.graph.isCellSelected(cell) &&
|
|
|
|
!this.graph.isCellSelected(parent) &&
|
2021-04-25 03:39:40 +00:00
|
|
|
parent.isVertex() &&
|
2021-04-24 12:30:27 +00:00
|
|
|
!this.graph.isValidRoot(parent)
|
|
|
|
) {
|
|
|
|
cell = parent;
|
2021-04-25 10:47:53 +00:00
|
|
|
parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Selection is delayed to mouseup if child selected
|
2022-01-08 01:49:35 +00:00
|
|
|
const graphHandlerIsDelayedSelection = SelectionHandler.prototype.isDelayedSelection;
|
|
|
|
SelectionHandler.prototype.isDelayedSelection = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
let result = graphHandlerIsDelayedSelection.apply(this, arguments);
|
2022-09-17 02:30:23 +00:00
|
|
|
const psel = this.graph.getSelectionCell()
|
|
|
|
? this.graph.getSelectionCell().getParent()
|
|
|
|
: null;
|
2021-04-25 03:39:40 +00:00
|
|
|
const parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (psel == null || (psel != cell && psel != parent)) {
|
|
|
|
if (
|
|
|
|
!this.graph.isCellSelected(cell) &&
|
2021-04-25 03:39:40 +00:00
|
|
|
parent.isVertex() &&
|
2021-04-24 12:30:27 +00:00
|
|
|
!this.graph.isValidRoot(parent)
|
|
|
|
) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Delayed selection of parent group
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler.prototype.selectDelayed = function (me) {
|
2021-04-24 12:30:27 +00:00
|
|
|
let cell = me.getCell();
|
|
|
|
|
|
|
|
if (cell == null) {
|
|
|
|
cell = this.cell;
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = this.graph.getDataModel();
|
2021-04-25 03:39:40 +00:00
|
|
|
let parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
while (
|
|
|
|
this.graph.isCellSelected(cell) &&
|
2021-04-25 03:39:40 +00:00
|
|
|
parent.isVertex() &&
|
2021-04-24 12:30:27 +00:00
|
|
|
!this.graph.isValidRoot(parent)
|
|
|
|
) {
|
|
|
|
cell = parent;
|
2021-04-25 03:39:40 +00:00
|
|
|
parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.graph.selectCellForEvent(cell, me.getEvent());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns last selected ancestor
|
2021-09-07 12:21:22 +00:00
|
|
|
PopupMenuHandler.prototype.getCellForPopupEvent = function (me) {
|
2021-04-24 12:30:27 +00:00
|
|
|
let cell = me.getCell();
|
2022-01-08 01:49:35 +00:00
|
|
|
const model = this.graph.getDataModel();
|
2021-04-25 03:39:40 +00:00
|
|
|
let parent = cell.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
while (parent.isVertex() && !this.graph.isValidRoot(parent)) {
|
2021-04-24 12:30:27 +00:00
|
|
|
if (this.graph.isCellSelected(parent)) {
|
|
|
|
cell = parent;
|
|
|
|
}
|
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
parent = parent.getParent();
|
2021-04-24 12:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.constrainChildren = false;
|
|
|
|
graph.extendParents = false;
|
|
|
|
graph.extendParentsOnAdd = false;
|
|
|
|
|
|
|
|
// Uncomment the following if you want the container
|
|
|
|
// to fit the size of the graph
|
|
|
|
// graph.setResizeContainer(true);
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Gets the default parent for inserting new cells. This
|
|
|
|
// is normally the first child of the root (ie. layer 0).
|
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
|
|
|
|
// Adds cells to the model in a single step
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-24 12:30:27 +00:00
|
|
|
const v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 120, 60);
|
|
|
|
const v2 = graph.insertVertex(v1, null, 'World!', 90, 20, 60, 20);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|