-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (174 loc) · 4.96 KB
/
app.js
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
const TYPES = [ 'json', 'xml', 'css', 'sql'];
var current_type = 'json';
var switchParser = (type) => {
// remove scurrent menu color
document.getElementById(current_type).style.color = "#007bff";
// need to check exists enum
if (TYPES.includes(type)) {
current_type = type;
}
// update to new menu color
document.getElementById(current_type).style.color = "#ff5200";
window.localStorage.setItem('type', current_type);
var input = window.localStorage.getItem(current_type);
if (input) {
var validate = isValidInputData(current_type, input);
if (validate === true) {
document.getElementById("input").value = input;
} else {
window.localStorage.removeItem(current_type);
clearInput();
}
} else {
clearInput();
}
clearOutput();
}
// For JSON
var validateJson = (txt) => {
try {
JSON.parse(txt);
} catch (e) {
return e;
}
return true;
}
// For XML
var validateXml = (txt) => {
const parser = new window.DOMParser();
const dom = parser.parseFromString(txt, "text/xml");
if (dom.getElementsByTagName('parsererror').length > 0) {
return dom.getElementsByTagName('parsererror')[0].getElementsByTagName('div')[0].innerHTML;
}
return true;
}
var isValidInputData = (type, txt) => {
let result;
switch(type) {
case "json":
result = validateJson(txt);
break;
case "xml":
result = validateXml(txt);
break;
case "css":
result = true;
break;
case "sql":
result = true;
break;
default:
result = "Invalid type";
}
return result;
}
// Init page
var initPage = () => {
let type = window.localStorage.getItem('type');
if (!type || !TYPES.includes(type)) {
window.localStorage.setItem('type', current_type);
} else {
current_type = type;
}
// update menu color
document.getElementById(current_type).style.color = "#ff5200";
// Load history data by type
var input = window.localStorage.getItem(current_type);
if (input) {
var validate = isValidInputData(current_type, input);
if (validate === true) {
document.getElementById("input").value = input;
} else {
window.localStorage.removeItem(current_type);
console.log(validate);
}
}
}
initPage();
// Common
var parseInputData = (type, input) => {
let result = input;
switch(type) {
case "json":
// result = JSON.stringify(JSON.parse(input), null, 4);
result = vkbeautify.json(input.trim(), 4);
break;
case "xml":
result = vkbeautify.xml(input.trim());
break;
case "css":
result = vkbeautify.css(input.trim(), ' ');;
break;
case "sql":
result = vkbeautify.sql(input.trim(), ' ');
break;
default:
result = "Invalid type";
}
return result;
}
var compressInputData = (type, input) => {
let result = input;
switch(type) {
case "json":
// result = JSON.stringify(JSON.parse(input), null, '');
result = vkbeautify.jsonmin(input.trim());
break;
case "xml":
result = vkbeautify.xmlmin(input.trim(), true);
break;
case "css":
result = vkbeautify.cssmin(input.trim());;
break;
case "sql":
result = vkbeautify.sqlmin(input.trim());
break;
default:
result = "Invalid type";
}
return result;
}
var parseData = () => {
var input = document.getElementById("input").value;
if (!input) {
document.getElementById("output").value = "Input is empty";
return;
}
var validate = isValidInputData(current_type, input);
if (validate !== true) {
document.getElementById("output").value = validate;
return;
}
document.getElementById("output").value = parseInputData(current_type, input);
window.localStorage.setItem(current_type, input)
}
var compressData = () => {
var input = document.getElementById("input").value;
if (!input || input.trim() === "") {
document.getElementById("output").value = "Input value is empty";
return;
}
var validate = isValidInputData(current_type, input);
if (validate !== true) {
document.getElementById("output").value = validate;
return;
}
document.getElementById("output").value = compressInputData(current_type, input);
window.localStorage.setItem(current_type, input)
}
var copyData = (id) => {
var copyText = document.getElementById(id);
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
document.execCommand("copy");
}
var clearInput = () => {
document.getElementById("input").value = "";
}
var clearOutput = () => {
document.getElementById("output").value = "";
}
var clearAll = () => {
clearOutput();
clearInput();
}