Add crudkit!
This commit is contained in:
parent
30ec29d497
commit
559fd56f33
28 changed files with 881 additions and 23 deletions
93
crudkit/html/templates/crudkit/_macros.html
Normal file
93
crudkit/html/templates/crudkit/_macros.html
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
{% macro options(items, value_attr="id", label_path="name", getp=None) -%}
|
||||
{%- for obj in items -%}
|
||||
<option value="{{ getp(obj, value_attr) }}">{{ getp(obj, label_path) }}</option>
|
||||
{%- endfor -%}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro lis(items, label_path="name", sublabel_path=None, getp=None) -%}
|
||||
{%- for obj in items -%}
|
||||
<li data-id="{{ obj.id }}">
|
||||
<div class="li-main">{{ getp(obj, label_path) }}</div>
|
||||
{%- if sublabel_path %}
|
||||
<div class="li-sub">{{ getp(obj, sublabel_path) }}</div>
|
||||
{%- endif %}
|
||||
</li>
|
||||
{%- else -%}
|
||||
<li class="empty"><em>No results.</em></li>
|
||||
{%- endfor -%}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro rows(items, fields, getp=None) -%}
|
||||
{%- for obj in items -%}
|
||||
<tr id="row-{{ obj.id }}">
|
||||
{%- for f in fields -%}
|
||||
<td data-field="{{ f }}">{{ getp(obj, f) }}</td>
|
||||
{%- endfor -%}
|
||||
</tr>
|
||||
{%- else -%}
|
||||
<tr>
|
||||
<td colspan="{{ fields|length }}"><em>No results.</em></td>
|
||||
</tr>
|
||||
{%- endfor -%}
|
||||
{%- endmacro %}
|
||||
|
||||
{% macro pager(model, page, pages, per_page, sort, filters) -%}
|
||||
<nav class="pager">
|
||||
{%- if page > 1 -%}
|
||||
<a hx-get="/{{ model }}/frag/rows?page={{ page-1 }}&per_page={{ per_page }}{% if sort %}&sort={{ sort }}{% endif %}{% for k,v in filters.items() %}&{{k}}={{v}}{% endfor %}"
|
||||
hx-target="#rows" hx-push-url="true">Prev</a>
|
||||
{%- endif -%}
|
||||
<span>Page {{ page }} / {{ pages }}</span>
|
||||
{%- if page < pages -%} <a
|
||||
hx-get="/{{ model }}/frag/rows?page={{ page+1 }}&per_page={{ per_page }}{% if sort %}&sort={{ sort }}{% endif %}{% for k,v in filters.items() %}&{{k}}={{v}}{% endfor %}"
|
||||
hx-target="#rows" hx-push-url="true">Next</a>
|
||||
{%- endif -%}
|
||||
</nav>
|
||||
{%- endmacro %}
|
||||
|
||||
{% macro form(schema, action, method="POST", obj_id=None, hx=False, csrf_token=None) -%}
|
||||
<form action="{{ action }}" method="post" {%- if hx %} hx-{{ "patch" if obj_id else "post" }}="{{ action }}"
|
||||
hx-target="closest dialog, #modal-body, body" hx-swap="innerHTML" hx-disabled-elt="button[type=submit]" {%- endif
|
||||
-%}>
|
||||
{%- if csrf_token %}<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">{% endif -%}
|
||||
{%- if obj_id %}<input type="hidden" name="id" value="{{ obj_id }}">{% endif -%}
|
||||
<input type="hidden" name="fields_csv" value="{{ request.args.get('fields_csv','id,name') }}">
|
||||
|
||||
{%- for f in schema -%}
|
||||
<div class="field" data-name="{{ f.name }}">
|
||||
{% set fid = 'f-' ~ f.name ~ '-' ~ (obj_id or 'new') %}
|
||||
<label for="{{ fid }}">{{ f.label or f.name|replace('_',' ')|title }}</label>
|
||||
{%- if f.type == "textarea" -%}
|
||||
<textarea id="{{ fid }}" name="{{ f.name }}" {%- if f.required %} required{% endif %}{% if f.maxlength %}
|
||||
maxlength="{{ f.maxlength }}" {% endif %}>{{ f.value or "" }}</textarea>
|
||||
{%- elif f.type == "select" -%}
|
||||
<select id="{{ fid }}" name="{{ f.name }}" {% if f.required %}required{% endif %}>
|
||||
<option value="">{{ f.placeholder or ("Choose " ~ (f.label or f.name|replace('_',' ')|title)) }}</option>
|
||||
{% if f.multiple %}
|
||||
{% set selected = (f.value or [])|list %}
|
||||
{% for val, lbl in f.choices %}
|
||||
<option value="{{ val }}" {{ 'selected' if val in selected else '' }}>{{ lbl }}</option>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% for val, lbl in f.choices %}
|
||||
<option value="{{ val }}" {{ 'selected' if (f.value|string)==(val|string) else '' }}>{{ lbl }}</option>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</select>
|
||||
{%- elif f.type == "checkbox" -%}
|
||||
<input type="hidden" name="{{ f.name }}" value="0">
|
||||
<input id="{{ fid }}" type="checkbox" name="{{ f.name }}" value="1" {{ "checked" if f.value else "" }}>
|
||||
{%- else -%}
|
||||
<input id="{{ fid }}" type="{{ f.type }}" name="{{ f.name }}"
|
||||
value="{{ f.value if f.value is not none else '' }}" {%- if f.required %} required{% endif %} {%- if
|
||||
f.maxlength %} maxlength="{{ f.maxlength }}" {% endif %}>
|
||||
{%- endif -%}
|
||||
{%- if f.help %}<div class="help">{{ f.help }}</div>{% endif -%}
|
||||
</div>
|
||||
{%- endfor -%}
|
||||
|
||||
<div class="actions">
|
||||
<button type="submit">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
{%- endmacro %}
|
||||
3
crudkit/html/templates/crudkit/form.html
Normal file
3
crudkit/html/templates/crudkit/form.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{% import "_macros.html" as ui %}
|
||||
{% set action = url_for('frags.save', model=model) %}
|
||||
{{ ui.form(schema, action, method="POST", obj_id=obj.id if obj else None, hx=true) }}
|
||||
2
crudkit/html/templates/crudkit/lis.html
Normal file
2
crudkit/html/templates/crudkit/lis.html
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{% import "_macros.html" as ui %}
|
||||
{{ ui.lis(items, label_path=label_path, sublabel_path=sublabel_path, getp=getp) }}
|
||||
3
crudkit/html/templates/crudkit/options.html
Normal file
3
crudkit/html/templates/crudkit/options.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{# Renders only <option>...</option> rows #}
|
||||
{% import "_macros.html" as ui %}
|
||||
{{ ui.options(items, value_attr=value_attr, label_path=label_path, getp=getp) }}
|
||||
2
crudkit/html/templates/crudkit/row.html
Normal file
2
crudkit/html/templates/crudkit/row.html
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{% import "_macros.html" as ui %}
|
||||
{{ ui.rows([obj], fields, getp=getp) }}
|
||||
3
crudkit/html/templates/crudkit/rows.html
Normal file
3
crudkit/html/templates/crudkit/rows.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{% import "_macros.html" as ui %}
|
||||
{{ ui.rows(items, fields, getp=getp) }}
|
||||
{{ ui.pager(model, page, pages, per_page, sort, filters) }}
|
||||
Loading…
Add table
Add a link
Reference in a new issue