navtree.py 4.6 KB
Newer Older
1 2 3 4 5 6 7 8
""" $Id$

"""

__version__ = "$Revision$"

import locale
from gluon import current
Renaud Le Gac's avatar
Renaud Le Gac committed
9
from helper import is_mathjax
10 11


12 13 14 15
def to_panelWithUrlSelector(formItems,
                            application=None,
                            baseUrl=None,
                            controller=None,
Renaud Le Gac's avatar
Renaud Le Gac committed
16 17 18
                            ctrlField=None, 
                            title='Configure'):
    """Return the configuration dictionary for an App.PanelWithUrlSelector.
19
    the main panel displays the URL content while the selector panel shows
Renaud Le Gac's avatar
Renaud Le Gac committed
20
    a form allowing to change the URL setting. 
21
    
22 23 24 25 26 27 28
        application and controller
            The web2py way of defining the URL: /application/controller.
            If the application is not defined the URL is:
            /current application/controller.
            These keyword arguments make sense when using the ctrlField.
            See also baseUrl.
            
Renaud Le Gac's avatar
Renaud Le Gac committed
29
        baseUrl
30 31 32
            Well-formed URL string, i.e http://blabla
            See also application and controller
                        
Renaud Le Gac's avatar
Renaud Le Gac committed
33
        ctrlField
34
            Name of the form field defining the controller
Renaud Le Gac's avatar
Renaud Le Gac committed
35 36 37
            When define the URL become http://blabla/ctrlFieldValue
        
        formItems
38
            A list of configuration dictionary defining the
Renaud Le Gac's avatar
Renaud Le Gac committed
39 40 41
            widget embedded in the form
            
        title
42
            Title of the box surrounding form widgets
43 44
            
    """
45 46 47 48 49 50 51 52
    url = baseUrl
    if not url:
        if application:
            url = '/%s/%s' % (application, controller)
        else:
            url = '/%s/%s' % (current.request.application, controller)
        
    cfg = {'baseUrl': url,
Renaud Le Gac's avatar
Renaud Le Gac committed
53
           'ctrlField': ctrlField,
54
           'isMathJax': is_mathjax(),
55
           'panelCfg': None,
56
           'selectorCfg':None,
57
           'xtype': 'xpanelwithurlselector'}
58

59
    # configuration of the child panel displaying URL
Renaud Le Gac's avatar
Renaud Le Gac committed
60
    cfg['panelCfg'] = {'plugins': [], 
61
                       'xtype': 'panel'}
62
                               
Renaud Le Gac's avatar
Renaud Le Gac committed
63
    # configuration of the selector
64
    cfg['selectorCfg'] = {'defaults': {'anchor': '99%'},
Renaud Le Gac's avatar
Renaud Le Gac committed
65 66
                          'items': formItems,
                          'title': title,
67
                          'xtype': 'fieldset'}
68
      
69 70 71
    return cfg


Renaud Le Gac's avatar
Renaud Le Gac committed
72
def to_urlPanel(url):
73
    """Return the configuration dictionary for an Ext.Panel
74
    displaying an URL.
75 76
    
        url
Renaud Le Gac's avatar
Renaud Le Gac committed
77
            well-formed URL string, i.e http://blabla
78
    
79
    """
80
    cfg = {'autoLoad': url,
Renaud Le Gac's avatar
Renaud Le Gac committed
81
           'plugins': [], 
82 83
           'preventBodyReset': True, 
           'xtype': 'panel'}
Renaud Le Gac's avatar
Renaud Le Gac committed
84 85 86

    if is_mathjax:
        cfg['plugins'].append('pPanelMathJax')
87 88
    
    return cfg
89

90

91 92 93 94
class Node(object):
    """Node associating a leaf to a widget.
    
    """
Renaud Le Gac's avatar
Renaud Le Gac committed
95
    def __init__(self, text):
96
        """Constructor of the node
97 98
        
                text
99
                    name of the node appearing in the viewport
Renaud Le Gac's avatar
Renaud Le Gac committed
100 101 102 103 104 105 106 107 108 109 110 111
                                    
        """
        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
112
                    
Renaud Le Gac's avatar
Renaud Le Gac committed
113 114 115 116 117 118 119 120 121 122 123 124
                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.
        
125
                leaves
Renaud Le Gac's avatar
Renaud Le Gac committed
126
                    a list of string
127
                    
Renaud Le Gac's avatar
Renaud Le Gac committed
128 129 130 131
                func
                    function to translate the leaf name into 
                    the configuration dictionary of the associated ExtJS widget

132
                hidden
Renaud Le Gac's avatar
Renaud Le Gac committed
133 134 135
                    List of children to be hidden
                    
        Leaf names are translated and sorted by alphabetic order.
136

Renaud Le Gac's avatar
Renaud Le Gac committed
137
        """
138
        # translator
139 140
        T = current.T
        
Renaud Le Gac's avatar
Renaud Le Gac committed
141
        # translate and order leaves in alphabetic order 
142 143 144 145
        # according to local setting
        cvt = {}
        for el in leaves:
            cvt[T(el)] = el
146
                  
147 148 149
        translate_leaves = cvt.keys()
        translate_leaves.sort(cmp=locale.strcoll)            

150
        # fill the node with its children        
151 152 153
        for tr_leaf in translate_leaves:
            leaf = cvt[tr_leaf]        
            if leaf in hidden:
154
                continue
155

Renaud Le Gac's avatar
Renaud Le Gac committed
156
            self.add_child(tr_leaf, func(leaf))
157
                
Renaud Le Gac's avatar
Renaud Le Gac committed
158

159 160
    def get_node(self):
        """Return the configuration dictionary for the node.
Renaud Le Gac's avatar
Renaud Le Gac committed
161
        
162 163
        """
        return {'text': self.text, 'children': self.children}