import * as d3 from 'd3'; import * as uuid from 'uuid'; import GraphView from './graph-view'; import { d3Selection, ICoord, getCurvePoints } from './util'; import SlotView from './slot-view'; import NodeView from './node-view'; import GEditor from './geditor'; import { EVENTS } from './constants'; const nodeClassname = 'x-node'; export interface IEdge { id?: string; srcNodeId: string; destNodeId: string; srcSlotId: string; destSlotId: string; [key: string]: any; } export interface InitialSettings { edgeConfig: IEdge; graphView: GraphView; srcSlotView: SlotView; destSlotView: SlotView; geditor: GEditor; } export default class EdgeView { public id: string; public $el: d3Selection; public srcNodeView: NodeView; public destNodeView: NodeView; public srcSlotView: SlotView; public destSlotView: SlotView; protected geditor: GEditor; protected graphView: GraphView; protected points: [ICoord, ICoord]; protected lineGenerator: d3.Line; protected edgeConfig: IEdge; private $path: d3Selection; private $edgePath: d3Selection; private $edgeKey: d3Selection; private $textPath: d3Selection; private status: 'INITIALIZED' | 'READY'; private edgeKey: string; constructor (settings: InitialSettings) { const { geditor, graphView, edgeConfig, srcSlotView, destSlotView, } = settings; this.id = edgeConfig.id || uuid.v4(); this.edgeConfig = edgeConfig; this.graphView = graphView; this.geditor = geditor; this.srcSlotView = srcSlotView; this.destSlotView = destSlotView; this.srcNodeView = srcSlotView.nodeView; console.log(edgeConfig); console.log(settings, destSlotView); this.destNodeView = destSlotView.nodeView; /** Edge shoud insert before node, to avoid edge cover node */ const { type } = edgeConfig; this.$el = graphView.$el.insert('g', `.${nodeClassname}`).classed('x-edge', true); this.lineGenerator = d3.line().x(d => d.x).y(d => d.y).curve(type && type === 'step' ? d3.curveStepBefore : d3.curveBasis); // this.lineGenerator = d3.line().x(d => d.x).y(d => d.y).curve(d3.curveBasis); this.points = [srcSlotView.getCoord(), destSlotView.getCoord()]; this.render(edgeConfig); this.initEvents(); } protected render (edgeConfig: IEdge) { const curvePoints = getCurvePoints(this.points[0], this.points[1]); // 三角 this.$el.append('svg:defs').append('svg:marker') .attr('id', `edge_triangle_${this.id}`) .attr('viewBox', '5 -5 10 10') .attr('refX', 10) .attr('refY', 0) .attr('markerWidth', 12) .attr('markerHeight', 10) .attr('orient', 'auto') .append('path') // .attr('xlink:href', ArrowIcon) .attr('id', 'marker-path') .attr('d', 'M0,-5L10,0L0,5') .attr('stroke-dasharray', edgeConfig.srcNodeId === 'start_node' ? 3 : 0) .style('fill', '#a8b7c7'); this.$path = this.$el.append('path').attr('d', this.lineGenerator(curvePoints)) // .attr("marker-end", `url(#edge_triangle_${this.id})`) .style('stroke', edgeConfig.status === 'INITIALIZED' ? '#a8b7c7' : '#358EDF') .attr('stroke-dasharray', edgeConfig.srcNodeId === 'start_node' ? 3 : 0) .attr('class', 'edge-path') .attr('id', this.id); /** Add a hidden path with a big stroke-width=50, make the edge easy to select */ this.$el.append('path') .attr('d', this.lineGenerator(curvePoints)) .attr('id', this.id) .classed('x-edge-virtual', true); const startX = Math.min(curvePoints[curvePoints.length - 1].x, curvePoints[0].x); const startY = Math.min(curvePoints[curvePoints.length - 1].y - curvePoints[0].y); const endX = Math.max(curvePoints[curvePoints.length - 1].x, curvePoints[0].x); const endY = Math.max(curvePoints[curvePoints.length - 1].y - curvePoints[0].y); const textPathX = (endX - startX) / 2; const textPathY = (endY - startY) / 2; this.$edgePath = this.$el.append('text') // .attr('text-anchor', 'middle') .attr('x', textPathX ? textPathX : 20) .attr('y', textPathY) .attr('fill', '#a8b7c7') .attr('style', 'font-size:14px'); this.$textPath = this.$edgePath.append('textPath') .attr('xlink:href', `#${this.id}`); this.$edgeKey = this.$textPath.append('tspan') .attr('class', 'tspan-layer') .attr('fill', '#333') .attr('x', textPathX ? textPathX : 24) .attr('y', textPathY) .text(edgeConfig.key || ''); this.updateEdgeStatus(edgeConfig.status || 'INITIALIZED'); } protected initEvents () { this.$el .on('click', () => this.onClick()) .on('contextmenu', () => this.onContextmenu()); } protected detachEvents() { this.$el .on('click', null) .on('contextmenu', null); } protected onClick () { d3.event.stopPropagation(); if (d3.event.shiftKey) { if (this.graphView.isSelectionExsits(this.id)) { this.graphView.removeSelectionId(this.id); } else { this.graphView.appendSelectionIds(this.id); } } else { this.graphView.resetSelectionIds(this.id); } } protected onContextmenu () { d3.event.preventDefault(); d3.event.stopPropagation(); this.geditor.emit(EVENTS.EDGE_CONTEXTMENU, this.id, d3.event); } public update () { this.points = [this.srcSlotView.getCoord(), this.destSlotView.getCoord()]; const curvePoints = getCurvePoints(this.points[0], this.points[1]); this.$el.selectAll('path').attr('d', this.lineGenerator(curvePoints)); } public destroy () { this.detachEvents(); this.$el.remove(); this.$el = null; this.geditor = null; this.destSlotView = null; this.srcSlotView = null; this.destNodeView = null; this.srcNodeView = null; this.lineGenerator = null; } public hightlightStyle (toggle = true) { if (this.$el) { this.$el.classed('selected', toggle); } return this; } public weakStyle (toggle = true) { if (this.$el) { this.$el.attr('opacity', toggle ? 0.8 : null); } return this; } public copyEdgeConfig (replacement: IEdge) { return { ...this.edgeConfig, ...replacement, id: uuid.v4(), originalId: this.id }; } public getEditable () { return this.srcNodeView.getEditable() || this.destNodeView.getEditable(); } public handleSelected (options: {[key: string]: any;} = {}) { this.hightlightStyle(true).weakStyle(false); } public handleDeselected (options: {[key: string]: any} = { isSelectionsEmpty: true }) { this.hightlightStyle(false).weakStyle(false); } public updateEdgeStatus(status: 'INITIALIZED' | 'READY') { if (this.status !== status) { this.$path.style('stroke', status === 'INITIALIZED' ? '#a8b7c7' : '#358EDF'); this.status = status; } } public updateEdgeKey(key: string) { if (this.edgeKey !== key) { this.$edgeKey.text(key); this.edgeKey = key; } } }