comparison src/tools/xml_tools.py @ 805:7c05c39156a2

core (XMLUI), frontends: advancedListContainer part 2:
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:21:51 +0100
parents 5174657b3378
children b11df74e49d2
comparison
equal deleted inserted replaced
804:5174657b3378 805:7c05c39156a2
73 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id) 73 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
74 74
75 if form.instructions: 75 if form.instructions:
76 form_ui.addText('\n'.join(form.instructions), 'instructions') 76 form_ui.addText('\n'.join(form.instructions), 'instructions')
77 77
78 labels = [field for field in form.fieldList if field.label] 78 form_ui.changeContainer("pairs")
79 if labels:
80 # if there is no label, we don't need to use pairs
81 form_ui.changeContainer("pairs")
82 79
83 for field in form.fieldList: 80 for field in form.fieldList:
84 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field) 81 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
85 if labels: 82 label = field.label or field.var
86 if field.label: 83 if label:
87 form_ui.addLabel(field.label) 84 form_ui.addLabel(label)
88 else: 85 else:
89 form_ui.addEmpty() 86 form_ui.addEmpty()
90 87
91 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs) 88 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs)
92 89
93 return form_ui 90 return form_ui
94 91
149 @xmlui_data: data returned by frontends for XMLUI form 146 @xmlui_data: data returned by frontends for XMLUI form
150 @return: dict of data usable by Wokkel's dataform 147 @return: dict of data usable by Wokkel's dataform
151 """ 148 """
152 return {key[len(SAT_FORM_PREFIX):]: value for key, value in xmlui_data.iteritems() if key.startswith(SAT_FORM_PREFIX)} 149 return {key[len(SAT_FORM_PREFIX):]: value for key, value in xmlui_data.iteritems() if key.startswith(SAT_FORM_PREFIX)}
153 150
151 def formEscape(name):
152 """ Return escaped name for forms """
153 return u"%s%s" % (SAT_FORM_PREFIX, name)
154
154 def XMLUIResultToElt(xmlui_data): 155 def XMLUIResultToElt(xmlui_data):
155 """ Construct result domish.Element from XMLUI result 156 """ Construct result domish.Element from XMLUI result
156 @xmlui_data: data returned by frontends for XMLUI form 157 @xmlui_data: data returned by frontends for XMLUI form
157 """ 158 """
158 form = data_form.Form('result') 159 form = data_form.Form('submit')
159 form.makeFields(XMLUIResult2DataFormResult(xmlui_data)) 160 form.makeFields(XMLUIResult2DataFormResult(xmlui_data))
160 return form.toElement() 161 return form.toElement()
161 162
162 def tupleList2dataForm(values): 163 def tupleList2dataForm(values):
163 """convert a list of tuples (name,value) to a wokkel submit data form""" 164 """convert a list of tuples (name,value) to a wokkel submit data form"""
308 type = 'row' 309 type = 'row'
309 310
310 def __init__(self, parent): 311 def __init__(self, parent):
311 assert(isinstance(parent, AdvancedListContainer)) 312 assert(isinstance(parent, AdvancedListContainer))
312 super(RowElement, self).__init__(parent.xmlui, parent) 313 super(RowElement, self).__init__(parent.xmlui, parent)
314 if parent.next_row_idx is not None:
315 if parent.auto_index:
316 raise exceptions.DataError(_("Can't set row index if auto_index is True"))
317 self.elem.setAttribute('index', parent.next_row_idx)
318 parent.next_row_idx = None
313 319
314 320
315 class HeaderElement(Element): 321 class HeaderElement(Element):
316 """" Used by AdvancedListContainer """ 322 """" Used by AdvancedListContainer """
317 type = 'header' 323 type = 'header'
391 397
392 398
393 class AdvancedListContainer(Container): 399 class AdvancedListContainer(Container):
394 type = "advanced_list" 400 type = "advanced_list"
395 401
396 def __init__(self, xmlui, name=None, headers=None, items=None, columns=None, parent=None): 402 def __init__(self, xmlui, callback_id=None, name=None, headers=None, items=None, columns=None, selectable = 'no', auto_index = False, parent=None):
397 """Create an advanced list 403 """Create an advanced list
398 @param headers: optional headers informations 404 @param headers: optional headers informations
399 @param items: list of Item instances 405 @param callback_id: id of the method to call when selection is done
406 @param items: list of widgets to add (just the first row)
407 @param columns: number of columns in this table, or None to autodetect
408 @param selectable: one of:
409 'no': nothing is done
410 'single': one row can be selected
411 @param auto_index: if True, indexes will be generated by frontends, starting from 0
400 @return: created element 412 @return: created element
401 """ 413 """
414 assert selectable in ('no', 'single')
402 if not items and columns is None: 415 if not items and columns is None:
403 raise DataError(_("either items or columns need do be filled")) 416 raise DataError(_("either items or columns need do be filled"))
404 if headers is None: 417 if headers is None:
405 headers = [] 418 headers = []
406 if items is None: 419 if items is None:
416 raise exceptions.DataError(_("Headers lenght doesn't correspond to columns")) 429 raise exceptions.DataError(_("Headers lenght doesn't correspond to columns"))
417 self.addHeaders(headers) 430 self.addHeaders(headers)
418 if items: 431 if items:
419 self.addItems(items) 432 self.addItems(items)
420 self.elem.setAttribute('columns', str(self._columns)) 433 self.elem.setAttribute('columns', str(self._columns))
434 if callback_id is not None:
435 self.elem.setAttribute('callback', callback_id)
436 self.elem.setAttribute('selectable', selectable)
437 self.auto_index = auto_index
438 if auto_index:
439 self.elem.setAttribute('auto_index', 'true')
440 self.next_row_idx = None
421 441
422 def addHeaders(self, headers): 442 def addHeaders(self, headers):
423 for header in headers: 443 for header in headers:
424 self.addHeader(header) 444 self.addHeader(header)
425 445
426 def addHeader(self, header): 446 def addHeader(self, header):
427
428 pass # TODO 447 pass # TODO
429 448
430 def addItems(self, items): 449 def addItems(self, items):
431 for item in items: 450 for item in items:
432 self.append(item) 451 self.append(item)
452
453 def setRowIndex(self, idx):
454 """ Set index for next row
455 index are returned when a row is selected, in data's "index" key
456 @param idx: string index to associate to the next row
457
458 """
459 self.next_row_idx = idx
433 460
434 def append(self, child): 461 def append(self, child):
435 if isinstance(child, RowElement): 462 if isinstance(child, RowElement):
436 return super(AdvancedListContainer, self).append(child) 463 return super(AdvancedListContainer, self).append(child)
437 if self._item_idx % self._columns == 0: 464 if self._item_idx % self._columns == 0: