Skip to content

Commit

Permalink
Refactor HTMX Implementation to avoid duplicate create forms (#1119)
Browse files Browse the repository at this point in the history
* refactor attachment viewset

* remove unused css class

* update eco implementation

* update labels

* remove tester css class

* html formatting

* epiv2 htmx refactor

* workflow htmx refactoring

* update binding htmx implementation

* delete unused workflow_list fragment

* restore attachment scroll functionality

* Add !important to hidden

* remove a few additions

---------

Co-authored-by: Andy Shapiro <[email protected]>
Co-authored-by: Andy Shapiro <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2024
1 parent e1dbe82 commit c1c58ff
Show file tree
Hide file tree
Showing 29 changed files with 85 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>{{object}}{% debug_badge object.id %}</h2>
{% endif %}
<a class="dropdown-item" href="{{ object.get_clear_cache_url }}">Clear assessment cache</a>
<a class="dropdown-item" href="{{ object.get_assessment_logs_url }}">View change logs</a>
<a class="dropdown-item" hx-get="{% url 'assessment:attachment-htmx' object.pk 'create' %}" hx-trigger="click" hx-target="#attach-tbody" hx-swap="beforeend" href="/">Add an attachment</a>
<a class="dropdown-item" hx-get="{% url 'assessment:attachment-htmx' object.pk 'create' %}" hx-trigger="click" hx-target=".attachment-edit-row.create-row" hx-swap="outerHTML" href="/">Add an attachment</a>
<a class="dropdown-item" href="{% url 'assessment:dataset_create' object.pk %}">Add a dataset</a>
<a class="dropdown-item" href="{% url 'assessment:values-create' object.pk %}">Add a value</a>
<a class="dropdown-item" href="{% url 'assessment:manage-labels' object.pk %}">Manage labels</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<tr hx-target="this" hx-swap="outerHTML" class="attachment-edit-row">
<tr hx-target="this" hx-swap="outerHTML" class="attachment-edit-row {% if form and not form.instance.id %} create-row {% endif %}">
{% if form %}
<td colspan="3" class="bg-lightblue">
<form class="attachment-form pad-form p-3" method="post" enctype="multipart/form-data">
Expand All @@ -24,7 +24,7 @@
</button>
<button class="btn btn-light"
type='button'
onclick="window.app.HAWCUtils.hideElement($(this).closest('.attachment-edit-row'), true)">
onclick="window.app.HAWCUtils.hideElement($(this).closest('.attachment-edit-row'), false)">
Cancel
</button>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="hide-empty-tbody">
<h2>Attachments</h2>
<table class="table table-sm table-striped">
{% if canEdit %}
{% if canEdit or permissions.edit %}
{% bs4_colgroup '25,50,25' %}
{% bs4_thead 'Attachment,Description,Editing' %}
{% else %}
Expand All @@ -12,6 +12,7 @@ <h2>Attachments</h2>
{% for object in object_list %}
{% include "assessment/fragments/attachment_row.html" %}
{% endfor %}
{% include "common/fragments/_create_row.html" with model="attachment" first=True %}
</tbody>
</table>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
</td>
{% endif %}
</tr>
{% include "common/fragments/_create_row.html" %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div hx-target="this" hx-swap="outerHTML" class="label-edit-row list-group-item d-flex {% if action == 'delete' %} bg-pink {% else %} bg-lightblue {% endif %}">
<div hx-target="this" hx-swap="outerHTML" class="label-edit-row list-group-item d-flex {% if action == 'delete' %} bg-pink {% else %} bg-lightblue {% endif %}{% if form and not form.instance.id %} create-row {% endif %}">
<form method="post" class="d-flex align-items-center p-1 w-100">
<div class="col-md-auto flex-fill pl-0 py-2">
{% crispy form %}
Expand Down Expand Up @@ -62,7 +62,7 @@
<button class="btn btn-light px-3 py-2 ml-2"
id="binding-cancel"
type='button'
onclick="window.app.HAWCUtils.hideElement($(this).closest('.label-edit-row'), true)">
onclick="window.app.HAWCUtils.hideElement($(this).closest('.label-edit-row'), false)">
Cancel
</button>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
</div>
</div>
</div>
{% if action == 'create' %}
<div class="create-row hidden"></div>
{% endif %}
5 changes: 3 additions & 2 deletions hawc/apps/assessment/templates/assessment/label_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<h2 class="mb-0">Labels</h2>
<button class="btn btn-primary ml-2 ml-auto align-self-start flex-shrink-0"
hx-get="{% url 'assessment:label-htmx' assessment.pk 'create' %}"
hx-target="#label-listgroup"
hx-swap="beforeend">
hx-target=".create-row"
hx-swap="outerHTML">
<i class="fa fa-fw fa-save"></i>&nbsp;
Create label
</button>
Expand All @@ -17,6 +17,7 @@ <h2 class="mb-0">Labels</h2>
{% include "assessment/fragments/label_row.html" with canEdit=True %}
{% endfor %}
<div class="alert alert-info text-center show-only-child my-0">No labels created (yet).</div>
<div class="create-row hidden"></div>
</div>
</div>
{% endblock content %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h3 class="card-title">{{title}}
<button id="{{model}}-create" class="btn btn-sm btn-primary float-right" hx-get="{{hx_get}}" hx-target="{{hx_target}}" hx-swap="beforeend"><i class="fa fa-plus fa-fw"></i>&nbsp;{{btn_text}}</button>
<button id="{{model}}-create" class="btn btn-sm btn-primary float-right" hx-get="{{hx_get}}" hx-target="{{hx_target}}" hx-swap="outerHTML"><i class="fa fa-plus fa-fw"></i>&nbsp;{{btn_text}}</button>
</h3>
3 changes: 3 additions & 0 deletions hawc/apps/common/templates/common/fragments/_create_row.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if action == 'create' or action == 'clone' or first == True %}
<tr class="{{model}}-edit-row create-row hidden"></tr>
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<tr hx-target="this" hx-swap="outerHTML swap:0s" class="{{app}}-edit-row {{model}}-edit-row">
<tr hx-target="this" hx-swap="outerHTML swap:0s" class="{{app}}-edit-row {{model}}-edit-row {% if form and not form.instance.id %} create-row {% endif %}">
{% if form %}
<td colspan="100%" class="p-3">
<form method="post" class="border p-2 pb-4 edit-form-background rounded pad-form form-{{model}}">
Expand All @@ -18,7 +18,7 @@
Save
</button>
<button id="{{model}}-cancel" class="btn btn-sm btn-light mx-2 py-2" type='button' style="width: 8rem; padding: 0.7rem;"
onclick="window.app.HAWCUtils.hideElement($(this).closest('.{{app}}-edit-row'), true)"><i
onclick="window.app.HAWCUtils.hideElement($(this).closest('.{{app}}-edit-row'), false)"><i
class="fa fa-fw fa-times mr-1"></i>
Cancel</button>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<button id="{{model}}-delete" class="btn btn-sm btn-danger" hx-get="{% crud_url app model 'delete' object.pk %}">
<i class="fa fa-trash" aria-hidden="true"></i></button>
<button id="{{model}}-clone" class="btn btn-sm btn-secondary" hx-post="{% crud_url app model 'clone' object.pk %}"
hx-target="#{{model}}-tbody" hx-swap="beforeend">
hx-target=".{{model}}-edit-row.create-row" hx-swap="outerHTML">
<i class="fa fa-clone" aria-hidden="true"></i></button>
</td>
{% endif %}
15 changes: 9 additions & 6 deletions hawc/apps/eco/templates/eco/design_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ <h2 class="d-inline-block">{{object.name}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'eco:cause-htmx' object.pk 'create' as cause_create %}
{% include "common/fragments/_create_card.html" with title="Causes" hx_get=cause_create hx_target="#cause-tbody" model="cause" btn_text="Create" %}
{% include "common/fragments/_create_card.html" with title="Causes" hx_get=cause_create hx_target=".cause-edit-row.create-row" model="cause" btn_text="Create" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '10,15,15,15,15,15,15' %}
{% bs4_thead 'Name,Term,Species,Level,Level Units,Comment,Edit' %}
<tbody id="cause-tbody">
{% include "common/fragments/_create_one.html" with text="No effects. Create one?" %}
{% for object in causes %}
{% include "eco/fragments/cause_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text="No causes. Create one?" %}
{% include "common/fragments/_create_row.html" with model="cause" first=True %}
</tbody>
</table>
</div>
Expand All @@ -30,15 +31,16 @@ <h2 class="d-inline-block">{{object.name}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'eco:effect-htmx' object.pk 'create' as effect_create %}
{% include "common/fragments/_create_card.html" with title="Effects" hx_get=effect_create hx_target="#effect-tbody" model="effect" btn_text="Create" %}
{% include "common/fragments/_create_card.html" with title="Effects" hx_get=effect_create hx_target=".effect-edit-row.create-row" model="effect" btn_text="Create" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '10,15,15,15,15,15,15' %}
{% bs4_thead 'Name,Term,Species,Units,As Reported,Comment,Edit' %}
<tbody id="effect-tbody">
{% include "common/fragments/_create_one.html" with text="No effects. Create one?" %}
{% for object in effects %}
{% include "eco/fragments/effect_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text="No effects. Create one?" %}
{% include "common/fragments/_create_row.html" with model="effect" first=True %}
</tbody>
</table>
</div>
Expand All @@ -47,15 +49,16 @@ <h2 class="d-inline-block">{{object.name}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'eco:result-htmx' object.pk 'create' as result_create %}
{% include "common/fragments/_create_card.html" with title="Results" hx_get=result_create hx_target="#result-tbody" model="result" btn_text="Create" %}
{% include "common/fragments/_create_card.html" with title="Results" hx_get=result_create hx_target=".result-edit-row.create-row" model="result" btn_text="Create" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '15,15,15,10,15,15,15' %}
{% bs4_thead 'Name,Cause,Effect,Relationship Direction,Modifying Factors,Value,Edit' %}
<tbody id="result-tbody">
{% include "common/fragments/_create_one.html" with text="No effects. Create one?" %}
{% for object in results %}
{% include "eco/fragments/result_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text="No results. Create one?" %}
{% include "common/fragments/_create_row.html" with model="result" first=True %}
</tbody>
</table>
</div>
Expand Down
1 change: 1 addition & 0 deletions hawc/apps/eco/templates/eco/fragments/cause_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
<td>{{object.comments }}</td>
{% include "common/fragments/_object_row.html" with app="eco" model="cause" %}
</tr>
{% include "common/fragments/_create_row.html" with model="cause" %}
1 change: 1 addition & 0 deletions hawc/apps/eco/templates/eco/fragments/effect_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
<td>{{object.comments }}</td>
{% include "common/fragments/_object_row.html" with app="eco" model="effect" %}
</tr>
{% include "common/fragments/_create_row.html" with model="effect" %}
1 change: 1 addition & 0 deletions hawc/apps/eco/templates/eco/fragments/result_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
<td>{{object.derived_value }}</td>
{% include "common/fragments/_object_row.html" with app="eco" model="result" %}
</tr>
{% include "common/fragments/_create_row.html" with model="result" %}
30 changes: 18 additions & 12 deletions hawc/apps/epiv2/templates/epiv2/design_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:chemical-htmx' object.pk 'create' as chemical_create %}
{% include "common/fragments/_create_card.html" with title="Chemicals" hx_get=chemical_create hx_target="#chemical-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Chemicals" hx_get=chemical_create hx_target=".chemical-edit-row.create-row" model="chemical" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '43,42,15' %}
{% bs4_thead 'Name,DTXSID,Edit' %}
<tbody id="chemical-tbody">
{% include "common/fragments/_create_one.html" with text='No chemicals to display. Click "Add Row" to create one.' %}
{% for object in object.chemicals.all %}
{% include "epiv2/fragments/chemical_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No chemicals to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="chemical" first=True %}
</tbody>
</table>
</div>
Expand All @@ -30,15 +31,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:exposure-htmx' object.pk 'create' as exposure_create %}
{% include "common/fragments/_create_card.html" with title="Exposure Measurements" hx_get=exposure_create hx_target="#exposure-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Exposure Measurements" hx_get=exposure_create hx_target=".exposure-edit-row.create-row" model="exposure" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '29,28,28,15' %}
{% bs4_thead 'Name,Measurement Type, Measurement Timing,Edit' %}
<tbody id="exposure-tbody">
{% include "common/fragments/_create_one.html" with text='No exposure measurements to display. Click "Add Row" to create one.' %}
{% for object in object.exposures.all %}
{% include "epiv2/fragments/exposure_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No exposure measurements to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="exposure" first=True %}
</tbody>
</table>
</div>
Expand All @@ -47,15 +49,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:exposurelevel-htmx' object.pk 'create' as exposurelevel_create %}
{% include "common/fragments/_create_card.html" with title="Exposure Levels" hx_get=exposurelevel_create hx_target="#exposurelevel-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Exposure Levels" hx_get=exposurelevel_create hx_target=".exposurelevel-edit-row.create-row" model="exposurelevel" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '17,17,17,17,17,15' %}
{% bs4_thead 'Name,Chemical,Exposure,Central Tendency,Comments,Edit' %}
<tbody id="exposurelevel-tbody">
{% include "common/fragments/_create_one.html" with text='No exposure levels to display. Click "Add Row" to create one.' %}
{% for object in object.exposure_levels.all %}
{% include "epiv2/fragments/exposurelevel_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No exposure levels to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="exposurelevel" first=True %}
</tbody>
</table>
</div>
Expand All @@ -64,15 +67,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:outcome-htmx' object.pk 'create' as outcome_create %}
{% include "common/fragments/_create_card.html" with title="Outcomes" hx_get=outcome_create hx_target="#outcome-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Outcomes" hx_get=outcome_create hx_target=".outcome-edit-row.create-row" model="outcome" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '22,21,21,21,15' %}
{% bs4_thead 'System,Effect,Effect Detail,Endpoint/Outcome,Edit' %}
<tbody id="outcome-tbody">
{% include "common/fragments/_create_one.html" with text='No outcomes to display. Click "Add Row" to create one.' %}
{% for object in object.outcomes.all %}
{% include "epiv2/fragments/outcome_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No outcomes to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="outcome" first=True %}
</tbody>
</table>
</div>
Expand All @@ -81,15 +85,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:adjustmentfactor-htmx' object.pk 'create' as adjustmentfactor_create %}
{% include "common/fragments/_create_card.html" with title="Adjustment Factors" hx_get=adjustmentfactor_create hx_target="#adjustmentfactor-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Adjustment Factors" hx_get=adjustmentfactor_create hx_target=".adjustmentfactor-edit-row.create-row" model="adjustmentfactor" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '20,40,25,15' %}
{% bs4_thead 'Name,Description,Comments,Edit' %}
<tbody id="adjustmentfactor-tbody">
{% include "common/fragments/_create_one.html" with text='No adjustment factors to display. Click "Add Row" to create one.' %}
{% for object in object.adjustment_factors.all %}
{% include "epiv2/fragments/adjustment_factor_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No adjustment factors to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="adjustmentfactor" first=True %}
</tbody>
</table>
</div>
Expand All @@ -98,15 +103,16 @@ <h2 class="d-inline-block">Update {{object.summary}}</h2>
<div class="card my-4 mx-2">
<div class="card-body">
{% url 'epiv2:dataextraction-htmx' object.pk 'create' as dataextraction_create %}
{% include "common/fragments/_create_card.html" with title="Data Extractions" hx_get=dataextraction_create hx_target="#dataextraction-tbody" btn_text="Add Row" %}
{% include "common/fragments/_create_card.html" with title="Data Extractions" hx_get=dataextraction_create hx_target=".dataextraction-edit-row.create-row" model="dataextraction" btn_text="Add Row" %}
<table class="table table-sm table-striped-invert">
{% bs4_colgroup '12,12,12,12,6,6,6,12,12,10' %}
{% bs4_thead 'Group,Outcome,Exposure<br/>Level,Timing,Estimate<br>Type,N,Value,Confidence,Comments,Edit' %}
<tbody id="dataextraction-tbody">
{% include "common/fragments/_create_one.html" with text='No extractions to display. Click "Add Row" to create one.' %}
{% for object in object.data_extractions.all %}
{% include "epiv2/fragments/data_extraction_row.html" %}
{% endfor %}
{% include "common/fragments/_create_one.html" with text='No extractions to display. Click "Add Row" to create one.' %}
{% include "common/fragments/_create_row.html" with model="dataextraction" first=True %}
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
<td>{{object.comments|default:"-"}}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="adjustmentfactor" %}
</tr>
{% include "common/fragments/_create_row.html" with model="adjustmentfactor" %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
<td>{{object.dsstox.verbose_link|safe|default:"-" }}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="chemical" %}
</tr>
{% include "common/fragments/_create_row.html" with model="chemical" %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
<td>{{object.comments|default:"-"}}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="dataextraction" %}
</tr>
{% include "common/fragments/_create_row.html" with model="dataextraction" %}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
<td>{{object.measurement_timing|default:"-"}}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="exposure" %}
</tr>
{% include "common/fragments/_create_row.html" with model="exposure" %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
<td>{{object.comments|default:"-"}}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="exposurelevel" %}
</tr>
{% include "common/fragments/_create_row.html" with model="exposurelevel" %}
1 change: 1 addition & 0 deletions hawc/apps/epiv2/templates/epiv2/fragments/outcome_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
<td>{{object.endpoint}}</td>
{% include "common/fragments/_object_row.html" with app="epiv2" model="outcome" %}
</tr>
{% include "common/fragments/_create_row.html" with model="outcome" %}
Loading

0 comments on commit c1c58ff

Please sign in to comment.