Pman.Tab.BuilderTree.js
[Pman.Builder] / Pman.Tab.BuilderTree.js
1 //<script type="text/javascript">
2
3 // Auto generated file - created by app.Builder.js- do not edit directly (at present!)
4
5
6
7 // register the module first
8 Pman.on('beforeload', function()
9 {
10     Pman.register({
11         modKey : '001-Pman.Tab.BuilderTree',
12         module : Pman.Tab.BuilderTree,
13         region : 'center',
14         parent : Pman.Tab.BuilderTab,
15         name : "Pman.Tab.BuilderTree",
16         disabled : false, 
17         permname: '' 
18     });
19 });
20
21 Pman.Tab.BuilderTree = new Roo.util.Observable({
22
23     panel : false,
24     disabled : false,
25     parentLayout:  false,
26
27     add : function(parentLayout, region)
28     {
29
30         var _this = this;
31         this.parentLayout = parentLayout;
32
33         this.panel = parentLayout.addxtype({
34             xtype: 'NestedLayoutPanel',
35             xns: Roo,
36             region : 'west',
37             layout : {
38                 xtype: 'BorderLayout',
39                 xns: Roo,
40                 items : [
41                     {
42                         xtype: 'TreePanel',
43                         xns: Roo,
44                         listeners : {
45                             activate : function (_self)
46                             {
47                                 
48                                 _this.tree = _self.tree;
49                                 if (_this.hasMouseEvent) {
50                                     return;
51                                 }
52                                 
53                                 _this.hasMouseEvent = true;
54                                  this.el.on('mouseover', function() { _this.isMouseOver= true; });
55                                 this.el.on('mouseout', function() { _this.isMouseOver = false; });
56                             
57                             }
58                         },
59                         region : 'center',
60                         tree : {
61                             xtype: 'TreePanel',
62                             xns: Roo.tree,
63                             listeners : {
64                                 beforenodedrop : function (e)
65                                 {
66                                     /*
67                                     tree - The TreePanel
68                                     target - The node being targeted for the drop
69                                     data - The drag data from the drag source
70                                     point - The point of the drop - append, above or below
71                                     source - The drag source
72                                     rawEvent - Raw mouse event
73                                     dropNode - Drop node(s) provided by the source OR you can supply node(s) to be inserted by setting them on this object.
74                                     cancel - Set this to true to cancel the drop.
75                                     */
76                                     Roo.log(e);
77                                     if (!e.tree || !e.dropNode) {
78                                         console.log('no tree or dropNode');
79                                         return; // fixme drop of elements from palete..
80                                     }
81                                     var np = false; // new parent
82                                     
83                                     switch (e.point) {
84                                         case 'above':
85                                         case 'below':
86                                             np = e.target.parentNode;
87                                             break;
88                                         case 'append':
89                                             np = e.target;
90                                             break;
91                                     }
92                                     
93                                     // always drop onto own parent
94                                     if (np == e.dropNode.parentNode) {
95                                         if (e.rawEvent.ctrlKey) {
96                                             e.dropNode = _this.dupeNode(e.dropNode);
97                                         }
98                                         return true;
99                                     }
100                                    if (_this.canAppend(np, e.dropNode.elConfig)) {
101                                         if (e.rawEvent.ctrlKey) {
102                                             e.dropNode = _this.dupeNode(e.dropNode);
103                                               
104                                             if (np.elConfig.xtype == 'GridEditor') {
105                                                 e.dropNode.elConfig['*prop'] = 'field';
106                                             }
107                                             
108                                         }
109                                         return true;
110                                     }  
111                                     console.log('can not drop ' + e.dropNode.elConfig.xtype + ' ontop of ' + np.elConfig.xtype);
112                                     
113                                     
114                                     
115                                     return false;
116                                                         
117                                 
118                                 },
119                                 nodedrop : function (dropEvent)
120                                 {
121                                     e.dropNode.setText(
122                                                 _this.configToText(e.dropNode.elConfig)
123                                                 );
124                                                 var bp = Pman.Tab.BuilderPanel;
125                                                 bp.redraw.defer(1000, bp, [true]);
126                                 
127                                 },
128                                 click : function (node, e)
129                                 {
130                                       e.preventDefault();
131                                                        // console.log(e.button);
132                                         this.setCurrentNode(node);
133                                 
134                                 },
135                                 contextmenu : function (node, e)
136                                 {
137                                     e.stopEvent();
138                                         
139                                         this.getSelectionModel().select(node);
140                                          _this.setCurrentNode(node);
141                                         var xt = node.elConfig.xtype;
142                                         var altx= false;
143                                         if (typeof(node.elConfig['*prop']) != 'undefined') {
144                                             altx = node.parentNode.elConfig.xtype +'.' + node.elConfig['*prop'];
145                                         }
146                                         var mn = Pman.Tab.BuilderPalete.getMenu(xt,altx);
147                                         
148                                         
149                                         if (mn) {
150                                             mn.show(e.getTarget());
151                                         }
152                                         
153                                 
154                                 
155                                 },
156                                 nodedragover : function (e)
157                                 {
158                                     Roo.log('nodedragover');
159                                      Roo.log(e);
160                                      // e.cancel..
161                                      // if we have within the same tree:
162                                        // dropNode (the node being dragged !!important!!) 
163                                        // point: below, append
164                                        // target - node 
165                                     // for palete
166                                         // dropNode = false;
167                                         // grid = the grid...
168                                         // source.dragData.selection[..] 
169                                    
170                                     
171                                     // we can only check parents... (we in theory can check dupe properties.. but let's ignore that for the time being.)
172                                     
173                                     // ok off we go.
174                                     
175                                     if (!e.dropNode) {
176                                         // drag from palete..
177                                         if (!e.source.dragData.selection.length) {
178                                             e.cancel = true;
179                                             return;
180                                         }
181                                         var drop_rec = e.source.dragData.selection[0];
182                                         var drop_xtype = drop_rec.data.name;
183                                         var ok_parents = drop_rec.json.parents;
184                                         
185                                         var new_parent = this.nodeXtype((e.point == 'append') ? e.target.parentNode :  e.target);
186                                         
187                                         // see if the new_parent is actually in the list of ok_parents
188                                         n.cancel = true;
189                                         Roo.each(ok_parents,function(n) {
190                                             if (n == new_parent || n.split(':').shift() == new_parent) {
191                                                 n.cancel = false;
192                                                 return true;
193                                             }
194                                         });
195                                         // done all the checks...
196                                         return;
197                                         
198                                     }
199                                      
200                                    
201                                 }
202                             },
203                             animate : false,
204                             containerScroll : true,
205                             ddGroup : 'component',
206                             enableDD : true,
207                             rootVisible : true,
208                             nodeXtype : function(n) {
209                                  if (!n) {return ''; }
210                                 var xt = n.elConfig.xtype ||  '';
211                                 var xns= n.elConfig['|xns'] ||   '';
212                                 xns += xns.length ? '.' : '';
213                                 return xns + xt;
214                             },
215                             appendNode : function(parent, inConfig, markUndo) {
216                                 
217                                     
218                              
219                                 var items = [];
220                                 if (inConfig.items) { // loading!
221                                     items = inConfig.items;
222                                     delete inConfig.items;
223                                 }
224                                 var config = this.cloneConfig(inConfig);
225                                 
226                                 if (!parent) {
227                                 parent = this.root;
228                                 }
229                                     
230                                     
231                                 //var canAppend = this.canAppend(parent,config);
232                                 //if (canAppend !== true) {
233                                 //    console.log("Unable to add element " + canAppend);
234                                 //      Roo.Msg.alert("Unable to add element", canAppend);
235                                 //              return false;
236                                //               }
237                                      
238                                 var newNode = new Roo.tree.TreeNode({
239                                         text: this.configToText(config)
240                                 });
241                                         
242                                 
243                                 newNode.elConfig = config;
244                                 //if (markUndo === true) {
245                                 //Pman.Tab.Builder.markUndo("Add " + newNode.text);
246                                 //
247                                     // appends to our tree...
248                                 parent.appendChild(newNode);
249                                 
250                                     
251                                 if (items.length) {
252                                     Roo.each(items, function(i) {
253                                         this.appendNode(newNode, i);
254                                     },this);
255                                     
256                                 }
257                                     
258                                     
259                                     /*
260                                     -- panels with panes...
261                                         if (items && items.length) {
262                                                 for (var i = 0; i < items.length; i++) {
263                                                                 this.appendConfig(items[i], newNode, false);
264                                                 }
265                                         }
266                                         if (opts.doUpdate !== false) {
267                                                 this.updateForm(false, newNode);
268                                         }
269                                     */
270                                 return newNode;
271                             
272                                  
273                             },
274                             clearAll : function() {
275                                 var rt = this.root;
276                                  if (rt.childNodes.length) {
277                                     rt.removeChild(rt.childNodes[0]);
278                                 }
279                                
280                                 this.root.elConfig  = Roo.apply({ }, this.defaultElConfig());  
281                                 //var btop = Pman.Tab.BuilderTop;
282                                 //if (btop.modsel && btop.modsel.lastData) {
283                                 //    this.tree.root.elConfig.app = btop.modsel.lastData.app;
284                                 //}
285                                 
286                                 this.setCurrentNode(this.root,true);
287                             
288                             },
289                             cloneConfig : function(config) {
290                                 if (!config) { return null; }
291                                 var newConfig = {};
292                                 
293                                 for (var i in config) {
294                                     if (typeof config[i] == 'object') {
295                                          newConfig[i] = this.cloneConfig(config[i]);
296                                     } else if (typeof config[i] != 'function') { // should this happen?
297                                          newConfig[i] = config[i];
298                                     }
299                                 }
300                                 return newConfig;
301                             },
302                             configToText : function(c) {
303                                  
304                                 var txt = [];
305                                 c = c || {};
306                                       var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
307                                     if (sr) txt.push('<s>');
308                                     if (typeof(c['*prop']) != 'undefined')   { txt.push(c['*prop']+ ':'); }
309                                 if (c.xtype)      { txt.push(c.xtype); }
310                                 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
311                                 if (c.boxLabel)   { txt.push('[' + c.boxLabel + ']'); }
312                                 
313                                 
314                                 if (c.layout)     { txt.push('<i>' + c.layout + '</i>'); }
315                                 if (c.title)      { txt.push('<b>' + c.title + '</b>'); }
316                                     if (c.header)    { txt.push('<b>' + c.header + '</b>'); }
317                                     if (c.legend)      { txt.push('<b>' + c.legend + '</b>'); }
318                                 if (c.text)       { txt.push('<b>' + c.text + '</b>'); }
319                                     if (c.name)       { txt.push('<b>' + c.name+ '</b>'); }
320                                 if (c.region)     { txt.push('<i>(' + c.region + ')</i>'); }
321                                     if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
322                                     if (sr) txt.push('</s>');
323                                 return (txt.length == 0 ? "Element" : txt.join(" "));
324                              
325                               
326                             },
327                             currentNodeType : function() {
328                                 return this.nodeXtype(this.currentNode);
329                              
330                             },
331                             defaultElConfig : function() {
332                                 return {
333                                    xtype : '*Module',
334                                     
335                                     module : 'TestApp',
336                                     part:   'Partname',
337                                     modkey : 0,
338                                     region : 'center',
339                                     parent : 'Pman',
340                                     name : 'Module Name',
341                                     items: [] 
342                                 };
343                             },
344                             loadBJS : function(module, part) {
345                                 var _t = this;
346                                 new Pman.Request({
347                                     url : baseURL + '/Roo/Builder_part.php',
348                                     method : 'GET',
349                                     params : {
350                                        _id : part
351                                     },
352                                     success : function(res)         
353                                     {
354                                         // data is in.. 
355                                         Roo.log(res);
356                                         _t.loadTree(JSON.parse(res.data.json));
357                                         
358                                      
359                                     },
360                             
361                                  })  
362                                     
363                                 
364                                 
365                             },
366                             loadTree : function(o) {
367                                 this.clearAll();
368                                 this.root.elConfig = o;
369                                 this.root.setText(this.configToText(this.root.elConfig));
370                                 this.appendNode(this.root, o.items[0]);
371                                 this.root.expand(true);
372                                 Pman.Tab.BuilderView.panel.redraw();
373                                 this.setCurrentNode(this.root,true);
374                             },
375                             renderer : function(n) { return n.text; },
376                             setCurrentNode : function(node,select) {
377                                     this.currentNode = node || this.root;
378                                     
379                                     //Pman.Tab.BuilderView.highlightElement(this.currentNode);
380                             
381                                     var p = Pman.Tab.BuilderProps.grid;
382                                     if (p) { //may not be ready yet..
383                                         p.setCurrrentNode(this.currentNode);
384                                     }
385                                     
386                                   
387                                     this.currentNode.setText(this.configToText(this.currentNode.elConfig));
388                                     
389                                     if (select) { //&& node !== this.tree.root) {
390                                         if (this.currentNode !== this.root)  {
391                                                  this.currentNode.ensureVisible();
392                                           }   
393                                          this.currentNode.expand(false,false);
394                                         this.currentNode.select();
395                                 }
396                                 // update palete..
397                                 Pman.Tab.BuilderPalette.grid.getSelectionModel().clearSelections();
398                                 Pman.Tab.BuilderPalette.grid.view.refresh();
399                             
400                             },
401                             sm : {
402                                 xtype: 'DefaultSelectionModel',
403                                 xns: Roo.tree
404                             },
405                             root : {
406                                 xtype: 'TreeNode',
407                                 xns: Roo.tree,
408                                 text : "Part",
409                                 elConfig : function() {
410                                     return  {
411                                          xtype : '*Module',
412                                             
413                                             app : 'TestApp',
414                                             // perm
415                                             // permtype
416                                             modkey : 0,
417                                             '|module' :  '',
418                                             region : 'center',
419                                             '|parent' : 'Pman',
420                                             name : "Module Name",
421                                             items: [] 
422                                 
423                                     
424                                     }
425                                 }
426                             }
427                         }
428                     }
429                 ],
430                 center : {
431                     xtype: 'LayoutRegion',
432                     xns: Roo
433                 },
434                 south : {
435                     xtype: 'LayoutRegion',
436                     xns: Roo,
437                     height : 300,
438                     split : true
439                 }
440             }
441         });
442         this.layout = this.panel.layout;
443
444     }
445 });