import * as d3 from 'd3'; import GraphView from '../../graph-view'; import NodeView, { INode } from '../../node-view'; import { ISlot } from '../../slot-view'; import * as uuid from 'uuid'; import { NODE_STATUS, DATA_NODE_UI, NODE_TYPE, NODE_STATUS_COLOR } from '../constants'; import { toTransform, createMutilineTextWrap, createSVGElement, d3Selection } from '../../util'; import statusIcons from '../static/icon/status'; import '../static/data-node-view.less'; const multilineTextWrap = createMutilineTextWrap(DATA_NODE_UI.TITLE_MAX_WIDTH); export default class TaskNodeView extends NodeView { private $label: d3Selection; private $status: d3Selection; private $statusIcon: d3Selection; private $progress: d3Selection; private $gradient: d3Selection; private status: NODE_STATUS; protected render (nodeConfig: INode) { const status = nodeConfig.status || NODE_STATUS.UNCONFIGURE; // const label = this.getLabelFromConfig(nodeConfig); const label = '任务'; this.status = status; this.renderBody(); this.renderTitle(nodeConfig.title || nodeConfig.name); this.renderStatus(nodeConfig); this.renderProgress(); // this.renderLabel(label); this.reLayout(); this.renderOutputSlots(nodeConfig.outputSlots); this.renderInputSlots(nodeConfig.inputSlots); if (status === NODE_STATUS.RUNNING) { this.updateProgress(nodeConfig.progress); } this.initEvents(); } protected initEvents () { super.initEvents(); } private renderBody () { this.$body = this.$el.append('rect'); this.$body .attr('x', 0) .attr('y', 0) .attr('width', DATA_NODE_UI.WIDTH) .attr('height', 12 + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2) .attr('rx', 0) .attr('ry', 0) .style('fill', '#fff') .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', 36) .attr('y', 24) .text(title) .classed('x-node-title', true); } private renderStatus(nodeConfig: any) { nodeConfig.status = nodeConfig.status || 'UNCONFIGURE' this.$status = this.$el.append('rect'); this.$status .attr('x', DATA_NODE_UI.WIDTH + 5) .attr('y', 1) .attr('width', DATA_NODE_UI.WIDTH / 5) .attr('height', 12 + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2) .attr('rx', 0) .attr('ry', 0) .style('fill', NODE_STATUS_COLOR[nodeConfig.status]) .classed('x-node-status', true); this.$statusIcon = this.$el.append('image'); this.$statusIcon .attr('xlink:href', statusIcons[nodeConfig.status.toLowerCase()]) .attr('width', 14) .attr('height', 14) .attr('x', 162) .attr('y', DATA_NODE_UI.TITLE_VERTICAL_GAP) .classed('x-node-status', true); } private renderProgress () { const { WIDTH, HEIGHT } = DATA_NODE_UI; const gradientId = uuid.v4(); const clipPathId = uuid.v4(); const $defs = this.$el.append('defs'); const $gradient = $defs.append('linearGradient').attr('id', gradientId); $gradient.append('stop').attr('offset', '0%').attr('stop-color', '#e8f7f1'); $gradient.append('stop').attr('offset', '100%').attr('stop-color', '#cdeddf'); // $defs.append('clipPath') // .attr('id', clipPathId) // .append('rect') // .attr('width', WIDTH) // .attr('height', HEIGHT) // .attr('rx', HEIGHT / 2) // .attr('ry', HEIGHT / 2); this.$progress = this.$el .append('text') .text('0%') .attr('x', WIDTH - DATA_NODE_UI.ICON_SIDE_GAP - DATA_NODE_UI.PROGRESS_TEXT_SIZE) .attr('y', 4) .attr('dy', DATA_NODE_UI.HEIGHT / 2) .classed('x-node-progress', true); this.$gradient = this.$el .insert('rect', '.x-node-title') .attr('width', DATA_NODE_UI.WIDTH / 5) .attr('height', 0) .attr('x', DATA_NODE_UI.WIDTH + 5) // .attr('clip-path', `url(#${clipPathId})`) .attr('fill', `url(#${gradientId})`); } 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', 42).selectAll('tspan').each(function () { d3.select(this).attr('x', 42); }); // this.$label.attr('transform', labelTransform); this.$body .attr('width', bodyWidth) .attr('height', bbox.height + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2); } /** * @param {number} [progress=0] * @memberof OperatorNodeView */ public updateProgress (progress = 0) { const text = Math.round(progress * 100) + '%'; const HEIGHT = DATA_NODE_UI.HEIGHT; const sBody = this.$body.node() as SVGTextElement; const bodyBox = sBody.getBBox(); this.$progress.text(text).classed('x-visible', true); this.$gradient.attr('height', HEIGHT * progress); this.$gradient.attr('y', bodyBox.height - HEIGHT * progress - 1); } /** * @param {NODE_STATUS} [status=NODE_STATUS.UNCONFIGURE] * @memberof OperatorNodeView */ /** * @param {NODE_STATUS} [status=NODE_STATUS.UNCONFIGURE] * @memberof OperatorNodeView */ public updateStatus (status = NODE_STATUS.UNCONFIGURE) { if (this.status === status) return; this.status = status; this.$status.attr('xlink:href', statusIcons[status.toLowerCase()]); if (status !== NODE_STATUS.RUNNING) { this.$progress.text(null).classed('x-visible', false); this.$gradient.attr('width', 0); } } public destroy() { this.$label = null; super.destroy(); } }