All files / modules/70-pipeline/components/Diagram/node DefaultNodeModel.ts

47.54% Statements 29/61
52.63% Branches 10/19
68.75% Functions 11/16
47.54% Lines 29/61

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                207x 207x       207x 207x                                                                               207x             209x         209x                                     209x 209x                                     328x 328x 164x 164x     164x 164x     328x       69x           69x     69x       1x       69x           69x     69x       64x                                                             338x                                 277x     277x           300x       377x                                        
/*
 * 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 type { CSSProperties } from 'react'
import { map } from 'lodash-es'
import { NodeModel, NodeModelGenerics, PortModelAlignment } from '@projectstorm/react-diagrams-core'
import type { BasePositionModelOptions, DeserializeEvent } from '@projectstorm/react-canvas-core'
import type { IconName } from '@wings-software/uicore'
import type { IconProps } from '@wings-software/uicore/dist/icons/Icon'
import { DefaultPortModel } from '../port/DefaultPortModel'
import { DiagramType } from '../Constants'
import type { DefaultLinkModel } from '../link/DefaultLinkModel'
 
export interface DefaultNodeModelOptions extends BasePositionModelOptions {
  name: string
  customNodeStyle?: CSSProperties
  nodeClassName?: string
  width?: number
  height?: number
  identifier?: string
  icon?: IconName
  allowAdd?: boolean
  draggable?: boolean
  iconStyle?: CSSProperties
  iconProps?: Omit<IconProps, 'name'>
  canDelete?: boolean
  isInComplete?: boolean
  skipCondition?: string
  conditionalExecutionEnabled?: boolean
  isTemplate?: boolean
  secondaryIcon?: IconName | null
  secondaryIconProps?: Omit<IconProps, 'name'>
  secondaryIconStyle?: CSSProperties
  showPorts?: boolean
  hideInPort?: boolean
  hideOutPort?: boolean
  tertiaryIcon?: IconName
  tertiaryIconProps?: Omit<IconProps, 'name'>
  tertiaryIconStyle?: CSSProperties
  iconSize?: number
  defaultSelected?: boolean
  allowDropOnLink?: boolean
  allowDropOnNode?: boolean
  disableClick?: boolean
}
 
export interface DefaultNodeModelGenerics extends NodeModelGenerics {
  OPTIONS: DefaultNodeModelOptions
}
 
export class DefaultNodeModel<G extends DefaultNodeModelGenerics = DefaultNodeModelGenerics> extends NodeModel<G> {
  protected portsIn: DefaultPortModel[]
  protected portsOut: DefaultPortModel[]
 
  constructor(name: string, color: string, icon: IconName)
  constructor(options?: DefaultNodeModelOptions)
  constructor(options: any = {}) {
    Iif (typeof options === 'string') {
      options = {
        name: options
      }
    }
    super({
      type: DiagramType.Default,
      name: 'Untitled',
      icon: 'add',
      nodeClassName: '',
      allowAdd: false,
      iconProps: {},
      iconStyle: {},
      draggable: false,
      canDelete: true,
      secondaryIcon: 'command-echo',
      customNodeStyle: {},
      showPorts: true,
      width: 64,
      height: 64,
      allowDropOnLink: true,
      allowDropOnNode: true,
      ...options
    })
    this.portsOut = []
    this.portsIn = []
  }
 
  doClone(lookupTable: any, clone: any): void {
    clone.portsIn = []
    clone.portsOut = []
    super.doClone(lookupTable, clone)
  }
 
  removePort(port: DefaultPortModel): void {
    super.removePort(port)
    if (port.getOptions().in) {
      this.portsIn.splice(this.portsIn.indexOf(port), 1)
    } else {
      this.portsOut.splice(this.portsOut.indexOf(port), 1)
    }
  }
 
  addPort<T extends DefaultPortModel>(port: T): T {
    super.addPort(port)
    if (port.getOptions().in) {
      Eif (this.portsIn.indexOf(port) === -1) {
        this.portsIn.push(port)
      }
    } else {
      Eif (this.portsOut.indexOf(port) === -1) {
        this.portsOut.push(port)
      }
    }
    return port
  }
 
  addInPort(label: string, after = true): DefaultPortModel {
    const port = new DefaultPortModel({
      in: true,
      name: label,
      label: label,
      alignment: PortModelAlignment.LEFT
    })
    Iif (!after) {
      this.portsIn.splice(0, 0, port)
    }
    return this.addPort(port)
  }
 
  getIdentifier(): string {
    return this.options.identifier || ''
  }
 
  addOutPort(label: string, after = true): DefaultPortModel {
    const port = new DefaultPortModel({
      in: false,
      name: label,
      label: label,
      alignment: PortModelAlignment.RIGHT
    })
    Iif (!after) {
      this.portsOut.splice(0, 0, port)
    }
    return this.addPort(port)
  }
 
  setOptions(options: DefaultNodeModelOptions): void {
    this.options = options
  }
 
  deserialize(event: DeserializeEvent<this>): void {
    super.deserialize(event)
    this.options.name = event.data.name
    this.options.width = event.data.width
    this.options.height = event.data.height
    this.options.identifier = event.data.identifier
    this.options.customNodeStyle = { ...event.data.customNodeStyle }
    this.options.icon = event.data.icon
    this.options.allowAdd = event.data.allowAdd
    this.options.canDelete = event.data.canDelete
    this.options.draggable = event.data.draggable
    this.options.iconProps = { ...event.data.iconProps }
    this.options.isInComplete = event.data.isInComplete
    this.options.secondaryIcon = event.data.secondaryIcon
    this.options.secondaryIconProps = { ...event.data.secondaryIconProps }
    this.options.secondaryIconStyle = { ...event.data.secondaryIconStyle }
    this.options.tertiaryIcon = event.data.tertiaryIcon
    this.options.tertiaryIconProps = { ...event.data.tertiaryIconProps }
    this.options.tertiaryIconStyle = { ...event.data.tertiaryIconStyle }
    this.portsIn = map(event.data.portsInOrder, id => {
      return this.getPortFromID(id)
    }) as DefaultPortModel[]
    this.portsOut = map(event.data.portsOutOrder, id => {
      return this.getPortFromID(id)
    }) as DefaultPortModel[]
  }
 
  serialize(): any {
    return {
      ...super.serialize(),
      name: this.options.name,
      width: this.options.width,
      height: this.options.height,
      identifier: this.options.identifier,
      customNodeStyle: { ...this.options.customNodeStyle },
      icon: this.options.icon,
      allowAdd: this.options.allowAdd,
      canDelete: this.options.canDelete,
      draggable: this.options.draggable,
      isInComplete: this.options.isInComplete,
      iconProps: { ...this.options.iconProps },
      secondaryIcon: this.options.secondaryIcon,
      secondaryIconProps: { ...this.options.secondaryIconProps },
      secondaryIconStyle: { ...this.options.secondaryIconStyle },
      portsInOrder: map(this.portsIn, port => {
        return port.getID()
      }),
      portsOutOrder: map(this.portsOut, port => {
        return port.getID()
      })
    }
  }
 
  getInPorts(): DefaultPortModel[] {
    return this.portsIn
  }
 
  getOutPorts(): DefaultPortModel[] {
    return this.portsOut
  }
}
 
export interface DefaultNodeEvent {
  entity: DefaultNodeModel
  isSelected: boolean
  callback: () => void
  target: HTMLElement
  firing: boolean
  stopPropagation: () => void
}
 
export interface DefaultLinkEvent {
  entity: DefaultLinkModel
  isSelected: boolean
  callback: () => void
  firing: boolean
  stopPropagation: () => void
}