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-01-08 01:49:35 +00:00
|
|
|
import { Graph, constants, RubberBandHandler, cloneUtils } from '@maxgraph/core';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Styles/HoverStyle',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-25 03:39:40 +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';
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
function updateStyle(state, hover) {
|
|
|
|
if (hover) {
|
2021-05-02 06:04:34 +00:00
|
|
|
state.style.fillColor = '#ff0000';
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets rounded style for both cases since the rounded style
|
|
|
|
// is not set in the default style and is therefore inherited
|
|
|
|
// once it is set, whereas the above overrides the default value
|
2021-05-02 06:04:34 +00:00
|
|
|
state.style.rounded = hover ? '1' : '0';
|
|
|
|
state.style.strokeWidth = hover ? '4' : '1';
|
2022-01-08 01:49:35 +00:00
|
|
|
state.style.fontStyle = hover ? constants.FONT.BOLD : '0';
|
2021-04-25 03:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Changes fill color to red on mouseover
|
|
|
|
graph.addMouseListener({
|
|
|
|
currentState: null,
|
|
|
|
previousStyle: null,
|
|
|
|
mouseDown(sender, me) {
|
|
|
|
if (this.currentState != null) {
|
|
|
|
this.dragLeave(me.getEvent(), this.currentState);
|
|
|
|
this.currentState = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mouseMove(sender, me) {
|
|
|
|
if (this.currentState != null && me.getState() == this.currentState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tmp = graph.view.getState(me.getCell());
|
|
|
|
|
|
|
|
// Ignores everything but vertices
|
2021-08-30 14:20:26 +00:00
|
|
|
if (graph.isMouseDown || (tmp != null && !tmp.cell.isVertex())) {
|
2021-04-25 03:39:40 +00:00
|
|
|
tmp = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp != this.currentState) {
|
|
|
|
if (this.currentState != null) {
|
|
|
|
this.dragLeave(me.getEvent(), this.currentState);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentState = tmp;
|
|
|
|
|
|
|
|
if (this.currentState != null) {
|
|
|
|
this.dragEnter(me.getEvent(), this.currentState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mouseUp(sender, me) {},
|
|
|
|
dragEnter(evt, state) {
|
|
|
|
if (state != null) {
|
|
|
|
this.previousStyle = state.style;
|
2022-01-08 01:49:35 +00:00
|
|
|
state.style = cloneUtils.clone(state.style);
|
2021-04-25 03:39:40 +00:00
|
|
|
updateStyle(state, true);
|
|
|
|
state.shape.apply(state);
|
|
|
|
state.shape.redraw();
|
|
|
|
|
|
|
|
if (state.text != null) {
|
|
|
|
state.text.apply(state);
|
|
|
|
state.text.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
dragLeave(evt, state) {
|
|
|
|
if (state != null) {
|
|
|
|
state.style = this.previousStyle;
|
|
|
|
updateStyle(state, false);
|
|
|
|
state.shape.apply(state);
|
|
|
|
state.shape.redraw();
|
|
|
|
|
|
|
|
if (state.text != null) {
|
|
|
|
state.text.apply(state);
|
|
|
|
state.text.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-25 03:39:40 +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-25 03:39:40 +00:00
|
|
|
const v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
|
|
|
|
const e1 = graph.insertEdge(parent, null, '', v1, v2);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|