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, RubberBandHandler, DomHelpers } from '@maxgraph/core';
|
2023-10-12 21:41:37 +00:00
|
|
|
import {
|
|
|
|
globalTypes,
|
|
|
|
globalValues,
|
|
|
|
rubberBandTypes,
|
|
|
|
rubberBandValues,
|
|
|
|
} from './shared/args.js';
|
|
|
|
// style required by RubberBand
|
|
|
|
import '@maxgraph/core/css/common.css';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Misc/Visibility',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
2023-10-12 21:41:37 +00:00
|
|
|
...rubberBandTypes,
|
|
|
|
},
|
|
|
|
args: {
|
|
|
|
...globalValues,
|
|
|
|
...rubberBandValues,
|
2021-08-30 14:20:26 +00:00
|
|
|
},
|
2021-04-25 03:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const Template = ({ label, ...args }) => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
|
|
|
|
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';
|
|
|
|
div.appendChild(container);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
let showOne = true;
|
|
|
|
let showTwo = true;
|
|
|
|
let showThree = true;
|
|
|
|
|
|
|
|
// Overridden to implement dynamic conditions
|
2021-08-30 14:20:26 +00:00
|
|
|
const isVisible = function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
// TODO super cannot be used here
|
|
|
|
// let result = super.isVisible();
|
2022-10-03 08:07:39 +00:00
|
|
|
let result = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
if (result && this.value != null) {
|
|
|
|
result =
|
|
|
|
(showOne && this.value == '1') ||
|
|
|
|
(showTwo && this.value == '2') ||
|
|
|
|
(showThree && this.value == '3');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Adds cells to the model in a single step
|
|
|
|
let v1;
|
|
|
|
graph.batchUpdate(() => {
|
|
|
|
v1 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
value: '1',
|
|
|
|
position: [20, 20],
|
|
|
|
size: [80, 30],
|
|
|
|
});
|
|
|
|
v1.isVisible = isVisible;
|
|
|
|
|
|
|
|
const v2 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
value: '2',
|
|
|
|
position: [200, 150],
|
|
|
|
size: [80, 30],
|
|
|
|
});
|
|
|
|
v2.isVisible = isVisible;
|
|
|
|
|
|
|
|
const e1 = graph.insertEdge({
|
|
|
|
parent,
|
|
|
|
value: '3',
|
|
|
|
source: v1,
|
|
|
|
target: v2,
|
|
|
|
});
|
|
|
|
e1.isVisible = isVisible;
|
|
|
|
});
|
|
|
|
|
|
|
|
const buttons = document.createElement('div');
|
|
|
|
div.appendChild(buttons);
|
|
|
|
|
|
|
|
// Dynamic conditions (requires refresh)
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Cond 1', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
showOne = !showOne;
|
|
|
|
graph.refresh();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Cond 2', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
showTwo = !showTwo;
|
|
|
|
graph.refresh();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Cond 3', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
showThree = !showThree;
|
|
|
|
graph.refresh();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Explicit show/hide
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Toggle cell', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.toggleCells(!v1.isVisible(), [v1], true);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Explicit remove/add
|
|
|
|
let removed = null;
|
|
|
|
|
|
|
|
buttons.appendChild(
|
2022-01-08 01:49:35 +00:00
|
|
|
DomHelpers.button('Add/remove cell', function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (removed != null) {
|
|
|
|
graph.addCells(removed);
|
|
|
|
removed = null;
|
|
|
|
} else {
|
|
|
|
removed = graph.removeCells([v1]);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return div;
|
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({});
|