Fixiaobai
2023-11-13 c1c20fe004cd930a95eb5758f927e6ab178799e9
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
<template>
  <div></div>
</template>
 
<script>
import 'gojs/release/go.js'
var $ = go.GraphObject.make
export default {
  name: 'diagram',
  props: ['modelData'], // accept model data as a parameter
  mounted: function() {
    var self = this
    var myDiagram = $(go.Diagram, this.$el, {
      // layout: $(go.TreeLayout, { angle: 0, arrangement: go.TreeLayout.ArrangementHorizontal }),
      'undoManager.isEnabled': true,
      // Model ChangedEvents get passed up to component users
      ModelChanged: function(e) {
        self.$emit('model-changed', e)
      },
      ChangedSelection: function(e) {
        self.$emit('changed-selection', e)
      }
    })
 
    myDiagram.nodeTemplate = $(
      go.Node,
      'Auto',
      {
        mouseEnter: this.mouseEnter,
        mouseLeave: this.mouseLeave
      },
      { desiredSize: new go.Size(65, 65) },
      new go.Binding('location', 'loc', go.Point.parse).makeTwoWay(
        go.Point.stringify
      ),
 
      $(
        go.Shape,
        'circle',
        {
          fill: 'white',
          strokeWidth: 2,
          stroke: null,
          portId: '',
          fromLinkable: true,
          toLinkable: true,
          cursor: 'pointer',
          name: 'SHAPE'
        },
        new go.Binding('fill', 'color')
      ),
      $(
        go.TextBlock,
        {
          margin: 8,
          editable: false,
          stroke: 'white',
          name: 'TEXT',
          width: 32,
          height: 32,
          textAlign: 'center',
          verticalAlignment: go.Spot.Center
        },
        new go.Binding('text').makeTwoWay()
      )
    )
 
    myDiagram.linkTemplate = $(
      go.Link,
      { relinkableFrom: true, relinkableTo: true },
      $(go.Shape, { stroke: 'gray', strokeWidth: 3 }),
      $(go.Shape, {
        toArrow: 'Triangle',
        scale: 1,
        fill: 'gray',
        strokeWidth: 0
      }),
      $(
        go.TextBlock,
        {
          editable: true,
          segmentOffset: new go.Point(0, -20),
          cursor: 'pointer'
        },
        new go.Binding('text', 'text')
      )
    )
 
    this.diagram = myDiagram
 
    this.updateModel(this.modelData)
 
    this.diagram.allowCopy = false
    this.diagram.allowClipboard = false
  },
  watch: {
    modelData: function(val) {
      this.updateModel(val)
    }
  },
  methods: {
    model: function() {
      return this.diagram.model
    },
    updateModel: function(val) {
      // No GoJS transaction permitted when replacing Diagram.model.
      if (val instanceof go.Model) {
        this.diagram.model = val
      } else {
        var m = new go.GraphLinksModel()
        if (val) {
          for (var p in val) {
            m[p] = val[p]
          }
        }
        this.diagram.model = m
      }
    },
    updateDiagramFromData: function() {
      this.diagram.startTransaction()
      // This is very general but very inefficient.
      // It would be better to modify the diagramData data by calling
      // Model.setDataProperty or Model.addNodeData, et al.
      this.diagram.updateAllRelationshipsFromData()
      this.diagram.updateAllTargetBindings()
      this.diagram.commitTransaction('updated')
    },
    mouseEnter: function(e, obj) {
      var shape = obj.findObject('SHAPE')
      shape.fill = '#6DAB80'
      shape.stroke = '#A6E6A1'
      var text = obj.findObject('TEXT')
      text.stroke = 'black'
    },
    mouseLeave: function(e, obj) {
      var shape = obj.findObject('SHAPE')
      // Return the Shape's fill and stroke to the defaults
      shape.fill = obj.data.color
      shape.stroke = null
      // Return the TextBlock's stroke to its default
      var text = obj.findObject('TEXT')
      text.stroke = 'white'
    }
  }
}
</script>