2 * The code that manages the tree...
4 * used to be inside the Interface, but has proved to be to difficult to manage.
6 * In principle, simple event handling code is put in the interface, and any hard
7 * lifting is done in nice files...
9 * It might be better to just extend 'tree', and use the extended class..
18 appendNode : function(parent, inConfig, point) {
21 var tree = Pman.Tab.BuilderTree.tree;
25 if (inConfig.items) { // loading!
26 items = inConfig.items;
27 delete inConfig.items;
30 var config = this.cloneConfig(inConfig);
36 var newNode = new Roo.tree.TreeNode({
37 text: this.configToText(config)
41 newNode.elConfig = config;
42 //if (markUndo === true) {
43 //Pman.Tab.Builder.markUndo("Add " + newNode.text);
45 // appends to our tree...
46 console.log("APPEND NODE: " + point);
49 parent.parentNode.insertBefore(newNode, parent);
52 // if it's the last node.. then we append..
53 var ix = parent.parentNode.indexOf(parent) + 1;
54 if (parent.parentNode.childNodes.length == ix) {
55 parent.parentNode.appendChild(newNode);
58 var bef = parent.parentNode.childNodes[ix];
59 parent.parentNode.insertBefore(newNode, bef);
64 parent.appendChild(newNode);
69 Roo.each(items, function(i) {
70 this.appendNode(newNode, i);
77 -- panels with panes...
78 if (items && items.length) {
79 for (var i = 0; i < items.length; i++) {
80 this.appendConfig(items[i], newNode, false);
83 if (opts.doUpdate !== false) {
84 this.updateForm(false, newNode);
91 clearAll : function() {
92 var tree = Pman.Tab.BuilderTree.tree;
95 if (rt.childNodes.length) {
96 rt.removeChild(rt.childNodes[0]);
99 tree.root.elConfig = Roo.apply({ }, this.defaultElConfig());
100 //var btop = Pman.Tab.BuilderTop;
101 //if (btop.modsel && btop.modsel.lastData) {
102 // this.tree.root.elConfig.app = btop.modsel.lastData.app;
105 this.setCurrentNode(tree.root,true);
108 cloneConfig : function(config) {
109 if (!config) { return null; }
112 for (var i in config) {
113 if (typeof config[i] == 'object') {
114 newConfig[i] = this.cloneConfig(config[i]);
115 } else if (typeof config[i] != 'function') { // should this happen?
116 newConfig[i] = config[i];
121 configToText : function(c) {
125 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
126 if (sr) txt.push('<s>');
127 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
128 if (c.xtype) { txt.push(c.xtype); }
129 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
130 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
133 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
134 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
135 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
136 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
137 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
138 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
139 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
140 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
141 if (sr) txt.push('</s>');
142 return (txt.length == 0 ? "Element" : txt.join(" "));
146 currentNodeType : function() {
147 return this.nodeXtype(this.currentNode);
150 defaultElConfig : function() {
159 name : 'Module Name',
163 deleteCurrent : function()
165 if (this.currentNode == tree.root) {
168 var cfg = this.currentNode.elConfig;
169 // things that can not be deleted...
172 var pn = this.currentNode.parentNode;
175 var ix = pn.indexOf(this.currentNode);
177 pn.removeChild(this.currentNode);
178 if (pn.childNodes.length) {
179 ix = Math.min(pn.childNodes.length-1, ix);
181 this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
184 dupeNode : function(node)
186 var cfg = this.cloneConfig(node.elConfig);
188 var newNode = new Roo.tree.TreeNode(
191 text: this.configToText(cfg)
194 newNode.elConfig = cfg;
195 node.eachChild(function(n) {
196 newNode.appendChild(this.dupeNode(n));
202 loadBJS : function(module, part) {
205 url : baseURL + '/Roo/Builder_part.php',
210 success : function(res)
215 if (!res.data.json.length) {
216 var cfg = _t.defaultElConfig();
217 cfg.name = Pman.Tab.BuilderTop.filesel.lastData.name;
218 cfg.part = Pman.Tab.BuilderTop.filesel.lastData.name;
225 _t.loadTree(JSON.parse(res.data.json));
235 loadTree : function(o)
237 var tree = Pman.Tab.BuilderTree.tree;
239 tree.root.elConfig = o;
240 if (typeof(o.xtype) == 'undefined') {
243 tree.root.setText(this.configToText(tree.root.elConfig));
244 this.appendNode(tree.root, o.items[0]);
245 tree.root.expand(true);
246 Pman.Tab.BuilderView.panel.redraw();
247 this.setCurrentNode(tree.root,true);
249 nodeXtype : function(n)
251 var tree = Pman.Tab.BuilderTree.tree;
252 if (!n) {return ''; }
253 var xt = n.elConfig.xtype || '';
254 var xns= n.elConfig['|xns'] || '';
255 xns += xns.length ? '.' : '';
258 setCurrentNode : function(node,select)
260 var tree = Pman.Tab.BuilderTree.tree;
262 this.currentNode = node || tree.root;
264 //Pman.Tab.BuilderView.highlightElement(this.currentNode);
266 var p = Pman.Tab.BuilderProps.grid;
267 if (p) { //may not be ready yet..
268 p.setCurrrentNode(this.currentNode);
272 this.currentNode.setText(this.configToText(this.currentNode.elConfig));
274 if (select) { //&& node !== this.tree.root) {
275 if (this.currentNode !== tree.root) {
276 this.currentNode.ensureVisible();
278 this.currentNode.expand(false,false);
279 this.currentNode.select();
282 Pman.Tab.BuilderPalette.grid.getSelectionModel().clearSelections();
283 Pman.Tab.BuilderPalette.grid.view.refresh();
289 var tree = Pman.Tab.BuilderTree.tree;
290 return this.toJS(tree.root);
293 var ret = this.cloneConfig(n.elConfig);
294 if (n.childNodes.length) {
296 n.eachChild(function(cn) {
297 ret.items.push(_this.toJS(cn));
310 handleDropNode : function (e)
312 // nodedragover handles the allow/disallow..
316 target - The node being targeted for the drop
317 data - The drag data from the drag source
318 point - The point of the drop - append, above or below
319 source - The drag source
320 rawEvent - Raw mouse event
321 dropNode - Drop node(s) provided by the source OR you can supply node(s) to be inserted by setting them on this object.
322 cancel - Set this to true to cancel the drop.
326 var np = e.point == 'append' ? e.target : e.target.parentNode ; // new parent
328 if (!e.tree || !e.dropNode) {
331 var data = e.source.dragData.selections[0].data;
333 var xar = data.name.split('.');
337 '|xns' : xar.join('.')
340 if (this.dragProp.length > 1) {
341 cfg['*prop'] = this.dragProp;
343 // at this point it should of a set of options...
344 var cls = cfg['|xns'] + '.' + cfg['xtype'];
347 if (typeof(Pman.Builder[cls]) != 'undefined') {
348 Pman.Dialog.BuilderAdd.show( cfg , function(fdata ) {
351 _t.appendNode(e.target, fdata , e.point);
356 this.appendNode(e.target, cfg, e.point);
357 Pman.Tab.BuilderView.panel.redraw();
359 return false; // fixme drop of elements from palete..
362 // always drop onto own parent
363 if (np == e.dropNode.parentNode) {
364 if (e.rawEvent.ctrlKey) {
365 e.dropNode = this.dupeNode(e.dropNode);
369 // can append has to use palete...
370 // this code should be in nodedragover.
372 Roo.log("move checks need moving");
375 if (_this.canAppend(np, e.dropNode.elConfig)) {
376 if (e.rawEvent.ctrlKey) {
377 e.dropNode = _this.dupeNode(e.dropNode);
379 if (np.elConfig.xtype == 'GridEditor') {
380 e.dropNode.elConfig['*prop'] = 'field';
386 Roo.log('can not drop ' + e.dropNode.elConfig.xtype + ' ontop of ' + np.elConfig.xtype);
394 handleDragOver : function(e)
396 Roo.log('nodedragover');
399 // if we have within the same tree:
400 // dropNode (the node being dragged !!important!!)
401 // point: below, append
405 // grid = the grid...
406 // source.dragData.selections[..]
409 // we can only check parents... (we in theory can check dupe properties.. but let's ignore that for the time being.)
414 // drag from palete..
415 if (!e.source.dragData.selections.length) {
419 var drop_rec = e.source.dragData.selections[0];
420 var drop_xtype = drop_rec.data.name;
421 var ok_parents = drop_rec.json.parents;
423 Roo.log("TEST PARENTS: " + ok_parents.join(', '));
424 var new_parent = this.nodeXtype((e.point == 'append') ? e.target : e.target.parentNode);
425 Roo.log("NEW PARENT: " + e.point + " = " + new_parent);
427 // see if the new_parent is actually in the list of ok_parents
431 Roo.each(ok_parents, function(n) {
432 if (n == new_parent || n.split(':').shift() == new_parent) {
433 Roo.log("got match!");
435 _t.dragProp = (n == new_parent) ? '' : n.split(':').pop();
441 // done all the checks...
449 // first see if first element has a name.. - we can not save otherwise..
450 var t = Pman.Tab.BuilderTree.tree;
451 if (!t.root.elConfig.name.length) {
452 Roo.MessageBox.alert("Error", "No name set for form");
456 var sid = (typeof(sid) == 'undefined') ?
457 (Pman.Tab.BuilderTop.filesel.lastData ? Pman.Tab.BuilderTop.filesel.lastData.id : 0) : sid;
460 var js = this.toJS();
461 var render = new Pman.Builder.JsRender(js);
464 // console.log(json);
466 // check the select box to see if that has been set... - save it with that id..
471 url: baseURL + '/Roo/Builder_part.php',
474 json : Roo.encode(js, null, 4),
475 jsource : render.toSource(),
477 module_id : _this.modsel.getValue(),
480 success : function(data) {
484 // _this.filesel.setFromData(data);
486 // cb.call(_this,data);
488 // _this.postCode(data);