core (XMLUI), frontends: advancedListContainer part 2:

This commit is contained in:
Goffi 2014-02-04 18:21:51 +01:00
commit 18aeb3a6aa

View file

@ -75,18 +75,15 @@ def dataForm2XMLUI(form, submit_id, session_id=None):
if form.instructions:
form_ui.addText('\n'.join(form.instructions), 'instructions')
labels = [field for field in form.fieldList if field.label]
if labels:
# if there is no label, we don't need to use pairs
form_ui.changeContainer("pairs")
form_ui.changeContainer("pairs")
for field in form.fieldList:
widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
if labels:
if field.label:
form_ui.addLabel(field.label)
else:
form_ui.addEmpty()
label = field.label or field.var
if label:
form_ui.addLabel(label)
else:
form_ui.addEmpty()
form_ui.addWidget(widget_type, *widget_args, **widget_kwargs)
@ -151,11 +148,15 @@ def XMLUIResult2DataFormResult(xmlui_data):
"""
return {key[len(SAT_FORM_PREFIX):]: value for key, value in xmlui_data.iteritems() if key.startswith(SAT_FORM_PREFIX)}
def formEscape(name):
""" Return escaped name for forms """
return u"%s%s" % (SAT_FORM_PREFIX, name)
def XMLUIResultToElt(xmlui_data):
""" Construct result domish.Element from XMLUI result
@xmlui_data: data returned by frontends for XMLUI form
"""
form = data_form.Form('result')
form = data_form.Form('submit')
form.makeFields(XMLUIResult2DataFormResult(xmlui_data))
return form.toElement()
@ -310,6 +311,11 @@ class RowElement(Element):
def __init__(self, parent):
assert(isinstance(parent, AdvancedListContainer))
super(RowElement, self).__init__(parent.xmlui, parent)
if parent.next_row_idx is not None:
if parent.auto_index:
raise exceptions.DataError(_("Can't set row index if auto_index is True"))
self.elem.setAttribute('index', parent.next_row_idx)
parent.next_row_idx = None
class HeaderElement(Element):
@ -393,12 +399,19 @@ class TabsContainer(Container):
class AdvancedListContainer(Container):
type = "advanced_list"
def __init__(self, xmlui, name=None, headers=None, items=None, columns=None, parent=None):
def __init__(self, xmlui, callback_id=None, name=None, headers=None, items=None, columns=None, selectable = 'no', auto_index = False, parent=None):
"""Create an advanced list
@param headers: optional headers informations
@param items: list of Item instances
@param callback_id: id of the method to call when selection is done
@param items: list of widgets to add (just the first row)
@param columns: number of columns in this table, or None to autodetect
@param selectable: one of:
'no': nothing is done
'single': one row can be selected
@param auto_index: if True, indexes will be generated by frontends, starting from 0
@return: created element
"""
assert selectable in ('no', 'single')
if not items and columns is None:
raise DataError(_("either items or columns need do be filled"))
if headers is None:
@ -418,19 +431,33 @@ class AdvancedListContainer(Container):
if items:
self.addItems(items)
self.elem.setAttribute('columns', str(self._columns))
if callback_id is not None:
self.elem.setAttribute('callback', callback_id)
self.elem.setAttribute('selectable', selectable)
self.auto_index = auto_index
if auto_index:
self.elem.setAttribute('auto_index', 'true')
self.next_row_idx = None
def addHeaders(self, headers):
for header in headers:
self.addHeader(header)
def addHeader(self, header):
pass # TODO
def addItems(self, items):
for item in items:
self.append(item)
def setRowIndex(self, idx):
""" Set index for next row
index are returned when a row is selected, in data's "index" key
@param idx: string index to associate to the next row
"""
self.next_row_idx = idx
def append(self, child):
if isinstance(child, RowElement):
return super(AdvancedListContainer, self).append(child)