@maxgraph/core
Version:
maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.
102 lines (98 loc) • 3.65 kB
JavaScript
;
/*
Copyright 2021-present The maxGraph project Contributors
Copyright (c) 2006-2015, JGraph Ltd
Copyright (c) 2006-2015, Gaudenz Alder
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.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Loop = void 0;
const Point_js_1 = __importDefault(require("../../geometry/Point.js"));
const mathUtils_js_1 = require("../../../util/mathUtils.js");
/**
* Implements a self-reference, aka. loop.
*
* This EdgeStyle is registered under `loopEdgeStyle` in {@link EdgeStyleRegistry} when using {@link Graph} or calling {@link registerDefaultEdgeStyles}.
*
* **IMPORTANT**: When registering it manually in {@link EdgeStyleRegistry}, the following metadata must be used:
* - handlerKind: 'elbow'
* - isOrthogonal: false
*/
const Loop = (state, source, _target, points, result) => {
const pts = state.absolutePoints;
const p0 = pts[0];
const pe = pts[pts.length - 1];
if (p0 != null && pe != null) {
if (points != null && points.length > 0) {
for (let i = 0; i < points.length; i += 1) {
let pt = points[i];
pt = state.view.transformControlPoint(state, pt);
result.push(new Point_js_1.default(pt.x, pt.y));
}
}
return;
}
if (source != null) {
const { view } = state;
const { graph } = view;
let pt = points != null && points.length > 0 ? points[0] : null;
if (pt != null) {
pt = view.transformControlPoint(state, pt);
if ((0, mathUtils_js_1.contains)(source, pt.x, pt.y)) {
pt = null;
}
}
let x = 0;
let dx = 0;
let y = 0;
let dy = 0;
const seg = (state.style.segment ?? graph.gridSize) * view.scale;
const dir = state.style?.direction ?? 'west';
if (dir === 'north' || dir === 'south') {
x = view.getRoutingCenterX(source);
dx = seg;
}
else {
y = view.getRoutingCenterY(source);
dy = seg;
}
if (pt == null || pt.x < source.x || pt.x > source.x + source.width) {
if (pt != null) {
x = pt.x;
dy = Math.max(Math.abs(y - pt.y), dy);
}
else if (dir === 'north') {
y = source.y - 2 * dx;
}
else if (dir === 'south') {
y = source.y + source.height + 2 * dx;
}
else if (dir === 'east') {
x = source.x - 2 * dy;
}
else {
x = source.x + source.width + 2 * dy;
}
}
else if (pt !== null) {
x = view.getRoutingCenterX(source);
dx = Math.max(Math.abs(x - pt.x), dy);
y = pt.y;
dy = 0;
}
result.push(new Point_js_1.default(x - dx, y - dy));
result.push(new Point_js_1.default(x + dx, y + dy));
}
};
exports.Loop = Loop;