-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.html
161 lines (148 loc) · 4.39 KB
/
home.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
<!DOCTYPE html>
<html>
<head>
<title>Sequence Diagram Playground</title>
<script src="static/bower-webfontloader/webfont.js"></script>
<script src="static/snap.svg/dist/snap.svg-min.js"></script>
<script src="static/underscore/underscore-min.js"></script>
<script src="static/js-sequence-diagrams/dist/sequence-diagram-min.js"></script>
<script src="static/canvg/dist/canvg.min.js"></script>
<script src="static/vue/dist/vue.js"></script>
<script src="static/axios/dist/axios.min.js"></script>
<script src="static/jquery/dist/jquery.min.js"></script>
<script src="static/bootstrap/dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="static/bootstrap/dist/css/bootstrap.min.css"></script>
<style>
body {
margin: 0 2vw 0 2vw;
}
pre {
color: red;
}
textarea {
width: 100%;
height: 30vh;
}
input {
width: 100%;
}
ul {
margin-top: 1.5em;
}
.control {
float: right;
}
</style>
</head>
<body>
<div id="app">
<div ref="diagramDiv"></div>
<pre v-show="!diagramValid">{{ errorText }}</pre>
<input v-model="diagramName">
<textarea v-model="diagramContent"></textarea>
<div class="btn-group btn-group-justified">
<a class="btn btn-primary" v-bind:disabled="!saveAvaliable()" v-on:click="save">Save</a>
<a class="btn btn-primary" v-bind:disabled="!downloadAvaliable()" v-bind:download="pngDownload" v-bind:href="pngHref" v-on:click="download">Download as PNG</a>
</div>
<ul class="list-group">
<li v-for="(_, key) in diagrams" class="list-group-item">
{{ key }}
<div class="control">
<a v-on:click="load(key)">Load</a> <a v-on:click="delete_(key)">Delete</a>
</div>
</li>
</ul>
<canvas ref="canvas" v-show="false"></canvas>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
diagrams: {},
diagramName: "Alice greets Bob",
diagramContent: "Alice->Bob: Hi!",
diagramValid: false,
errorText: "",
pngDownload: "",
pngHref: ""
},
methods: {
download: function() {
if (this.$refs.diagramDiv.childNodes.length == 1) {
var resizableSVG = this.$refs.diagramDiv.childNodes[0].cloneNode(true);
// Improve render quality by resizing svg
var resizeFactor = 8;
var newWidth = resizableSVG.width.baseVal.value * resizeFactor;
var newHeight = resizableSVG.height.baseVal.value * resizeFactor;
resizableSVG.viewBox.baseVal.width = newWidth / resizeFactor;
resizableSVG.viewBox.baseVal.height = newHeight / resizeFactor;
resizableSVG.width.baseVal.value = newWidth;
resizableSVG.height.baseVal.value = newHeight;
var serializedSVG = new XMLSerializer().serializeToString(resizableSVG);
canvg(this.$refs.canvas, serializedSVG);
this.pngDownload = this.diagramName + ".png";
this.pngHref = this.$refs.canvas.toDataURL();
}
},
render: function() {
this.$refs.diagramDiv.innerHTML = "";
try {
Diagram.parse(this.diagramContent).drawSVG(this.$refs.diagramDiv, {theme: "simple"});
this.diagramValid = true;
} catch (e) {
this.errorText = e.message;
this.diagramValid = false;
}
},
save: function() {
var params = new URLSearchParams();
params.append("content", this.diagramContent);
axios({
url: "/diagram/" + this.diagramName,
data: params,
headers: {"Content-Type": "application/x-www-form-urlencoded"},
method: "post"
}).then(function (response) {
this.$set(this.diagrams, this.diagramName, this.diagramContent);
}.bind(this));
},
delete_: function(key) {
axios({
url: "/diagram/" + key,
method: "delete"
}).then(function (response) {
this.$delete(this.diagrams, key);
}.bind(this)).catch(function(error) {
this.retrieve();
}.bind(this));
},
load: function(key) {
this.diagramName = key;
this.diagramContent = this.diagrams[key];
},
saveAvaliable: function() {
return this.diagramValid && this.diagramName.length > 0;
},
downloadAvaliable: function() {
return this.saveAvaliable();
},
retrieve: function() {
axios.get("/diagrams").then(function (response) {
this.diagrams = response.data;
}.bind(this));
}
},
watch: {
diagramContent: function() {
this.render();
this.download();
}
},
mounted() {
this.retrieve();
this.render();
}
});
</script>
</body>
</html>