import * as d3 from 'd3'; import NodeView, { INode } from '../../node-view'; import { NODE_STATUS, OPERATOR_NODE_UI, EVENTS } from '../constants'; import { createTextWrap, d3Selection } from '../../util'; import statusIcons from '../static/icon/status'; import commonIcons from '../static/icon/common'; import lifeCycleIcons from '../static/icon/lifecycle'; import DagSlotView from '../slot/slot-view'; import '../static/operator-node-view.less'; const FUNCTION_WIDTH = 140; const FUNCTION_HEIGHT = 40; const FUNCIONT_LIFECYCLE_WIDTH = 4; const GROUP_BG_COLOR_LIST1 = ['#61a0a8', '#d48265', '#358EDF', '#ca8622', '#bda29a', '#6e7074']; const wrap = createTextWrap(80); export default class FunctionNodeView extends NodeView { private $avatar: d3Selection; private $status: d3Selection; private $onStart: d3Selection; private $onCondition: d3Selection; private $onComplete: d3Selection; private $onError: d3Selection; private $onTimeout: d3Selection; private status: NODE_STATUS; 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(nodeConfig); this.renderTitle(title); this.renderAvatar(nodeConfig.type); this.renderStatus(nodeConfig.status || NODE_STATUS.UNCONFIGURE); this.renderInputSlots(nodeConfig.inputSlots); this.renderOutputSlots(nodeConfig.outputSlots); this.renderLifeCycles(nodeConfig); this.initEvents(); } private renderBody (nodeConfig: INode) { this.$body = this.$el.append('rect'); this.$body .attr('x', 0) .attr('y', 0) .attr('width', FUNCTION_WIDTH) .attr('height', FUNCTION_HEIGHT) .attr('rx', 5) .attr('ry', 5) .attr('stroke-width', 1) .style('stroke', nodeConfig.group ? GROUP_BG_COLOR_LIST1[nodeConfig.groupIndex] : '#9fa7b1') .style('fill', '#fff') .classed('x-node-body-function', true); } private renderTitle (title: string) { this.$title = this.$el.append('text'); this.$title .attr('text-anchor', 'left') .attr('x', 40) .attr('y', 24) .text(title).each(wrap) .classed('x-node-title', true); } protected getSlotPositionCalculator (slotCount: number) { const width = this.getBodySize().width; return d3.scaleLinear().domain([0, slotCount + 1]).range([0, width]); } private renderLifeCycles (nodeConfig: INode) { if (nodeConfig.onStart.function) { this.renderPreCondition(nodeConfig, 'onStart'); } if (nodeConfig.onCondition.function) { this.renderPreCondition(nodeConfig, 'onCondition'); } if (nodeConfig.onTimeout.function) { this.renderPostCondition('onTimeout'); } if (nodeConfig.onComplete.function) { this.renderPostCondition('onComplete'); } if (nodeConfig.onError.function) { this.renderPostCondition('onError'); } } private renderPreCondition (nodeConfig: INode, type: string) { const SlotPostionMap:{[key:string]: number} = { onStart: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 4), onCondition: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 4), }; switch (type) { case 'onStart': this.$onStart = this.$el.append('circle'); this.$onStart .attr('r', FUNCIONT_LIFECYCLE_WIDTH) .attr('fill', '#358EDF') .attr('cx', SlotPostionMap[type]) .attr('cy', 7); 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', FUNCTION_WIDTH / 2 - 10) .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 * 4) - 30, onError: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 4) - 15, onComplete: FUNCTION_WIDTH - (FUNCIONT_LIFECYCLE_WIDTH * 4), }; 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 - 7); 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 - 7); 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 - 7); break; } } private renderAvatar (type: string) { this.$avatar = this.$el.append('image'); this.$avatar .attr('xlink:href', type === 'FUNCTION' ? commonIcons.functionimg : commonIcons[type.toLocaleLowerCase()]) .attr('width', OPERATOR_NODE_UI.AVATAR_SIZE) .attr('height', OPERATOR_NODE_UI.AVATAR_SIZE) .attr('x', OPERATOR_NODE_UI.ICON_SIDE_GAP) .attr('y', 12) .classed('x-node-avatar', true); } private renderStatus (status: NODE_STATUS) { const bodyHeight = FUNCTION_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', FUNCTION_WIDTH - 24) .attr('y', (bodyHeight - statusSize) / 2) .classed('x-node-status', true); } private getTitleFromConfig (nodeConfig: INode) { return nodeConfig.alias || nodeConfig.title || nodeConfig.name; } protected initEvents () { super.initEvents(); if (this.$onStart) { this.$onStart .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.PRE_START_CLICKED, this.id, 'onStart'); }); } if (this.$onComplete) { this.$onComplete .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.COMPLETE_CLICKED, this.id, 'onComplete'); }); } if (this.$onError) { this.$onError .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.ERROR_CLICKED, this.id, 'onError'); }); } if (this.$onTimeout) { this.$onTimeout .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.TIMTOUT_CLICKED, this.id, 'onTimeout'); }); } if (this.$onCondition) { this.$onCondition .on('click', () => { d3.event.stopPropagation(); this.geditor.emit(EVENTS.CONDITION_CLICKED, this.id, 'onCondition'); }); } } protected detachEvents() { super.detachEvents(); } public handleSelected (options: any) { super.handleSelected(options); } public handleDeselected () { super.handleDeselected(); } /** * @param {string} title * @memberof OperatorNodeView */ public updateTitle (title: string) { this.$title.text(title).attr('title', title).each(wrap); } /** * @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()]); } /** * @param {LIFE_CYCLE, LIFE_CYCLE_STATUS} * @memberof FunctionOperatorNodeView */ public updateLifeCycle(lifeCycleKey:string, status:boolean) { if (lifeCycleKey === 'onStart') { if (status && !this.$onStart) { this.renderPreCondition(this.nodeConfig, 'onStart'); } if (!status && this.$onStart) { this.$onStart.style('display', 'none'); } if (status && this.$onStart) { this.$onStart.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 destroy () { super.destroy(); } }