Ticket #5912: tree_custom_nodetype.1.patch

File tree_custom_nodetype.1.patch, 3.2 kB (added by rcoup, 11 months ago)

minor formatting/style improvements

  • tests/test_Tree.html

     
    2020                dojo.require("dijit.ColorPalette"); 
    2121                dojo.require("dijit.Menu"); 
    2222                dojo.require("dojo.parser");    // scan page for widgets and instantiate them 
     23 
     24        // Custom TreeNode implementation, useful for overriding templates, 
     25        // adding checkboxes, or doing other clever things.              
     26        dojo.declare("MyNodeType", [dijit._TreeNode], { 
     27            // override something simple & obvious 
     28                setLabelNode: function(label){ 
     29                        this.labelNode.innerHTML=""; 
     30                        this.labelNode.appendChild(dojo.doc.createTextNode("[MYNODE] " + label)); 
     31                }             
     32        }); 
    2333        </script> 
    2434 
    2535</head> 
     
    112122                        alert("Execute of node " + this.store.getLabel(item) 
    113123                                +", population=" + continentStore.getValue(item, "population")); 
    114124                </script> 
    115 </div> 
     125    </div> 
     126     
     127    <h2>A Tree with a custom TreeNode implementation</h2> 
    116128 
     129    <p>Each label should start with "[MYNODE]"</p> 
     130        <div dojoType="dijit.Tree" id="customNodeTree" 
     131                store="continentStoreDecorated" labelAttr="name" childrenAttr="children" 
     132                openOnClick="true" nodeType="MyNodeType"> 
     133                <script type="dojo/method" event="onClick" args="item"> 
     134                        alert("Execute of node " + continentStore.getLabel(item) 
     135                                +", population=" + continentStore.getValue(item, "population")); 
     136                </script> 
     137        </div> 
    117138</body> 
    118139</html> 
  • Tree.js

     
    175175 
    176176                        // Create _TreeNode widget for each specified tree node, unless one already 
    177177                        // exists and isn't being used (presumably it's from a DnD move and was recently 
    178                         // released 
     178                        // released. We use this.constructor() so that if we're not an actual TreeNode 
     179                        // it continues to work. 
    179180                        dojo.forEach(items, function(item){ 
    180181                                var id = store.getIdentity(item), 
    181182                                        existingNode = tree._itemNodeMap[id], 
    182183                                        node =  
    183184                                                ( existingNode && !existingNode.getParent() ) ? 
    184185                                                existingNode : 
    185                                                 new dijit._TreeNode({ 
     186                                                new this.constructor({ 
    186187                                                        item: item, 
    187188                                                        tree: this.tree, 
    188189                                                        isExpandable: this.tree.mayHaveChildren(item), 
     
    332333        onDndCancel:null, 
    333334        checkAcceptance:null,    
    334335        checkItemAcceptance:null, 
    335  
     336         
     337        // nodeType: Function 
     338        //  constructor function to use for the tree nodes, defaults to dijit._TreeNode 
     339        nodeType: dijit._TreeNode, 
     340         
    336341        _publish: function(/*String*/ topicName, /*Object*/ message){ 
    337342                // summary: 
    338343                //              Publish a message for this widget/topic 
     
    358363                        this.connect(this.store, "onDelete", "_onDeleteItem"); 
    359364                        this.connect(this.store, "onSet", "_onSetItem"); 
    360365                } 
     366 
     367                // resolve a string nodeType into a function 
     368                if (dojo.isString(this.nodeType)) { 
     369                        this.nodeType = dojo.getObject(this.nodeType); 
     370                } 
    361371        }, 
    362372 
    363373        postCreate: function(){ 
     
    409419                                        item = items[0]; 
    410420                                } 
    411421 
    412                                 var rn = this.rootNode = new dijit._TreeNode({ 
     422                                var rn = this.rootNode = new this.nodeType({ 
    413423                                        item: item, 
    414424                                        tree: this, 
    415425                                        isExpandable: true,