-
Notifications
You must be signed in to change notification settings - Fork 2
/
codegenerator.js
493 lines (429 loc) · 16.1 KB
/
codegenerator.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
var textAreaInput, textAreaOutput;
var views;
var initialized = false;
var cOutputMode = "cookieOutputMode";
var cAddPrefix = "cookieAddPrefix";
var cDoTypeCast ="cookieDoTypeCast";
var outputType = 1; //1: Activity 2: Fragment 3: ButterKnife 4: ButterKnifeLib
var liActivities, liFragments, liButterKnife, liButterKnifeLib, lis, chkAddPrefix, chkDoTypeCast;
/*This will set initial value for input and set output for the same.
It sets event listener to inputArea so that on every change in inputText will regenerate output.
Then it focuses on inputArea so user can directly paste it with out additional click*/
function init() {
textAreaInput = document.getElementById("textAreaInput");
textAreaOutput = document.getElementById("textAreaOutput");
liActivities = document.getElementById("li-activity");
liFragments = document.getElementById("li-fragment");
liButterKnife = document.getElementById("li-butterknife");
liButterKnifeLib = document.getElementById("li-butterknifeLib");
chkAddPrefix = document.getElementById("checkbox_add_prefix");
chkDoTypeCast = document.getElementById("checkbox_do_type_cast");
textAreaInput.value = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:tools=\"http://schemas.android.com/tools\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\">\n\n <LinearLayout\n android:id=\"@+id/linear_parent\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\">\n\n <EditText\n android:id=\"@+id/edittext_userName\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"/>\n\n <EditText\n android:id=\"@+id/edittext_password\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"/>\n\n <Button\n android:id=\"@+id/button_login\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"/>\n</RelativeLayout>\n";
textAreaInput.addEventListener("input", on_input_updated, false);
lis = new Array();
lis.push(liActivities);
lis.push(liFragments);
lis.push(liButterKnife);
lis.push(liButterKnifeLib);
set_do_type_cast_from_cookie();
set_add_prefix_check_from_cookie();
update_output_type(getOutputModeFromCookie());
textAreaInput.focus();
}
function set_add_prefix_check_from_cookie() {
var storedValue = getCookie(cAddPrefix);
if (storedValue == "") {
storedValue = "false";
}
if (storedValue == "false") {
chkAddPrefix.checked = false;
} else {
chkAddPrefix.checked = true;
}
}
function set_do_type_cast_from_cookie() {
var storedCastValue = getCookie(cDoTypeCast);
if (storedCastValue == "") {
storedCastValue = "false";
}
if (storedCastValue == "false") {
chkDoTypeCast.checked = false;
} else {
chkDoTypeCast.checked = true;
}
}
function getOutputModeFromCookie() {
var storedValue = getCookie(cOutputMode);
if (storedValue == "") { //means no value stored
return 1; //default is for activity output
} else {
return storedValue;
}
}
function update_output_type(typeID) {
outputType = typeID;
for (var i = 0; i < lis.length; i++) {
if (i + 1 == typeID) { //active type
setCookie(cOutputMode, i + 1);
lis[i].className = "li-custom active";
} else {
lis[i].className = "li-custom";
}
};
updateOutput();
}
function on_input_updated() {
if (initialized) {
ga('send', 'inputUpdated');
}
updateOutput();
}
/*This will process on inputText and puts according output in output area.
*/
function updateOutput() {
// initialize views array
views = new Array();
// checks inputText and fill views array
readAllViews();
// final output variable
var finalResult = "";
if (outputType == 1) {
finalResult = getOutputForActivity();
} else if (outputType == 2) {
finalResult = getOutputForFragment();
} else if (outputType == 3) {
finalResult = getOutputForButterKnife();
} else if (outputType == 4) {
finalResult = getOutputForButterKnifeLib();
}
console.log("output is " + finalResult);
//analytics
if (initialized) {
ga('send', 'event', 'codeGeneration', 'outPut updated', finalResult);
}
//sets final result to output
textAreaOutput.value = finalResult;
//sets foucus on output and select entire text of output. Now developer is only cltr + c press ways from copy the code.
textAreaOutput.focus();
//set flag
initialized = true;
}
function getOutputForButterKnife() {
var finalResult = "";
console.log("I'm in function butterknife");
//when atleast 1 view with android:id is found, appends output of each single view to finalResult
if (views.length != 0) {
finalResult = "/** ButterKnife Code **/";
for (var i = 0; i < views.length; i++) {
var view = views[i];
var singleOutPut = getOutputLineForButterKnifeView(view);
finalResult = finalResult + singleOutPut;
}
finalResult = finalResult + "\n /** ButterKnife Code **/"
} else {
finalResult = "/**No view found**/ ";
}
return finalResult;
}
function getOutputForButterKnifeLib() {
var finalResult = "";
console.log("I'm in function butterknife");
//when atleast 1 view with android:id is found, appends output of each single view to finalResult
if (views.length != 0) {
finalResult = "/** ButterKnife Code **/";
for (var i = 0; i < views.length; i++) {
var view = views[i];
var singleOutPut = getOutputLineForButterKnifeLibView(view);
finalResult = finalResult + singleOutPut;
}
finalResult = finalResult + "\n /** ButterKnife Code **/"
} else {
finalResult = "/**No view found**/ ";
}
return finalResult;
}
function onCheckBoxClicked(argument) {
if (initialized) {
setCookie(cDoTypeCast, "True");
setCookie(cAddPrefix, chkAddPrefix.checked);
console.log("check box cast values"+chkAddPrefix.checked+":"+chkDoTypeCast.checked);
console.log("prefix cast value:"+getCookie(cAddPrefix));
console.log("Type cast value:"+getCookie(cDoTypeCast));
updateOutput();
}
}
function getOutputForActivity() {
var finalResult = "Activity Output";
var viewDeclaration = getViewDeclaration();
var activityBinder = getActivityBinderCode();
return viewDeclaration + "\n\n" + activityBinder;
}
function getOutputForFragment() {
var finalResult = "Fragment output";
var viewDeclaration = getViewDeclaration();
var fragmentBinder = getFragmentBinderCode();
return viewDeclaration + "\n\n" + fragmentBinder;
}
function getViewDeclaration() {
var finalResult = "";
if (views.length != 0) {
finalResult = "/** Android Views **/";
for (var i = 0; i < views.length; i++) {
var view = views[i];
var singleOutPut = getOutputLineForViewDeclaration(view);
finalResult = finalResult + singleOutPut;
}
finalResult = finalResult + "\n /** Android Views **/"
} else {
finalResult = "/**No view found**/ ";
}
return finalResult;
}
function getActivityBinderCode() {
var finalResult = "";
if (views.length != 0) {
finalResult = "/**\n * Binds XML views \n * Call this function after setContentView() in onCreate().\n**/ ";
finalResult = finalResult + " \nprivate void bindViews(){"
for (var i = 0; i < views.length; i++) {
var view = views[i];
var singleOutPut = getOutputLineForActivityBind(view);
finalResult = finalResult + "\n" + singleOutPut;
}
finalResult = finalResult + "\n}"
}
return finalResult;
}
function getFragmentBinderCode() {
var finalResult = "";
if (views.length != 0) {
finalResult = "/**\n * Binds XML views \n * Call this function after layout is ready.\n**/ ";
finalResult = finalResult + " \nprivate void bindViews(View rootView){"
for (var i = 0; i < views.length; i++) {
var view = views[i];
var singleOutPut = getOutputLineForFragmentBind(view);
finalResult = finalResult + "\n" + singleOutPut;
}
finalResult = finalResult + "\n}"
}
return finalResult;
}
/*This will find each and every view from input text
if the view has 'android:id=' then that view will be added to views*/
function readAllViews() {
var xmlText = textAreaInput.value;
var lastViewEnd = -1;
while (lastViewEnd <= xmlText.length) {
if (xmlText.indexOf("<", lastViewEnd + 1) > -1) {
var viewBeginIndex = xmlText.indexOf("<", lastViewEnd + 1);
var viewEndIndex = xmlText.indexOf(">", viewBeginIndex);
if (viewBeginIndex > -1 && viewEndIndex > -1) {
var view = xmlText.substring(viewBeginIndex, viewEndIndex + 1);
if (isEligibleView(view)) {
views.push(view);
}
lastViewEnd = viewEndIndex;
} else {
break;
}
} else {
break;
}
}
}
/*If view has "android:id" and className and id are not of length 0 then retun true or false otherwise.*/
function isEligibleView(view) {
if (view.indexOf("android:id=") > -1) {
if (getClassNameForView(view).trim().length > 0 && getIdFromView(view).trim().length > 0) {
console.log("Eligibility: true=>" + view);
return true;
} else {
console.log("Eligibility: false=>" + view);
return false;
}
} else {
console.log("Eligibility noID: false=>" + view);
return false;
}
}
/*
Returns output for single view.
this is sample oneViewXml
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
output will be=>
@Bind(R.id.loginButton)
Button loginButton;
*/
function getOutputLineForButterKnifeView(oneViewXml) {
// ga('send', 'event', 'Process', 'singleOutPut request', "");
return "\n@BindView(R.id." + getIdFromView(oneViewXml).trim() + ") \n" + getClassNameForView(oneViewXml).trim() + " " + getJavaNameForView(oneViewXml).trim() + ";";
}
/*
Returns output for single view.
this is sample oneViewXml
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
output will be=>
@BindView(R.id.loginButton)
Button loginButton;
*/
function getOutputLineForButterKnifeLibView(oneViewXml) {
// ga('send', 'event', 'Process', 'singleOutPut request', "");
return "\n@BindView(R2.id." + getIdFromView(oneViewXml).trim() + ") \n" + getClassNameForView(oneViewXml).trim() + " " + getJavaNameForView(oneViewXml).trim() + ";";
}
/*
Returns output for single view.
this is sample oneViewXml
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
output will be=>
Button loginButton;
*/
function getOutputLineForViewDeclaration(oneViewXml) {
// ga('send', 'event', 'Process', 'singleOutPut request', "");
return "\n" + getClassNameForView(oneViewXml).trim() + " " + getJavaNameForView(oneViewXml).trim() + ";";
}
/*
Returns output for single view.
this is sample oneViewXml
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
output will be=>
loginButton = (Button)findViewById(R.id.login_button);
*/
function getOutputLineForActivityBind(oneViewXml) {
// ga('send', 'event', 'Process', 'singleOutPut request', "");
return "\t " + getJavaNameForView(oneViewXml).trim() + " = " + getClassNameForViewCast(oneViewXml).trim() + " findViewById(R.id." + getIdFromView(oneViewXml).trim() + ");";
}
/*
Returns output for single view.
this is sample oneViewXml
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
output will be=>
loginButton = (Button)rootView.findViewById(R.id.login_button);
*/
function getOutputLineForFragmentBind(oneViewXml) {
// ga('send', 'event', 'Process', 'singleOutPut request', "");
return "\t " + getJavaNameForView(oneViewXml).trim() + " = " + getClassNameForViewCast(oneViewXml).trim() + " rootView.findViewById(R.id." + getIdFromView(oneViewXml).trim() + ");";
}
/*this will return class name from xml view
sample view
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
returns=>
"Button"
*/
function getClassNameForView(oneViewXml) {
var output;
var firstAngularIndex = oneViewXml.indexOf("<");
if (firstAngularIndex > -1) {
var firstSpaceIndex = oneViewXml.indexOf(" ", firstAngularIndex);
output = oneViewXml.substring(firstAngularIndex + 1, firstSpaceIndex);
return output;
}
return "Object";
}
/**
* If typecasting is enabled, then returns class with () otherwise none
* @param {XML View} oneViewXml
*/
function getClassNameForViewCast(oneViewXml) {
if(chkDoTypeCast.checked){
return "("+getClassNameForView(oneViewXml).trim()+")";
}else{
return "";
}
}
/*this will return ID from xml view
sample view
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
returns=>
"login_button"
*/
function getIdFromView(oneViewXml) {
if (oneViewXml.indexOf("android:id=") > -1) {
var lineBegin = oneViewXml.indexOf("android:id=");
var lineFirstComma = oneViewXml.indexOf('/', lineBegin);
var lineLastComma = oneViewXml.indexOf('"', lineFirstComma + 1);
var outPut = lineBegin + ":linebegin " + lineFirstComma + ":lineFirstComma " + lineLastComma + ":lineLastComma";
outPut = oneViewXml.substring(lineFirstComma + 1, lineLastComma);
return outPut;
} else {
return "";
}
}
/*this will return javaname from xml view
sample view
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
returns=>
"loginButton"
if add m prefix is selected, it will be mLoginButton
*/
function getJavaNameForView(oneViewXml) {
var xmlId = getIdFromView(oneViewXml).trim();
if (chkAddPrefix.checked) {
xmlId = "m_" + xmlId;
}
var javaName = "";
var underScoreIndex = xmlId.indexOf("_");
if (underScoreIndex == -1) { //no '_' in id then use id as name
javaName = xmlId;
} else {
var tokens = xmlId.split("_");
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i].trim();
if (token.length > 0) {
var firstChar = token.charAt(0);
var capToken = "";
if (javaName.length == 0) {
capToken = token;
} else {
capToken = firstChar.toUpperCase() + token.slice(1);
}
javaName = javaName + capToken;
}
}
}
// ga('send', 'event', 'Process', 'javaname generation', "xmlId:" + xmlId + "; javaName:" + javaName);
return javaName;
}
// get cookie and set cookie code is from w3schools
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); //30 days
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}