Blame view

frontend/mobile/src/views/matter/components/processForecast/packages/Graph.js 4.02 KB
8d73e917   陈威   初始化提交
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
/**
 * User: CHT
 * Date: 2020/5/8
 * Time: 14:00
 */

import GraphEvent from './GraphEvent'
import GraphNode from './GraphNode'
import GraphLink from './GraphLink'

import {
  mark,
  arrayReplace
} from './utils'


class Graph extends GraphEvent {
  constructor (options) {
    const {
      relationMark,
      startMark,
      endMark,
      nodeList = [],
      linkList = [],
      origin
    } = options

    super()

    Object.assign(mark, {
      relationMark,
      startMark,
      endMark
    })

    this.nodeList = []
    this.linkList = []

    this.origin = origin

    this.mouseOnLink = null
    this.mouseonNode = null

    this.graphSelected = false
    this.maskBoundingClientRect = {}

    this.initNode(nodeList)
    this.initLink(linkList)
  }

  pointMap () {
    const map = {}
    this.nodeList.forEach(point => {
      map[point[mark.relationMark]] = point
    })
    return map
  }

  initNode (nodeList) {
    arrayReplace(this.nodeList, nodeList.map(node => this.createNode(node)))
    return this.nodeList
  }

  initLink (linkList) {

    const list = []

    linkList.forEach(link => {

      const {
        startAt = [0, 0],
        endAt = [0, 0],
        meta = null
      } = link

     const startId = link[mark.startMark] || ''
     const endId = link[mark.endMark] || ''
      const pointMap = this.pointMap()
      console.log(startId, endId)
      const start = pointMap[startId]
      const end = pointMap[endId]
      if (start && end) {
        list.push(
          this.createLink({
            start,
            end,
            meta,
            startAt,
            endAt
          })
        )
      }
    })

    arrayReplace(this.linkList, list)

    return this.linkList
  }

  createNode (options) {
    return new GraphNode(options, this)
  }

  createLink (options) {
    return new GraphLink(options, this)
  }

  addNode (options) {
    const node = options.constructor === GraphNode
      ? options
      : this.createNode(options)

    this.nodeList.push(node)
    return node
  }

  addLink (options) {
    const newLink = options.constructor === GraphLink
      ? options
      : this.createLink(options)

    const currentLink = this.linkList.find(item => {
      return item.start === newLink.start && item.end === newLink.end
    })

    if (currentLink) {
      currentLink.startAt = newLink.startAt
      currentLink.endAt = newLink.endAt
    } else if (newLink.start && newLink.end) {
      this.linkList.push(newLink)
    }

    return newLink
  }

  removeNode (node) {
    const idx = this.nodeList.indexOf(node)
    this.linkList.filter(link => {
      return link.start === node || link.end === node
    }).forEach(link => {
      this.removeLink(link)
    })

    this.nodeList.splice(idx, 1)

    return node
  }

  removeLink (link) {
    const idx = this.linkList.indexOf(link)
    this.linkList.splice(idx, 1)
    if (this.mouseOnLink === link) {
      this.mouseOnLink = null
    }
    return link
  }

  toLastNode (idx) {
    const nodeList = this.nodeList
    nodeList.splice(
      nodeList.length - 1, 0,
      ...nodeList.splice(idx, 1)
    )
  }

  toLastLink (idx) {
    const linkList = this.linkList
    linkList.splice(
      linkList.length - 1, 0,
      ...linkList.splice(idx, 1)
    )
  }

  toJSON () {
    return {
      origin: this.origin,
      nodeList: this.nodeList.map(node => node.toJSON()),
      linkList: this.linkList.map(link => link.toJSON())
    }
  }

  selectAll () {
    const nodeList = this.nodeList
    const margin = 20

    this.maskBoundingClientRect = {
      top: Math.min(
        ...nodeList.map(
          node => node.center[1] - node.height / 2)
      ) - margin,

      right: Math.max(
        ...nodeList.map(
          node => node.center[0] + node.width / 2)
      ) + margin,

      bottom: Math.max(
        ...nodeList.map(
          node => node.center[1] + node.height / 2)
      ) + margin,

      left: Math.min(
        ...nodeList.map(
          node => node.center[0] - node.width / 2)
      ) - margin
    }

    this.graphSelected = true
  }
}

export default Graph