import * as d3 from 'd3'; import GraphView from '../../graph-view'; import NodeView, { INode } from '../../node-view'; import { ISlot } from '../../slot-view'; import { NODE_STATUS, DATA_NODE_UI, NODE_TYPE, OPERATOR_NODE_UI } from '../constants'; import { toTransform, createMutilineTextWrap, createSVGElement, d3Selection } from '../../util'; import '../static/data-node-view.less'; const multilineTextWrap = createMutilineTextWrap(DATA_NODE_UI.TITLE_MAX_WIDTH); export default class DataNodeView extends NodeView { private $label: d3Selection; protected render (nodeConfig: INode) { const label = '逻辑'; // const label = this.getLabelFromConfig(nodeConfig); this.renderBody(); this.renderTitle(nodeConfig.title || nodeConfig.name); // this.renderLabel(label); this.reLayout(); this.renderLogicInputSlots(nodeConfig.inputSlots); this.renderLogicOutputSlots(nodeConfig.outputSlots); } private renderBody () { this.$body = this.$el.append('polygon'); this.$body .attr('points', "50,0 0,40 50,80 100,40") .style('fill', 'rgba(177, 137, 100, 0.5)') .classed('x-node-body', true); } private renderTitle (title: string) { if (!title) throw new Error(`nodeConfig missing either name or title field`); this.$title = this.$el.append('text'); this.$title .attr('text-anchor', 'middle') .attr('x', OPERATOR_NODE_UI.WIDTH / 2) .attr('y', 36) .text(title) .classed('x-node-title', true); } private renderLabel (label: string) { const LABEL_SIZE = DATA_NODE_UI.LABEL_SIZE; this.$label = this.$el .append('g') .classed('x-node-label', true); this.$label.append('rect') .attr('width', LABEL_SIZE) .attr('height',LABEL_SIZE) .attr('rx', LABEL_SIZE / 2) .attr('ry', LABEL_SIZE); this.$label.append('text') .attr('text-anchor', 'middle') .attr('x', LABEL_SIZE / 2) .attr('y', (LABEL_SIZE - 10) / 2) .attr('dy', LABEL_SIZE / 2) .attr('fill', '#fff') .text(label.slice(0, 1)) .classed('x-node-label', true); } private getLabelFromConfig (nodeConfig: INode) { let label = ''; switch (nodeConfig.type) { case NODE_TYPE.TABLE: label = '数据'; break; case NODE_TYPE.INSTANCE: label = '样本'; break; case NODE_TYPE.MODEL: label = '模型'; break; case NODE_TYPE.GRAPH: label = '图表'; break; default: break; } return label; } private reLayout () { this.$title.each(multilineTextWrap); const WIDTH = DATA_NODE_UI.WIDTH; const LABEL_SIZE = DATA_NODE_UI.LABEL_SIZE; const sTitle = this.$title.node() as SVGTextElement; const bbox = sTitle.getBBox(); const bodyWidth = WIDTH + DATA_NODE_UI.TITLE_SIDE_GAP * 2; const labelTransform = toTransform({ translate: [(bodyWidth - LABEL_SIZE) / 2, -1 * LABEL_SIZE / 2], }); this.$title.attr('x', bodyWidth / 2).selectAll('tspan').each(function () { d3.select(this).attr('x', bodyWidth / 2); }); // this.$label.attr('transform', labelTransform); this.$body .attr('width', bodyWidth) .attr('height', bbox.height + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2); } public destroy() { this.$label = null; super.destroy(); } }