Files
workflow/app/public/logicflow/extension/cjs/components/mini-map/index.js
T
2022-11-28 14:55:01 +08:00

354 lines
14 KiB
JavaScript

"use strict";
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MiniMap = void 0;
var MiniMap = /** @class */ (function () {
function MiniMap(_a) {
var _this = this;
var lf = _a.lf, LogicFlow = _a.LogicFlow;
this.__lf = null;
this.__container = null;
this.__miniMapWrap = null;
this.__miniMapContainer = null;
this.__lfMap = null;
this.__viewport = null;
this.__width = 150;
this.__height = 220;
this.__miniMapWidth = 450;
this.__miniMapHeight = 660;
this.__viewPortTop = 0;
this.__viewPortLeft = 0;
this.__startPosition = null;
this.__viewPortScale = 1;
this.__viewPortWidth = 150;
this.__viewPortHeight = 75;
this.__resetDataX = 0;
this.__resetDataY = 0;
this.__LogicFlow = null;
this.__isShow = false;
this.__disabledPlugins = ['miniMap', 'control', 'selectionSelect'];
/**
* 显示mini map
*/
this.show = function (leftPosition, topPosition) {
_this.__setView();
if (!_this.__isShow) {
_this.__createMiniMap(leftPosition, topPosition);
}
_this.__isShow = true;
};
/**
* 隐藏mini map
*/
this.hide = function () {
if (_this.__isShow) {
_this.__removeMiniMap();
}
_this.__isShow = false;
};
this.__startDrag = function (e) {
document.addEventListener('mousemove', _this.__drag);
document.addEventListener('mouseup', _this.__drop);
_this.__startPosition = {
x: e.x,
y: e.y,
};
};
this.moveViewport = function (top, left) {
var viewStyle = _this.__viewport.style;
_this.__viewPortTop = top;
_this.__viewPortLeft = left;
viewStyle.top = _this.__viewPortTop + "px";
viewStyle.left = _this.__viewPortLeft + "px";
};
this.__drag = function (e) {
var top = _this.__viewPortTop + e.y - _this.__startPosition.y;
var left = _this.__viewPortLeft + e.x - _this.__startPosition.x;
_this.moveViewport(top, left);
_this.__startPosition = {
x: e.x,
y: e.y,
};
var centerX = (_this.__viewPortLeft + _this.__viewPortWidth / 2)
/ _this.__viewPortScale;
var centerY = (_this.__viewPortTop + _this.__viewPortHeight / 2)
/ _this.__viewPortScale;
_this.__lf.focusOn({
coordinate: {
x: centerX + _this.__resetDataX,
y: centerY + _this.__resetDataY,
},
});
};
this.__drop = function () {
document.removeEventListener('mousemove', _this.__drag);
document.removeEventListener('mouseup', _this.__drop);
var top = _this.__viewPortTop;
var left = _this.__viewPortLeft;
if (_this.__viewPortLeft > _this.__width) {
left = _this.__width - _this.__viewPortWidth;
}
if (_this.__viewPortTop > _this.__height) {
top = _this.__height - _this.__viewPortHeight;
}
if (_this.__viewPortLeft < -_this.__width) {
left = 0;
}
if (_this.__viewPortTop < -_this.__height) {
top = 0;
}
_this.moveViewport(top, left);
};
this.__lf = lf;
this.__miniMapWidth = lf.graphModel.width;
this.__miniMapHeight = (lf.graphModel.width * 220) / 150;
this.__LogicFlow = LogicFlow;
this.__init();
}
MiniMap.prototype.render = function (lf, container) {
var _this = this;
this.__container = container;
this.__lf.on('history:change', function () {
if (_this.__isShow) {
_this.__setView();
}
});
this.__lf.on('blank:drop', function () {
if (_this.__isShow) {
_this.__setView();
}
});
};
MiniMap.prototype.init = function (option) {
this.__disabledPlugins = this.__disabledPlugins.concat(option.disabledPlugins || []);
};
MiniMap.prototype.__init = function () {
var miniMapWrap = document.createElement('div');
miniMapWrap.className = 'lf-mini-map-graph';
miniMapWrap.style.width = this.__width + "px";
miniMapWrap.style.height = this.__height + "px";
this.__lfMap = new this.__LogicFlow({
width: this.__lf.graphModel.width,
height: (this.__lf.graphModel.width * 220) / 150,
container: miniMapWrap,
isSilentMode: true,
stopZoomGraph: true,
stopScrollGraph: true,
stopMoveGraph: true,
hideAnchors: true,
hoverOutline: false,
disabledPlugins: this.__disabledPlugins,
});
// minimap中禁用adapter。
this.__lfMap.adapterIn = function (a) { return a; };
this.__lfMap.adapterOut = function (a) { return a; };
this.__miniMapWrap = miniMapWrap;
this.__createViewPort();
};
MiniMap.prototype.__createMiniMap = function (left, top) {
var miniMapContainer = document.createElement('div');
var miniMapWrap = this.__miniMapWrap;
miniMapContainer.appendChild(miniMapWrap);
if (typeof left !== 'undefined' && typeof top !== 'undefined') {
miniMapContainer.style.left = left + "px";
miniMapContainer.style.top = top + "px";
}
miniMapContainer.style.position = 'absolute';
miniMapContainer.className = 'lf-mini-map';
this.__container.appendChild(miniMapContainer);
this.__miniMapWrap.appendChild(this.__viewport);
var header = document.createElement('div');
header.className = 'lf-mini-map-header';
header.innerText = '导航';
miniMapContainer.appendChild(header);
var close = document.createElement('span');
close.className = 'lf-mini-map-close';
close.addEventListener('click', this.hide);
miniMapContainer.appendChild(close);
this.__miniMapContainer = miniMapContainer;
};
MiniMap.prototype.__removeMiniMap = function () {
this.__container.removeChild(this.__miniMapContainer);
};
/**
* 计算所有图形一起,占领的区域范围。
* @param data
*/
MiniMap.prototype.__getBounds = function (data) {
var left = 0;
var right = this.__miniMapWidth;
var top = 0;
var bottom = this.__miniMapHeight;
var nodes = data.nodes;
if (nodes && nodes.length > 0) {
// 因为获取的节点不知道真实的宽高,这里需要补充一点数值
nodes.forEach(function (_a) {
var x = _a.x, y = _a.y, _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c;
var nodeLeft = x - width / 2;
var noderight = x + width / 2;
var nodeTop = y - height / 2;
var nodeBottom = y + height / 2;
left = nodeLeft < left ? nodeLeft : left;
right = noderight > right ? noderight : right;
top = nodeTop < top ? nodeTop : top;
bottom = nodeBottom > bottom ? nodeBottom : bottom;
});
}
return {
left: left,
top: top,
bottom: bottom,
right: right,
};
};
/**
* 将负值的平移转换为正值。
* 保证渲染的时候,minimap能完全展示。
* 获取将画布所有元素平移到0,0开始时,所有节点数据
*/
MiniMap.prototype.__resetData = function (data) {
var nodes = data.nodes, edges = data.edges;
var left = 0;
var top = 0;
if (nodes && nodes.length > 0) {
// 因为获取的节点不知道真实的宽高,这里需要补充一点数值
nodes.forEach(function (_a) {
var x = _a.x, y = _a.y, _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c;
var nodeLeft = x - width / 2;
var nodeTop = y - height / 2;
left = nodeLeft < left ? nodeLeft : left;
top = nodeTop < top ? nodeTop : top;
});
if (left < 0 || top < 0) {
this.__resetDataX = left;
this.__resetDataY = top;
nodes.forEach(function (node) {
node.x = node.x - left;
node.y = node.y - top;
if (node.text) {
node.text.x = node.text.x - left;
node.text.y = node.text.y - top;
}
});
edges.forEach(function (edge) {
if (edge.startPoint) {
edge.startPoint.x = edge.startPoint.x - left;
edge.startPoint.y = edge.startPoint.y - top;
}
if (edge.endPoint) {
edge.endPoint.x = edge.endPoint.x - left;
edge.endPoint.y = edge.endPoint.y - top;
}
if (edge.text) {
edge.text.x = edge.text.x - left;
edge.text.y = edge.text.y - top;
}
if (edge.pointsList) {
edge.pointsList.forEach(function (point) {
point.x = point.x - left;
point.y = point.y - top;
});
}
});
}
}
return data;
};
/**
* 显示导航
* 显示视口范围
* 1. 基于画布的范围比例,设置视口范围比例。宽度默认为导航宽度。
*/
MiniMap.prototype.__setView = function () {
var e_1, _a;
// 1. 获取到图中所有的节点中的位置,将其偏移到原点开始(避免节点位置为负的时候无法展示问题)。
var data = this.__resetData(this.__lf.getGraphRawData());
// 由于随时都会有新节点注册进来,需要同步将注册的
var viewMap = this.__lf.viewMap;
var modelMap = this.__lf.graphModel.modelMap;
var minimapViewMap = this.__lfMap.viewMap;
try {
// todo: no-restricted-syntax
for (var _b = __values(viewMap.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
var key = _c.value;
if (!minimapViewMap.has(key)) {
this.__lfMap.setView(key, viewMap.get(key));
this.__lfMap.graphModel.modelMap.set(key, modelMap.get(key));
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
this.__lfMap.render(data);
// 2. 将偏移后的数据渲染到minimap画布上
// 3. 计算出所有节点在一起的边界。
var _d = this.__getBounds(data), left = _d.left, top = _d.top, right = _d.right, bottom = _d.bottom;
// 4. 计算所有节点的边界与minimap看板的边界的比例.
var realWidthScale = this.__width / (right - left);
var realHeightScale = this.__height / (bottom - top);
// 5. 取比例最小的值,将渲染的画布缩小对应比例。
var innerStyle = this.__miniMapWrap.firstChild.style;
var scale = Math.min(realWidthScale, realHeightScale);
innerStyle.transform = "matrix(" + scale + ", 0, 0, " + scale + ", 0, 0)";
innerStyle.transformOrigin = 'left top';
innerStyle.height = bottom - Math.min(top, 0) + "px";
innerStyle.width = right - Math.min(left, 0) + "px";
this.__viewPortScale = scale;
this.__setViewPort(scale, {
left: left,
top: top,
right: right,
bottom: bottom,
});
};
// 设置视口
MiniMap.prototype.__setViewPort = function (scale, _a) {
var left = _a.left, right = _a.right, top = _a.top, bottom = _a.bottom;
var viewStyle = this.__viewport.style;
viewStyle.width = this.__width - 4 + "px";
viewStyle.height = (this.__width - 4) / (this.__lf.graphModel.width / this.__lf.graphModel.height) + "px";
// top
var _b = this.__lf.getTransform(), TRANSLATE_X = _b.TRANSLATE_X, TRANSLATE_Y = _b.TRANSLATE_Y;
var realWidth = right - left;
// 视口实际宽 = 视口默认宽 / (所有元素一起占据的真实宽 / 绘布宽)
var realViewPortWidth = (this.__width - 4) / (realWidth / this.__lf.graphModel.width);
// 视口实际高 = 视口实际宽 / (绘布宽 / 绘布高)
var graphRatio = (this.__lf.graphModel.width / this.__lf.graphModel.height);
var realViewPortHeight = realViewPortWidth / graphRatio;
this.__viewPortTop = TRANSLATE_Y > 0 ? 0 : -TRANSLATE_Y * scale;
this.__viewPortLeft = TRANSLATE_X > 0 ? 0 : -TRANSLATE_X * scale;
this.__viewPortWidth = realViewPortWidth;
this.__viewPortHeight = realViewPortHeight;
viewStyle.top = this.__viewPortTop + "px";
viewStyle.left = this.__viewPortLeft + "px";
viewStyle.width = realViewPortWidth + "px";
viewStyle.height = realViewPortHeight + "px";
};
// 预览视窗
MiniMap.prototype.__createViewPort = function () {
var div = document.createElement('div');
div.className = 'lf-minimap-viewport';
div.addEventListener('mousedown', this.__startDrag);
this.__viewport = div;
};
MiniMap.pluginName = 'miniMap';
return MiniMap;
}());
exports.MiniMap = MiniMap;
exports.default = MiniMap;