-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
253 lines (215 loc) · 7.2 KB
/
script.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
function saveTextAsFile(downloadText,fileName,fileType)
{
var textToWrite = downloadText; //Your text input;
var textFileAsBlob = new Blob([textToWrite], {type:fileType});
var fileNameToSaveAs = fileName;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
// downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
var fileType = {};
fileType.css = 'text/css';
fileType.js = 'application/javascript';
fileType.html = 'text/html';
fileType.json = 'application/json';
var resultText ='';
var renderError = function(text) {
$(".err-panel")
.html(text)
.slideDown('slow');
setTimeout(function(){
$(".err-panel").slideUp();
},3000)
}
$(function(){
$(".upload-file-control").click(function(){
var selectFileType = $('input[name=texttype]:checked').val();
if(selectFileType == undefined) {
renderError('Please Select Any Of File Type');
return false;
}
})
$("#minfiyfile").change(function() {
var file = document.getElementById("minfiyfile").files[0];
if (file) {
var file_type = file.type ;
var selectFileType = $('input[name=texttype]:checked').val();
if (fileType[selectFileType] == file_type) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
$(".minifierContainer").val( evt.target.result);
}
reader.onerror = function (evt) {
//document.getElementById("fileContents").innerHTML = "error reading file";
}
} else {
renderError("Please select any file with ext "+selectFileType+" or change the option");
return false;
}
}
})
$("#beautyfile").change(function() {
var file = document.getElementById("beautyfile").files[0];
if (file) {
var file_type = file.type ;
var selectFileType = $('input[name=texttype]:checked').val();
if (fileType[selectFileType] == file_type) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
$(".beautifierContainer").val( evt.target.result);
}
reader.onerror = function (evt) {
//document.getElementById("fileContents").innerHTML = "error reading file";
}
} else {
renderError("Please select any file with ext "+selectFileType+" or change the option");
return false;
}
}
})
$(".modifyBtn").click(function(){
//try {
const fileType = $('input[name=texttype]:checked').val();
if (fileType == undefined) {
renderError("Please select any file type.");
return false;
}
$(".downloadBtn").attr("data-type", $(this).attr('data-type'));
if ($(this).attr('data-type') == 'beautify') {
resultText = $(".minifierContainer").val();
} else if($(this).attr('data-type') == 'minify') {
resultText = $(".beautifierContainer").val();
}
/* CSS */
if (fileType == 'css') {
if ($(this).attr('data-type') == 'beautify') {
var default_opts = {
indent_size: 4,
indent_char: ' ',
preserve_newlines: true,
jslint_happy: false,
keep_array_indentation: false,
brace_style: 'collapse',
space_before_conditional: true,
break_chained_methods: false,
selector_separator: '\n',
end_with_newline: false
};
resultText = css_beautify(resultText, default_opts);
} else if($(this).attr('data-type') == 'minify') {
resultText = cssmin(resultText,0);
}
}
/* HTML */
else if (fileType == 'html') {
if ($(this).attr('data-type') == 'beautify') {
var default_opts = {
indent_size: 4,
indent_char: ' ',
preserve_newlines: true,
jslint_happy: false,
keep_array_indentation: false,
brace_style: 'collapse',
space_before_conditional: true,
break_chained_methods: false,
selector_separator: '\n',
end_with_newline: false
};
resultText = html_beautify(resultText, default_opts);
} else if($(this).attr('data-type') == 'minify') {
resultText= resultText.replace(/>\s+</g,'><');
}
}
/* JS */
else if (fileType == 'js') {
if ($(this).attr('data-type') == 'beautify') {
var default_opts = {
indent_size: 4,
indent_char: ' ',
preserve_newlines: true,
jslint_happy: false,
keep_array_indentation: false,
brace_style: 'collapse',
space_before_conditional: true,
break_chained_methods: false,
selector_separator: '\n',
end_with_newline: false
};
resultText = js_beautify(resultText, default_opts);
} else if($(this).attr('data-type') == 'minify') {
resultText = jsmin(resultText);
}
}
/* JSON */
else if (fileType == 'json') {
resultText = JSON.parse(resultText);
if ($(this).attr('data-type') == 'beautify') {
resultText = JSON.stringify(resultText, null, 4);
} else if($(this).attr('data-type') == 'minify') {
resultText = JSON.stringify(resultText, null, 0);
}
}
/*} catch(err) {
var resultText = err.message;
}*/
if ($(this).attr('data-type') == 'beautify') {
$(".beautifierContainer").val(resultText);
} else if($(this).attr('data-type') == 'minify') {
$(".minifierContainer").val(resultText);
}
});
$(".optionBtn input[type=radio]").click(function() {
$(".optionBtn").removeClass("btn-danger");
$(this).parents(".optionBtn").addClass("btn-danger");
});
$(".downloadBtn").click(function(){
const fileType = $('input[name=texttype]:checked').val();
const type = $(this).attr("data-type");
if (fileType == undefined) {
renderError("Please select any file type.");
return false;
} else if(type == undefined || resultText == '') {
renderError("Please do any minify or beautify first");
return false;
}
var fileName = "download";
if ((fileType=='css' || fileType == 'js') && type == 'minify') {
fileName +=".min";
}
fileName += "."+fileType;
$("input[name=downloadfilename]").val(fileName);
$("#downloadModal").modal("show");
});
$(".downloadMainBtn").click(function(){
var fType = $('input[name=texttype]:checked').val();
var filename = $("input[name=downloadfilename]").val();
if (filename == '' ){
alert("Please Enter any name");
return false;
}
saveTextAsFile(resultText,filename,fileType[fType]);
$("#downloadModal").modal("hide");
})
});