Mercurial > libervia-templates
changeset 53:dfef430a26ef
input(xmlui): added a macro to generate a table from list of XMLUI items + many improvments in item generation (see docstrings)
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 27 Oct 2017 18:55:25 +0200 |
parents | 87680eed9e25 |
children | a5dc14675d5e |
files | default/input/xmlui.html |
diffstat | 1 files changed, 118 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/default/input/xmlui.html Fri Oct 27 18:54:01 2017 +0200 +++ b/default/input/xmlui.html Fri Oct 27 18:55:25 2017 +0200 @@ -2,73 +2,121 @@ {# generate methods #} -{% macro generate_container(cont) %} +{% macro generate_container(cont, config) %} {% if cont.type == 'vertical' %} - {{ vertical_container(cont) }} + {{ vertical_container(cont, config) }} {% elif cont.type == 'pairs' %} - {{ pairs_container(cont) }} + {{ pairs_container(cont, config) }} {% elif cont.type == 'label' %} - {{ label_container(cont) }} + {{ label_container(cont, config) }} {% endif %} {% endmacro %} -{% macro generate_widget(wid, id=none) %} +{% macro generate_widget(wid, config, id=none) %} {% if wid.type == 'text' %} - {{ text_widget(wid, id=id) }} + {{ text_widget(wid, config, id=id) }} {% elif wid.type == 'label' %} - {{ label_widget(wid) }} + {{ label_widget(wid, config) }} {% elif wid.type == 'string' %} - {{ string_widget(wid, id=id) }} + {{ string_widget(wid, config, id=id) }} {% elif wid.type == 'textbox' %} - {{ textbox_widget(wid, id=id) }} + {{ textbox_widget(wid, config, id=id) }} {% elif wid.type == 'list' %} - {{ list_widget(wid, id=id) }} + {{ list_widget(wid, config, id=id) }} {% endif %} {% endmacro %} -{% macro generate_children(cont) %} +{% macro generate_children(cont, config) %} {% for child in cont.children %} {% if child.category == 'container' %} - {{ generate_container(child) }} + {{ generate_container(child, config) }} {% else %} - {{ generate_widget(child) }} + {{ generate_widget(child, config) }} {% endif %} {% endfor %} {% endmacro %} -{% macro generate(xmlui) %} - {{ generate_container(xmlui.main_cont) }} +{% macro generate(xmlui, form=true, filters=none) %} +{# generate HTML from XMLUI + @param xmlui(template_xmlui.XMLUIPanel): xmlui to use + @param form(bool): if true will generate form elements + @param filters(dict,none): filters as expected by item_filter +#} + {% set config = {'form':form, 'filters':filters or {}} %} + {{ generate_container(xmlui.main_cont, config) }} +{% endmacro %} + +{% macro generate_table(xmlui_items, fields, formatters, tr_class_fields, on_click) %} +{# generate a HTML table from requested widgets names + @param xmlui_items(iterable[unicode]): list of xmlui to show (one per row) + @param fields(tuple[unicode,unicode]): fields to show (name, label) + @param formatters(dict): dictionary of templates to format values: + field_name => template + if no formatter is set (or None is used) for a field, it will be used unmodified. + current xmlui items will be set as "item" key + @param tr_class_fields(iterable[unicode]): name of fields to use as class + class will be "{name}_{value}" where name is field name, and value field value + all lowercase/stripped + @param on_click(data_objects.OnClick): thing to do when clicking on a row +#} + {% if formatters is undefined %} + {% set formatters = {} %} + {% endif %} + <table> + <thead> + <tr> + {% for name,label in fields %} + <th>{{ label }}</th> + {% endfor %} + </tr> + </thead> + <tbody> + {% for xmlui in xmlui_items %} + {% set link=on_click.formatUrl(xmlui.widget_value) if on_click.url else none %} + <tr {{ {'class': xmlui|xmlui_class(tr_class_fields)}|xmlattr }}> + + {% for name,label in fields %} + <td {{ {'class': 'td_'+name}|xmlattr }}> + {% for value in xmlui.widgets[name].values %} + <a {{ {'href':link}|xmlattr }}>{{ value|adv_format(formatters.get(name),item=xmlui.widget_value) }}</a> + {% endfor %} + </td> + {% endfor %} + </tr> + {% endfor %} + </tbody> + </table> {% endmacro %} {# containers #} -{% macro vertical_container(cont) %} +{% macro vertical_container(cont, config) %} <div class="xmlui_cont xmlui_cont_vertical"> - {{ generate_children(cont) }} + {{ generate_children(cont, config) }} </div> {% endmacro %} -{% macro pairs_container(cont) %} +{% macro pairs_container(cont, config) %} {# TODO: proper impelmentation (do the same as vertical container for now #} <div class="xmlui_cont xmlui_cont_vertical"> - {{ generate_children(cont) }} + {{ generate_children(cont, config) }} </div> {% endmacro %} -{% macro label_container(cont) %} +{% macro label_container(cont, config) %} <div class="xmlui_cont xmlui_cont_vertical"> {% for child in cont.children %} {% if loop.index is odd %} {# label #} - {% set id = 'widget'|next_gidx %} {% if child.type == 'label' %} - {{ label_widget(child, for=id) }} + {% set for_ = ('wid_' + (child.for_name or child.name or '_noname'))|next_gidx %} + {{ label_widget(child, config, for=for_) }} {% endif %} {% else %} {# widget #} - {% set id = 'widget'|cur_gidx %} - {{ generate_widget(child, id=id) }} + {% set id = ('wid_' + (child.name or '_noname'))|cur_gidx %} + {{ generate_widget(child, config, id=id) }} {% endif %} {% endfor %} </div> @@ -77,32 +125,60 @@ {# widgets #} -{% macro text_widget(wid, id=none) %} +{% macro text_widget(wid, config, id=none) %} <p class="xmlui_widget xmlui_text" {{ {'id':id}|xmlattr }}> - {{wid.value}} + {{- wid|item_filter(config.filters)|default('\u00A0',true) -}} </p> {% endmacro%} -{% macro label_widget(wid, for=none) %} - <label class="xmlui_widget xmlui_label"{{ {'for':for}|xmlattr }}> - {{wid.value}} - </label> +{% macro label_widget(wid, config, for=none) %} + {% if config.form %} + <label class="xmlui_widget xmlui_label" {{ {'for':for}|xmlattr }}> + {{wid|item_filter(config.filter)}} + </label> + {% else %} + <span class="xmlui_widget xmlui_label" {{ {'id':none if not for else 'label_%s'|format(for)}|xmlattr }}>{{wid|item_filter(config.filters)}}</span> + {% endif %} {% endmacro%} -{% macro string_widget(wid, id=none) %} - <input class="xmlui_widget xmlui_string" type="text" {{ {'name':wid.name, 'id':id, 'value':wid.value}|xmlattr }}> +{% macro string_widget(wid, config, id=none) %} + {% if config.form %} + <input class="xmlui_widget xmlui_string" type="text" {{ {'name':wid.name, 'id':id, 'value':wid|item_filter(config.filters)}|xmlattr }}> + {% else %} + <div class="xmlui_widget xmlui_string" {{ {'id':id}|xmlattr }}> + {{- wid|item_filter(config.filters)|default('\u00A0',true) -}} + </div> + {% endif %} {% endmacro%} -{% macro textbox_widget(wid, id=none) %} - <textarea class="xmlui_widget xmlui_textbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|xmlattr }}> - {{- wid.value -}} - </textarea> +{% macro textbox_widget(wid, config, id=none) %} + {% if config.form %} + <textarea class="xmlui_widget xmlui_textbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|xmlattr }}> + {{- wid|item_filter(config.filters) -}} + </textarea> + {% else %} + <p class="xmlui_widget xmlui_textbox" {{ {'id':id}|xmlattr }}> + {{- wid|item_filter(config.filters) -}} + </p> + {% endif %} {% endmacro%} -{% macro list_widget(wid, id=none) %} - <select class="xmlui_widget xmlui_list"{{ {'name':wid.name, 'id':id}|xmlattr }}> - {% for value,label in wid.options %} - <option {{ {'value':value}|xmlattr }} {{ 'selected' if value == wid.selected }}>{{label}}</option> - {% endfor %} - </select> +{% macro list_widget(wid, config, id=none) %} + {% if config.form %} + <select class="xmlui_widget xmlui_list" {{ {'name':wid.name, 'id':id}|xmlattr }}> + {% for value,label in wid.options %} + <option {{ {'value':value}|xmlattr }} {{ 'selected' if value == wid.selected }}> + {{- label -}} + </option> + {% endfor %} + </select> + {% else %} + <div class="xmlui_widget xmlui_list" {{ {'id':id}|xmlattr }}> + {% for value,label in wid.items %} + <span class="xmlui_list_item value_{{value|attr_escape}}"> + {{- label -}} + </span> + {% endfor %} + </div> + {% endif %} {% endmacro%}