2 * This is baed on the code in the app builder..
8 render = new Pman.Builder.JsRender(
9 Pman.Builder.Tree.toJS()
12 var source = render.toSource();
15 * @cfg {Array} items - the array of items..
16 * @cfg {String} name - eg. Pman.Tab.XXXXyyyyy
18 * @cfg {String} path - needs the full path << we only have the class name..
19 * @cfg {String} region - ?? needed anymore?
20 * @cfg {String} parent
22 * @cfg {String} disable? function call to disable it?
23 * @cfg {String} permname
24 * @cfg {Number} modOrder
25 * @cfg {Number} modkey ???
29 Pman.Builder.JsRender = function(cfg) {
33 Pman.Builder.JsRender.prototype = {
37 path : '', // the file path..
38 modOrder : '001', /// sequence id that this uses.
41 title : '', // the title on displayed when loading.
42 disable : '', // use a function to that returns false to disable this..
43 permname: '', /// permission name
48 * @type {Object} the properties that have to be double quoted to enable translation
65 * munge JSON tree into Javascript code.
67 * FIXME: + or / prefixes to properties hide it from renderer.
68 * FIXME: '*props' - not supported by this.. ?? - upto rendering code..
69 * FIXME: needs to understand what properties might be translatable (eg. double quotes)
71 * @arg {object} obj the object or array to munge..
72 * @arg {boolean} isListener - is the array being sent a listener..
73 * @arg {string} pad - the padding to indent with.
77 mungeToString: function(obj, isListener, pad)
84 isListener = isListener || false;
86 // am I munging a object or array...
87 if (obj.constructor.toString() === Array.toString()) {
88 for (var i= 0; i < obj.length; i++) {
102 typeof(obj['|xns']) != 'undefined' &&
103 typeof(obj['xtype']) != 'undefined'
105 this.mungeXtype(obj['|xns'] + '.' + obj['xtype'], els);
106 //els.push('xtype: '+ obj['|xns'] + '.' + obj['xtype']);
107 skip.push('|xns','xtype');
111 if (!isArray && obj.items && obj.items.length) {
114 obj.items.forEach(function(pl) {
115 if (typeof(pl['*prop']) == 'undefined') {
120 //print(JSON.stringify(pl,null,4));
122 var prop = pl['*prop'] + '';
124 if (!prop.match(/\[\]$/)) {
125 // it's a standard prop..
127 // munge property..??
134 prop = prop.substring(0, prop.length -2); //strip []
135 // it's an array type..
136 obj[prop] = obj[prop] || [];
138 // print("ADDNG PROP:" + prop + ' ' + keys.indexOf(prop) );
139 if (keys.indexOf(prop) < 0) {
146 obj.items = newitems;
147 if (!obj.items.length) {
154 //if (isArray) { print(JSON.stringify(keys, null,4)); }
155 // keys is just the real keys of the object.
160 keys.forEach(function(i) {
162 if (i[0] == '.') { // skip builder data.
166 if (typeof(obj[i]) == 'undefined') { // empty or removed.
170 if (!isArray && skip.indexOf(i) > -1) { // things we do not write..
174 // set the key to be quoted with singel quotes..
175 var leftv = i[0] == '|' ? i.substring(1) : i;
176 if (Pman.Builder.Lang.isKeyword(leftv) || Pman.Builder.Lang.isBuiltin(leftv)) {
177 left = "'" + leftv + "'";
178 } else if (leftv.match(/[^A-Z_]+/i)) { // not plain a-z... - quoted.
179 var val = JSON.stringify(leftv);
180 left = "'" + val.substring(1, val.length-1).replace(/'/g, "\\'") + "'";
190 // change the lines...
191 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,""); // remove bar.
192 var lines = str.split("\n");
193 if (lines.length > 1) {
194 str = lines.join("\n" + pad);
197 els.push(left + str);
203 //var left = isArray ? '' : (JSON.stringify(i) + " : " )
206 // does not hapepnd with arrays..
207 if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
210 // this needs to go...
211 //if (typeof(el) == 'string' && obj[i].match(new RegExp("Gtk.main" + "_quit"))) { // we can not handle this very well..
215 var str= ('' + obj[i]).replace(/^\s+|\s+$/g,"");;
216 var lines = str.split("\n");
217 if (lines.length > 1) {
218 str = lines.join("\n" + pad);
221 els.push(left + str);
228 if (typeof(el) == 'object') {
230 // we can skip empty items lists and empty listeners..
231 //if (!isArray && i == 'items' && !el.length) {
235 var right = _this.mungeToString(el, i == 'listeners', pad + ' ');
237 //if (!left.length && isArray) print(right);
239 if ((typeof(right) != 'undefined') && right.length){
240 els.push(left + right);
246 if (typeof(obj[i]) != 'string') {
247 els.push(left + JSON.stringify(obj[i]));
251 if (!_this.doubleStringProps) {
252 els.push(left + JSON.stringify(obj[i]));
255 if (_this.doubleStringProps.indexOf(i) > -1) {
256 els.push(left + JSON.stringify(obj[i]));
260 els.push(left + "'" + obj[i].replace(/'/g, "\\'") + "'");
265 if (!isArray && !els.length) {
269 var spad = pad.substring(0, pad.length-4);
270 return (isArray ? '[' : '{') + "\n" +
271 pad + els.join(",\n" + pad ) +
272 "\n" + spad + (isArray ? ']' : '}');
279 setNSID : function(id)
282 this.items[0]['|module'] = id;
288 getType: function() {
294 * old code had broken xtypes and used arrays differently,
295 * this code should try and clean it up..
299 fixItems : function(node, fixthis)
303 var fn = this.guessName(node);
304 //print("guessname got " + fn);
306 var bits = fn.split('.');
307 node.xtype = bits.pop();
308 node['|xns'] = bits.join('.');
315 if (!node.items || !node.items.length) {
321 node.items.forEach(function(i) {
325 _this.fixItems(i, true);
326 if (i.xtype == 'Array') {
334 if (!aitems.length) {
338 aitems.forEach(function(i) {
340 if (!i.items || !i.items.length) {
343 var prop = i['*prop'] + '[]';
345 i.items.forEach(function(c) {
364 * convert xtype for munged output..
367 mungeXtype : function(xtype, els)
369 var bits = xtype.split('.');
370 // assume it has lenght!
372 els.push("xtype: '"+ bits.pop()+"'");
373 els.push('xns: '+ bits.join('.'));
377 * This needs to use some options on the project
378 * to determine how the file is output..
380 * At present we are hard coding it..
386 // dump the file tree back out to a string.
388 // we have 2 types = dialogs and components
390 var top = this.guessName(this.items[0]);
395 if (top.match(/Dialog/) || top.match(/Modal/)) {
396 return this.toSourceDialog();
398 return this.toSourceLayout();
401 eventually support 'classes??'
402 return this.toSourceStdClass();
407 outputHeader : function()
410 "//<script type=\"text/javascript\">",
412 "// Auto generated file - created by app.Builder.js- do not edit directly (at present!)",
418 // a standard dialog module.
419 // fixme - this could be alot neater..
420 toSourceDialog : function()
422 var items = JSON.parse(JSON.stringify(this.items[0]));
423 var o = this.mungeToString(items, false, ' ');
432 " show : function(data, cb)",
434 " if (!this.dialog) {",
438 " this.callback = cb;",
439 " this.data = data;",
440 " this.dialog.show(this.data._el);",
442 " this.form.reset();",
443 " this.form.setValues(data);",
444 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
449 " create : function()",
451 " var _this = this;",
452 " this.dialog = Roo.factory(" + o + ");",
464 * try to remove Pman.Tab.XXXXXYYYY
465 * where XXXX= the project name..
468 pathToPart : function()
472 var modname = this.name.split('.').pop();
474 var ret = modname.match(/([A-Z][a-z]+)/g);
475 var mm = ret.shift();
476 return [ mm, ret.join('')];
478 //return [ modname , npart];
486 toSourceLayout : function()
488 var items = JSON.parse(JSON.stringify(this.items[0]));
489 var o = this.mungeToString(items, false, ' ');
491 var modkey = this.modkey + '-' + this.name.replace(/[^A-Z.]+/ig, '-');
493 var name = this.name.replace(/[^a-z_]+/i, '_');
499 name + " = new Roo.XComponent({",
500 " order : '" +modkey+"',",
501 " region : '" + this.region +"',",
502 " parent : "+ (this.parent ? "'" + this.parent + "'" : 'false') + ",",
503 " name : " + JSON.stringify(this.title || "unnamed module") + ",",
504 " disabled : " + (this.disabled || 'false') +", ",
505 " tree : function()",
507 " var _this = this;", // bc
508 " var MODULE = this;", /// this looks like a better name.
509 " return " + o + ';',
518 guessName : function(ar) // turns the object into full name.
520 // eg. xns: Roo, xtype: XXX -> Roo.xxx
525 ret.push(typeof( ar['|xns'] ) == 'undefined' ? 'Roo' : ar['|xns'] );
529 if (typeof( ar['xtype'] ) == 'undefined' || !ar['xtype'].length) {
532 var xtype = ar['xtype'] + '';
533 if (xtype[0] == '*') { // prefixes????
534 xtype = xtype.substring(1);
536 if (xtype.match(/^Roo/)) {
537 // already starts with roo...
541 return ret.join('.');