forked from pythonzm/Ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg_chart.html
168 lines (152 loc) · 6.93 KB
/
org_chart.html
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
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'jsplumb/css/jsplumbtoolkit-defaults.css' %}">
<style>
#main {
width: 80%;
height: 500px;
overflow: auto;
position: relative;
}
.item {
background: #CCFFFF;
height: 30px;
width: 200px;
border: 1px solid #5F9EDF;
margin: 5px;
font: 15px/30px "微软雅黑";
text-align: center;
border-radius: 5px;
}
.btn-primary {
width: 10%;
float: right;
margin-right: 2px;
}
</style>
<div align="center" id="edit-org">
<a href="{% url 'proj_org' project_obj.id %}" target="_blank">
<button type="button" class="btn btn-block btn-primary">编辑</button>
</a>
<button type="button" class="btn btn-block btn-primary export-canvas">导出</button>
</div>
<div id="main"></div>
<script src="{% static 'jsplumb/js/jsplumb.min.js' %}"></script>
<script src="{% static 'jsplumb/js/html2canvas.min.js' %}"></script>
<script src="{% static 'jsplumb/js/rgbcolor.min.js' %}"></script>
<script src="{% static 'jsplumb/js/canvg.min.js' %}"></script>
<script>
// 生成架构图
$(function () {
if ('{{ project_org }}'.length !== 0) {
let elementSign = 0;
let origin = {//起点
endpoint: ['Dot', {radius: 5}], //端点的形状
connectorStyle: {
outlineStroke: 'black',
strokeWidth: 1
},//连接线的颜色,大小样式
connectorHoverStyle: {
strokeWidth: 3
},
paintStyle: {fill: "lightblue", strokeWidth: 3}, //端点的颜色样式
isSource: true, //是否可以拖动(作为连线起点)
connector: 'Straight', //连接线的样式种类有[Bezier],[Flowchart],[StateMachine ],[Straight ]
isTarget: false, //是否可以放置(连线终点)
maxConnections: -1, // 设置连接点最多可以连接几条线,-1表示无限大
connectorOverlays: [["Arrow", {width: 10, length: 10, location: 1}]]
};
let destination = {//终点
endpoint: ['Dot', {radius: 5}], //端点的形状
connectorStyle: {
outlineStroke: 'black',
strokeWidth: 1
},//连接线的颜色,大小样式
connectorHoverStyle: {
strokeWidth: 3
},
paintStyle: {fill: "lightblue", strokeWidth: 3}, //端点的颜色样式
isSource: false, //是否可以拖动(作为连线起点)
connector: 'Straight', //连接线的样式种类有[Bezier],[Flowchart],[StateMachine ],[Straight ]
isTarget: true, //是否可以放置(连线终点)
maxConnections: -1, // 设置连接点最多可以连接几条线,-1表示无限大
connectorOverlays: [["Arrow", {width: 10, length: 10, location: 1}]]
};
$.get('/api/project/{{ pk }}/', function (res) {
let project_org = JSON.parse(res.project_org);
let mainHTML = "";
for (let i = 0; i < project_org.mainArr.length; i++) {
if (elementSign < project_org.mainArr[i].sign) {//如果已经保存过,即将标记更新
elementSign = project_org.mainArr[i].sign;
}
mainHTML += '<div class="item" data-sign="' + project_org.mainArr[i].sign + '" data-index="' + project_org.mainArr[i].index + '" style="left:' + project_org.mainArr[i].offset.left + 'px;top:' + project_org.mainArr[i].offset.top + 'px;position:absolute;margin:0" >' + project_org.mainArr[i].text + '</div>';
}
$("#main").append(mainHTML);
$("#main .item").each(function () {
jsPlumb.addEndpoint(this, {anchors: "BottomCenter"}, deepCopy(origin, {uuid: $(this).attr("data-sign") + "origin"}));//起点
jsPlumb.addEndpoint(this, {anchors: "TopCenter"}, deepCopy(destination, {uuid: $(this).attr("data-sign") + "destination"}));//终点
});
//固定连线
for (let i = 0; i < project_org.connects.length; i++) {
jsPlumb.connect({uuids: [project_org.connects[i].originSign + "origin", project_org.connects[i].destinationSign + "destination"]});
}
})
} else {
$('#main').html('<h3>暂无架构图</h3>')
}
});
function deepCopy(p, c) { //克隆对象
let d = c || {};
for (let i in p) {
if (!p.hasOwnProperty(i)) {
continue;
}
if (typeof p[i] === 'object') {
d[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(p[i], d[i]);
} else {
d[i] = p[i];
}
}
return d;
}
// 将架构图导出图片
$('.export-canvas').on('click', function () {
if (typeof html2canvas !== 'undefined') {
//以下是对svg的处理
let cloneDom = $("#main");
let nodesToRecover = [];
let nodesToRemove = [];
let svgElem = cloneDom.find('svg');
svgElem.each(function (index, node) {
let parentNode = node.parentNode;
let svg = node.outerHTML.trim();
let canvas = document.createElement('canvas');
canvas.width = 650;
canvas.height = 798;
canvg(canvas, svg);
if (node.style.position) {
canvas.style.position += node.style.position;
canvas.style.left += node.style.left;
canvas.style.top += node.style.top;
}
nodesToRecover.push({
parent: parentNode,
child: node
});
parentNode.removeChild(node);
nodesToRemove.push({
parent: parentNode,
child: canvas
});
parentNode.appendChild(canvas);
});
html2canvas(document.querySelector("#main")).then(canvas => {
let imgUri = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // 获取生成的图片的url
let saveLink = document.createElement('a');
saveLink.href = imgUri;
saveLink.download = '{{ project_obj.project_name }}_{{ project_obj.get_project_env_display }}.png';
saveLink.click();
});
}
})
</script>