Blame view

frontend/front/src/views/matter/processForecast/packages/GraphNode.js 3.24 KB
8ea9c133   陈威   初始化提交
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
/**
 * User: CHT
 * Date: 2020/5/8
 * Time: 14:01
 */

import {
  minus,
  uuid,
  vector,
  mark
} from './utils'

import {
  direction,
  directionVector
} from './types'


export default class GraphNode {
  constructor (props, graph) {
    const {
      width = 180,
      height = 100,
      coordinate = [0, 0],
      meta = null
    } = props

    this.$options = props

    const id = props[mark.relationMark] || uuid('node')

    this.key = uuid('node')
    this.graph = graph

    this[mark.relationMark] = id
    this.coordinate = [...coordinate]
    this.meta = meta

    this.width = width
    this.height = height
  }

  get position () {
    return vector(this.coordinate)
      .add(this.graph.origin)
      .end
  }

  set position (position) {
    this.coordinate = vector(position)
      .minus(this.graph.origin)
      .end
  }

  get center () {
    return vector(this.coordinate)
      .add([this.width / 2, this.height / 2])
      .end
  }

  set center (position) {
    this.coordinate = vector(position)
      .minus([this.width / 2, this.height / 2])
      .end
  }

  get width () {
    return this._width
  }

  set width (w) {
    w = Math.floor(w)
    this._width = w > 50 ? w : 50
    this.angle()
  }

  get height () {
    return this._height
  }

  set height (h) {
    h = Math.floor(h)
    this._height = h > 20 ? h : 20
    this.angle()
  }

  angle () {
    const
      w = this.width / 2
      , h = this.height / 2
      , center = [0, 0]

    const topLeft = vector(center)
      .minus([w, h])
      .angle()
      .end

    const topRight = vector(center)
      .add([w, 0])
      .minus([0, h])
      .angle()
      .end

    const bottomRight = vector(center)
      .add([w, h])
      .angle()
      .end

    const bottomLeft = vector(center)
      .add([0, h])
      .minus([w, 0])
      .angle()
      .end

    this.angleList = [
      topLeft,
      topRight,
      bottomRight,
      bottomLeft
    ]
  }

  relative (offset) {
    const angle = vector(offset)
      .minus([this.width / 2, this.height / 2])
      .angle()
      .end
    const angleList = this.angleList
    const directionList = [
      direction.top,
      direction.right,
      direction.bottom,
      direction.left
    ]

    let dir = direction.left

    angleList.reduce((prev, current, idx) => {
      if (angle >= prev && angle < current) {
        dir = directionList[idx - 1]
      }
      return current
    })

    return {
      position: this.fixOffset(offset, dir),
      direction: directionVector[dir]
    }
  }

  fixOffset (offset, dir) {
    switch (dir) {
      case direction.top:
        offset[0] = this.width / 2
        offset[1] = 0
        break
      case direction.right:
        offset[0] = this.width
        offset[1] = this.height / 2
        break
      case direction.bottom:
        offset[0] = this.width / 2
        offset[1] = this.height
        break
      case direction.left:
      default:
        offset[0] = 0
        offset[1] = this.height / 2
        break
    }
    return offset
  }

  remove () {
    return this.graph.removeNode(this)
  }

  toJSON () {
    return {
      [mark.relationMark]: this[mark.relationMark],
      width: this.width,
      height: this.height,
      coordinate: [...this.coordinate],
      meta: this.meta
    }
  }
}