forked from roojs/Pman.Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pman.Builder.JsRender.js
594 lines (463 loc) · 16.7 KB
/
Pman.Builder.JsRender.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* This is baed on the code in the app builder..
*
*
* usage:
*
<pre><code>
render = new Pman.Builder.JsRender(
Pman.Builder.Tree.toJS()
);
var source = render.toSource();
</code></pre>
*
* @cfg {Array} items - the array of items..
* @cfg {String} name - eg. Pman.Tab.XXXXyyyyy
* @cfg {String} path - needs the full path << we only have the class name..
* @cfg {String} region - ?? needed anymore?
* @cfg {String} parent
* @cfg {String} title
* @cfg {String} disable? function call to disable it?
* @cfg {String} permname
* @cfg {Number} modOrder
* @cfg {Number} modkey ???
*
*
*/
Pman.Builder.JsRender = function(cfg) {
Roo.apply(this, cfg);
}
Pman.Builder.JsRender.prototype = {
name : '',
path : '', // the file path..
modOrder : '001', /// sequence id that this uses.
region : 'center',
parent : '',
title : '', // the title on displayed when loading.
disable : '', // use a function to that returns false to disable this..
permname: '', /// permission name
items: false,
/**
* @type {Object} the properties that have to be double quoted to enable translation
*/
doubleStringProps : [
'title',
'legend',
'loadingText',
'emptyText',
'qtip',
'value',
'text',
'emptyMsg',
'displayMsg'
],
/**
*
* munge JSON tree into Javascript code.
*
* FIXME: + or / prefixes to properties hide it from renderer.
* FIXME: '*props' - not supported by this.. ?? - upto rendering code..
* FIXME: needs to understand what properties might be translatable (eg. double quotes)
*
* @arg {object} obj the object or array to munge..
* @arg {boolean} isListener - is the array being sent a listener..
* @arg {string} pad - the padding to indent with.
*/
mungeToString: function(obj, isListener, pad)
{
pad = pad || ' ';
var keys = [];
var isArray = false;
isListener = isListener || false;
// am I munging a object or array...
if (obj.constructor.toString() === Array.toString()) {
for (var i= 0; i < obj.length; i++) {
keys.push(i);
}
isArray = true;
} else {
for (var i in obj) {
keys.push(i);
}
}
var els = [];
var skip = [];
if (!isArray &&
typeof(obj['|xns']) != 'undefined' &&
typeof(obj['xtype']) != 'undefined'
) {
this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els);
//els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
skip.push('|xns','xtype');
}
if (!isArray && obj.items && obj.items.length) {
// look for props..
var newitems = [];
obj.items.forEach(function(pl) {
if (typeof(pl['*prop']) == 'undefined') {
newitems.push(pl);
return;
}
//print(JSON.stringify(pl,null,4));
// we have a prop...
var prop = pl['*prop'] + '';
delete pl['*prop'];
if (!prop.match(/\[\]$/)) {
// it's a standard prop..
// munge property..??
obj[prop] = pl;
keys.push(prop);
return;
}
prop = prop.substring(0, prop.length -2); //strip []
// it's an array type..
obj[prop] = obj[prop] || [];
obj[prop].push(pl);
// print("ADDNG PROP:" + prop + ' ' + keys.indexOf(prop) );
if (keys.indexOf(prop) < 0) {
keys.push(prop);
}
});
obj.items = newitems;
if (!obj.items.length) {
delete obj.items;
}
}
//if (isArray) { print(JSON.stringify(keys, null,4)); }
// keys is just the real keys of the object.
var _this = this;
var left = '';
keys.forEach(function(i) {
if (i[0] == '.') { // skip builder data.
return;
}
if (typeof(obj[i]) == 'undefined') { // empty or removed.
return;
}
var el = obj[i];
if (!isArray && skip.indexOf(i) > -1) { // things we do not write..
return;
}
if (!isArray) {
// set the key to be quoted with singel quotes..
var leftv = i[0] == '|' ? i.substring(1) : i;
if (Pman.Builder.Lang.isKeyword(leftv) || Pman.Builder.Lang.isBuiltin(leftv)) {
left = "'" + leftv + "'";
} else if (leftv.match(/[^A-Z_]+/i)) { // not plain a-z... - quoted.
var val = JSON.stringify(leftv);
left = "'" + val.substring(1, val.length-1).replace(/'/g, "\\'") + "'";
} else {
left = '' + leftv;
}
left += ' : ';
}
if (isListener) {
// change the lines...
var str= ('' + obj[i]).replace(/^\s+|\s+$/g,""); // remove bar.
var lines = str.split("\n");
if (lines.length > 1) {
str = lines.join("\n" + pad);
}
els.push(left + str);
return;
}
//var left = isArray ? '' : (JSON.stringify(i) + " : " )
if (i[0] == '|') {
// does not hapepnd with arrays..
if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
return;
}
// this needs to go...
//if (typeof(el) == 'string' && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
// return;
//}
var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
var lines = str.split("\n");
if (lines.length > 1) {
str = lines.join("\n" + pad);
}
els.push(left + str);
return;
}
if (typeof(el) == 'object') {
// we can skip empty items lists and empty listeners..
//if (!isArray && i == 'items' && !el.length) {
// return;
//}
//
var right = _this.mungeToString(el, i == 'listeners', pad + ' ');
//if (!left.length && isArray) print(right);
if ((typeof(right) != 'undefined') && right.length){
els.push(left + right);
}
return;
}
// standard. .
if (typeof(obj[i]) != 'string') {
els.push(left + JSON.stringify(obj[i]));
return;
}
// strings..
if (!_this.doubleStringProps) {
els.push(left + JSON.stringify(obj[i]));
return;
}
if (_this.doubleStringProps.indexOf(i) > -1) {
els.push(left + JSON.stringify(obj[i]));
return;
}
// single quote..
els.push(left + "'" + obj[i].replace(/'/g, "\\'") + "'");
});
if (!isArray && !els.length) {
return '';
}
//output the thing.
var spad = pad.substring(0, pad.length-4);
return (isArray ? '[' : '{') + "\n" +
pad + els.join(",\n" + pad ) +
"\n" + spad + (isArray ? ']' : '}');
},
setNSID : function(id)
{
this.items[0]['|module'] = id;
},
getType: function() {
return 'Roo';
},
/**
* old code had broken xtypes and used arrays differently,
* this code should try and clean it up..
*
*
*/
fixItems : function(node, fixthis)
{
if (fixthis) {
// fix xtype.
var fn = this.guessName(node);
//print("guessname got " + fn);
if (fn) {
var bits = fn.split('.');
node.xtype = bits.pop();
node['|xns'] = bits.join('.');
}
// fix array???
}
if (!node.items || !node.items.length) {
return;
}
var _this = this;
var aitems = [];
var nitems = [];
node.items.forEach(function(i) {
_this.fixItems(i, true);
if (i.xtype == 'Array') {
aitems.push(i);
return;
}
nitems.push(i);
});
node.items = nitems;
if (!aitems.length) {
return;
}
aitems.forEach(function(i) {
if (!i.items || !i.items.length) {
return;
}
var prop = i['*prop'] + '[]';
// colModel to cm?
i.items.forEach(function(c) {
c['*prop'] = prop;
node.items.push(c);
});
});
// array handling..
},
/**
* convert xtype for munged output..
*
*/
mungeXtype : function(xtype, els)
{
var bits = xtype.split('.');
// assume it has lenght!
els.push("xtype: '"+ bits.pop()+"'");
els.push('xns: '+ bits.join('.'));
},
/**
* This needs to use some options on the project
* to determine how the file is output..
*
* At present we are hard coding it..
*
*
*/
toSource: function()
{
// dump the file tree back out to a string.
// we have 2 types = dialogs and components
//
var top = this.guessName(this.items[0]);
if (!top) {
return false;
}
if (top.match(/Dialog/)) {
return this.toSourceDialog();
}
return this.toSourceLayout();
/*
eventually support 'classes??'
return this.toSourceStdClass();
*/
},
outputHeader : function()
{
return [
"//<script type=\"text/javascript\">",
"",
"// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
""
].join("\n");
},
// a standard dialog module.
// fixme - this could be alot neater..
toSourceDialog : function()
{
var items = JSON.parse(JSON.stringify(this.items[0]));
var o = this.mungeToString(items, false, ' ');
return [
this.outputHeader(),
this.name + " = {",
"",
" dialog : false,",
" callback: false,",
"",
" show : function(data, cb)",
" {",
" if (!this.dialog) {",
" this.create();",
" }",
"",
" this.callback = cb;",
" this.data = data;",
" this.dialog.show(this.data._el);",
" if (this.form) {",
" this.form.reset();",
" this.form.setValues(data);",
" this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
" }",
"",
" },",
"",
" create : function()",
" {",
" var _this = this;",
" this.dialog = Roo.factory(" + o + ");",
" }",
"};",
""
].join("\n");
},
/**
* try to remove Pman.Tab.XXXXXYYYY
* where XXXX= the project name..
*
*/
pathToPart : function()
{
var modname = this.name.split('.').pop();
var ret = modname.match(/([A-Z][a-z]+)/g);
var mm = ret.shift();
return [ mm, ret.join('')];
//return [ modname , npart];
},
// a layout compoent
toSourceLayout : function()
{
var items = JSON.parse(JSON.stringify(this.items[0]));
var o = this.mungeToString(items, false, ' ');
var modkey = this.modkey + '-' + this.name.replace(/[^A-Z.]+/ig, '-');
if (this.name.match(/^Pman/)) {
// old BC way we did things..
return [
this.outputHeader(),
"",
"",
"// register the module first",
"Pman.on('beforeload', function()",
"{",
" Pman.register({",
" part : "+ JSON.stringify(this.pathToPart()) + ",", /// critical used by builder to associate modules/parts/persm
" modKey : '" +modkey+"',",
" module : " + this.name + ",",
" region : '" + this.region +"',",
" parent : " + (this.parent || 'false') + ",",
" name : " + JSON.stringify(this.title || "unnamed module") + ",",
" disabled : " + (this.disabled || 'false') +", ",
" permname: '" + (this.permname|| '') +"' ",
" });",
"});",
"",
this.name + " = new Roo.util.Observable({",
"",
" panel : false,",
" disabled : false,",
" parentLayout: false,",
"",
" add : function(parentLayout, region)",
" {",
"",
" var _this = this;", // standard avaialbe..
" this.parentLayout = parentLayout;",
"",
" this.panel = parentLayout.addxtype(" + o + ");",
" this.layout = this.panel.layout;",
"",
" }",
"});",
""
].join("\n");
}
return [
this.outputHeader(),
this.name + " = new Roo.XComponent({",
" order : '" +modkey+"',",
" region : '" + this.region +"',",
" parent : "+ (this.parent ? "'" + this.parent + "'" : 'false') + ",",
" name : " + JSON.stringify(this.title || "unnamed module") + ",",
" disabled : " + (this.disabled || 'false') +", ",
" tree : function()",
" {",
" var _this = this;", // bc
" var MODULE = this;", /// this looks like a better name.
" return " + o + ';',
" }",
"});",
""
].join("\n");
},
guessName : function(ar) // turns the object into full name.
{
// eg. xns: Roo, xtype: XXX -> Roo.xxx
if (!ar) {
return false;
}
var ret = [];
ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
return false;
}
var xtype = ar['xtype'] + '';
if (xtype[0] == '*') { // prefixes????
xtype = xtype.substring(1);
}
if (xtype.match(/^Roo/)) {
// already starts with roo...
ret = [];
}
ret.push(xtype);
return ret.join('.');
},
};