static: add refresh button, ensure forms in modal work correctly

closes #262
This commit is contained in:
Jens Langhammer 2020-11-21 12:27:19 +01:00
parent aa6eacaf6b
commit 277f960113
6 changed files with 69 additions and 29 deletions

View file

@ -20,7 +20,15 @@
<div class="pf-c-toolbar__content"> <div class="pf-c-toolbar__content">
{% include 'partials/toolbar_search.html' %} {% include 'partials/toolbar_search.html' %}
<div class="pf-c-toolbar__bulk-select"> <div class="pf-c-toolbar__bulk-select">
<a href="{% url 'passbook_admin:application-create' %}?back={{ request.get_full_path }}" class="pf-c-button pf-m-primary" type="button">{% trans 'Create' %}</a> <pb-modal-button href="{% url 'passbook_admin:application-create' %}">
<button slot="trigger" class="pf-c-button pf-m-primary">
{% trans 'Create' %}
</button>
<div slot="modal"></div>
</pb-modal-button>
<button role="pb-refresh" class="pf-c-button pf-m-primary">
{% trans 'Refresh' %}
</button>
</div> </div>
{% include 'partials/pagination.html' %} {% include 'partials/pagination.html' %}
</div> </div>
@ -29,6 +37,7 @@
<thead> <thead>
<tr role="row"> <tr role="row">
<th role="columnheader" scope="col">{% trans 'Name' %}</th> <th role="columnheader" scope="col">{% trans 'Name' %}</th>
<th role="columnheader" scope="col">{% trans 'Slug' %}</th>
<th role="columnheader" scope="col">{% trans 'Provider' %}</th> <th role="columnheader" scope="col">{% trans 'Provider' %}</th>
<th role="columnheader" scope="col">{% trans 'Provider Type' %}</th> <th role="columnheader" scope="col">{% trans 'Provider Type' %}</th>
<th role="cell"></th> <th role="cell"></th>
@ -45,6 +54,9 @@
{% endif %} {% endif %}
</div> </div>
</th> </th>
<td role="cell">
<code>{{ application.slug }}</span>
</td>
<td role="cell"> <td role="cell">
<span> <span>
{{ application.get_provider }} {{ application.get_provider }}
@ -56,13 +68,13 @@
</span> </span>
</td> </td>
<td> <td>
<pb-modal-button href="{% url 'passbook_admin:application-update' pk=application.pk %}?back={{ request.get_full_path }}"> <pb-modal-button href="{% url 'passbook_admin:application-update' pk=application.pk %}">
<button slot="trigger" class="pf-c-button pf-m-secondary"> <button slot="trigger" class="pf-c-button pf-m-secondary">
{% trans 'Edit' %} {% trans 'Edit' %}
</button> </button>
<div slot="modal"></div> <div slot="modal"></div>
</pb-modal-button> </pb-modal-button>
<pb-modal-button href="{% url 'passbook_admin:application-delete' pk=application.pk %}?back={{ request.get_full_path }}"> <pb-modal-button href="{% url 'passbook_admin:application-delete' pk=application.pk %}">
<button slot="trigger" class="pf-c-button pf-m-danger"> <button slot="trigger" class="pf-c-button pf-m-danger">
{% trans 'Delete' %} {% trans 'Delete' %}
</button> </button>
@ -95,7 +107,12 @@
{% trans 'Currently no applications exist. Click the button below to create one.' %} {% trans 'Currently no applications exist. Click the button below to create one.' %}
{% endif %} {% endif %}
</div> </div>
<a href="{% url 'passbook_admin:application-create' %}?back={{ request.get_full_path }}" class="pf-c-button pf-m-primary" type="button">{% trans 'Create' %}</a> <pb-modal-button href="{% url 'passbook_admin:application-create' %}">
<button slot="trigger" class="pf-c-button pf-m-primary">
{% trans 'Create' %}
</button>
<div slot="modal"></div>
</pb-modal-button>
</div> </div>
</div> </div>
{% endif %} {% endif %}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -47,6 +47,7 @@ export class AdminSiteShell extends LitElement {
fetch(url).then(r => r.text()).then((t) => { fetch(url).then(r => r.text()).then((t) => {
this.querySelector("[slot=body]")!.innerHTML = t; this.querySelector("[slot=body]")!.innerHTML = t;
}).then(() => { }).then(() => {
// Ensure anchors only change the hash
this.querySelectorAll("a").forEach(a => { this.querySelectorAll("a").forEach(a => {
if (a.href === "") { if (a.href === "") {
return; return;
@ -59,6 +60,12 @@ export class AdminSiteShell extends LitElement {
a.href = `#${a.href}`; a.href = `#${a.href}`;
} }
}); });
// Create refresh buttons
this.querySelectorAll("[role=pb-refresh]").forEach(rt => {
rt.addEventListener("click", e => {
this.loadContent();
});
});
this.loading = false; this.loading = false;
}); });
} }

View file

@ -19,7 +19,7 @@ export class FlowShellCard extends LitElement {
flowBodyUrl: string = ""; flowBodyUrl: string = "";
@property() @property()
flowBody?: string = undefined; flowBody?: string;
createRenderRoot() { createRenderRoot() {
return this; return this;

View file

@ -5,6 +5,7 @@ import ModalBoxStyle from "@patternfly/patternfly/components/ModalBox/modal-box.
import BullseyeStyle from "@patternfly/patternfly/layouts/Bullseye/bullseye.css"; import BullseyeStyle from "@patternfly/patternfly/layouts/Bullseye/bullseye.css";
// @ts-ignore // @ts-ignore
import BackdropStyle from "@patternfly/patternfly/components/Backdrop/backdrop.css"; import BackdropStyle from "@patternfly/patternfly/components/Backdrop/backdrop.css";
import { updateMessages } from "./Messages";
const PRIMARY_CLASS = "pf-m-primary"; const PRIMARY_CLASS = "pf-m-primary";
const SUCCESS_CLASS = "pf-m-success"; const SUCCESS_CLASS = "pf-m-success";
@ -24,6 +25,41 @@ export class ModalButton extends LitElement {
return [ModalBoxStyle, BullseyeStyle, BackdropStyle] return [ModalBoxStyle, BullseyeStyle, BackdropStyle]
} }
setContent(content: string) {
this.querySelector("[slot=modal]")!.innerHTML = content;
// Ensure links close the modal
this.querySelectorAll<HTMLAnchorElement>("[slot=modal] a").forEach(a => {
// Make click on a close the modal
a.addEventListener("click", e => {
e.preventDefault();
this.open = false;
});
});
// Ensure forms sends in AJAX
this.querySelectorAll<HTMLFormElement>("[slot=modal] form").forEach(form => {
form.addEventListener('submit', (e) => {
e.preventDefault();
let formData = new FormData(form);
fetch((form.action === window.location.toString()) ? this.href : form.action, {
method: form.method,
body: formData,
}).then((response) => {
return response.text();
}).then(data => {
if (data.indexOf("csrfmiddlewaretoken") !== -1) {
this.setContent(data);
} else {
this.open = false;
this.dispatchEvent(new CustomEvent('hashchange', { bubbles: true }));
updateMessages();
}
}).catch((e) => {
console.error(e);
});
});
});
}
onClick(e: MouseEvent) { onClick(e: MouseEvent) {
const request = new Request( const request = new Request(
this.href, this.href,
@ -31,27 +67,7 @@ export class ModalButton extends LitElement {
fetch(request, { fetch(request, {
mode: 'same-origin', mode: 'same-origin',
}).then(r => r.text()).then((t) => { }).then(r => r.text()).then((t) => {
this.querySelector("[slot=modal]")!.innerHTML = t; this.setContent(t);
// Ensure links close the modal
this.querySelectorAll<HTMLAnchorElement>("[slot=modal] a").forEach(a => {
// Make click on a close the modal
a.addEventListener("click", e => {
this.open = false;
});
});
// Ensure input type submit submits the form without reloading the page
this.querySelectorAll<HTMLInputElement>("[slot=modal] input[type=submit]").forEach(i => {
i.form?.addEventListener("submit", e => {
e.preventDefault();
return false;
});
i.addEventListener("click", e => {
console.log("on submit");
e.preventDefault();
i.form?.submit();
this.open = false;
});
});
this.open = true; this.open = true;
}).catch(e => { }).catch(e => {
console.error(e); console.error(e);
@ -62,7 +78,7 @@ export class ModalButton extends LitElement {
return html`<div class="pf-c-backdrop"> return html`<div class="pf-c-backdrop">
<div class="pf-l-bullseye"> <div class="pf-l-bullseye">
<div class="pf-c-modal-box pf-m-md" role="dialog" aria-modal="true" aria-labelledby="modal-md-title" aria-describedby="modal-md-description"> <div class="pf-c-modal-box pf-m-md" role="dialog" aria-modal="true" aria-labelledby="modal-md-title" aria-describedby="modal-md-description">
<button class="pf-c-button pf-m-plain" type="button" aria-label="Close dialog"> <button @click=${() => this.open = false} class="pf-c-button pf-m-plain" type="button" aria-label="Close dialog">
<i class="fas fa-times" aria-hidden="true"></i> <i class="fas fa-times" aria-hidden="true"></i>
</button> </button>
<slot name="modal"> <slot name="modal">