import * as d3 from 'd3'; import * as uuid from 'uuid'; import { SLOT_TYPE, SLOT_UI, SLOT_CONNECT_AVTIVE_DISTANCE } from './constants'; import { ISlot } from './slot-view'; import { fromTransform, d3Selection } from './util'; import NodeView from './node-view'; import GraphView from './graph-view'; import TrackerView from './tracker-view'; import GEditor from './geditor'; export interface ISlot { id?: string; name?: string; [key: string]: any; } export interface InitialSettings { geditor: GEditor; graphView: GraphView; nodeView: NodeView; type: SLOT_TYPE; slotConfig: ISlot; } export default class SlotView { public $el: d3Selection; public type: SLOT_TYPE; protected graphView: GraphView; public id: string; public nodeView: NodeView; public slotConfig: ISlot; public geditor: GEditor; constructor (settings: InitialSettings) { const { geditor, nodeView, graphView, type, slotConfig } = settings; this.id = slotConfig.id || uuid.v4(); this.type = type; this.geditor = geditor; this.nodeView = nodeView; this.graphView = graphView; this.slotConfig = slotConfig; this.render(); this.initDragEvents(); } protected render () { this.$el = this.nodeView.$el.append('circle'); this.$el .attr('r', SLOT_UI.RADIUS) .classed('x-slot', true) .classed(`x-${this.type.toLowerCase()}-slot`, true); } protected initDragEvents () { let trackerView: TrackerView; let connectable: [SlotView]; let unconnectable: [SlotView]; let activeSlot: SlotView; const dragBehavior = d3.drag() .on('start', () => { if (!this.editable) return; trackerView = new TrackerView({ graphView: this.graphView, slotView: this, }); const connectivity = this.graphView.groupSlotsByConnectivity(this); connectable = connectivity.connectable; unconnectable = connectivity.unconnectable; connectable.forEach(slotView => slotView.connectableStyle()); unconnectable.forEach(slotView => slotView.unconnectableStyle()); }) .on('drag', () => { if (!this.editable) return; trackerView.render(d3.event.dx, d3.event.dy); activeSlot = trackerView.findConnectActiveSlot(connectable); }) .on('end', () => { if (!this.editable) return; trackerView.destory(); connectable.forEach(slotView => slotView.connectableStyle(false)); unconnectable.forEach(slotView => slotView.unconnectableStyle(false)); if (activeSlot) { activeSlot.activeStyle(false); const edgeSettings = { srcNodeId: this.nodeView.id, destNodeId: activeSlot.nodeView.id, srcSlotId: this.id, destSlotId: activeSlot.id, }; if (this.graphView.isSlotConnected(activeSlot)) { this.graphView.removeEdgeBySlotView(activeSlot); } this.graphView.createEdge(edgeSettings, this, activeSlot); } }); this.$el.call(dragBehavior); } public index () { const type = this.type; const INPUT_SLOT = SLOT_TYPE.INPUT_SLOT; const slotViewList = this.nodeView[type === INPUT_SLOT ? 'inputSlots' : 'outputSlots']; const index = slotViewList.findIndex(slotView => slotView === this); return index; } public get editable() { return true; } public updatePosition (cx: number, cy: number) { this.$el.attr('cx', cx).attr('cy', cy); } public getCoord () { const transform = this.nodeView.$el.attr('transform'); const translate = fromTransform(transform).translate; const cx = this.$el.attr('cx'); const cy = this.$el.attr('cy'); return { x: parseFloat(translate[0]) + parseFloat(cx), y: parseFloat(translate[1]) + parseFloat(cy), }; } public connectableStyle (toggle: boolean = true) { this.$el.classed('connectable', toggle); } public unconnectableStyle (toggle: boolean = true) { this.$el.classed('unconnectable', toggle); } public activeStyle (toggle: boolean = true) { this.$el.classed('active', toggle); } public connectedStyle (toggle: boolean = true) { this.$el.classed('connected', toggle); } public resetStyle () { this.$el .classed('active', false) .classed('connectable', false) .classed('unconnectable', false) .classed('connected', false); } public destroy () { this.$el.remove(); } }