| 1 | {% import 'input/field.html' as field %} |
| 2 | |
| 3 | {# generate methods #} |
| 4 | |
| 5 | {% macro generate_container(cont, config) %} |
| 6 | {% if cont.type == 'vertical' %} |
| 7 | {{ vertical_container(cont, config) }} |
| 8 | {% elif cont.type == 'pairs' %} |
| 9 | {{ pairs_container(cont, config) }} |
| 10 | {% elif cont.type == 'label' %} |
| 11 | {{ label_container(cont, config) }} |
| 12 | {% endif %} |
| 13 | {% endmacro %} |
| 14 | |
| 15 | {% macro generate_widget(wid, config, id=none) %} |
| 16 | {% if wid.type == 'text' %} |
| 17 | {{ text_widget(wid, config, id=id) }} |
| 18 | {% elif wid.type == 'label' %} |
| 19 | {{ label_widget(wid, config) }} |
| 20 | {% elif wid.type == 'string' %} |
| 21 | {{ string_widget(wid, config, id=id) }} |
| 22 | {% elif wid.type == 'jid' %} |
| 23 | {# TODO: proper JID widget #} |
| 24 | {{ string_widget(wid, config, id=id) }} |
| 25 | {% elif wid.type == 'textbox' %} |
| 26 | {{ textbox_widget(wid, config, id=id) }} |
| 27 | {% elif wid.type == 'xhtmlbox' %} |
| 28 | {{ xhtmlbox_widget(wid, config, id=id) }} |
| 29 | {% elif wid.type == 'list' %} |
| 30 | {{ list_widget(wid, config, id=id) }} |
| 31 | {% endif %} |
| 32 | {% endmacro %} |
| 33 | |
| 34 | {% macro generate_children(cont, config) %} |
| 35 | {% for child in cont.children %} |
| 36 | {% if child.category == 'container' %} |
| 37 | {{ generate_container(child, config) }} |
| 38 | {% else %} |
| 39 | {{ generate_widget(child, config) }} |
| 40 | {% endif %} |
| 41 | {% endfor %} |
| 42 | |
| 43 | {% endmacro %} |
| 44 | |
| 45 | {% macro generate(xmlui, form=true, filters=none, attributes=none) %} |
| 46 | {# generate HTML from XMLUI |
| 47 | @param xmlui(template_xmlui.XMLUIPanel): xmlui to use |
| 48 | @param form(bool): if true will generate form elements |
| 49 | @param filters(dict,none): filters as expected by item_filter |
| 50 | @param attributes(dict,none): extra attributes to put on named widgets |
| 51 | #} |
| 52 | {% set config = {'form':form, 'filters':filters or {}, 'attrs': attributes or {}} %} |
| 53 | {{ generate_container(xmlui.main_cont, config) }} |
| 54 | {% endmacro %} |
| 55 | |
| 56 | {% macro generate_table(xmlui_items, fields, formatters, tr_class_fields, on_click) %} |
| 57 | {# generate a HTML table from requested widgets names |
| 58 | @param xmlui_items(iterable[unicode]): list of xmlui to show (one per row) |
| 59 | @param fields(tuple[unicode,unicode]): fields to show (name, label) |
| 60 | @param formatters(dict): dictionary of templates to format values: |
| 61 | field_name => template |
| 62 | if no formatter is set (or None is used) for a field, it will be used unmodified. |
| 63 | current xmlui items will be set as "item" key |
| 64 | @param tr_class_fields(iterable[unicode]): name of fields to use as class |
| 65 | class will be "{name}_{value}" where name is field name, and value field value |
| 66 | all lowercase/stripped |
| 67 | @param on_click(data_objects.OnClick): thing to do when clicking on a row |
| 68 | #} |
| 69 | {% if formatters is undefined %} |
| 70 | {% set formatters = {} %} |
| 71 | {% endif %} |
| 72 | {% if on_click is undefined %} |
| 73 | {% set on_click = {} %} |
| 74 | {% endif %} |
| 75 | <table> |
| 76 | <thead> |
| 77 | <tr> |
| 78 | {% for name,label in fields %} |
| 79 | <th>{{ label }}</th> |
| 80 | {% endfor %} |
| 81 | </tr> |
| 82 | </thead> |
| 83 | <tbody> |
| 84 | {% for xmlui in xmlui_items %} |
| 85 | {% set link=on_click.format_url(item=xmlui.widget_value) if on_click.url else none %} |
| 86 | <tr {{ {'class': xmlui|xmlui_class(tr_class_fields)}|xmlattr }}> |
| 87 | |
| 88 | {% for name,label in fields %} |
| 89 | <td {{ {'class': 'td_'+name}|xmlattr }}> |
| 90 | {% for value in xmlui.widgets[name].values %} |
| 91 | <a {{ {'href':link}|xmlattr }}>{{ value|adv_format(formatters.get(name),item=xmlui.widget_value) }}</a> |
| 92 | {% endfor %} |
| 93 | </td> |
| 94 | {% endfor %} |
| 95 | </tr> |
| 96 | {% endfor %} |
| 97 | </tbody> |
| 98 | </table> |
| 99 | {% endmacro %} |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | {% macro generate_list(xmlui_items, fields, formatters, item_class_fields, on_click) %} |
| 105 | {# generate a list of rendered XMLUI from requested widgets names |
| 106 | very similar to generate_table but generate a list instead of a tabme |
| 107 | @param xmlui_items(iterable[unicode]): list of xmlui to show |
| 108 | @param fields(tuple[unicode,unicode]): fields to show (name, label) |
| 109 | @param formatters(dict): dictionary of templates to format values: |
| 110 | field_name => template |
| 111 | if no formatter is set (or None is used) for a field, it will be used unmodified. |
| 112 | current xmlui items will be set as "item" key for the template |
| 113 | @param item_class_fields(iterable[unicode]): name of fields to use as class |
| 114 | class will be "{name}_{value}" where name is field name, and value field value |
| 115 | all lowercase/stripped |
| 116 | @param on_click(data_objects.OnClick): thing to do when clicking on a row |
| 117 | #} |
| 118 | {% if formatters is undefined %} |
| 119 | {% set formatters = {} %} |
| 120 | {% endif %} |
| 121 | {% if on_click is undefined %} |
| 122 | {% set on_click = {} %} |
| 123 | {% endif %} |
| 124 | <ul class="xmlui_list"> |
| 125 | {% for xmlui in xmlui_items %} |
| 126 | <li> |
| 127 | {% set link=on_click.format_url(item=xmlui.widget_value) if on_click.url else none %} |
| 128 | <a {{ {'class': xmlui|xmlui_class(item_class_fields), |
| 129 | 'href':link}|xmlattr }}> |
| 130 | {% for name,label in fields %} |
| 131 | <span {{ {'class': 'xmlui_field__'+name}|xmlattr }}> |
| 132 | {% for label in xmlui.widgets.get(name, {}).labels %} |
| 133 | <span>{{ label|adv_format(formatters.get(name),item=xmlui.widget_value) }}</span> |
| 134 | {% endfor %} |
| 135 | </span> |
| 136 | {% endfor %} |
| 137 | </a> |
| 138 | </li> |
| 139 | {% endfor %} |
| 140 | </ul> |
| 141 | {% endmacro %} |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | {# containers #} |
| 148 | |
| 149 | {% macro vertical_container(cont, config) %} |
| 150 | <div class="xmlui_cont xmlui_cont_vertical"> |
| 151 | {{ generate_children(cont, config) }} |
| 152 | </div> |
| 153 | {% endmacro %} |
| 154 | |
| 155 | {% macro pairs_container(cont, config) %} |
| 156 | {# TODO: proper impelmentation (do the same as vertical container for now #} |
| 157 | <div class="xmlui_cont xmlui_cont_vertical"> |
| 158 | {{ generate_children(cont, config) }} |
| 159 | </div> |
| 160 | {% endmacro %} |
| 161 | |
| 162 | {% macro label_container(cont, config) %} |
| 163 | <div class="xmlui_cont xmlui_cont_vertical"> |
| 164 | {% for child in cont.children %} |
| 165 | {% if loop.index is odd %} |
| 166 | {# label #} |
| 167 | {% if child.type == 'label' %} |
| 168 | {% set for_ = ('wid_' + (child.for_name or child.name or '_noname'))|next_gidx %} |
| 169 | {{ label_widget(child, config, for=for_) }} |
| 170 | {% endif %} |
| 171 | {% else %} |
| 172 | {# widget #} |
| 173 | {% set id = ('wid_' + (child.name or '_noname'))|cur_gidx %} |
| 174 | {{ generate_widget(child, config, id=id) }} |
| 175 | {% endif %} |
| 176 | {% endfor %} |
| 177 | </div> |
| 178 | {% endmacro %} |
| 179 | |
| 180 | |
| 181 | {# widgets #} |
| 182 | |
| 183 | {% macro text_widget(wid, config, id=none) %} |
| 184 | <p class="xmlui_widget xmlui_text" {{ {'id':id}|xmlattr }}> |
| 185 | {{- wid|item_filter(config.filters)|default('\u00A0',true) -}} |
| 186 | </p> |
| 187 | {% endmacro%} |
| 188 | |
| 189 | {% macro label_widget(wid, config, for=none) %} |
| 190 | {% if config.form %} |
| 191 | <label class="xmlui_widget xmlui_label" {{ {'for':for}|xmlattr }}> |
| 192 | {{wid|item_filter(config.filters)}} |
| 193 | </label> |
| 194 | {% else %} |
| 195 | <span class="xmlui_widget xmlui_label" {{ {'id':none if not for else 'label_%s'|format(for)}|xmlattr }}>{{wid|item_filter(config.filters)}}</span> |
| 196 | {% endif %} |
| 197 | {% endmacro%} |
| 198 | |
| 199 | {% macro string_widget(wid, config, id=none) %} |
| 200 | {% if config.form %} |
| 201 | <input class="xmlui_widget xmlui_string" type="text" {{ {'name':wid.name, 'id':id, 'value':wid|item_filter(config.filters)}|dict_ext(config.attrs, wid.name)|xmlattr }} |
| 202 | {{ "readonly" if wid.read_only }} > |
| 203 | {% else %} |
| 204 | <div class="xmlui_widget xmlui_string" {{ {'id':id}|xmlattr }}> |
| 205 | {{- wid|item_filter(config.filters)|default('\u00A0',true) -}} |
| 206 | </div> |
| 207 | {% endif %} |
| 208 | {% endmacro%} |
| 209 | |
| 210 | {% macro textbox_widget(wid, config, id=none) %} |
| 211 | {% if config.form %} |
| 212 | <textarea class="xmlui_widget xmlui_textbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|dict_ext(config.attrs, wid.name)|xmlattr }}> |
| 213 | {{- wid|item_filter(config.filters) -}} |
| 214 | </textarea> |
| 215 | {% else %} |
| 216 | <p class="xmlui_widget xmlui_textbox" {{ {'id':id}|xmlattr }}> |
| 217 | {{- wid|item_filter(config.filters) -}} |
| 218 | </p> |
| 219 | {% endif %} |
| 220 | {% endmacro%} |
| 221 | |
| 222 | {% macro xhtmlbox_widget(wid, config, id=none) %} |
| 223 | {% if not config.form or wid.read_only %} |
| 224 | <div class="xmlui_widget xmlui_xhtmlbox" {{ {'id':id}|xmlattr }}> |
| 225 | {{- wid|item_filter(config.filters) -}} |
| 226 | </div> |
| 227 | {% else %} |
| 228 | <textarea class="xmlui_widget xmlui_xhtmlbox" rows="10" cols="50" {{ {'name':wid.name, 'id':id}|dict_ext(config.attrs, wid.name)|xmlattr }}> |
| 229 | {{- wid|item_filter(config.filters) -}} |
| 230 | </textarea> |
| 231 | {% endif %} |
| 232 | {% endmacro%} |
| 233 | |
| 234 | {% macro list_widget(wid, config, id=none) %} |
| 235 | {% if config.form %} |
| 236 | <select class="xmlui_widget xmlui_list" {{ {'name':wid.name, 'id':id}|dict_ext(config.attrs, wid.name)|xmlattr }}> |
| 237 | {% for value,label in wid.options %} |
| 238 | <option {{ {'value':value}|xmlattr }} {{ 'selected' if value in wid.selected }}> |
| 239 | {{- label -}} |
| 240 | </option> |
| 241 | {% endfor %} |
| 242 | </select> |
| 243 | {% else %} |
| 244 | <div class="xmlui_widget xmlui_list" {{ {'id':id}|xmlattr }}> |
| 245 | {% for value,label in wid.items %} |
| 246 | <span class="xmlui_list_item value_{{value|attr_escape}}"> |
| 247 | {{- label -}} |
| 248 | </span> |
| 249 | {% endfor %} |
| 250 | </div> |
| 251 | {% endif %} |
| 252 | {% endmacro%} |