Advertisement
Guest User

Untitled

a guest
Dec 12th, 2009
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Ext.tree.ColumnTree = Ext.extend(Ext.tree.TreePanel, {
  2.  
  3.     lines: false,
  4.     borderWidth: Ext.isBorderBox ? 0 : 2, // the combined left/right border for each cell
  5.     cls: 'x-column-tree',
  6.     collapsible: false,
  7.    
  8.     onRender: function(){
  9.    
  10.         Ext.tree.ColumnTree.superclass.onRender.apply(this, arguments);
  11.  
  12.         this.headers = this.body.createChild({
  13.             cls: 'x-tree-headers'
  14.         }, this.body.dom);
  15.        
  16.         var cols = this.columns, c;
  17.         var totalWidth = 0;
  18.        
  19.         for (var i = 0, len = cols.length; i < len; i++) {
  20.        
  21.             c = cols[i];
  22.            
  23.             totalWidth += c.width;
  24.            
  25.             this.headers.createChild({
  26.                 cls: 'x-tree-hd ' + (c.cls ? c.cls + '-hd' : ''),
  27.                 cn: {
  28.                     cls: 'x-tree-hd-text',
  29.                     html: c.header
  30.                 },
  31.                 style: "width:" + (c.width - this.borderWidth) + "px;" + "text-align: " +  (c.align ? c.align : "inherit") + ";"
  32.             });
  33.         }
  34.        
  35.         this.headers.createChild({
  36.             cls: 'x-clear'
  37.         });
  38.        
  39.         // Update height to correct scroll
  40.         this.on("bodyresize", function() {
  41.            
  42.             this.getTreeEl().setHeight( this.getTreeEl().getHeight() - this.headers.getHeight() );
  43.            
  44.         });
  45.        
  46.     }
  47.    
  48. });
  49.  
  50. Ext.tree.ColumnNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
  51.  
  52.     focus: Ext.emptyFn, // prevent odd scrolling behavior
  53.     renderElements: function(n, a, targetNode, bulkRender){
  54.    
  55.         this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';
  56.        
  57.         var t = n.getOwnerTree();
  58.         var cols = t.columns;
  59.         var bw = t.borderWidth;
  60.         var c = cols[0];
  61.        
  62.         n.cols = [];
  63.        
  64.         var cell = {
  65.             css: "",
  66.             attr: ""
  67.         }
  68.        
  69.         var text = n.text || (c.renderer ? c.renderer(a[c.dataIndex], n, a, cell) : a[c.dataIndex]);
  70.  
  71.         n.cols[n.cols.length] = cols[0].dataIndex;
  72.        
  73.         var buf = [
  74.             '<li class="x-tree-node" unselectable="on">',
  75.                 '<div ext:tree-node-id="', n.id, '" class="x-tree-node-el x-tree-node-leaf ', a.cls, '" unselectable="on" ext:column="', c.dataIndex ,'">',
  76.                     '<div class="x-tree-col" style="width:', c.width - bw, 'px;" unselectable="on" ext:column="', c.dataIndex ,'">',
  77.                         '<span class="x-tree-node-indent" unselectable="on">', this.indentMarkup, "</span>",
  78.                         '<img src="', this.emptyIcon, '" class="x-tree-ec-icon x-tree-elbow" unselectable="on" />',
  79.                         '<img src="', a.icon || this.emptyIcon, '" "', cell.attr, '" class="x-tree-node-icon  ', (a.icon ? " x-tree-node-inline-icon" : ""), (a.iconCls ? " " + a.iconCls : ""), '" unselectable="on" />',
  80.                         '<a hidefocus="on" class="x-tree-node-anchor" href="', a.href ? a.href : "#", '" tabIndex="1" ', a.hrefTarget ? ' target="' + a.hrefTarget + '"' : "", ' unselectable="on">',
  81.                             '<span unselectable="on" class="', cell.css ,'" ext:column="', c.dataIndex ,'" style="text-align: ', cols[0].align ? cols[0].align : "inherit"  ,'; ">', text, "</span>",
  82.                         "</a>",
  83.                     "</div>"
  84.         ];
  85.        
  86.         for (var i = 1, len = cols.length; i < len; i++) {
  87.        
  88.             c = cols[i];
  89.            
  90.             var cell = {
  91.                 css: "",
  92.                 attr: ""
  93.             }
  94.            
  95.             var text = (c.renderer ? c.renderer(a[c.dataIndex], n, a, cell) : a[c.dataIndex]);
  96.  
  97.             n.cols[n.cols.length] = cols[i].dataIndex;
  98.  
  99.             buf.push(
  100.                 '<div class="x-tree-col ', (c.cls ? c.cls : ''), '" style="width:', c.width - bw, 'px;" unselectable="on">',
  101.                 '<div class="x-tree-col-text ', cell.css,'" ', cell.attr,' ext:column="', c.dataIndex ,'" unselectable="on" id="', Ext.id(), '" style="text-align: ', c.align ? c.align : "inherit"  ,'; ">', text, "</div>",
  102.                 '</div>'
  103.             );
  104.            
  105.         }
  106.        
  107.         buf.push(
  108.             '<div class="x-clear" unselectable="on"></div></div>',
  109.             '<ul class="x-tree-node-ct" style="display:none;" unselectable="on"></ul>',
  110.             '</li>'
  111.         );
  112.        
  113.         if (bulkRender !== true && n.nextSibling && n.nextSibling.ui.getEl()) {
  114.        
  115.             this.wrap = Ext.DomHelper.insertHtml("beforeBegin", n.nextSibling.ui.getEl(), buf.join(""));
  116.            
  117.         } else {
  118.        
  119.             this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf.join(""));
  120.            
  121.         }
  122.        
  123.         this.elNode = this.wrap.childNodes[0];
  124.         this.ctNode = this.wrap.childNodes[1];
  125.         var cs = this.elNode.firstChild.childNodes;
  126.        
  127.         this.indentNode = cs[0];
  128.         this.ecNode     = cs[1];
  129.         this.iconNode   = cs[2];
  130.         this.anchor     = cs[3];
  131.         this.textNode   = cs[3].firstChild;
  132.     }
  133. });
  134.  
  135. Ext.tree.ColumnTreeEditor = Ext.extend(Ext.util.Observable, {
  136.  
  137.     tree    : null,
  138.     editor  : null,
  139.    
  140.     maxWidth  : 250,
  141.     editDelay : 150,
  142.     boundScroll : false,
  143.    
  144.     constructor: function(tree, config) {
  145.    
  146.         Ext.tree.ColumnTreeEditor.superclass.constructor.apply(this);
  147.        
  148.         this.addEvents({
  149.             "afterNodeColumnEdit"  : true,
  150.             "beforeNodeColumnEdit" : true
  151.         });
  152.        
  153.         this.tree = tree;
  154.        
  155.         if( !tree.rendered ) {
  156.        
  157.             tree.on('render', this.initEditor, this);
  158.            
  159.         } else {
  160.        
  161.             this.initEditor(tree);
  162.            
  163.         }      
  164.        
  165.     },
  166.    
  167.     initEditor: function(tree){
  168.    
  169.         tree.on('beforeclick', this.beforeNodeClick, this);
  170.        
  171.     },
  172.    
  173.     fitToTree: function(ed, el){
  174.  
  175.         var td = this.tree.getTreeEl().dom, nd = el.dom;
  176.        
  177.         if (td.scrollLeft > nd.offsetLeft) {
  178.             td.scrollLeft = nd.offsetLeft; }
  179.        
  180.         var w = Math.min(
  181.             this.maxWidth,
  182.             (td.clientWidth > 20 ? td.clientWidth : td.offsetWidth) -
  183.             Math.max(0, nd.offsetLeft - td.scrollLeft) -
  184.             5
  185.         );
  186.        
  187.         this.editor.setSize(w, '');
  188.        
  189.     },
  190.        
  191.     triggerEdit: function(node, e) {
  192.  
  193.         if( this.editor ) {
  194.             this.editor.completeEdit(); }
  195.        
  196.         if( node.attributes.editable == false ) {
  197.             return; }
  198.  
  199.         var obj = e.target;
  200.  
  201.         if (Ext.select(".x-tree-node-anchor", false, obj).getCount() == 1) {
  202.  
  203.             obj = Ext.select(".x-tree-node-anchor", false, obj).elements[0].firstChild;
  204.            
  205.         } else if (obj.nodeName == 'SPAN' || obj.nodeName == 'DIV') {
  206.  
  207.             obj = e.target;
  208.            
  209.         } else { return false; }
  210.  
  211.         e.stopEvent();
  212.         e.stopPropagation();
  213.  
  214.         this.editNode  = node;
  215.         this.editCol   = obj;
  216.         this.editColID = Ext.get(obj).dom.getAttribute("ext:column");
  217.  
  218.         if( !this.editColID ) {
  219.             return false; }
  220.  
  221.         for (var i = 0; i < this.tree.columns.length; i++) {
  222.            
  223.             if( this.tree.columns[i].dataIndex == this.editColID ) {
  224.            
  225.                 if(this.tree.columns[i].editable == false) {
  226.                     return; }
  227.                    
  228.                 if (this.tree.columns[i].editor) {
  229.                     this.field = this.tree.columns[i].editor; }
  230.                    
  231.                 this.editColIndex = i;
  232.                
  233.                 break;
  234.                
  235.             }
  236.            
  237.         }
  238.  
  239.         if( this.editor ) {
  240.             this.editor.destroy(); }
  241.  
  242.         if( !this.fireEvent(
  243.             "beforeNodeColumnEdit",
  244.             this, this.tree, this.editNode, this.editColID, this.editNode.attributes[this.editColID]
  245.         ) ) { return false; }
  246.  
  247.         this.editor = new Ext.Editor({
  248.             alignment : 'l-l',
  249.             autoSize  : false,
  250.             hideEl    : false,
  251.             cls       : 'x-small-editor x-tree-editor',
  252.             shim      : true,
  253.             shadow    : false
  254.         });
  255.        
  256.         // Fix
  257.         this.editor.alignment = "l-l";
  258.        
  259.         this.editor.on('beforestartedit', this.fitToTree,    this);
  260.         this.editor.on('complete',        this.updateNode,   this);
  261.         this.editor.on('specialkey',      this.onSpecialKey, this);
  262.        
  263.         this.editor.field = this.field.cloneConfig({
  264.         });
  265.        
  266.         if( !this.boundScroll ) {
  267.            
  268.             this.tree.getTreeEl().on(
  269.                 'scroll',
  270.                 function() { if( this.editor ) { this.editor.cancelEdit(); } },
  271.                 this
  272.             );
  273.            
  274.             this.boundScroll = true;
  275.            
  276.         }
  277.        
  278.         this.autoEditTimer = (function(obj, value){
  279.            
  280.             this.editor.startEdit(obj, value);
  281.  
  282.             if (obj.nodeName == 'DIV' ) {
  283.                
  284.                 var width = obj.offsetWidth;
  285.                 this.editor.setSize(width);
  286.                
  287.             }
  288.    
  289.         }).defer(this.editDelay, this, [obj, this.editNode.attributes[this.editColID]]);
  290.        
  291.         return false;
  292.     },
  293.        
  294.     beforeNodeClick: function(node, e){
  295.    
  296.         var sinceLast  = (this.lastClick ? this.lastClick.getElapsed() : 0);
  297.         this.lastClick = new Date();
  298.  
  299.         if( sinceLast > this.editDelay && this.tree.getSelectionModel().isSelected(node)) {
  300.            
  301.             e.stopEvent();
  302.             e.stopPropagation();
  303.  
  304.             this.triggerEdit(node, e);
  305.             return false;
  306.            
  307.         } else {
  308.            
  309.             e.stopEvent();
  310.             e.stopPropagation();
  311.            
  312.             if( this.editor ) { this.editor.completeEdit(); }
  313.             this.triggerEdit(node, e);
  314.            
  315.         }
  316.        
  317.     },
  318.    
  319.     updateNode: function(ed, value, startValue){
  320.    
  321.         if( !this.editor.field.isValid() ) {
  322.             return false; }
  323.    
  324.         this.editNode.cols[this.editColID]       = value; //for internal use only
  325.         this.editNode.attributes[this.editColID] = value; //duplicate into array of node attributes
  326.        
  327.         if( this.tree.columns[this.editColIndex].renderer instanceof Function ) {
  328.        
  329.             var cell = {
  330.                 css : "",
  331.                 attr: ""
  332.             };
  333.            
  334.             this.editCol.innerHTML = this.tree.columns[this.editColIndex].renderer(
  335.                 value,
  336.                 this.tree.getRootNode(),
  337.                 this.editNode.attributes,
  338.                 cell
  339.             );
  340.            
  341.             Ext.get(this.editCol).set({ "class": ("x-tree-col-text " + cell.css) });
  342.                        
  343.         } else {
  344.        
  345.             this.editCol.innerHTML = value;
  346.            
  347.         }   // if
  348.        
  349.         this.fireEvent("afterNodeColumnEdit", this, this.tree, this.editNode, this.editColID, value, startValue);
  350.        
  351.     },
  352.    
  353.     onHide: function(){
  354.        
  355.         Ext.tree.TreeEditor.superclass.onHide.call(this);
  356.        
  357.         if (this.editNode) {
  358.             this.editNode.ui.focus(); }
  359.        
  360.     },
  361.    
  362.     onSpecialKey: function(field, e){
  363.    
  364.         var k = e.getKey();
  365.        
  366.         if (k == e.ESC) {
  367.            
  368.             e.stopEvent();
  369.             this.editor.cancelEdit();
  370.            
  371.         } else if (k == e.ENTER && !e.hasModifier()) {
  372.  
  373.             e.stopEvent();
  374.             this.editor.completeEdit();
  375.            
  376.         }
  377.        
  378.     }
  379.    
  380. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement