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);
38 var newNode = new Roo.tree.TreeNode({
39 text: this.configToText(config)
43 newNode.elConfig = config;
44 //if (markUndo === true) {
45 //Pman.Tab.Builder.markUndo("Add " + newNode.text);
47 // appends to our tree...
48 console.log("APPEND NODE: " + point);
51 parent.parentNode.insertBefore(newNode, parent);
52 parent.parentNode.expand(true);
55 // if it's the last node.. then we append..
56 var ix = parent.parentNode.indexOf(parent) + 1;
57 if (parent.parentNode.childNodes.length == ix) {
58 parent.parentNode.appendChild(newNode);
61 var bef = parent.parentNode.childNodes[ix];
62 parent.parentNode.insertBefore(newNode, bef);
63 parent.parentNode.expand(true);
68 parent.appendChild(newNode);
74 Roo.each(items, function(i) {
75 this.appendNode(newNode, i);
82 -- panels with panes...
83 if (items && items.length) {
84 for (var i = 0; i < items.length; i++) {
85 this.appendConfig(items[i], newNode, false);
88 if (opts.doUpdate !== false) {
89 this.updateForm(false, newNode);
96 clearAll : function() {
97 var tree = Pman.Tab.BuilderTree.tree;
100 if (rt.childNodes.length) {
101 rt.removeChild(rt.childNodes[0]);
104 tree.root.elConfig = Roo.apply({ }, this.defaultElConfig());
105 //var btop = Pman.Tab.BuilderTop;
106 //if (btop.modsel && btop.modsel.lastData) {
107 // this.tree.root.elConfig.app = btop.modsel.lastData.app;
110 this.setCurrentNode(tree.root,true);
113 cloneConfig : function(config) {
114 if (!config) { return null; }
117 for (var i in config) {
118 if (typeof config[i] == 'object') {
119 newConfig[i] = this.cloneConfig(config[i]);
120 } else if (typeof config[i] != 'function') { // should this happen?
121 newConfig[i] = config[i];
126 configToText : function(c) {
130 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
131 if (sr) txt.push('<s>');
132 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
133 if (c.xtype) { txt.push(c.xtype); }
134 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
135 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
138 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
139 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
140 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
141 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
142 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
143 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
144 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
145 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
146 if (sr) txt.push('</s>');
147 return (txt.length == 0 ? "Element" : txt.join(" "));
151 currentNodeType : function() {
152 return this.nodeXtype(this.currentNode);
155 defaultElConfig : function() {
164 name : 'Module Name',
169 replaceCurrentNode : function(cfg)
171 var tree = Pman.Tab.BuilderTree.tree;
173 if (this.currentNode == tree.root) {
176 var pn = this.currentNode.parentNode;
178 var ix = pn.indexOf(this.currentNode);
179 pn.removeChild(this.currentNode);
181 if (!pn.childNodes.length) {
182 this.appendNode(pn, cfg, 'append')
187 this.appendNode(pn.childNodes[0], cfg, 'above');
192 this.appendNode(pn.childNodes[ix-1], cfg, 'below');
198 deleteCurrent : function()
201 var tree = Pman.Tab.BuilderTree.tree;
203 if (this.currentNode == tree.root) {
206 var cfg = this.currentNode.elConfig;
207 // things that can not be deleted...
210 var pn = this.currentNode.parentNode;
212 // work out if we make the next or parent the 'current node'
213 var ix = pn.indexOf(this.currentNode);
215 pn.removeChild(this.currentNode);
216 if (pn.childNodes.length) {
217 ix = Math.min(pn.childNodes.length-1, ix);
219 this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
222 dupeNode : function(node)
224 var cfg = this.cloneConfig(node.elConfig);
226 var newNode = new Roo.tree.TreeNode(
229 text: this.configToText(cfg)
232 newNode.elConfig = cfg;
233 node.eachChild(function(n) {
234 newNode.appendChild(this.dupeNode(n));
240 loadBJS : function(module, part)
244 url : baseURL + '/Roo/Builder_part.php',
249 success : function(res)
254 if (!res.data.json.length) {
255 var cfg = _t.defaultElConfig();
256 cfg.name = Pman.Tab.BuilderTop.filesel.lastData.name;
257 cfg.part = Pman.Tab.BuilderTop.filesel.lastData.name;
264 _t.loadTree(JSON.parse(res.data.json));
274 loadTree : function(o)
276 var tree = Pman.Tab.BuilderTree.tree;
278 tree.root.elConfig = o;
279 if (typeof(o.xtype) == 'undefined') {
282 tree.root.setText(this.configToText(tree.root.elConfig));
283 this.appendNode(tree.root, o.items[0]);
284 tree.root.expand(true);
285 Pman.Tab.BuilderView.panel.redraw();
286 this.setCurrentNode(tree.root,true);
288 nodeXtype : function(n)
290 var tree = Pman.Tab.BuilderTree.tree;
291 if (!n || !n.elConfig) {return ''; }
292 var xt = n.elConfig.xtype || '';
293 var xns= n.elConfig['|xns'] || '';
294 xns += xns.length ? '.' : '';
297 setCurrentNode : function(node,select)
299 var tree = Pman.Tab.BuilderTree.tree;
301 this.currentNode = node || tree.root;
303 //Pman.Tab.BuilderView.highlightElement(this.currentNode);
305 var p = Pman.Tab.BuilderProps.grid;
306 if (p) { //may not be ready yet..
307 p.setCurrrentNode(this.currentNode);
311 this.currentNode.setText(this.configToText(this.currentNode.elConfig));
313 if (select) { //&& node !== this.tree.root) {
314 if (this.currentNode !== tree.root) {
315 this.currentNode.ensureVisible();
317 this.currentNode.expand(false,false);
318 this.currentNode.select();
321 Pman.Tab.BuilderPalette.grid.getSelectionModel().clearSelections();
322 Pman.Tab.BuilderPalette.grid.view.refresh();
328 var tree = Pman.Tab.BuilderTree.tree;
329 return this.toJS(tree.root);
332 var ret = this.cloneConfig(n.elConfig);
333 if (n.childNodes.length) {
335 n.eachChild(function(cn) {
336 ret.items.push(_this.toJS(cn));
349 handleDropNode : function (e)
351 // nodedragover handles the allow/disallow..
355 target - The node being targeted for the drop
356 data - The drag data from the drag source
357 point - The point of the drop - append, above or below
358 source - The drag source
359 rawEvent - Raw mouse event
360 dropNode - Drop node(s) provided by the source OR you can supply node(s) to be inserted by setting them on this object.
361 cancel - Set this to true to cancel the drop.
365 var np = e.point == 'append' ? e.target : e.target.parentNode ; // new parent
367 if (!e.tree || !e.dropNode) {
370 var data = e.source.dragData.selections[0].data;
372 var xar = data.name.split('.');
376 '|xns' : xar.join('.')
379 if (this.dragProp.length > 1) {
380 cfg['*prop'] = this.dragProp;
382 // at this point it should of a set of options...
383 var cls = cfg['|xns'] + '.' + cfg['xtype'];
386 if (typeof(Pman.Builder.Wizard[cls]) != 'undefined') {
387 Pman.Dialog.BuilderAdd.show( cfg , function(fdata ) {
389 _t.appendNode(e.target, fdata , e.point);
390 Pman.Tab.BuilderView.panel.redraw();
395 this.appendNode(e.target, cfg, e.point);
396 Pman.Tab.BuilderView.panel.redraw();
398 return false; // fixme drop of elements from palete..
401 // always drop onto own parent
402 if (np == e.dropNode.parentNode) {
403 if (e.rawEvent.ctrlKey) {
404 e.dropNode = this.dupeNode(e.dropNode);
408 // can append has to use palete...
409 // this code should be in nodedragover.
411 var cfg = this.toJS(e.dropNode);
412 this.appendNode(e.target, cfg, e.point);
414 Roo.log('no e.dropNode?');
415 return false; // do not allow movement..
417 if (_this.canAppend(np, e.dropNode.elConfig)) {
418 if (e.rawEvent.ctrlKey) {
419 e.dropNode = _this.dupeNode(e.dropNode);
421 if (np.elConfig.xtype == 'GridEditor') {
422 e.dropNode.elConfig['*prop'] = 'field';
428 Roo.log('can not drop ' + e.dropNode.elConfig.xtype + ' ontop of ' + np.elConfig.xtype);
436 handleDragOver : function(e)
438 Roo.log('nodedragover');
441 // if we have within the same tree:
442 // dropNode (the node being dragged !!important!!)
443 // point: below, append
447 // grid = the grid...
448 // source.dragData.selections[..]
451 // we can only check parents... (we in theory can check dupe properties.. but let's ignore that for the time being.)
456 // drag from palete..
457 if (!e.source.dragData.selections.length) {
461 var drop_rec = e.source.dragData.selections[0];
462 var drop_xtype = drop_rec.data.name;
463 var ok_parents = drop_rec.json.parents;
465 Roo.log("TEST PARENTS: " + ok_parents.join(', '));
466 var new_parent = this.nodeXtype((e.point == 'append') ? e.target : e.target.parentNode);
467 Roo.log("NEW PARENT: " + e.point + " = " + new_parent);
469 // see if the new_parent is actually in the list of ok_parents
473 Roo.each(ok_parents, function(n) {
474 Roo.log("compare "+n+" ? " + new_parent);
475 if (n == new_parent || n.split(':').shift() == new_parent) {
476 Roo.log("got match!");
478 _t.dragProp = (n == new_parent) ? '' : n.split(':').pop();
484 // done all the checks...
490 // have a drop node - hence comming from the same object..
491 var drop_xtype = this.nodeXtype(e.dropNode);
492 // currently we can not determine validity..
501 // first see if first element has a name.. - we can not save otherwise..
502 var t = Pman.Tab.BuilderTree.tree;
503 if (!t.root.elConfig.name.length) {
504 Roo.MessageBox.alert("Error", "No name set for form");
508 var sid = (typeof(sid) == 'undefined') ?
509 (Pman.Tab.BuilderTop.filesel.lastData ? Pman.Tab.BuilderTop.filesel.lastData.id : 0) : sid;
512 var js = this.toJS();
513 var render = new Pman.Builder.JsRender(js);
516 // console.log(json);
518 // check the select box to see if that has been set... - save it with that id..
523 url: baseURL + '/Roo/Builder_part.php',
526 json : Roo.encode(js, null, 4),
527 jsource : render.toSource(),
529 module_id : Pman.Tab.BuilderTop.modsel.getValue(),
532 success : function(data) {
536 // _this.filesel.setFromData(data);
538 // cb.call(_this,data);
540 // _this.postCode(data);