import * as d3 from 'd3'; import DagSlotView from '../slot/slot-view'; import OutputCircleGroup from './output-circle-group'; import { d3Selection } from '../../util'; import { EVENTS, OUTPUT_CIRCLE_GROUP_UI, SLOT_ELEMENT_TYPE, SLOT_ELEMENT_NAME, } from '../constants'; import '../static/output-circle.less'; import GEditor from '../../geditor'; import SlotView from '../../slot-view'; import NodeView from '../node/operator-node-view'; const { RADIUS } = OUTPUT_CIRCLE_GROUP_UI; export interface InitialSettings { geditor: GEditor; outputExists: boolean; groupView: OutputCircleGroup; nodeView: NodeView; slotView: DagSlotView; position: number[]; } export default class CircleView { private $el: d3Selection; private $circle: d3Selection; private geditor: GEditor; private groupView: OutputCircleGroup; private slotView: SlotView; private nodeView: NodeView; private position: InitialSettings['position']; private outputExists: boolean; private elementType: SLOT_ELEMENT_TYPE; constructor(settings: InitialSettings) { const { outputExists, geditor, groupView, slotView, nodeView, position } = settings; this.geditor = geditor; this.groupView = groupView; this.slotView = slotView; this.nodeView = nodeView; this.position = position; this.outputExists = outputExists; this.elementType = slotView.slotConfig.elementType; this.$el = groupView.$el .append('g') .classed('x-output-circle', true) .classed('x-output-exists', outputExists); this.render(); this.initEvents(); } private render() { const [x, y] = this.position; const r = 12; this.$circle = this.$el .append('circle') .attr('cx', x) .attr('cy', y) .attr('r', RADIUS); const $detail = this.$el.append('g').classed('x-output-detail', true); $detail.append('rect') .attr('width', 68) .attr('height', r * 2) .attr('x', x - r) .attr('y', y - r) .attr('rx', r) .attr('ry', r); $detail.append('text') .attr('x', x + r) .attr('y', 4) .attr('dy', y) .text(SLOT_ELEMENT_NAME[this.elementType]); this.$el.append('image') .attr('width', r) .attr('height', r) .attr('x', x - r / 2) .attr('y', y - r / 2); } private onClick = (params: any) => { const { outputCircleView } = params; if (outputCircleView === this) { this.hoveredStyle(false).selectedStyle(true); } else { this.hoveredStyle(false).selectedStyle(false); } } private initEvents() { this.$el .on('mouseenter', () => this.hoveredStyle(true)) .on('mouseleave', () => this.hoveredStyle(false)) .on('click', () => { d3.event.stopPropagation(); this.geditor.emit( EVENTS.OUTPUT_CIRCLE_CLICKED, { nodeId: this.nodeView.id, slotIndex: this.nodeView.getSlotIndexById(this.slotView.id), slotId: this.slotView.id, outputCircleView: this, }, ); }); this.geditor.on(EVENTS.OUTPUT_CIRCLE_CLICKED, this.onClick); } private detachEvents() { this.$el .on('mouseenter', null) .on('mouseleave', null) .on('click', null); this.geditor.removeListener(EVENTS.OUTPUT_CIRCLE_CLICKED, this.onClick); } public hoveredStyle(toggle = true) { this.$el.classed('x-output-circle-hover', toggle); return this; } public selectedStyle(toggle = true) { this.$el.classed('x-output-circle-select', toggle); return this; } public destroy() { this.detachEvents(); this.position = null; this.$el.remove(); this.$el = null; this.groupView = null; this.geditor = null; } }