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);
34 var hidden = config['builder.hidden'] || false;
40 var newNode = new Roo.tree.TreeNode({
41 text: this.configToText(config)
44 newNode.elConfig = config;
46 //if (markUndo === true) {
47 //Pman.Tab.Builder.markUndo("Add " + newNode.text);
49 // appends to our tree...
50 console.log("APPEND NODE: " + point);
53 parent.parentNode.insertBefore(newNode, parent);
54 parent.parentNode.expand(true);
57 // if it's the last node.. then we append..
58 var ix = parent.parentNode.indexOf(parent) + 1;
59 if (parent.parentNode.childNodes.length == ix) {
60 parent.parentNode.appendChild(newNode);
63 var bef = parent.parentNode.childNodes[ix];
64 parent.parentNode.insertBefore(newNode, bef);
65 parent.parentNode.expand(true);
70 parent.appendChild(newNode);
75 newNode.ui.ctNode.style.display = hidden ? 'none' : '';
78 Roo.each(items, function(i) {
79 this.appendNode(newNode, i);
86 -- panels with panes...
87 if (items && items.length) {
88 for (var i = 0; i < items.length; i++) {
89 this.appendConfig(items[i], newNode, false);
92 if (opts.doUpdate !== false) {
93 this.updateForm(false, newNode);
100 clearAll : function() {
101 var tree = Pman.Tab.BuilderTree.tree;
104 if (rt.childNodes.length) {
105 rt.removeChild(rt.childNodes[0]);
108 tree.root.elConfig = Roo.apply({ }, this.defaultElConfig());
109 //var btop = Pman.Tab.BuilderTop;
110 //if (btop.modsel && btop.modsel.lastData) {
111 // this.tree.root.elConfig.app = btop.modsel.lastData.app;
114 this.setCurrentNode(tree.root,true);
117 cloneConfig : function(config) {
118 if (!config) { return null; }
121 for (var i in config) {
122 if (i.match(/^builder\./)) {
125 if (typeof config[i] == 'object') {
126 newConfig[i] = this.cloneConfig(config[i]);
127 } else if (typeof config[i] != 'function') { // should this happen?
128 newConfig[i] = config[i];
133 configToText : function(c) {
137 var sr = (typeof(c['+buildershow']) != 'undefined') && !c['+buildershow'] ? true : false;
141 if (typeof(c['*prop']) != 'undefined') { txt.push(c['*prop']+ ':'); }
142 if (c.xtype) { txt.push(c.xtype); }
143 if (c.fieldLabel) { txt.push('[' + c.fieldLabel + ']'); }
144 if (c.boxLabel) { txt.push('[' + c.boxLabel + ']'); }
147 if (c.layout) { txt.push('<i>' + c.layout + '</i>'); }
148 if (c.title) { txt.push('<b>' + c.title + '</b>'); }
149 if (c.header) { txt.push('<b>' + c.header + '</b>'); }
150 if (c.legend) { txt.push('<b>' + c.legend + '</b>'); }
151 if (c.text) { txt.push('<b>' + c.text + '</b>'); }
152 if (c.name) { txt.push('<b>' + c.name+ '</b>'); }
153 if (c.region) { txt.push('<i>(' + c.region + ')</i>'); }
154 if (c.dataIndex) { txt.push('[' + c.dataIndex+ ']'); }
158 return (txt.length == 0 ? "Element" : txt.join(" "));
162 currentNodeType : function() {
163 return this.nodeXtype(this.currentNode);
166 defaultElConfig : function() {
175 name : 'Module Name',
180 replaceCurrentNode : function(cfg)
182 var tree = Pman.Tab.BuilderTree.tree;
184 if (this.currentNode == tree.root) {
187 var pn = this.currentNode.parentNode;
189 var ix = pn.indexOf(this.currentNode);
190 pn.removeChild(this.currentNode);
192 if (!pn.childNodes.length) {
193 this.appendNode(pn, cfg, 'append');
198 this.appendNode(pn.childNodes[0], cfg, 'above');
203 this.appendNode(pn.childNodes[ix-1], cfg, 'below');
209 collapseToggle : function()
211 var tree = Pman.Tab.BuilderTree.tree;
213 if (this.currentNode == tree.root) {
216 var cfg = this.currentNode.elConfig;
217 // things that can not be deleted...
218 var hidden = cfg['builder.hidden'] || 0;
220 delete cfg['builder.hidden'];
222 cfg['builder.hidden'] = 1;
226 this.currentNode.ui.ctNode.style.display = !hidden ? 'none' : '';
229 //this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
236 deleteCurrent : function()
239 var tree = Pman.Tab.BuilderTree.tree;
241 if (this.currentNode == tree.root) {
244 var cfg = this.currentNode.elConfig;
245 // things that can not be deleted...
248 var pn = this.currentNode.parentNode;
250 // work out if we make the next or parent the 'current node'
251 var ix = pn.indexOf(this.currentNode);
253 pn.removeChild(this.currentNode);
254 if (pn.childNodes.length) {
255 ix = Math.min(pn.childNodes.length-1, ix);
257 this.setCurrentNode(pn.childNodes.length ? pn.childNodes[ix] : pn ,true);
260 dupeNode : function(node)
262 var cfg = this.cloneConfig(node.elConfig);
264 var newNode = new Roo.tree.TreeNode(
267 text: this.configToText(cfg)
270 newNode.elConfig = cfg;
271 node.eachChild(function(n) {
272 newNode.appendChild(this.dupeNode(n));
278 loadBJS : function(module, part)
282 url : baseURL + '/Roo/Builder_part.php',
287 success : function(res)
292 if (!res.data.json.length) {
293 var cfg = _t.defaultElConfig();
294 cfg.name = Pman.Tab.BuilderTop.filesel.lastData.name;
295 cfg.part = Pman.Tab.BuilderTop.filesel.lastData.name;
302 _t.loadTree(JSON.parse(res.data.json));
312 loadTree : function(o)
314 var tree = Pman.Tab.BuilderTree.tree;
316 tree.root.elConfig = o;
317 if (typeof(o.xtype) == 'undefined') {
320 tree.root.setText(this.configToText(tree.root.elConfig));
321 this.appendNode(tree.root, o.items[0]);
322 tree.root.expand(true);
323 Pman.Tab.BuilderView.panel.redraw();
324 this.setCurrentNode(tree.root,true);
326 nodeXtype : function(n)
328 var tree = Pman.Tab.BuilderTree.tree;
329 if (!n || !n.elConfig) {return ''; }
330 var xt = n.elConfig.xtype || '';
331 var xns= n.elConfig['|xns'] || '';
332 xns += xns.length ? '.' : '';
335 setCurrentNode : function(node,select)
337 var tree = Pman.Tab.BuilderTree.tree;
339 this.currentNode = node || tree.root;
341 //Pman.Tab.BuilderView.highlightElement(this.currentNode);
343 var p = Pman.Tab.BuilderProps.grid;
344 if (p) { //may not be ready yet..
345 p.setCurrrentNode(this.currentNode);
349 this.currentNode.setText(this.configToText(this.currentNode.elConfig));
351 if (select) { //&& node !== this.tree.root) {
352 if (this.currentNode !== tree.root) {
353 this.currentNode.ensureVisible();
355 this.currentNode.expand(false,false);
356 this.currentNode.select();
359 Pman.Tab.BuilderPalette.grid.getSelectionModel().clearSelections();
360 Pman.Tab.BuilderPalette.grid.view.refresh();
366 var tree = Pman.Tab.BuilderTree.tree;
367 return this.toJS(tree.root);
370 var ret = this.cloneConfig(n.elConfig);
371 if (n.childNodes.length) {
373 n.eachChild(function(cn) {
374 ret.items.push(_this.toJS(cn));
387 handleDropNode : function (e)
389 // nodedragover handles the allow/disallow..
393 target - The node being targeted for the drop
394 data - The drag data from the drag source
395 point - The point of the drop - append, above or below
396 source - The drag source
397 rawEvent - Raw mouse event
398 dropNode - Drop node(s) provided by the source OR you can supply node(s) to be inserted by setting them on this object.
399 cancel - Set this to true to cancel the drop.
403 var np = e.point == 'append' ? e.target : e.target.parentNode ; // new parent
405 if (!e.tree || !e.dropNode) {
408 var data = e.source.dragData.selections[0].data;
410 var xar = data.name.split('.');
414 '|xns' : xar.join('.')
417 if (this.dragProp.length > 1) {
418 cfg['*prop'] = this.dragProp;
420 // at this point it should of a set of options...
421 var cls = cfg['|xns'] + '.' + cfg['xtype'];
424 if (typeof(Pman.Builder.Wizard[cls]) != 'undefined') {
425 Pman.Dialog.BuilderAdd.show( cfg , function(fdata ) {
427 _t.appendNode(e.target, fdata , e.point);
428 Pman.Tab.BuilderView.panel.redraw();
433 this.appendNode(e.target, cfg, e.point);
434 Pman.Tab.BuilderView.panel.redraw();
436 return false; // fixme drop of elements from palete..
439 // always drop onto own parent
440 if (np == e.dropNode.parentNode) {
441 if (e.rawEvent.ctrlKey) {
442 e.dropNode = this.dupeNode(e.dropNode);
446 // can append has to use palete...
447 // this code should be in nodedragover.
449 var cfg = this.toJS(e.dropNode);
450 this.appendNode(e.target, cfg, e.point);
452 Roo.log('no e.dropNode?');
453 return false; // do not allow movement..
455 if (_this.canAppend(np, e.dropNode.elConfig)) {
456 if (e.rawEvent.ctrlKey) {
457 e.dropNode = _this.dupeNode(e.dropNode);
459 if (np.elConfig.xtype == 'GridEditor') {
460 e.dropNode.elConfig['*prop'] = 'field';
466 Roo.log('can not drop ' + e.dropNode.elConfig.xtype + ' ontop of ' + np.elConfig.xtype);
474 handleDragOver : function(e)
476 Roo.log('nodedragover');
479 // if we have within the same tree:
480 // dropNode (the node being dragged !!important!!)
481 // point: below, append
485 // grid = the grid...
486 // source.dragData.selections[..]
489 // we can only check parents... (we in theory can check dupe properties.. but let's ignore that for the time being.)
494 // drag from palete..
495 if (!e.source.dragData.selections.length) {
499 var drop_rec = e.source.dragData.selections[0];
500 var drop_xtype = drop_rec.data.name;
501 var ok_parents = drop_rec.json.parents;
503 Roo.log("TEST PARENTS: " + ok_parents.join(', '));
504 var new_parent = this.nodeXtype((e.point == 'append') ? e.target : e.target.parentNode);
505 Roo.log("NEW PARENT: " + e.point + " = " + new_parent);
507 // see if the new_parent is actually in the list of ok_parents
511 Roo.each(ok_parents, function(n) {
512 Roo.log("compare "+n+" ? " + new_parent);
513 if (n == new_parent || n.split(':').shift() == new_parent) {
514 Roo.log("got match!");
516 _t.dragProp = (n == new_parent) ? '' : n.split(':').pop();
522 // done all the checks...
528 // have a drop node - hence comming from the same object..
529 var drop_xtype = this.nodeXtype(e.dropNode);
530 // currently we can not determine validity..
539 // first see if first element has a name.. - we can not save otherwise..
540 var t = Pman.Tab.BuilderTree.tree;
541 if (!t.root.elConfig.name.length) {
542 Roo.MessageBox.alert("Error", "No name set for form");
546 var sid = (typeof(sid) == 'undefined') ?
547 (Pman.Tab.BuilderTop.filesel.lastData ? Pman.Tab.BuilderTop.filesel.lastData.id : 0) : sid;
550 var js = this.toJS();
551 var render = new Pman.Builder.JsRender(js);
554 // console.log(json);
556 // check the select box to see if that has been set... - save it with that id..
561 url: baseURL + '/Roo/Builder_part.php',
564 json : Roo.encode(js, null, 4),
565 jsource : render.toSource(),
567 module_id : Pman.Tab.BuilderTop.modsel.getValue(),
570 success : function(data) {
574 // _this.filesel.setFromData(data);
576 // cb.call(_this,data);
578 // _this.postCode(data);