Pman.Builder.View.js
[Pman.Builder] / Pman.Builder.View.js
1 /**
2  * The code that renders the view  - used to be in the Builder.View etc...
3  *
4  * used to be inside the Interface, but has proved to be to difficult to manage.
5  *
6  * In principle, simple event handling code is put in the interface, and any hard
7  * lifting is done in nice files...
8  *
9  * It might be better to just extend 'tree', and use the extended class..
10  * 
11  */
12
13 Pman.Builder.View = {
14     
15     panel : false,
16     
17     panelroot : false,
18     dialogroot : false,
19     
20     
21     renderObj : false,
22     
23     doc : function() {
24         var iframe = this.container.el.select('iframe',true).first().dom;
25         
26         return iframe.contentDocument || iframe.contentWindow.document;
27     },
28     win : function() {
29         var iframe = this.container.el.select('iframe',true).first().dom;
30         return iframe.contentWindow;
31     },
32     
33     init : function (comp) {
34         if (this.panel || !comp.layout) {
35             return;
36         }
37         this.panel = comp.panel;
38         this.layout  = comp.panel.layout;
39         // see if we can render a iframe..
40         
41         var app = 'test';
42         
43         this.container = comp.layout.getRegion('center').getPanel(0);
44         this.container.setContent(
45                 '<iframe width="100%" height="100%" src="'+baseURL+'/Builder/App/' + app + '?no_parts=1"></iframe>'
46         );
47         var iframe = this.container.el.select('iframe',true).first()
48         
49         iframe.setSize( this.panel.el.getSize());
50         
51         //this.doc = iframe.contentDocument || iframe.contentWindow.document;
52         
53         
54     },
55     
56     clearAll : function(isAuto) {
57         try {
58             this.win().Pman.Builder.View.frameClearAll(isAuto);
59         } catch(e) {
60             //alert(e);
61         }
62         
63        
64     },
65     
66     frameToHTML : function()
67     {
68         return this.win().Pman.Builder.View.toHTML();  
69         
70         
71         
72     },
73     toHTML : function ()  {
74         var ret = '';
75         Roo.select('body > div',true).each(function(el) {
76             this.traverseDOMTree(function(s) { ret+=s; }, el.dom, 1);
77             ret+="\n";
78         }, this);
79         return ret;
80         
81     },
82     traverseDOMTree : function(cb, currentElement, depth) {
83         if (currentElement) {
84             
85             if (currentElement.className.match(/roo-dynamic/)) {
86                 return;
87             }
88             //Roo.log(currentElement);
89             var j;
90             var nodeName = currentElement.nodeName;
91             var tagName = currentElement.tagName;
92             
93             if  (nodeName == '#text') {
94                 cb(currentElement.nodeValue);
95                 return;
96             
97             }
98             var i = 0;
99           // Prints the node tagName, such as <A>, <IMG>, etc
100             if (tagName) {
101                 var attr = [];
102                 for(i = 0; i < currentElement.attributes.length;i++) {
103                     var aname = currentElement.attributes.item(i).name;
104                     if (aname=='id') {
105                         aname= 'xbuilderid';
106                     }
107                     attr.push(aname + '="' + currentElement.attributes.item(i).value + '"' );
108                 }
109                 
110                 
111                 cb("<"+currentElement.tagName+ ( attr.length ? (' ' + attr.join(' ') ) : '') + ">");
112             } 
113             else {
114               cb("[unknown tag]");
115             }
116             // Traverse the tree
117             i = 0;
118             var currentElementChild = currentElement.childNodes[i];
119             var allText = true;
120             while (currentElementChild) {
121                 // Formatting code (indent the tree so it looks nice on the screen)
122                 
123                 if  (currentElementChild.nodeName == '#text') {
124                     cb(currentElementChild.nodeValue);
125                     i++;
126                     currentElementChild=currentElement.childNodes[i];
127                     continue;
128                 }   
129                 allText = false;
130                 cb("\n");
131                 for (j = 0; j < depth; j++) {
132                   // &#166 is just a vertical line
133                   cb("  ");
134                 }               
135                 
136                     
137                 // Recursively traverse the tree structure of the child node
138                 this.traverseDOMTree(cb, currentElementChild, depth+1);
139                 i++;
140                 currentElementChild=currentElement.childNodes[i];
141             }
142             if (!allText) {
143                 // The remaining code is mostly for formatting the tree
144                 cb("\n");
145                 for (j = 0; j < depth - 1; j++) {
146                   cb("  ");
147                 }     
148             }
149             if (tagName) {
150                 cb("</"+tagName+">");
151             }
152         }
153     },
154
155      
156     
157     frameClearAll : function(isAuto) {
158 //        this.renderObj = { isBuilder : true };
159
160         
161         if (this.panelroot) {
162             this.scroll = this.panelroot.el.getScroll();
163             this.layout.remove('center', this.panelroot);
164             this.panelroot = false;
165         }
166         if (this.dialogroot) {
167             this.dialogroot.remove();
168             this.dialogroot = false;
169         }
170         Roo.select('body > div').remove();
171         
172     },
173     munge : function(cfg, keyname, add_xattr) {
174         keyname = keyname || false;
175         
176         this.renderObj = this.renderObj || {};
177         
178         var xitems = false;
179         if (cfg.items) {
180             xitems = cfg.items;
181             delete cfg.items;
182         }
183         
184         if (typeof(cfg.background) != 'undefined') {
185             cfg.background = false;
186         }
187         
188         
189         for(var p in cfg){
190             // key is not string?!?!?!!?
191             if (typeof(p) != 'string') {
192                 continue;
193             }
194             
195             if (typeof(cfg[p]) == 'object') { // listeners!!!
196                 this.munge(cfg[p], p, add_xattr);
197                 continue;
198             }
199             // SPECIAL - PIPE
200             if (p.charAt(0) == '|' || keyname=='listeners') {
201                 
202                 if (!cfg[p].length) {
203                     delete cfg[p];
204                     continue;
205                 }
206                 var str = cfg[p];
207                 if (str.match(/\s*function/)) {
208                     var btz = str.split('{');
209                     str = btz.shift()  +'{ try {' + btz.join('{') + 
210                         ' catch (e) { Roo.log(e) } }';
211                 }
212                 try {
213                     Roo.log("interpret? " + str);
214                     var _tmp = false;
215                     var _this = this.renderObj; /// fake '_this' object..
216                     // stupid IE can not return objects evaluated..
217                     /**
218                      eval:var:_this  
219                      eval:var:_tmp 
220                     **/
221                     
222                     
223                     eval('_tmp =(' + str + ')');
224                     cfg[p.replace(/^\|/, '')] = _tmp;
225                     if (typeof(_tmp) == 'undefined') {
226                         alert(cfg[p]);
227                     }
228                    
229                 } catch(e) {  console.log('Error evaluating: '  + str); };
230                 //if (p.charAt(0) == '|' ) {
231                 //    delete cfg[p];
232                 //}
233                     
234                 
235                 continue;
236             }
237             // skip '*'
238             if ((p.charAt(0) == '*') || (p.charAt(0) == '+')) {
239                 delete cfg[p];
240                 continue;
241             }
242             // normal..
243               
244         }
245         
246         // add xattr data?
247         if (add_xattr) {
248             cfg.xattr = {
249                 xtype : cfg['|xns'] + '.' + cfg.xtype 
250             };
251             
252         }
253         
254         
255         
256         // now for all the children.. (items)
257         if (xitems === false) {
258             return;
259         }
260         cfg.items = [];
261         for (var i = 0; i < xitems.length; i++) {
262             // if +builderhide set !!!! drop it!!
263             
264             
265             var xi = xitems[i];
266             if (typeof(xi['*prop']) != 'undefined') {
267                 var pr = xi['*prop'];
268                 this.munge(xi, false, add_xattr);
269                 // if prop is an array - then it's items are really the value..
270                 if (pr.match(/\[\]$/)) {
271                     pr = pr.replace(/\[\]$/, '');
272                     if (typeof(cfg[pr]) == 'undefined') {
273                         cfg[pr] = [];
274                     }
275                     cfg[pr].push(xi);
276                     continue;
277                 }
278                 if (xi.xtype && xi.xtype  == 'Array') {
279                     cfg[pr] = xi.items;
280                 } else {
281                     cfg[pr] = xi;
282                 }
283                 
284                 
285                 continue;
286             }
287             this.munge(xi, false, add_xattr);
288             cfg.items.push(xi);
289         }
290         
291         if (cfg.items.length == 0) {
292             delete cfg.items;
293         }
294         // remove listeners if there are none..
295         if (typeof(cfg['listeners']) != 'undefined') {
296             var n =0;
297             for (var i in cfg.listeners) { n++; }
298             if (!n) {
299                 delete cfg['listeners'];
300             }
301             
302         }
303         
304         if (typeof(cfg.id) != 'undefined') {
305             cfg.id = 'pman-dyn-' + cfg.id;
306         }
307             
308             
309     },
310     toJS : function(n)
311     {
312     
313         if (!n) {
314             return Pman.Builder.Tree.toJS(Pman.Tab.BuilderTree.tree.root);
315         }
316    
317         var _this = this;
318         var ret = Pman.Builder.Tree.cloneConfig(n.elConfig);
319         
320         // flag to prevent rendering..
321         if ((typeof(ret['+buildershow']) != 'undefined') && !ret['+buildershow']) {
322             return false;
323         }
324     
325         ret.id = typeof(ret.id) == 'undefined' ? 'builder-' + n.id : ret.id;
326     
327         if (n.childNodes.length) {
328             ret.items = [];
329             n.eachChild(function(cn) {
330                 var add = _this.toJS(cn);
331                 if (add === false) {
332                     return;
333                 }
334                 
335                 
336                 ret.items.push(add);
337             });
338                 
339         }
340         return ret;
341     },
342
343     redraw : function(isAuto)
344     {
345         this.container.el.select('iframe',true).first().setSize(this.panel.el.getSize());
346  
347        // return;
348         // top level is not relivant
349
350 //          var btop =  Pman.Tab.BuilderTop2;
351   //      if (isAuto && btop.redrawBtn  && !btop.redrawBtn.auto) {
352     //        return; /// auto redraw is turned off..
353       //  }
354         
355         this.clearAll(isAuto);
356         
357         var cfg =  this.toJS();
358         
359         this.win().Pman.Builder.View.draw( cfg );
360         
361     },
362     draw :function(cfg)
363     {
364         Roo.log(cfg);
365         
366         if (!cfg.items[0]) {
367             return;
368         }
369         
370         
371         this.munge(cfg.items[0],false , true );
372         
373         Roo.log(cfg);
374         //  return;
375         // we draw either a dialog or a tab..
376         
377         if (cfg.items[0].xtype == 'LayoutDialog') {
378             
379             cfg.items[0].modal = false;
380             var xy  = this.el.getXY();
381             cfg.items[0].x = xy[0];
382             cfg.items[0].y = xy[1];
383             cfg.items[0].constraintoviewport = false;
384             
385             this.dialogroot = Roo.get( this.doc).createChild();
386             try { 
387                 this.dialog = new Roo[cfg.items[0].xtype](this.dialogroot, cfg.items[0]);
388               //  this.dialog.el.on('click', this.panelClick, this);
389                 this.dialog.show();
390                 var dlg = this.dialog;
391                 (function () {
392                     dlg.moveTo(xy[0], xy[1]);
393                 }).defer(100);
394             } catch(e) {
395                 Roo.log("Error rendering dialog: " + e.toString());
396                 Roo.log(e);
397             }
398             return;
399             
400             
401         }
402         if (cfg.items[0]['|xns'] == 'Roo.bootstrap') {
403             
404             Roo.log("bootstrap build!?");
405             var top = new Roo.bootstrap.Body({});
406             top.onRender(false,false);
407             Roo.log('cfg');
408             Roo.log(cfg);
409             this.bootstrapModal = top.addxtype(cfg.items[0]);
410             
411             this.applyFlexy(top);
412             
413             if (cfg.items[0].xtype =='Modal' ) {
414                 this.bootstrapModal.show();
415             }
416             
417             return;
418         }
419         
420         
421         
422         // should we render this into a dialog???
423              // force center region..
424         cfg.items[0].region = 'center';
425         cfg.items[0].background = false;
426         
427         try {
428             
429             cfg.parent = '#';
430             
431             
432             this.panelroot = this.layout.addxtype(cfg.items[0]);
433         
434         } catch(e) {
435             Roo.log("Error rendering: " + e.toString());
436             Roo.log(e);
437         }
438         //this.highlightElement(Pman.Tab.BuilderTree.currentNode);
439         
440         if (this.panelroot && this.panelroot.el) {
441                 this.panelroot.el.scrollTo('top', 0);
442                 this.panelroot.el.scrollTo('left', 0);
443             
444         }
445     },
446     frameUpdateTheme: function(name)
447     {
448          return this.win().Pman.Builder.View.updateTheme(name);  
449     },
450     updateTheme : function(name)
451     {
452        Roo.select('head link[rel=stylesheet]').each(function(e) {
453             if (!e.dom.href.match(/bootstrap\.\min.css$/)) {
454                 return;
455             }
456             e.dom.setAttribute('href', rootURL +'/bootswatch/' + name + '/bootstrap.min.css' );
457             
458         
459        });
460     },
461     
462     applyFlexy: function(tree)
463     {
464         if (typeof(tree['flexy:foreach']) != 'undefined') {
465             tree.el.attr('flexy:foreach', tree['flexy:foreach']);
466         }
467         if (typeof(tree['flexy:if']) != 'undefined') {
468             tree.el.attr('flexy:if', tree['flexy:if']);
469         }
470         if (!tree.items || !tree.items.length) { return; }
471         
472         for (var i = 0; i < tree.items.length; i++){
473             this.applyFlexy(tree.items[i]);
474         }
475     }
476     
477     
478     
479 }