index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.ContextMenu = void 0;
  20. var COMMON_TYPE_KEY = 'menu-common';
  21. var NEXT_X_DISTANCE = 200;
  22. var NEXT_Y_DISTANCE = 100;
  23. var ContextMenu = /** @class */ (function () {
  24. function ContextMenu(_a) {
  25. var _this = this;
  26. var lf = _a.lf;
  27. this.menuTypeMap = new Map();
  28. this.listenDelete = function () {
  29. _this.hideContextMenu();
  30. };
  31. this.lf = lf;
  32. this.__menuDOM = document.createElement('div');
  33. this.__menuDOM.className = 'lf-inner-context';
  34. this.menuTypeMap.set(COMMON_TYPE_KEY, []);
  35. this.lf.setContextMenuByType = function (type, menus) {
  36. _this.setContextMenuByType(type, menus);
  37. };
  38. this.lf.setContextMenuItems = function (menus) {
  39. _this.setContextMenuItems(menus);
  40. };
  41. this.lf.showContextMenu = function (data) {
  42. _this.showContextMenu(data);
  43. };
  44. this.lf.hideContextMenu = function () {
  45. _this.hideContextMenu();
  46. };
  47. }
  48. ContextMenu.prototype.render = function (lf, container) {
  49. var _this = this;
  50. this.container = container;
  51. lf.on('node:click', function (_a) {
  52. var data = _a.data;
  53. _this._activeData = data;
  54. _this.createContextMenu();
  55. });
  56. lf.on('edge:click', function (_a) {
  57. var data = _a.data;
  58. // 获取右上角坐标
  59. _this._activeData = data;
  60. _this.createContextMenu();
  61. });
  62. lf.on('blank:click', function () {
  63. _this.hideContextMenu();
  64. });
  65. };
  66. ContextMenu.prototype.setContextMenuByType = function (type, menus) {
  67. this.menuTypeMap.set(type, menus);
  68. };
  69. /**
  70. * 隐藏菜单
  71. */
  72. ContextMenu.prototype.hideContextMenu = function () {
  73. this.__menuDOM.innerHTML = '';
  74. this.__menuDOM.style.display = 'none';
  75. if (this.isShow) {
  76. this.container.removeChild(this.__menuDOM);
  77. }
  78. this.lf.off('node:delete,edge:delete,node:drag,graph:transform', this.listenDelete);
  79. this.isShow = false;
  80. };
  81. /**
  82. * 显示指定元素菜单
  83. * @param data 节点id、节点类型、菜单位置
  84. */
  85. ContextMenu.prototype.showContextMenu = function (data) {
  86. if (!data || !data.id) {
  87. console.warn('请检查传入的参数');
  88. return;
  89. }
  90. this._activeData = data;
  91. this.createContextMenu();
  92. };
  93. ContextMenu.prototype.setContextMenuItems = function (menus) {
  94. this.menuTypeMap.set(COMMON_TYPE_KEY, menus);
  95. };
  96. /**
  97. * 获取新菜单位置
  98. */
  99. ContextMenu.prototype.getContextMenuPosition = function () {
  100. var data = this._activeData;
  101. var Model = this.lf.graphModel.getElement(data.id);
  102. if (!Model) {
  103. console.warn("\u627E\u4E0D\u5230\u5143\u7D20" + data.id);
  104. return;
  105. }
  106. var x;
  107. var y;
  108. if (Model.BaseType === 'edge') {
  109. x = Number.MIN_SAFE_INTEGER;
  110. y = Number.MAX_SAFE_INTEGER;
  111. var edgeData = Model.getData();
  112. x = Math.max(edgeData.startPoint.x, x);
  113. y = Math.min(edgeData.startPoint.y, y);
  114. x = Math.max(edgeData.endPoint.x, x);
  115. y = Math.min(edgeData.endPoint.y, y);
  116. if (edgeData.pointsList) {
  117. edgeData.pointsList.forEach(function (point) {
  118. x = Math.max(point.x, x);
  119. y = Math.min(point.y, y);
  120. });
  121. }
  122. }
  123. if (Model.BaseType === 'node') {
  124. x = data.x + Model.width / 2;
  125. y = data.y - Model.height / 2;
  126. }
  127. return this.lf.graphModel.transformModel.CanvasPointToHtmlPoint([x, y]);
  128. };
  129. ContextMenu.prototype.createContextMenu = function () {
  130. var _this = this;
  131. var isSilentMode = this.lf.options.isSilentMode;
  132. // 静默模式不显示菜单
  133. if (isSilentMode) {
  134. return;
  135. }
  136. var items = this.menuTypeMap.get(this._activeData.type) || [];
  137. items = items.concat(this.menuTypeMap.get(COMMON_TYPE_KEY));
  138. var menus = document.createDocumentFragment();
  139. items.forEach(function (item) {
  140. var menuItem = document.createElement('div');
  141. menuItem.className = 'lf-context-item';
  142. var img = document.createElement('img');
  143. img.src = item.icon;
  144. img.className = 'lf-context-img';
  145. if (item.className) {
  146. menuItem.className = menuItem.className + " " + item.className;
  147. }
  148. img.addEventListener('click', function () {
  149. _this.hideContextMenu();
  150. if (item.callback) {
  151. item.callback(_this._activeData);
  152. }
  153. else {
  154. _this.addNode({
  155. sourceId: _this._activeData.id,
  156. x: _this._activeData.x,
  157. y: _this._activeData.y,
  158. properties: item.properties,
  159. type: item.type,
  160. });
  161. }
  162. });
  163. menuItem.appendChild(img);
  164. menus.appendChild(menuItem);
  165. });
  166. this.__menuDOM.innerHTML = '';
  167. this.__menuDOM.appendChild(menus);
  168. this.showMenu();
  169. };
  170. ContextMenu.prototype.addNode = function (node, y) {
  171. var isDeep = y !== undefined;
  172. if (y === undefined) {
  173. y = node.y;
  174. }
  175. var nodeModel = this.lf.getNodeModelById(node.sourceId);
  176. var leftTopX = node.x - nodeModel.width + NEXT_X_DISTANCE;
  177. var leftTopY = y - node.y / 2 - 20;
  178. var rightBottomX = node.x + nodeModel.width + NEXT_X_DISTANCE;
  179. var rightBottomY = y + node.y / 2 + 20;
  180. var existElements = this.lf.getAreaElement([leftTopX, leftTopY], [rightBottomX, rightBottomY]);
  181. if (existElements.length) {
  182. y = y + NEXT_Y_DISTANCE;
  183. this.addNode(node, y);
  184. return;
  185. }
  186. var newNode = this.lf.addNode({
  187. type: node.type,
  188. x: node.x + 200,
  189. y: y,
  190. properties: node.properties,
  191. });
  192. var startPoint;
  193. var endPoint;
  194. if (isDeep) {
  195. startPoint = {
  196. x: node.x,
  197. y: node.y + nodeModel.height / 2,
  198. };
  199. endPoint = {
  200. x: newNode.x - newNode.width / 2,
  201. y: newNode.y,
  202. };
  203. }
  204. this.lf.addEdge({
  205. sourceNodeId: node.sourceId,
  206. targetNodeId: newNode.id,
  207. startPoint: startPoint,
  208. endPoint: endPoint,
  209. });
  210. };
  211. ContextMenu.prototype.showMenu = function () {
  212. var _a = __read(this.getContextMenuPosition(), 2), x = _a[0], y = _a[1];
  213. this.__menuDOM.style.display = 'flex';
  214. this.__menuDOM.style.top = y + "px";
  215. this.__menuDOM.style.left = x + 10 + "px";
  216. this.container.appendChild(this.__menuDOM);
  217. // 菜单显示的时候,监听删除,同时隐藏
  218. !this.isShow && this.lf.on('node:delete,edge:delete,node:drag,graph:transform', this.listenDelete);
  219. this.isShow = true;
  220. };
  221. ContextMenu.pluginName = 'contextMenu';
  222. return ContextMenu;
  223. }());
  224. exports.ContextMenu = ContextMenu;
  225. exports.default = ContextMenu;