navtree.py 2.45 KB
Newer Older
Renaud Le Gac's avatar
Renaud Le Gac committed
1
""" Author: R. Le Gac
2 3 4 5 6

"""

import locale
from gluon import current
7

8

9 10 11 12
class Node(object):
    """Node associating a leaf to a widget.
    
    """
Renaud Le Gac's avatar
Renaud Le Gac committed
13
    def __init__(self, text):
14
        """Constructor of the node
15 16
        
                text
17
                    name of the node appearing in the viewport
Renaud Le Gac's avatar
Renaud Le Gac committed
18 19 20 21 22 23 24 25 26 27 28 29
                                    
        """
        self.text = text
        self.children = []
        self.hidden = []


    def add_child(self, text, cfg):
        """Add a child (leaf) to the node.
        
                text
                    the name of the leaf
30
                    
Renaud Le Gac's avatar
Renaud Le Gac committed
31 32 33 34 35 36 37 38 39 40 41 42
                cfg
                    configuration dictionary for the associated
                    ExtJS widget

        """
        di = {'cfg': cfg, 'leaf': True, 'text': text}
        self.children.append(di)


    def add_children(self, leaves, func=None, hidden=[]):
        """Helper method to add children to the node.
        
43
                leaves
Renaud Le Gac's avatar
Renaud Le Gac committed
44
                    a list of string
45
                    
Renaud Le Gac's avatar
Renaud Le Gac committed
46 47 48 49
                func
                    function to translate the leaf name into 
                    the configuration dictionary of the associated ExtJS widget

50
                hidden
Renaud Le Gac's avatar
Renaud Le Gac committed
51 52 53
                    List of children to be hidden
                    
        Leaf names are translated and sorted by alphabetic order.
54

Renaud Le Gac's avatar
Renaud Le Gac committed
55
        """
56
        # translator
57 58
        T = current.T
        
Renaud Le Gac's avatar
Renaud Le Gac committed
59
        # translate and order leaves in alphabetic order 
60 61 62 63
        # according to local setting
        cvt = {}
        for el in leaves:
            cvt[T(el)] = el
64
                  
65 66 67
        translate_leaves = cvt.keys()
        translate_leaves.sort(cmp=locale.strcoll)            

68
        # fill the node with its children        
69 70 71
        for tr_leaf in translate_leaves:
            leaf = cvt[tr_leaf]        
            if leaf in hidden:
72
                continue
73

Renaud Le Gac's avatar
Renaud Le Gac committed
74
            self.add_child(tr_leaf, func(leaf))
75
                
Renaud Le Gac's avatar
Renaud Le Gac committed
76

77 78
    def get_node(self):
        """Return the configuration dictionary for the node.
Renaud Le Gac's avatar
Renaud Le Gac committed
79
        
80 81
        """
        return {'text': self.text, 'children': self.children}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


    def sort_children(self):
        """sort children according to alphabetical order.
        
        """
        di = {}
        for child in self.children:
            di[child['text']] = child
            
        names = di.keys()
        names.sort(cmp=locale.strcoll)
        
        self.children = []
        for name in names:
            self.children.append(di[name])