All files / modules/70-pipeline/components/Diagram/link DefaultLinkModel.ts

25% Statements 12/48
33.33% Branches 15/45
44.44% Functions 4/9
25% Lines 12/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145              207x               207x   207x 207x                                                         207x   230x                                                 77x                                                                   55x                                   1x     1x 1x 1x                          
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import {
  LabelModel,
  LinkModel,
  LinkModelGenerics,
  LinkModelListener,
  PortModel,
  PortModelAlignment
} from '@projectstorm/react-diagrams-core'
import { Point } from '@projectstorm/geometry'
import type { BaseEntityEvent, BaseModelOptions, DeserializeEvent } from '@projectstorm/react-canvas-core'
import { DefaultLabelModel } from '../label/DefaultLabelModel'
import { DiagramType, Event } from '../Constants'
 
export interface DefaultLinkModelListener extends LinkModelListener {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  colorChanged?(event: BaseEntityEvent<DefaultLinkModel> & { color: null | string }): void
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  widthChanged?(event: BaseEntityEvent<DefaultLinkModel> & { width: 0 | number }): void
}
 
export interface DefaultLinkModelOptions extends BaseModelOptions {
  width?: number
  color?: string
  selectedColor?: string
  curvyness?: number
  strokeDasharray?: number
  type?: string
  maxLinePartLength?: { type: 'in' | 'out'; size: number }
  allowAdd?: boolean
  testName?: string
  curve?: number
}
 
export interface DefaultLinkModelGenerics extends LinkModelGenerics {
  LISTENER: DefaultLinkModelListener
  OPTIONS: DefaultLinkModelOptions
}
 
export class DefaultLinkModel extends LinkModel<DefaultLinkModelGenerics> {
  constructor(options: DefaultLinkModelOptions = {}) {
    super({
      type: DiagramType.Default,
      width: options.width || 2,
      curve: options.curve || 12,
      strokeDasharray: options.strokeDasharray ?? 0,
      allowAdd: options.allowAdd || true,
      color: options.color || 'var(--diagram-link)',
      selectedColor: options.selectedColor || 'var(--diagram-hover-link-color)',
      curvyness: 50,
      ...options
    })
  }
 
  calculateControlOffset(port: PortModel): [number, number] {
    if (port.getOptions().alignment === PortModelAlignment.RIGHT) {
      return [this.options.curvyness || 0, 0]
    } else if (port.getOptions().alignment === PortModelAlignment.LEFT) {
      return [-(this.options.curvyness || 0), 0]
    } else if (port.getOptions().alignment === PortModelAlignment.TOP) {
      return [0, -(this.options.curvyness || 0)]
    }
    return [0, this.options.curvyness || 0]
  }
 
  setColorOfLink(color: string): void {
    this.options.color = color
  }
 
  getSVGPath(): string | undefined {
    if (this.points.length == 2) {
      const firstPoint = this.getFirstPoint().clone().getPosition()
      firstPoint.x += 3
      const lastPoint = this.getLastPoint().clone().getPosition()
      lastPoint.x -= 3
      if (Math.abs(firstPoint.y - lastPoint.y) > 3 && this.options.curve) {
        const diameter = this.options.curve * 2
        const topToBottom = lastPoint.y - firstPoint.y > 0
        let middlePoint = (firstPoint.x + lastPoint.x) / 2
        if (this.options.maxLinePartLength) {
          middlePoint =
            this.options.maxLinePartLength.type === 'in'
              ? firstPoint.x + this.options.maxLinePartLength.size
              : lastPoint.x - this.options.maxLinePartLength.size
        }
        const midX = new Point(middlePoint, firstPoint.y)
        const midX1 = new Point(midX.x - diameter, midX.y)
        const midX2 = topToBottom ? new Point(midX.x, midX.y + diameter) : new Point(midX.x, midX.y - diameter)
        const midY = new Point(midX.x, lastPoint.y)
        const midY1 = topToBottom
          ? new Point(midX.x, lastPoint.y - diameter)
          : new Point(midX.x, lastPoint.y + diameter)
        const midY2 = new Point(midX.x + diameter, lastPoint.y)
        return `M${firstPoint.toSVG()} L${midX1.toSVG()} Q${midX.toSVG()} ${midX2.toSVG()} L${midY1.toSVG()} Q${midY.toSVG()} ${midY2.toSVG()} L${lastPoint.toSVG()}`
      }
      return `M${firstPoint.toSVG()} L${lastPoint.toSVG()}`
    }
  }
 
  serialize(): any {
    return {
      ...super.serialize(),
      width: this.options.width,
      color: this.options.color,
      curvyness: this.options.curvyness,
      selectedColor: this.options.selectedColor
    }
  }
 
  deserialize(event: DeserializeEvent<this>): void {
    super.deserialize(event)
    this.options.color = event.data.color
    this.options.width = event.data.width
    this.options.curvyness = event.data.curvyness
    this.options.selectedColor = event.data.selectedColor
  }
 
  addLabel(label: LabelModel | string): void {
    Iif (label instanceof LabelModel) {
      return super.addLabel(label)
    }
    const labelOb = new DefaultLabelModel()
    labelOb.setLabel(label)
    return super.addLabel(labelOb)
  }
 
  setWidth(width: number): void {
    this.options.width = width
    this.fireEvent({ width }, Event.widthChanged)
  }
 
  setColor(color: string): void {
    this.options.color = color
    this.fireEvent({ color }, Event.colorChanged)
  }
}