import * as d3 from 'd3'; import * as uuid from 'uuid'; import * as invariant from 'invariant'; import { NODE_UI, SLOT_TYPE, SLOT_CONNECT_AVTIVE_DISTANCE, EVENTS } from './constants'; import { toTransform, createSVGElement, d3Selection, fromTransform } from './util'; import GEditor from './geditor'; import GraphView from './graph-view'; import SlotView, { ISlot } from './slot-view'; import { NODE_TYPE } from './dag-editor/constants'; export interface INode { id?: string; type?: any; name?: string; title?: string; editable?: boolean; inputSlots?: ISlot[]; outputSlots?: ISlot[]; [key: string]: any; } export interface InitialSettings { geditor: GEditor; graphView: GraphView; nodeConfig: INode; translate: [number, number]; } export default class NodeView { public readonly id: string; public title: string; public $el: d3Selection; protected $body: d3Selection; protected $title: d3Selection; protected translate: [number, number]; protected editable: boolean; protected graphView: GraphView; protected geditor: GEditor; protected nodeConfig: INode; public inputSlots = []; public outputSlots = []; constructor (settings: InitialSettings) { const { graphView, nodeConfig, geditor, translate } = settings; const type = nodeConfig.type ? nodeConfig.type.toLowerCase() : false; this.id = nodeConfig.id || uuid.v4(); this.nodeConfig = nodeConfig; this.editable = typeof nodeConfig.editable === 'undefined' ? true : nodeConfig.editable; this.geditor = geditor; this.graphView = graphView; this.$el = graphView.$el.append('g') .attr('transform', toTransform({ translate })) .classed('x-node', true); if (type) { this.$el.classed(`x-${type}-node`, true); } this.render(nodeConfig); this.initEvents(); } protected render (nodeConfig: INode) { } protected initEvents () { const dragBehavior = d3.drag() .on('start', () => this.onDragStart()) .on('drag', () => this.onDrag()) .on('end', () => this.onDragEnd()); this.$el.call(dragBehavior); this.$el .on('click', () => this.onClick()) .on('contextmenu', () => this.onContextmenu()); } protected detachEvents () { this.$el .on('mousedown.drag', null) .on('click', null) .on('contextmenu', null); } protected onDragStart () { if (!this.getEditable()) return; } protected onDrag () { if (!this.getEditable()) return; const { dx, dy } = d3.event; const graphView = this.graphView; if (graphView.isEntitySelected(this)) { graphView.moveSelections(dx, dy); } else { this.move(dx, dy); graphView.updateRelatedEdgesPosition(this); } } protected onDragEnd () { if (!this.getEditable()) return; } protected onClick () { d3.event.stopPropagation(); if (d3.event.shiftKey) { if (this.graphView.isSelectionExsits(this.id)) { this.graphView.removeSelectionId(this.id); return; } this.graphView.appendSelectionIds(this.id); } else { this.graphView.resetSelectionIds(this.id); } } protected onContextmenu () { d3.event.preventDefault(); d3.event.stopPropagation(); this.graphView.resetSelectionIds(this.id); this.geditor.emit(EVENTS.NODE_CONTEXTMENU, this.id, d3.event); } protected getBodySize () { const body = this.$body.node() as SVGRectElement; const bbox = body.getBBox(); return { width: bbox.width || NODE_UI.WIDTH, height: bbox.height || NODE_UI.HEIGHT, }; } protected getSlotPositionCalculator (slotCount: number) { const width = this.getBodySize().width; return d3.scaleLinear().domain([0, slotCount + 1]).range([0, width]); } protected renderInputSlots (inputSlots: ISlot[]) { if (!inputSlots || !inputSlots.length) return; const height = this.getBodySize().height; const calculator = this.getSlotPositionCalculator(inputSlots.length); inputSlots.forEach((slotConfig, index) => { const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const slotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.INPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); const cx = calculator(index + 1); slotView.updatePosition(cx, 0); this.inputSlots.push(slotView); }); } protected renderLogicInputSlots (inputSlots: ISlot[]) { if (!inputSlots || !inputSlots.length) return; // const width = this.getBodySize().width; const height = this.getBodySize().height; const calculator = this.getSlotPositionCalculator(inputSlots.length); inputSlots.forEach((slotConfig, index) => { const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const slotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.INPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); const cx = calculator(index + 1); slotView.updatePosition(cx, 0); this.inputSlots.push(slotView); }); } protected renderOutputSlots (outputSlots: ISlot[]) { if (!outputSlots || !outputSlots.length) return; const bodySize = this.getBodySize(); const width = bodySize.width; const height = bodySize.height; const calculator = this.getSlotPositionCalculator(outputSlots.length); outputSlots.forEach((slotConfig, index) => { const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const slotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.OUTPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); const cx = calculator(index + 1); // slotView.updatePosition(width, height/2); slotView.updatePosition(cx, height); this.outputSlots.push(slotView); }); } protected renderLogicOutputSlots (outputSlots: ISlot[]) { if (!outputSlots || !outputSlots.length) return; const bodySize = this.getBodySize(); const { width, height } = bodySize; const calculator = this.getSlotPositionCalculator(outputSlots.length); outputSlots.forEach((slotConfig, index) => { const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const slotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.OUTPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); const cxList = [{ cx: width / 2,cy: height }, { cx: 0, cy: height / 2 }, { cx: width, cy: height / 2 }]; const cx = calculator(index + 1); const position = cxList[index]; slotView.updatePosition(position.cx, position.cy); this.outputSlots.push(slotView); }); } public appendOutputSlot (slotConfig: ISlot) { invariant(this.getEditable(), `Can not append output slot when the editable is false`); const count = this.outputSlots.length + 1; const height = this.getBodySize().height; const calculator = this.getSlotPositionCalculator(count); this.outputSlots.forEach((slotView, index) => { const cx = calculator(index + 1); slotView.updatePosition(cx, height); }); const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const newSlotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.OUTPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); newSlotView.updatePosition(calculator(count), height); this.graphView.updateRelatedEdgesPosition(this); this.outputSlots.push(newSlotView); const config = { id: newSlotView.id, ...slotConfig, }; this.geditor.emit(EVENTS.OUTPUT_SLOT_CREATED, this.id, config); } public appendInputSlot (slotConfig: ISlot) { invariant(this.getEditable(), `Can not append input slot when the editable is false`); const count = this.inputSlots.length + 1; const calculator = this.getSlotPositionCalculator(count); this.inputSlots.forEach((slotView, index) => { const cx = calculator(index + 1); slotView.updatePosition(cx, 0); }); const slotViewClass = this.geditor.getSlotViewClass(slotConfig); const newSlotView = new slotViewClass({ slotConfig, type: SLOT_TYPE.INPUT_SLOT, geditor: this.geditor, nodeView: this, graphView: this.graphView, }); newSlotView.updatePosition(calculator(count), 0); this.graphView.updateRelatedEdgesPosition(this); this.inputSlots.push(newSlotView); const config = { id: newSlotView.id, ...slotConfig, }; this.geditor.emit(EVENTS.INPUT_SLOT_CREATED, this.id, config); } public removeOutputSlotByIndex (index: number) { invariant(this.getEditable(), `Can not remove output slot when the editable is false`); const removedSlotView = this.outputSlots.splice(index, 1)[0]; const count = this.outputSlots.length; const height = this.getBodySize().height; const calculator = this.getSlotPositionCalculator(count); this.graphView.removeEdgeBySlotView(removedSlotView); removedSlotView.destroy(); this.outputSlots.forEach((slotView, index) => { const cx = calculator(index + 1); slotView.updatePosition(cx, height); }); this.graphView.updateRelatedEdgesPosition(this); const data = { index, id: removedSlotView.id, }; this.geditor.emit(EVENTS.OUTPUT_SLOT_REMOVED, this.id, data); } public removeInputSlotByIndex (index: number) { invariant(this.getEditable(), `Can not remove input slot when the editable is false`); const removedSlotView = this.inputSlots.splice(index, 1)[0]; const count = this.inputSlots.length; const calculator = this.getSlotPositionCalculator(count); this.graphView.removeEdgeBySlotView(removedSlotView); removedSlotView.destroy(); this.inputSlots.forEach((slotView, index) => { const cx = calculator(index + 1); slotView.updatePosition(cx, 0); }); this.graphView.updateRelatedEdgesPosition(this); const data = { index, id: removedSlotView.id, }; this.geditor.emit(EVENTS.INPUT_SLOT_REMOVED, this.id, data); } public highlightStyle (toggle = true) { this.$el.classed('select', toggle); return this; } public weakStyle (toggle = true) { this.$el.attr('opacity', toggle ? 0.8 : null); return this; } public getEditable () { return this.editable && this.graphView.getEditable(); } public setEditable (editable = true) { this.editable = editable; } /** * Method called by graphView when the Node being selected. * Subclass can extends this method to perform custom logic. */ public handleSelected (options: {[key: string]: any;} = { isBatchSelection: false }) { this.highlightStyle().weakStyle(false); } /** * Method called by graphView, when the node deselected. * SubClass can extends this method to perform custom logic. * Such as hide the operator node output circles group in OperatorNodeView. */ public handleDeselected (options: {[key: string]: any} = { isSelectionsEmpty: true }) { this.highlightStyle(false).weakStyle(!options.isSelectionsEmpty); } public move (deltaX: number, deltaY: number) { let translate = fromTransform(this.$el.attr('transform')).translate; translate = [translate[0] + deltaX, translate[1] + deltaY]; this.$el.attr('transform', toTransform({ translate })); this.handleMovement(); } /** * Abstract method called when node being moved. * Subclass can implements this method to perform custom logic. */ public handleMovement () { } /** * @param {string} id * @returns * @memberof NodeView */ public getSlotViewById (id: string) { return this.inputSlots.concat(this.outputSlots).find(slotView => slotView.id === id); } public getSlotIndexById (id: string) { const index = this.inputSlots.findIndex(slotView => slotView.id === id); if (index !== -1) return index; return this.outputSlots.findIndex(slotView => slotView.id === id); } /** * @param {MouseEvent} event * @memberof NodeView */ public manuallyTriggerDragEvent (event: MouseEvent) { const mousedownEvent = document.createEvent ('MouseEvent'); mousedownEvent.initMouseEvent( 'mousedown', true, true, window, 0, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, null, ); (this.$el.node() as Element).dispatchEvent(mousedownEvent); } public destroy () { this.detachEvents(); this.inputSlots.forEach(slotView => slotView.destroy()); this.outputSlots.forEach(slotView => slotView.destroy()); this.$el.remove(); this.$el = null; this.$body = null; this.$title = null; this.$el = null; this.geditor = null; this.graphView = null; this.inputSlots = null; this.outputSlots = null; } public getTranslate () { if (this.$el) { return fromTransform(this.$el.attr('transform')).translate; } } }