-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget-input-generate-output.js
307 lines (226 loc) · 9.32 KB
/
get-input-generate-output.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
$(document).ready(function(){
$("#submit").click(function(){
var editor = ace.edit("editor-input-code");
var xmlCodeEntered = editor.getValue();
// remove empty lines
// imp - 1
xmlCodeEntered = xmlCodeEntered.split("\n").map($.trim).filter(function(line) { return line != "" }).join("\n");
var modelNameEntered = $("#model-class-name").val();
var adapterNameEntered = $("#adapter-class-name").val();
var rowXMLNameEntered = $("#row-xml-name").val();
modelNameEntered = modelNameEntered.replace(".java", "");
adapterNameEntered = adapterNameEntered.replace(".java", "");
rowXMLNameEntered = rowXMLNameEntered.replace(".xml", "");
modelNameEntered = modelNameEntered.replace(".Java", "");
adapterNameEntered = adapterNameEntered.replace(".Java", "");
rowXMLNameEntered = rowXMLNameEntered.replace(".Xml", "");
modelNameEntered = modelNameEntered.replace(".JAVA", "");
adapterNameEntered = adapterNameEntered.replace(".JAVA", "");
rowXMLNameEntered = rowXMLNameEntered.replace(".XML", "");
// making global variables so that we can use it in other JS files
window.modelNameEntered = modelNameEntered;
window.adapterNameEntered = adapterNameEntered;
window.rowXMLNameEntered = rowXMLNameEntered;
var dataString = 'xmlCode='+xmlCodeEntered+'&modelName='+modelNameEntered+"&adapterName="+adapterNameEntered;
// alert(modelNameEntered + " " + adapterNameEntered);
// check and show 'Android Toast like error' for empty String or String with just white spaces
if( (xmlCodeEntered=='') || (/^\s+$/.test(xmlCodeEntered)) ||
(modelNameEntered=='') || (/^\s+$/.test(modelNameEntered)) ||
(adapterNameEntered=='') || (/^\s+$/.test(adapterNameEntered)) ||
(rowXMLNameEntered=='') || (/^\s+$/.test(rowXMLNameEntered))
)
{
$('body').scrollTop(0);
$('.error').fadeIn(400).delay(3000).fadeOut(400);
}
else
{
// AJAX Code To Submit Form data to 'process.php'
$.ajax({
type: "POST",
url: "./process.php",
data: dataString,
cache: false,
success: function(result){
// when getting the result from the PHP script, hide the TextArea and change the text in the breadcrumb and display the generated code
$('#submit').hide();
$('#editor-input-code').hide();
$("#input-names-boxes-and-titles").hide();
$(".breadcrumb").text("Your code is here");
$('.output-form').show();
// The PHP script that we are getting the result from is sending a Json encoded array
// That thing here in Javascript variable 'result' is a JSON object and inside which are 2 JSON objects -
// View and id
// JSON parsing for the JSON we are getting from PHP script
var json = JSON.parse(result);
var Views = [];
var ids = [];
for(var key in json) {
if(json.hasOwnProperty(key)) {
if(key === 'View') {
for(var innerKey in json[key]) {
if(json[key].hasOwnProperty(innerKey)) {
Views.push(json[key][innerKey]);
// alert(Views);
}
}
}
else if(key === 'id'){
for(var innerKey in json[key]) {
if(json[key].hasOwnProperty(innerKey)) {
json[key][innerKey] = json[key][innerKey].replace('@','');
json[key][innerKey] = json[key][innerKey].replace('+','');
json[key][innerKey] = json[key][innerKey].replace('id/','');
ids.push(json[key][innerKey]);
// alert(ids);
}
}
}
}
}
if(ids[0] == null){
$('#submit').show();
$('#editor-input-code').show();
$("#input-names-boxes-and-titles").show();
$(".breadcrumb").text("This tool generates Model class and Adapter class code along with the RecyclerView implementaion code");
$('.output-form').hide();
$('.error').fadeIn(400).delay(3000).fadeOut(400).text("May be you forgot to give 'android:id' in your Views. Try again!");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//generate the output codes here
//********************************************************************************************************************
//1. the Model class first
var editor = ace.edit("editor-output-model");
editor.session.setOptions({
mode: "ace/mode/java",
tabSize: 8,
useSoftTabs: true
});
// Clear the editor first before composing code and setting on it
editor.setValue("");
// code for Class start
var temp = "public class "+capitalizeFirstLetter(window.modelNameEntered)+" {\n\n"+
" private String";
editor.insert(temp);
// code to declare variables along with the 'public String' 3 lines above
for (var i = 0; i < ids.length; i++){
var temp = ids[i];
editor.insert(temp);
if((i+1) != ids.length){
editor.insert(",");
}
}
editor.insert(";");
editor.insert("\n");
editor.insert("\n");
// code to define setter and getter methods
for (var i = 0; i < ids.length; i++){
// every id has an extra space now, i don't know why
// but delete those extra space
ids[i] = ids[i].replace(" ", "");
editor.insert("public String get"+capitalizeFirstLetter(ids[i])+"() {");
var temp = "\n";
editor.insert(temp);
editor.insert("return "+ids[i]+";");
editor.insert(temp);
editor.insert(" }");
editor.insert(temp);
editor.insert("public void set"+capitalizeFirstLetter(ids[i])+"(String "+ids[i]+") {");
editor.insert(temp);
editor.insert("this."+ids[i]+" = "+ids[i]+";");
editor.insert(temp);
editor.insert(" }");
editor.insert(temp);
editor.insert(temp);
}
editor.insert("}");
editor.scrollToLine(1, true, true, function () {});
//********************************************************************************************************************
//2. the Adapter class second
var editor2 = ace.edit("editor-output-adapter");
editor2.session.setOptions({
mode: "ace/mode/java",
tabSize: 8,
useSoftTabs: true
});
// Clear the editor first before composing code and setting on it
editor2.setValue("");
jQuery.get('./files/adapter.txt', function(data) {
editor2.insert(data);
editor2.find('TagAdapter');
editor2.replaceAll(capitalizeFirstLetter(window.adapterNameEntered));
editor2.find('TagsModel');
editor2.replaceAll(capitalizeFirstLetter(window.modelNameEntered));
// Views declaration part inside 'public static class Viewholder extends RecyclerView.ViewHolder'
editor2.find("public TextView tagText;");
var temp3 = "";
for(var j = 0; j<Views.length; j++){
if(j == 0){
temp3 = temp3 + "public " + Views[j] + " " + ids[j] + ";" + "\n";
}
else{
// add extra spacing for 2nd row onwards for Views definition as they don't get indented in ace editor
temp3 = temp3 + " public " + Views[j] + " " + ids[j] + ";" + "\n";
}
}
editor2.replace(temp3);
// Views definitiom part inside 'public static class Viewholder extends RecyclerView.ViewHolder'
editor2.find("tagText = (TextView)itemView.findViewById(R.id.tag_text);");
var temp4 = "";
for(var k = 0; k<Views.length; k++){
if(k == 0){
temp4 = temp4 + ids[k] + " = " + "(" + Views[k] + ")" + "itemView.findViewById(R.id." + ids[k] + ")" + ";" + "\n";
}
else{
// add extra spacing for 2nd row onwards for Views declaration as they don't get indented in ace editor
temp4 = temp4 + " " + ids[k] + " = " + "(" + Views[k] + ")" + "itemView.findViewById(R.id." + ids[k] + ")" + ";" + "\n";
}
}
editor2.replace(temp4);
// Replace the XML row layout name in the adapter
editor2.find("R.layout.tags_row");
var temp10 = "R.layout."+window.rowXMLNameEntered;
editor2.replace(temp10);
editor2.setValue(editor2.getValue(), 1);
editor2.scrollToLine(1, true, true, function () {});
});
//********************************************************************************************************************
//3. the implementation codes third
var editor3 = ace.edit("editor-output-implementation");
editor3.session.setOptions({
mode: "ace/mode/java",
tabSize: 8,
useSoftTabs: true
});
// Clear the editor first before composing code and setting on it
editor3.setValue("");
jQuery.get('./files/implementation.txt', function(data) {
editor3.insert(data);
editor3.find("YourModelName");
editor3.replaceAll(capitalizeFirstLetter(window.modelNameEntered));
editor3.find("YourAdapterName");
editor3.replaceAll(capitalizeFirstLetter(window.adapterNameEntered));
editor3.find("model.<SetterMethod>();");
var temp5 = "";
for(var l = 0; l < ids.length; l++){
if(l == 0){
temp5 = temp5 + 'model.set'+capitalizeFirstLetter(ids[l])+'("some dummy data");'+"\n";
}
else{
temp5 = temp5 + ' model.set'+capitalizeFirstLetter(ids[l])+'("some dummy data");'+"\n";
}
}
editor3.replace(temp5);
editor3.setValue(editor3.getValue(), 1);
editor3.scrollToLine(1, true, true, function () {});
});
}
});
}
return false;
});
});
// useful functions defined
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}