lihang
2023-07-13 7ce592bb5a5b9e0dc78786e727f042b95ebc81c8
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
<!-- eslint-disable vue/valid-v-for -->
<!-- eslint-disable vue/require-v-for-key -->
<template>
  <vue-flowchart-editor class="vue-flowchart-editor" ref="flowChart">
    <div class="vfe-chart">
      <!-- Top Menu -->
      <div class="vfe-chart-header" v-if="type !== 'detail'">
        <editor-toolbar :chart-data="flowChartData" />
      </div>
      <div class="vfe-chart-container">
        <!-- Left Items -->
        <div class="vfe-chart-sidebar" v-if="type !== 'detail'">
          <editor-item-panel :node-items="flowChartNodeItems" />
        </div>
        <!-- Main Chart -->
        <div class="vfe-chart-main">
          <flow :data="flowChartData" :onAfterChange="onAfterChange" />
          <div class="tooltip">
            <template v-for="item in tooltipData">
              <p>{{ item.name }}: {{ item.value }}</p>
            </template>
          </div>
        </div>
        <div class="vfe-chart-panel" v-if="type !== 'detail'">
          <div class="vfe-chart-panel-detail">
            <editor-detail-panel :rowData="rowData" ref="EditorDetailPanel" :existNodes="existNodes" :type="type"/>
          </div>
        </div>
      </div>
    </div>
    <!-- Mouse Right Button Context Menu -->
    <editor-context-menu v-if="type !== 'detail'" />
    <register-edge
      name="custom-polyline"
      extend="flow-polyline"
      :config="customEdgeConfig"
    />
    <custom-command :download="downloadImage" />
  </vue-flowchart-editor>
</template>
 
<script>
import VueFlowchartEditor, { Flow, RegisterEdge } from "vue-flowchart-editor";
import EditorToolbar from "./components/Toolbar";
import EditorItemPanel from "./components/ItemPanel";
import EditorDetailPanel from "./components/DetailPanel";
import EditorMinimap from "./components/EditorMinimap";
import EditorContextMenu from "./components/ContextMenu";
import CustomCommand from "./components/CustomCommand";
 
export default {
  name: "FlowchartEditor",
 
  components: {
    VueFlowchartEditor,
    Flow,
    EditorToolbar,
    EditorItemPanel,
    EditorDetailPanel,
    EditorMinimap,
    EditorContextMenu,
    CustomCommand,
    RegisterEdge,
  },
 
  props: ["chartData", "chartDataNodeItems", "saveData", "rowData", "type"],
 
  data() {
    return {
      flowChartData: this.chartData,
      flowChartNodeItems: this.chartDataNodeItems,
      customEdgeConfig: {
        getActivedStyle(/*item*/) {
          return {
            lineWidth: 3,
          };
        },
        getSelectedStyle(/*item*/) {
          return {
            lineWidth: 3,
          };
        },
      },
      tooltipShow: true,
      tooltipData: [],
      existNodes: this.chartData.nodes || []
    };
  },
 
  mounted() {
    if (this.type === "add") {
      this.$nextTick(() => {
        this.$refs.flowChart.propsAPI.executeCommand("autoZoom");
      });
    }
  },
 
  methods: {
    onAfterChange(e) {
      try {
        if (e.action === "add" && e.model.type === "node") {
          if (!e.model.change) {
            this.$refs.flowChart.propsAPI.remove(e.item);
            e.model.id = e.model.label;
            e.model.change = true;
            this.$refs.flowChart.propsAPI.add("node", e.model);
          }
        }
      } catch (err) {
        if (
          err.message ===
          `id:${e.model.label} has already been set, please set new one`
        ) {
          this.$message.error("不能添加已存在的节点!");
        }
      }
      const { nodes } = this.$refs.flowChart.propsAPI.save()
      this.existNodes = nodes || []
      console.log(this.$refs.flowChart.propsAPI.save());
    },
    _downloadImage(data, filename = "flowchart.png") {
      const a = document.createElement("a");
      a.href = data;
      a.download = filename;
      document.body.appendChild(a);
      a.click();
    },
 
    downloadImage() {
      const page = this.$refs["flowChart"].propsAPI.editor.getCurrentPage();
      this._downloadImage(page.saveImage().toDataURL("image/png"));
    },
    getNewRowData() {
      return this.$refs.EditorDetailPanel.getNewRowDate();
    },
    getFlowData() {
      return this.$refs.flowChart.propsAPI.save();
    },
  },
};
</script>
 
<style lang="scss" scoped>
.vue-flowchart-editor {
  display: flex;
  flex: 1;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: #fff;
}
 
.vfe-chart {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
 
  .vfe-chart-header {
    border: 1px solid #e6e9ed;
    padding: 8px;
  }
 
  .vfe-chart-container {
    flex: 1;
    display: flex;
    height: 550px;
    overflow: hidden;
    .vfe-chart-main {
      position: relative;
      flex: 1;
      max-height: calc(100% - 5px); // fix scroll show
 
      .tooltip {
        position: absolute;
        display: none;
        top: 0;
        left: 0;
        width: 100px;
        height: auto;
        padding: 15px;
        border-radius: 10px;
        z-index: 999;
        opacity: 0.8;
        color: #ffffff;
        font-size: 12px;
        background-color: #000;
 
        p {
          margin: 0;
        }
      }
    }
 
    .vfe-chart-sidebar {
      position: relative;
      display: flex;
      justify-content: center;
      width: 15%;
      background-color: #fafafa;
      border-right: 1px solid #e6e9ed;
    }
 
    .vfe-chart-panel {
      position: relative;
      width: 300px;
      background-color: #fafafa;
      border-left: 1px solid #e6e9ed;
      overflow-y: scroll;
 
      .vfe-chart-panel-detail {
        box-sizing: border-box;
        padding: 10px;
      }
    }
  }
}
</style>