""" Author: R. Le Gac """ import locale import os from gluon import current from helper import is_mathjax def to_panel(**kwargs): """Return the configuration dictionary of an Ext.Panel. The keyword arguments are the configuration options of the Ext.Panel widget. """ cfg = {'xtype': 'panel'} cfg.update(kwargs) return cfg def to_fieldset(fields, **kwargs): """Return the confiuration dictionary of Ext.form.Fieldset. This widget contains list of Ext.form.Field. fields is a list of configuration dictioanry for form.Fields. The keyword arguments are the configuration options of the Ext.form.FieldSet widget. """ cfg = {'defaults': {'anchor': '99%'}, 'items': fields, 'xtype': 'fieldset'} cfg.update(kwargs) return cfg def to_panelWithUrlSelector(panelCfg, selectorCfg, baseUrl=None, application=None, controller=None, function=None, ctrlField=None, extField=None, funcField=None): """Return the configuration dictionary for an App.PanelWithUrlSelector. This widget is split in two part a main panel and a selector. The selector display a list of fields organized in fieldset. There values define the URL parameters. The main panel displays the URL content. Configuration dictionary for the main panel and fieldset are set via: panelCfg selectorCfg In web2py an URL is defined as application/controller/function.extension There are several ways to define it. application The base URL is: /application By default the application is the current application. baseUrl Another way to defined the URL associated to the panel. It should be a well-formed URL string, i.e http://blabla. It is a more general approach where the URL can point to a any links. controller The base URL is: /application/controller. function The base URL is: /application/controller/function. The URL can be modified dynamicaly by extracting the name of the controller, function and/or extension from the selector fields. ctrlField Name of the field defining the name of the controller. When define the URL become baseURL/ctrlFieldValue. extField Name of the field defining the name of the extension. When define the URL become baseUrl.extFieldValue. Useful to play with different view rendering the same controller/function. funcField Name of the form field defining the name of the function. When define the URL become baseURL/funcFieldValue. To be used with ctrlField. In that case the URL is /application/ctrFieldValue/funcFieldValue. """ url = baseUrl if not url: if application: url = os.path.join('/', application) else: url = os.path.join('/', current.request.application) if controller: url = os.path.join(url, controller) if function: url = os.path.join(url, function) cfg = {'baseUrl': url, 'ctrlField': ctrlField, 'extField': extField, 'funcField': funcField, 'isMathJax': is_mathjax(), 'panelCfg': panelCfg, 'selectorCfg': selectorCfg, 'xtype': 'xpanelwithurlselector'} return cfg def to_urlPanel(url): """Return the configuration dictionary for an Ext.Panel displaying an URL. url well-formed URL string, i.e http://blabla """ cfg = to_panel(autoload=url, preventBodyReset=True) if is_mathjax(): cfg['plugins'] = ['pPanelMathJax'] return cfg class Node(object): """Node associating a leaf to a widget. """ def __init__(self, text): """Constructor of the node text name of the node appearing in the viewport """ 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 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. leaves a list of string func function to translate the leaf name into the configuration dictionary of the associated ExtJS widget hidden List of children to be hidden Leaf names are translated and sorted by alphabetic order. """ # translator T = current.T # translate and order leaves in alphabetic order # according to local setting cvt = {} for el in leaves: cvt[T(el)] = el translate_leaves = cvt.keys() translate_leaves.sort(cmp=locale.strcoll) # fill the node with its children for tr_leaf in translate_leaves: leaf = cvt[tr_leaf] if leaf in hidden: continue self.add_child(tr_leaf, func(leaf)) def get_node(self): """Return the configuration dictionary for the node. """ return {'text': self.text, 'children': self.children} 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])