import * as d3 from 'd3'; import * as uuid from 'uuid'; import GraphView from '../../graph-view'; import NodeView, { INode } from '../../node-view'; import { ISlot } from '../../slot-view'; import OutputCircleGroup from './output-circle-group'; import { NODE_STATUS, DATA_NODE_UI, OPERATOR_NODE_UI, EVENTS, NODE_STATUS_COLOR, NODE_TYPE } from '../constants'; import { toTransform, createTextWrap, d3Selection } from '../../util'; import statusIcons from '../static/icon/status'; import typeIcons from '../static/icon/type'; import commonIcons from '../static/icon/common'; import LifeCycleIcons from '../static/icon/lifecycle'; import '../static/operator-node-view.less'; import DagSlotView from '../slot/slot-view'; import Tooltip from '../../widget/tooltip'; const wrap = createTextWrap(160); const FUNCTION_WIDTH = 184; const FUNCTION_HEIGHT = 68; const FUNCIONT_LIFECYCLE_WIDTH = 4; export default class OperatorNodeView extends NodeView { private $avatar: d3Selection; private $status: d3Selection; private $statusBg: d3Selection; private $progress: d3Selection; private $gradient: d3Selection; private $runButton: d3Selection; private $handstatus: d3Selection; private $tag: d3Selection; private $tag1: d3Selection; private outputCircleGroup: OutputCircleGroup; private status: NODE_STATUS; private handStatus: boolean; private needManualDetermined: boolean; private errorTooltip: Tooltip; private $onPreStart: d3Selection; private $onCondition: d3Selection; private $onComplete: d3Selection; private $onError: d3Selection; private $onTimeout: d3Selection; private $type: d3Selection; public inputSlots: DagSlotView[]; public outputSlots: DagSlotView[]; protected render (nodeConfig: INode) { const status = nodeConfig.status || NODE_STATUS.UNCONFIGURE; const title = this.getTitleFromConfig(nodeConfig); this.status = status; this.renderBody(); this.renderTitle(title); this.renderProgress(); this.renderStatusBg(status); this.renderStatus(nodeConfig.status || NODE_STATUS.UNCONFIGURE); this.renderInputSlots(nodeConfig.inputSlots); this.renderOutputSlots(nodeConfig.outputSlots); // this.renderRunButton(); if (status === NODE_STATUS.RUNNING) { this.updateProgress(nodeConfig.progress); } // SENSOR 节点 && 需主动触发 // nodeConfig.needManualDetermined = true; // nodeConfig.determinedStatus = true; this.renderRunIcon(nodeConfig); // if (nodeConfig.type === NODE_TYPE.SENSOR) { // this.updateHandStatus(false); // } this.renderLifeCycles(nodeConfig); this.initEvents(); } private renderBody () { this.$body = this.$el.append('rect'); this.$body .attr('x', 0) .attr('y', 0) .attr('width', DATA_NODE_UI.WIDTH + 34) .attr('height', 12 + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2) .attr('rx', 5) .attr('ry', 5) .style('fill', '#fff') .classed('x-node-body', true); } private renderTitle (title: string) { this.$title = this.$el.append('text'); this.$title .attr('text-anchor', 'left') .attr('x', 36) .attr('y', 24) .text(title).each(wrap) .classed('x-node-title', true); const bodyHeight = OPERATOR_NODE_UI.HEIGHT; const statusSize = OPERATOR_NODE_UI.STATUS_SIZE; this.$type = this.$el.append('image'); this.$type .attr('xlink:href', typeIcons[this.nodeConfig.indexType.toLowerCase()]) .attr('width', 18) .attr('height', 18) .attr('x', 12) .attr('y', 10) .classed('x-node-status', true); if (this.nodeConfig.type === NODE_TYPE.SENSOR) { this.$tag = this.$el.append('rect'); this.$tag .attr('width', 60) .attr('height', 24) .attr('x', 18) .attr('y', 32) .attr('fill', 'rgba(255, 171, 0, 0.1)'); this.$el .append('text') .text('SENSOR') .attr('x', 22) .attr('y', 48) .attr('fill', 'rgb(255, 171, 0)') .attr('style', 'font-size: 12px'); } if (!this.nodeConfig.resourceDependency) return; this.$tag1 = this.$el.append('rect'); this.$tag1 .attr('width', 132) .attr('height', 24) .attr('x', this.nodeConfig.type === NODE_TYPE.SENSOR ? 81 : 12) // .attr('y', this.nodeConfig.type === NODE_TYPE.SENSOR ? 32 : 36) .attr('y', 32) .attr('fill', '#f0f5ff'); this.$el .append('text') .text(`${this.nodeConfig.resourceDependency.group}:${this.nodeConfig.resourceDependency.key}@${this.nodeConfig.resourceDependency.version}`).each(wrap) .attr('x', this.nodeConfig.type === NODE_TYPE.SENSOR ? 84 : 18) .attr('y', 48) .attr('fill', '#2f54eb') .attr('style', 'font-size: 12px'); } private renderStatus (status: NODE_STATUS) { const bodyWidth = OPERATOR_NODE_UI.WIDTH; const bodyHeight = OPERATOR_NODE_UI.HEIGHT; const statusSize = OPERATOR_NODE_UI.STATUS_SIZE; this.$status = this.$el.append('image'); this.$status .attr('xlink:href', statusIcons[status.toLowerCase()]) .attr('width', 14) .attr('height', 14) .attr('x', 162) .attr('y', (bodyHeight - statusSize) / 2) .classed('x-node-status', true); } private renderRunIcon(nodeConfig: INode) { const bodyWidth = OPERATOR_NODE_UI.WIDTH; const bodyHeight = OPERATOR_NODE_UI.HEIGHT; const statusSize = OPERATOR_NODE_UI.STATUS_SIZE; this.$handstatus = this.$el.append('image'); this.$handstatus .attr('width', 18) .attr('height', 18) .attr('x', 124) .attr('y', 22); if (nodeConfig.type === NODE_TYPE.SENSOR && nodeConfig.needManualDetermined) { this.$handstatus.attr('xlink:href', nodeConfig.determinedStatus ? commonIcons.hand : commonIcons.handinit); } } 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'); 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 - 2) .attr('height', 0) .attr('x', DATA_NODE_UI.WIDTH + 5) // .attr('clip-path', `url(#${clipPathId})`) .attr('fill', `url(#${gradientId})`); } private renderStatusBg(status: NODE_STATUS) { this.$statusBg = this.$el.append('rect'); this.$statusBg .attr('x', DATA_NODE_UI.WIDTH + 5) .attr('y', 1) .attr('width', DATA_NODE_UI.WIDTH / 5 - 2) .attr('height', 12 + DATA_NODE_UI.TITLE_VERTICAL_GAP * 2 - 2) .attr('rx', 0) .attr('ry', 0) .style('fill', NODE_STATUS_COLOR[status]) .classed('x-node-status', true); } private renderLifeCycles (nodeConfig: INode) { const postPostPostion = [FUNCTION_WIDTH / 2 - 15, FUNCTION_WIDTH / 2 + 5, FUNCTION_WIDTH / 2 + 25]; if (nodeConfig.onPreStart) { this.renderPreCondition(nodeConfig, 'onPreStart'); } if (nodeConfig.onCondition) { this.renderPreCondition(nodeConfig, 'onCondition'); } if (nodeConfig.onTimeout) { this.renderPostCondition('onTimeout'); } if (nodeConfig.onComplete) { this.renderPostCondition('onComplete'); } if (nodeConfig.onError) { this.renderPostCondition('onError'); } } private renderPreCondition (nodeConfig: INode, type: string) { const calculator = this.getSlotPositionCalculator(nodeConfig.inputSlots.length); const SlotPostionMap:{[key:string]: number} = { onPreStart: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 10), onCondition: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 10), }; switch (type) { case 'onPreStart': this.$onPreStart = this.$el.append('circle'); this.$onPreStart .attr('r', FUNCIONT_LIFECYCLE_WIDTH) .attr('fill', '#358EDF') .attr('cx', SlotPostionMap[type]) .attr('cy', 12); break; case 'onCondition': this.$onCondition = this.$el.append('image'); this.$onCondition .attr('xlink:href', LifeCycleIcons.onCondition) .attr('width', OPERATOR_NODE_UI.AVATAR_SIZE) .attr('height', OPERATOR_NODE_UI.AVATAR_SIZE) .attr('x', calculator(1) - 8) .attr('y', -25) .classed('x-node-avatar', true); this.$onCondition.raise(); } } private renderPostCondition (type: string) { const SlotPostionMap:{[key:string]: number} = { onTimeout: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 10) - 30, onError: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 10) - 15, onComplete: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 10), }; switch (type) { case 'onTimeout': this.$onTimeout = this.$el.append('circle'); this.$onTimeout .attr('r', FUNCIONT_LIFECYCLE_WIDTH) .attr('fill', '#fa8c16') .attr('cx', SlotPostionMap[type]) .attr('cy', FUNCTION_HEIGHT - 12); break; case 'onError': this.$onError = this.$el.append('circle'); this.$onError .attr('r', FUNCIONT_LIFECYCLE_WIDTH) .attr('fill', '#D63B63') .attr('cx', SlotPostionMap[type]) .attr('cy', FUNCTION_HEIGHT - 12); break; case 'onComplete': this.$onComplete = this.$el.append('circle'); this.$onComplete .attr('r', FUNCIONT_LIFECYCLE_WIDTH) .attr('fill', '#6AC68C') .attr('cx', SlotPostionMap[type]) .attr('cy', FUNCTION_HEIGHT - 12); break; } } private getTitleFromConfig (nodeConfig: INode) { return nodeConfig.alias || nodeConfig.title || nodeConfig.name; } private runable () { const status = [ NODE_STATUS.UNCONFIGURE, NODE_STATUS.RUNNING, NODE_STATUS.AWAITING, NODE_STATUS.RESUMING, NODE_STATUS.SUCCEEDED, ]; return !status.includes(this.status); } private showErrorTooltip() { if ( this.status === NODE_STATUS.UNCONFIGURE || this.status === NODE_STATUS.FAILED ) { const getNodeErrorMsg = this.geditor.methods.getNodeErrorMsg; let msg = ''; if (typeof getNodeErrorMsg === 'function') { try { msg = getNodeErrorMsg(this.id); } catch (ex) { console.error('Uncaught Error in getNodeErrorMsg', ex); } finally { msg = msg || '未知错误'; } } else if (this.nodeConfig.errorMsg) { msg = this.nodeConfig.errorMsg; } else if (this.status === NODE_STATUS.UNCONFIGURE) { msg = '配置错误'; } else if (this.status === NODE_STATUS.FAILED) { msg = '运行失败'; } this.errorTooltip = new Tooltip({ target: this.$status.node() as SVGElement, html: msg, className: 'x-operator-node-tip', }); this.errorTooltip.show(); } } private hideErrorTooltip() { if (this.errorTooltip) { this.errorTooltip.destroy(); this.errorTooltip = null; } } protected initEvents () { super.initEvents(); this.$handstatus .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.RUN_BUTTON_CLICKED, this.id); }); // this.$el // .on('mouseenter', () => { // if (this.getEditable() && this.runable()) { // this.$el.classed('mouseover', true); // } // }) // .on('mouseleave', () => { // this.$el.classed('mouseover', false); // }); // this.$runButton // .on('click', () => { // d3.event.stopPropagation(); // this.geditor.emit(EVENTS.RUN_BUTTON_CLICKED, this.id); // }); // this.$status // .on('mouseover', this.showErrorTooltip.bind(this)) // .on('mouseout', this.hideErrorTooltip.bind(this)); } protected detachEvents() { this.$el.on('mouseenter', null) .on('mouseleave', null); this.$handstatus.on('click', null); this.$status .on('mouseover', null) .on('mouseout', null); super.detachEvents(); } public handleSelected (options: any) { super.handleSelected(options); if (!options.isBatchSelection) { // this.showOutputCircleGroup(); } } public handleDeselected () { super.handleDeselected(); if (this.outputCircleGroup) { // this.hideOutputCircleGroup(); } } public handleMovement () { if (this.outputCircleGroup) { // this.hideOutputCircleGroup(); } } private showOutputCircleGroup () { const translate = this.getTranslate(); const bodySize = this.getBodySize(); this.outputCircleGroup = new OutputCircleGroup({ geditor: this.geditor, graphView: this.graphView, nodeView: this, slotViews: this.outputSlots, position: [ translate[0] + bodySize.width, translate[1] + bodySize.height / 2, ], }); } private hideOutputCircleGroup () { this.outputCircleGroup.destroy(); this.outputCircleGroup = null; } /** * @param {string} title * @memberof OperatorNodeView */ public updateTitle (title: string) { this.$title.text(title).attr('title', title).each(wrap); } /** * 切换智能助跑 badge */ public updateAutoConfig (autoConfig: boolean) { // @TODO } /** * @param {number} [progress=0] * @memberof OperatorNodeView */ public updateProgress (progress = 0) { const text = Math.round(progress * 100) + '%'; const HEIGHT = OPERATOR_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 */ public updateStatus (status = NODE_STATUS.UNCONFIGURE) { if (this.status === status) return; this.status = status; this.$status.attr('xlink:href', statusIcons[status.toLowerCase()]); this.$statusBg.style('fill', NODE_STATUS_COLOR[status]); if (status !== NODE_STATUS.RUNNING) { this.$progress.text(null).classed('x-visible', false); this.$gradient.attr('width', 0); } } /** * @param {LIFE_CYCLE, LIFE_CYCLE_STATUS} * @memberof FunctionOperatorNodeView */ public updateLifeCycle(lifeCycleKey:string, status:boolean) { if (lifeCycleKey === 'onPreStart') { if (status && !this.$onPreStart) { this.renderPreCondition(this.nodeConfig, 'onPreStart'); } if (!status && this.$onPreStart) { this.$onPreStart.style('display', 'none'); } if (status && this.$onPreStart) { this.$onPreStart.style('display', 'block'); } return; } if (lifeCycleKey === 'onCondition') { if (status && !this.$onCondition) { this.renderPreCondition(this.nodeConfig, 'onCondition'); } if (!status && this.$onCondition) { this.$onCondition.style('display', 'none'); } if (status && this.$onCondition) { this.$onCondition.style('display', 'block'); } return; } if (lifeCycleKey === 'onTimeout') { if (status && !this.$onTimeout) { this.renderPostCondition('onTimeout'); } if (!status && this.$onTimeout) { this.$onTimeout.style('display', 'none'); } if (status && this.$onTimeout) { this.$onTimeout.style('display', 'block'); } return; } if (lifeCycleKey === 'onComplete') { if (status && !this.$onComplete) { this.renderPostCondition('onComplete'); } if (!status && this.$onComplete) { this.$onComplete.style('display', 'none'); } if (status && this.$onComplete) { this.$onComplete.style('display', 'block'); } return; } if (lifeCycleKey === 'onError') { if (status && !this.$onError) { this.renderPostCondition('onError'); } if (!status && this.$onError) { this.$onError.style('display', 'none'); } if (status && this.$onError) { this.$onError.style('display', 'block'); } return; } } public updateHandStatus (determinedStatus: boolean) { if (this.handStatus === determinedStatus) return; this.handStatus = determinedStatus; this.$handstatus.attr('xlink:href', determinedStatus ? commonIcons.hand : commonIcons.handinit); } public updateHandInit (needManualDetermined: boolean) { if (this.needManualDetermined === needManualDetermined) return; this.needManualDetermined = needManualDetermined; this.$handstatus.attr('xlink:href', needManualDetermined ? commonIcons.handinit : null); } public updateStatusBG(status = NODE_STATUS.UNCONFIGURE) { if (this.status === status) return; this.status = status; this.$statusBg.style('fill', NODE_STATUS_COLOR[status]); } public destroy () { if (this.outputCircleGroup) { this.hideOutputCircleGroup(); } super.destroy(); this.$runButton = null; } }