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;
23 if (typeof(inConfig) == 'undefined') {
27 if (typeof(inConfig) !== 'undefined' && inConfig.items) { // loading!
28 items = inConfig.items;
29 delete inConfig.items;
32 var config = this.cloneConfig(inConfig);
33 config['builder.hidden'] = config['builder.hidden'] || 0;
39 var newNode = new Roo.tree.TreeNode({
40 text: this.configToText(config)
43 newNode.elConfig = config;
45 //if (markUndo === true) {
46 //Pman.Tab.Builder.markUndo("Add " + newNode.text);
48 // appends to our tree...
49 console.log("APPEND NODE: " + point);
52 parent.parentNode.insertBefore(newNode, parent);
53 parent.parentNode.expand(true);
56 // if it's the last node.. then we append..
57 var ix = parent.parentNode.indexOf(parent) + 1;
58 if (parent.parentNode.childNodes.length == ix) {
59 parent.parentNode.appendChild(newNode);
62 var bef = parent.parentNode.childNodes[ix];
63 parent.parentNode.insertBefore(newNode, bef);
64 parent.parentNode.expand(true);
69 parent.appendChild(newNode);
74 newNode.ui.ctNode.style.display = config['builder.hidden'] ? 'none' : '';
77 Roo.each(items, function(i) {
78 this.appendNode(newNode, i);
85 -- panels with panes...
86 if (items && items.length) {
87 for (var i = 0; i < items.length; i++) {
88 this.appendConfig(items[i], newNode, false);
91 if (opts.doUpdate !== false) {
92 this.updateForm(false, newNode);
99 clearAll : function() {
100 var tree = Pman.Tab.BuilderTree.tree;
103 if (rt.childNodes.length) {
104 rt.removeChild(rt.childNodes[0]);
107 tree.root.elConfig = Roo.apply({ }, this.defaultElConfig());
108 //var btop = Pman.Tab.BuilderTop;
109 //if (btop.modsel && btop.modsel.lastData) {
110 // this.tree.root.elConfig.app = btop.modsel.lastData.app;
113 this.setCurrentNode(tree.root,true);
116 cloneConfig : function(config) {
117 if (!config) { return null; }
120 for (var i in config) {
121 if (i.match(/^builder\./)) {
124 if (typeof config[i] == 'object') {
125 newConfig[i] = this.cloneConfig(config[i]);
126 } else if (typeof config[i] != 'function') { // should this happen?
127 newConfig[i] = config[i];
132 configToText : function(c) {
136 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
137 if (sr) txt.push('<s>');
138 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
139 if (c.xtype) { txt.push(c.xtype); }
140 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
141 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
144 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
145 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
146 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
147 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
148 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
149 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
150 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
151 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
152 if (sr) txt.push('</s>');
153 return (txt.length == 0 ? "Element" : txt.join(" "));
157 currentNodeType : function() {
158 return this.nodeXtype(this.currentNode);
161 defaultElConfig : function() {
170 name : 'Module Name',
175 replaceCurrentNode : function(cfg)
177 var tree = Pman.Tab.BuilderTree.tree;
179 if (this.currentNode == tree.root) {
182 var pn = this.currentNode.parentNode;
184 var ix = pn.indexOf(this.currentNode);
185 pn.removeChild(this.currentNode);
187 if (!pn.childNodes.length) {
188 this.appendNode(pn, cfg, 'append')
193 this.appendNode(pn.childNodes[0], cfg, 'above');
198 this.appendNode(pn.childNodes[ix-1], cfg, 'below');
204 collapseToggle : function()
206 var tree = Pman.Tab.BuilderTree.tree;
208 if (this.currentNode == tree.root) {
211 var cfg = this.currentNode.elConfig;
212 // things that can not be deleted...
214 cfg['builder.hidden'] = !(cfg['builder.hidden'] || 0);
216 this.currentNode.ui.ctNode.style.display = cfg['builder.hidden'] ? 'none' : '';
219 //this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
226 deleteCurrent : function()
229 var tree = Pman.Tab.BuilderTree.tree;
231 if (this.currentNode == tree.root) {
234 var cfg = this.currentNode.elConfig;
235 // things that can not be deleted...
238 var pn = this.currentNode.parentNode;
240 // work out if we make the next or parent the 'current node'
241 var ix = pn.indexOf(this.currentNode);
243 pn.removeChild(this.currentNode);
244 if (pn.childNodes.length) {
245 ix = Math.min(pn.childNodes.length-1, ix);
247 this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
250 dupeNode : function(node)
252 var cfg = this.cloneConfig(node.elConfig);
254 var newNode = new Roo.tree.TreeNode(
257 text: this.configToText(cfg)
260 newNode.elConfig = cfg;
261 node.eachChild(function(n) {
262 newNode.appendChild(this.dupeNode(n));
268 loadBJS : function(module, part)
272 url : baseURL + '/Roo/Builder_part.php',
277 success : function(res)
282 if (!res.data.json.length) {
283 var cfg = _t.defaultElConfig();
284 cfg.name = Pman.Tab.BuilderTop.filesel.lastData.name;
285 cfg.part = Pman.Tab.BuilderTop.filesel.lastData.name;
292 _t.loadTree(JSON.parse(res.data.json));
302 loadTree : function(o)
304 var tree = Pman.Tab.BuilderTree.tree;
306 tree.root.elConfig = o;
307 if (typeof(o.xtype) == 'undefined') {
310 tree.root.setText(this.configToText(tree.root.elConfig));
311 this.appendNode(tree.root, o.items[0]);
312 tree.root.expand(true);
313 Pman.Tab.BuilderView.panel.redraw();
314 this.setCurrentNode(tree.root,true);
316 nodeXtype : function(n)
318 var tree = Pman.Tab.BuilderTree.tree;
319 if (!n || !n.elConfig) {return ''; }
320 var xt = n.elConfig.xtype || '';
321 var xns= n.elConfig['|xns'] || '';
322 xns += xns.length ? '.' : '';
325 setCurrentNode : function(node,select)
327 var tree = Pman.Tab.BuilderTree.tree;
329 this.currentNode = node || tree.root;
331 //Pman.Tab.BuilderView.highlightElement(this.currentNode);
333 var p = Pman.Tab.BuilderProps.grid;
334 if (p) { //may not be ready yet..
335 p.setCurrrentNode(this.currentNode);
339 this.currentNode.setText(this.configToText(this.currentNode.elConfig));
341 if (select) { //&& node !== this.tree.root) {
342 if (this.currentNode !== tree.root) {
343 this.currentNode.ensureVisible();
345 this.currentNode.expand(false,false);
346 this.currentNode.select();
349 Pman.Tab.BuilderPalette.grid.getSelectionModel().clearSelections();
350 Pman.Tab.BuilderPalette.grid.view.refresh();
356 var tree = Pman.Tab.BuilderTree.tree;
357 return this.toJS(tree.root);
360 var ret = this.cloneConfig(n.elConfig);
361 if (n.childNodes.length) {
363 n.eachChild(function(cn) {
364 ret.items.push(_this.toJS(cn));
377 handleDropNode : function (e)
379 // nodedragover handles the allow/disallow..
383 target - The node being targeted for the drop
384 data - The drag data from the drag source
385 point - The point of the drop - append, above or below
386 source - The drag source
387 rawEvent - Raw mouse event
388 dropNode - Drop node(s) provided by the source OR you can supply node(s) to be inserted by setting them on this object.
389 cancel - Set this to true to cancel the drop.
393 var np = e.point == 'append' ? e.target : e.target.parentNode ; // new parent
395 if (!e.tree || !e.dropNode) {
398 var data = e.source.dragData.selections[0].data;
400 var xar = data.name.split('.');
404 '|xns' : xar.join('.')
407 if (this.dragProp.length > 1) {
408 cfg['*prop'] = this.dragProp;
410 // at this point it should of a set of options...
411 var cls = cfg['|xns'] + '.' + cfg['xtype'];
414 if (typeof(Pman.Builder.Wizard[cls]) != 'undefined') {
415 Pman.Dialog.BuilderAdd.show( cfg , function(fdata ) {
417 _t.appendNode(e.target, fdata , e.point);
418 Pman.Tab.BuilderView.panel.redraw();
423 this.appendNode(e.target, cfg, e.point);
424 Pman.Tab.BuilderView.panel.redraw();
426 return false; // fixme drop of elements from palete..
429 // always drop onto own parent
430 if (np == e.dropNode.parentNode) {
431 if (e.rawEvent.ctrlKey) {
432 e.dropNode = this.dupeNode(e.dropNode);
436 // can append has to use palete...
437 // this code should be in nodedragover.
439 var cfg = this.toJS(e.dropNode);
440 this.appendNode(e.target, cfg, e.point);
442 Roo.log('no e.dropNode?');
443 return false; // do not allow movement..
445 if (_this.canAppend(np, e.dropNode.elConfig)) {
446 if (e.rawEvent.ctrlKey) {
447 e.dropNode = _this.dupeNode(e.dropNode);
449 if (np.elConfig.xtype == 'GridEditor') {
450 e.dropNode.elConfig['*prop'] = 'field';
456 Roo.log('can not drop ' + e.dropNode.elConfig.xtype + ' ontop of ' + np.elConfig.xtype);
464 handleDragOver : function(e)
466 Roo.log('nodedragover');
469 // if we have within the same tree:
470 // dropNode (the node being dragged !!important!!)
471 // point: below, append
475 // grid = the grid...
476 // source.dragData.selections[..]
479 // we can only check parents... (we in theory can check dupe properties.. but let's ignore that for the time being.)
484 // drag from palete..
485 if (!e.source.dragData.selections.length) {
489 var drop_rec = e.source.dragData.selections[0];
490 var drop_xtype = drop_rec.data.name;
491 var ok_parents = drop_rec.json.parents;
493 Roo.log("TEST PARENTS: " + ok_parents.join(', '));
494 var new_parent = this.nodeXtype((e.point == 'append') ? e.target : e.target.parentNode);
495 Roo.log("NEW PARENT: " + e.point + " = " + new_parent);
497 // see if the new_parent is actually in the list of ok_parents
501 Roo.each(ok_parents, function(n) {
502 Roo.log("compare "+n+" ? " + new_parent);
503 if (n == new_parent || n.split(':').shift() == new_parent) {
504 Roo.log("got match!");
506 _t.dragProp = (n == new_parent) ? '' : n.split(':').pop();
512 // done all the checks...
518 // have a drop node - hence comming from the same object..
519 var drop_xtype = this.nodeXtype(e.dropNode);
520 // currently we can not determine validity..
529 // first see if first element has a name.. - we can not save otherwise..
530 var t = Pman.Tab.BuilderTree.tree;
531 if (!t.root.elConfig.name.length) {
532 Roo.MessageBox.alert("Error", "No name set for form");
536 var sid = (typeof(sid) == 'undefined') ?
537 (Pman.Tab.BuilderTop.filesel.lastData ? Pman.Tab.BuilderTop.filesel.lastData.id : 0) : sid;
540 var js = this.toJS();
541 var render = new Pman.Builder.JsRender(js);
544 // console.log(json);
546 // check the select box to see if that has been set... - save it with that id..
551 url: baseURL + '/Roo/Builder_part.php',
554 json : Roo.encode(js, null, 4),
555 jsource : render.toSource(),
557 module_id : Pman.Tab.BuilderTop.modsel.getValue(),
560 success : function(data) {
564 // _this.filesel.setFromData(data);
566 // cb.call(_this,data);
568 // _this.postCode(data);