comparison default/input/xmlui.html @ 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 7d5cca978eeb
children d58fdd57df49
comparison
equal deleted inserted replaced
52:87680eed9e25 53:dfef430a26ef
1 {% import 'input/field.html' as field %} 1 {% import 'input/field.html' as field %}
2 2
3 {# generate methods #} 3 {# generate methods #}
4 4
5 {% macro generate_container(cont) %} 5 {% macro generate_container(cont, config) %}
6 {% if cont.type == 'vertical' %} 6 {% if cont.type == 'vertical' %}
7 {{ vertical_container(cont) }} 7 {{ vertical_container(cont, config) }}
8 {% elif cont.type == 'pairs' %} 8 {% elif cont.type == 'pairs' %}
9 {{ pairs_container(cont) }} 9 {{ pairs_container(cont, config) }}
10 {% elif cont.type == 'label' %} 10 {% elif cont.type == 'label' %}
11 {{ label_container(cont) }} 11 {{ label_container(cont, config) }}
12 {% endif %} 12 {% endif %}
13 {% endmacro %} 13 {% endmacro %}
14 14
15 {% macro generate_widget(wid, id=none) %} 15 {% macro generate_widget(wid, config, id=none) %}
16 {% if wid.type == 'text' %} 16 {% if wid.type == 'text' %}
17 {{ text_widget(wid, id=id) }} 17 {{ text_widget(wid, config, id=id) }}
18 {% elif wid.type == 'label' %} 18 {% elif wid.type == 'label' %}
19 {{ label_widget(wid) }} 19 {{ label_widget(wid, config) }}
20 {% elif wid.type == 'string' %} 20 {% elif wid.type == 'string' %}
21 {{ string_widget(wid, id=id) }} 21 {{ string_widget(wid, config, id=id) }}
22 {% elif wid.type == 'textbox' %} 22 {% elif wid.type == 'textbox' %}
23 {{ textbox_widget(wid, id=id) }} 23 {{ textbox_widget(wid, config, id=id) }}
24 {% elif wid.type == 'list' %} 24 {% elif wid.type == 'list' %}
25 {{ list_widget(wid, id=id) }} 25 {{ list_widget(wid, config, id=id) }}
26 {% endif %} 26 {% endif %}
27 {% endmacro %} 27 {% endmacro %}
28 28
29 {% macro generate_children(cont) %} 29 {% macro generate_children(cont, config) %}
30 {% for child in cont.children %} 30 {% for child in cont.children %}
31 {% if child.category == 'container' %} 31 {% if child.category == 'container' %}
32 {{ generate_container(child) }} 32 {{ generate_container(child, config) }}
33 {% else %} 33 {% else %}
34 {{ generate_widget(child) }} 34 {{ generate_widget(child, config) }}
35 {% endif %} 35 {% endif %}
36 {% endfor %} 36 {% endfor %}
37 37
38 {% endmacro %} 38 {% endmacro %}
39 39
40 {% macro generate(xmlui) %} 40 {% macro generate(xmlui, form=true, filters=none) %}
41 {{ generate_container(xmlui.main_cont) }} 41 {# generate HTML from XMLUI
42 @param xmlui(template_xmlui.XMLUIPanel): xmlui to use
43 @param form(bool): if true will generate form elements
44 @param filters(dict,none): filters as expected by item_filter
45 #}
46 {% set config = {'form':form, 'filters':filters or {}} %}
47 {{ generate_container(xmlui.main_cont, config) }}
48 {% endmacro %}
49
50 {% macro generate_table(xmlui_items, fields, formatters, tr_class_fields, on_click) %}
51 {# generate a HTML table from requested widgets names
52 @param xmlui_items(iterable[unicode]): list of xmlui to show (one per row)
53 @param fields(tuple[unicode,unicode]): fields to show (name, label)
54 @param formatters(dict): dictionary of templates to format values:
55 field_name => template
56 if no formatter is set (or None is used) for a field, it will be used unmodified.
57 current xmlui items will be set as "item" key
58 @param tr_class_fields(iterable[unicode]): name of fields to use as class
59 class will be "{name}_{value}" where name is field name, and value field value
60 all lowercase/stripped
61 @param on_click(data_objects.OnClick): thing to do when clicking on a row
62 #}
63 {% if formatters is undefined %}
64 {% set formatters = {} %}
65 {% endif %}
66 <table>
67 <thead>
68 <tr>
69 {% for name,label in fields %}
70 <th>{{ label }}</th>
71 {% endfor %}
72 </tr>
73 </thead>
74 <tbody>
75 {% for xmlui in xmlui_items %}
76 {% set link=on_click.formatUrl(xmlui.widget_value) if on_click.url else none %}
77 <tr {{ {'class': xmlui|xmlui_class(tr_class_fields)}|xmlattr }}>
78
79 {% for name,label in fields %}
80 <td {{ {'class': 'td_'+name}|xmlattr }}>
81 {% for value in xmlui.widgets[name].values %}
82 <a {{ {'href':link}|xmlattr }}>{{ value|adv_format(formatters.get(name),item=xmlui.widget_value) }}</a>
83 {% endfor %}
84 </td>
85 {% endfor %}
86 </tr>
87 {% endfor %}
88 </tbody>
89 </table>
42 {% endmacro %} 90 {% endmacro %}
43 91
44 {# containers #} 92 {# containers #}
45 93
46 {% macro vertical_container(cont) %} 94 {% macro vertical_container(cont, config) %}
47 <div class="xmlui_cont xmlui_cont_vertical"> 95 <div class="xmlui_cont xmlui_cont_vertical">
48 {{ generate_children(cont) }} 96 {{ generate_children(cont, config) }}
49 </div> 97 </div>
50 {% endmacro %} 98 {% endmacro %}
51 99
52 {% macro pairs_container(cont) %} 100 {% macro pairs_container(cont, config) %}
53 {# TODO: proper impelmentation (do the same as vertical container for now #} 101 {# TODO: proper impelmentation (do the same as vertical container for now #}
54 <div class="xmlui_cont xmlui_cont_vertical"> 102 <div class="xmlui_cont xmlui_cont_vertical">
55 {{ generate_children(cont) }} 103 {{ generate_children(cont, config) }}
56 </div> 104 </div>
57 {% endmacro %} 105 {% endmacro %}
58 106
59 {% macro label_container(cont) %} 107 {% macro label_container(cont, config) %}
60 <div class="xmlui_cont xmlui_cont_vertical"> 108 <div class="xmlui_cont xmlui_cont_vertical">
61 {% for child in cont.children %} 109 {% for child in cont.children %}
62 {% if loop.index is odd %} 110 {% if loop.index is odd %}
63 {# label #} 111 {# label #}
64 {% set id = 'widget'|next_gidx %}
65 {% if child.type == 'label' %} 112 {% if child.type == 'label' %}
66 {{ label_widget(child, for=id) }} 113 {% set for_ = ('wid_' + (child.for_name or child.name or '_noname'))|next_gidx %}
114 {{ label_widget(child, config, for=for_) }}
67 {% endif %} 115 {% endif %}
68 {% else %} 116 {% else %}
69 {# widget #} 117 {# widget #}
70 {% set id = 'widget'|cur_gidx %} 118 {% set id = ('wid_' + (child.name or '_noname'))|cur_gidx %}
71 {{ generate_widget(child, id=id) }} 119 {{ generate_widget(child, config, id=id) }}
72 {% endif %} 120 {% endif %}
73 {% endfor %} 121 {% endfor %}
74 </div> 122 </div>
75 {% endmacro %} 123 {% endmacro %}
76 124
77 125
78 {# widgets #} 126 {# widgets #}
79 127
80 {% macro text_widget(wid, id=none) %} 128 {% macro text_widget(wid, config, id=none) %}
81 <p class="xmlui_widget xmlui_text" {{ {'id':id}|xmlattr }}> 129 <p class="xmlui_widget xmlui_text" {{ {'id':id}|xmlattr }}>
82 {{wid.value}} 130 {{- wid|item_filter(config.filters)|default('\u00A0',true) -}}
83 </p> 131 </p>
84 {% endmacro%} 132 {% endmacro%}
85 133
86 {% macro label_widget(wid, for=none) %} 134 {% macro label_widget(wid, config, for=none) %}
87 <label class="xmlui_widget xmlui_label"{{ {'for':for}|xmlattr }}> 135 {% if config.form %}
88 {{wid.value}} 136 <label class="xmlui_widget xmlui_label" {{ {'for':for}|xmlattr }}>
89 </label> 137 {{wid|item_filter(config.filter)}}
138 </label>
139 {% else %}
140 <span class="xmlui_widget xmlui_label" {{ {'id':none if not for else 'label_%s'|format(for)}|xmlattr }}>{{wid|item_filter(config.filters)}}</span>
141 {% endif %}
90 {% endmacro%} 142 {% endmacro%}
91 143
92 {% macro string_widget(wid, id=none) %} 144 {% macro string_widget(wid, config, id=none) %}
93 <input class="xmlui_widget xmlui_string" type="text" {{ {'name':wid.name, 'id':id, 'value':wid.value}|xmlattr }}> 145 {% if config.form %}
146 <input class="xmlui_widget xmlui_string" type="text" {{ {'name':wid.name, 'id':id, 'value':wid|item_filter(config.filters)}|xmlattr }}>
147 {% else %}
148 <div class="xmlui_widget xmlui_string" {{ {'id':id}|xmlattr }}>
149 {{- wid|item_filter(config.filters)|default('\u00A0',true) -}}
150 </div>
151 {% endif %}
94 {% endmacro%} 152 {% endmacro%}
95 153
96 {% macro textbox_widget(wid, id=none) %} 154 {% macro textbox_widget(wid, config, id=none) %}
97 <textarea class="xmlui_widget xmlui_textbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|xmlattr }}> 155 {% if config.form %}
98 {{- wid.value -}} 156 <textarea class="xmlui_widget xmlui_textbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|xmlattr }}>
99 </textarea> 157 {{- wid|item_filter(config.filters) -}}
158 </textarea>
159 {% else %}
160 <p class="xmlui_widget xmlui_textbox" {{ {'id':id}|xmlattr }}>
161 {{- wid|item_filter(config.filters) -}}
162 </p>
163 {% endif %}
100 {% endmacro%} 164 {% endmacro%}
101 165
102 {% macro list_widget(wid, id=none) %} 166 {% macro list_widget(wid, config, id=none) %}
103 <select class="xmlui_widget xmlui_list"{{ {'name':wid.name, 'id':id}|xmlattr }}> 167 {% if config.form %}
104 {% for value,label in wid.options %} 168 <select class="xmlui_widget xmlui_list" {{ {'name':wid.name, 'id':id}|xmlattr }}>
105 <option {{ {'value':value}|xmlattr }} {{ 'selected' if value == wid.selected }}>{{label}}</option> 169 {% for value,label in wid.options %}
106 {% endfor %} 170 <option {{ {'value':value}|xmlattr }} {{ 'selected' if value == wid.selected }}>
107 </select> 171 {{- label -}}
172 </option>
173 {% endfor %}
174 </select>
175 {% else %}
176 <div class="xmlui_widget xmlui_list" {{ {'id':id}|xmlattr }}>
177 {% for value,label in wid.items %}
178 <span class="xmlui_list_item value_{{value|attr_escape}}">
179 {{- label -}}
180 </span>
181 {% endfor %}
182 </div>
183 {% endif %}
108 {% endmacro%} 184 {% endmacro%}