Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2081a60

Browse files
add download button, disable copy method on timeout
1 parent f17fc96 commit 2081a60

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

‎spring/src/main/kotlin/com/fractalwrench/json2kotlin/ConversionController.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ConversionController {
3232
val os = conversionService.convert(conversionForm.json)
3333
model.addAttribute(formKey, conversionForm)
3434
model.addAttribute("kotlin", String(os.toByteArray()))
35+
model.addAttribute("filename", "Foo.kt") // FIXME!
3536
return displayConversionForm(model)
3637
}
3738

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function validateForm() {
2+
var json = document.forms["jsonForm"]["json"].value;
3+
try {
4+
JSON.parse(json);
5+
} catch (e) {
6+
alert("Input was not interpreted as valid JSON.");
7+
return false;
8+
}
9+
}
10+
11+
function copyToClipboard() {
12+
var kotlinSource = document.getElementById("kotlinText");
13+
kotlinSource.select();
14+
document.execCommand("Copy");
15+
disableCopyBtn();
16+
}
17+
18+
function disableCopyBtn() {
19+
var btn = document.getElementById("copyBtn");
20+
btn.disabled = true;
21+
btn.value = "Copied";
22+
setTimeout(enableCopyBtn, 1000);
23+
}
24+
25+
function enableCopyBtn() {
26+
var btn = document.getElementById("copyBtn");
27+
btn.disabled = false;
28+
btn.value = "Copy to Clipboard";
29+
}
30+
31+
See: https://stackoverflow.com/a/18197341/5144991
32+
function downloadAsFile() {
33+
var filename = document.getElementById("filename").value;
34+
var text = document.getElementById("kotlinText").value;
35+
36+
var element = document.createElement('a');
37+
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
38+
element.setAttribute('download', filename);
39+
element.style.display = 'none';
40+
document.body.appendChild(element);
41+
element.click();
42+
document.body.removeChild(element);
43+
}

‎spring/src/main/resources/templates/conversion.html‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ <h1>Convert JSON to Kotlin Data Class</h1>
3535
</div>
3636
<div class="j2kcontainer" th:if="${kotlin != null}">
3737
<h2 id="results">Generated Kotlin</h2>
38+
39+
<input type="hidden" id="filename" th:value="${filename}"/>
3840
<textarea id="kotlinText" th:text="${kotlin}"></textarea>
3941

40-
<form id="downloadForm" action="#" method="post" onsubmit="return validateForm()">
41-
<p>
42-
<input type="button" value="Copy to Clipboard"
43-
onclick="copyToClipboard()" />
44-
<input type="button" value="Download"/>
45-
</p>
46-
</form>
42+
<p>
43+
<input id="copyBtn" type="button" value="Copy to Clipboard"
44+
onclick="copyToClipboard()" />
45+
<input type="button" value="Download" onclick="downloadAsFile()"/>
46+
</p>
47+
4748
</div>
4849
<div class="j2kcontainer">
4950
<!-- TODO -->
@@ -71,23 +72,7 @@ <h2 id="results">Generated Kotlin</h2>
7172
<link rel="stylesheet" type="text/css" href="/main.css"/>
7273
<link rel="stylesheet" type="text/css" href="/fonticons.css"/>
7374

74-
<script type="text/javascript">
75-
function validateForm() {
76-
var json = document.forms["jsonForm"]["json"].value;
77-
try {
78-
JSON.parse(json);
79-
} catch (e) {
80-
alert("Input was not interpreted as valid JSON.");
81-
return false;
82-
}
83-
}
84-
85-
function copyToClipboard() {
86-
var kotlinSource = document.getElementById("kotlinText");
87-
kotlinSource.select();
88-
document.execCommand("Copy");
89-
}
90-
</script>
75+
<script src="/main.js"></script>
9176

9277
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
9378
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /