index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __read = (this && this.__read) || function (o, n) {
  14. var m = typeof Symbol === "function" && o[Symbol.iterator];
  15. if (!m) return o;
  16. var i = m.call(o), r, ar = [], e;
  17. try {
  18. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  19. }
  20. catch (error) { e = { error: error }; }
  21. finally {
  22. try {
  23. if (r && !r.done && (m = i["return"])) m.call(i);
  24. }
  25. finally { if (e) throw e.error; }
  26. }
  27. return ar;
  28. };
  29. var __spread = (this && this.__spread) || function () {
  30. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  31. return ar;
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.Highlight = void 0;
  35. // 后续并入FlowPath
  36. var getPath = function (id, lf) {
  37. var el = lf.getModelById(id);
  38. return getNodePath(el.BaseType === 'node' ? el : el.targetNode, lf);
  39. };
  40. // dfs + 动态规划
  41. // todo 算法优化
  42. var getNodePath = function (node, lf) {
  43. var incomingPaths = [];
  44. var outgoingPaths = [];
  45. var getIncomingPaths = function (curNode, path, prevNode) {
  46. if (prevNode === void 0) { prevNode = null; }
  47. if (prevNode) {
  48. // * 上个节点和当前节点中间边
  49. path.unshift.apply(path, __spread(lf
  50. .getEdgeModels({
  51. sourceNodeId: curNode.id,
  52. targetNodeId: prevNode.id,
  53. })
  54. .map(function (item) { return item.id; })));
  55. }
  56. // * 路径中存在节点,则不再继续查找,说明出现环情况
  57. if (path.includes(curNode.id)) {
  58. incomingPaths.push(path);
  59. return;
  60. }
  61. // * 路径中当前加入节点
  62. path.unshift(curNode.id);
  63. if (!curNode.incoming.nodes.length) {
  64. incomingPaths.push(path);
  65. return;
  66. }
  67. // * 往下找
  68. curNode.incoming.nodes.forEach(function (nextNode) {
  69. getIncomingPaths(nextNode, path.slice(), curNode);
  70. });
  71. };
  72. // * 同上逻辑
  73. var getOutgoingPaths = function (curNode, path, prevNode) {
  74. if (prevNode === void 0) { prevNode = null; }
  75. if (prevNode) {
  76. path.push.apply(path, __spread(lf
  77. .getEdgeModels({
  78. sourceNodeId: prevNode.id,
  79. targetNodeId: curNode.id,
  80. })
  81. .map(function (item) { return item.id; })));
  82. }
  83. if (path.includes(curNode.id)) {
  84. outgoingPaths.push(path);
  85. return;
  86. }
  87. path.push(curNode.id);
  88. if (!curNode.outgoing.nodes.length) {
  89. outgoingPaths.push(path);
  90. return;
  91. }
  92. curNode.outgoing.nodes.forEach(function (nextNode) {
  93. getOutgoingPaths(nextNode, path.slice(), curNode);
  94. });
  95. };
  96. getIncomingPaths(node, []);
  97. getOutgoingPaths(node, []);
  98. return __spread(new Set(__spread(incomingPaths.flat(), outgoingPaths.flat())));
  99. };
  100. var Highlight = /** @class */ (function () {
  101. function Highlight(_a) {
  102. var lf = _a.lf;
  103. this.mode = 'path';
  104. this.manual = false;
  105. this.tempStyles = {};
  106. this.lf = lf;
  107. }
  108. Highlight.prototype.setMode = function (mode) {
  109. this.mode = mode;
  110. };
  111. Highlight.prototype.setManual = function (manual) {
  112. this.manual = manual;
  113. };
  114. Highlight.prototype.highlightSingle = function (id) {
  115. var model = this.lf.getModelById(id);
  116. if (model.BaseType === 'node') {
  117. // 高亮节点
  118. model.updateStyles(this.tempStyles[id]);
  119. }
  120. else if (model.BaseType === 'edge') {
  121. // 高亮边及对应的节点
  122. model.updateStyles(this.tempStyles[id]);
  123. model.sourceNode.updateStyles(this.tempStyles[model.sourceNode.id]);
  124. model.targetNode.updateStyles(this.tempStyles[model.targetNode.id]);
  125. }
  126. };
  127. Highlight.prototype.highlightPath = function (id) {
  128. var _this = this;
  129. var path = getPath(id, this.lf);
  130. path.forEach(function (_id) {
  131. // 高亮路径上所有的边和节点
  132. _this.lf.getModelById(_id).updateStyles(_this.tempStyles[_id]);
  133. });
  134. };
  135. Highlight.prototype.highlight = function (id, mode) {
  136. var _this = this;
  137. if (mode === void 0) { mode = this.mode; }
  138. if (this.manual)
  139. return;
  140. if (Object.keys(this.tempStyles).length) {
  141. this.restoreHighlight();
  142. }
  143. Object.values(this.lf.graphModel.modelsMap).forEach(function (item) {
  144. // 所有节点样式都进行备份
  145. var oStyle = item.BaseType === 'node' ? item.getNodeStyle() : item.getEdgeStyle();
  146. _this.tempStyles[item.id] = __assign({}, oStyle);
  147. // 所有节点都设置透明度为0.1
  148. item.setStyles({ opacity: 0.1 });
  149. });
  150. var modeTrigger = {
  151. single: this.highlightSingle.bind(this),
  152. path: this.highlightPath.bind(this),
  153. };
  154. modeTrigger[mode](id);
  155. };
  156. Highlight.prototype.restoreHighlight = function () {
  157. var _this = this;
  158. // 恢复所有节点的样式
  159. if (!Object.keys(this.tempStyles).length)
  160. return;
  161. Object.values(this.lf.graphModel.modelsMap).forEach(function (item) {
  162. var _a;
  163. var oStyle = (_a = _this.tempStyles[item.id]) !== null && _a !== void 0 ? _a : {};
  164. item.updateStyles(__assign({}, oStyle));
  165. });
  166. this.tempStyles = {};
  167. };
  168. Highlight.prototype.render = function (lf, domContainer) {
  169. var _this = this;
  170. this.lf.on('node:mouseenter', function (_a) {
  171. var data = _a.data;
  172. return _this.highlight(data.id);
  173. });
  174. this.lf.on('edge:mouseenter', function (_a) {
  175. var data = _a.data;
  176. return _this.highlight(data.id);
  177. });
  178. this.lf.on('node:mouseleave', this.restoreHighlight.bind(this));
  179. this.lf.on('edge:mouseleave', this.restoreHighlight.bind(this));
  180. this.lf.on('history:change', this.restoreHighlight.bind(this));
  181. };
  182. Highlight.prototype.destroy = function () { };
  183. Highlight.pluginName = 'highlight';
  184. return Highlight;
  185. }());
  186. exports.Highlight = Highlight;