2020-12-01 16:27:19 +00:00
|
|
|
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
|
2021-02-08 22:10:45 +00:00
|
|
|
import { unsafeHTML } from "lit-html/directives/unsafe-html";
|
2021-03-17 16:11:39 +00:00
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
|
|
|
import PFButton from "@patternfly/patternfly/components/Button/button.css";
|
|
|
|
import PFModalBox from "@patternfly/patternfly/components/ModalBox/modal-box.css";
|
|
|
|
import PFForm from "@patternfly/patternfly/components/Form/form.css";
|
|
|
|
import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css";
|
|
|
|
import PFBullseye from "@patternfly/patternfly/layouts/Bullseye/bullseye.css";
|
|
|
|
import PFBackdrop from "@patternfly/patternfly/components/Backdrop/backdrop.css";
|
|
|
|
import PFPage from "@patternfly/patternfly/components/Page/page.css";
|
|
|
|
import PFStack from "@patternfly/patternfly/layouts/Stack/stack.css";
|
|
|
|
import PFCard from "@patternfly/patternfly/components/Card/card.css";
|
|
|
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
2021-03-17 18:00:57 +00:00
|
|
|
import AKGlobal from "../../authentik.css";
|
2020-11-21 23:06:25 +00:00
|
|
|
|
2020-11-29 21:14:48 +00:00
|
|
|
import { convertToSlug } from "../../utils";
|
2020-11-29 17:10:12 +00:00
|
|
|
import { SpinnerButton } from "./SpinnerButton";
|
2021-03-23 14:16:56 +00:00
|
|
|
import { PRIMARY_CLASS, EVENT_REFRESH } from "../../constants";
|
2020-12-12 22:32:55 +00:00
|
|
|
import { showMessage } from "../messages/MessageContainer";
|
2021-03-27 22:11:44 +00:00
|
|
|
import { MessageLevel } from "../messages/Message";
|
2020-11-20 21:08:00 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-modal-button")
|
2020-11-20 21:08:00 +00:00
|
|
|
export class ModalButton extends LitElement {
|
|
|
|
@property()
|
2020-11-22 20:35:52 +00:00
|
|
|
href?: string;
|
2020-11-20 21:08:00 +00:00
|
|
|
|
2020-12-02 14:44:40 +00:00
|
|
|
@property({type: Boolean})
|
2020-12-01 08:15:41 +00:00
|
|
|
open = false;
|
2020-11-21 10:28:00 +00:00
|
|
|
|
2021-02-08 22:10:45 +00:00
|
|
|
@property()
|
2021-02-09 09:00:33 +00:00
|
|
|
modal = "<slot name='modal'></slot>";
|
2021-02-08 22:10:45 +00:00
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-03-29 10:16:32 +00:00
|
|
|
return [PFBase, PFButton, PFModalBox, PFForm, PFFormControl, PFBullseye, PFBackdrop, PFPage, PFStack, PFCard, PFContent, AKGlobal].concat(
|
2020-11-22 00:03:13 +00:00
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
text-align: left;
|
|
|
|
}
|
2021-02-09 08:57:59 +00:00
|
|
|
.pf-c-modal-box.pf-m-lg {
|
2020-11-22 20:04:09 +00:00
|
|
|
overflow-y: auto;
|
|
|
|
}
|
2021-02-09 08:57:59 +00:00
|
|
|
.pf-c-modal-box > .pf-c-button + * {
|
2021-02-08 22:10:45 +00:00
|
|
|
margin-right: 0;
|
|
|
|
}
|
2021-03-17 16:11:39 +00:00
|
|
|
/* fix multiple selects height */
|
|
|
|
select[multiple] {
|
|
|
|
height: 15em;
|
|
|
|
}
|
2021-02-08 22:10:45 +00:00
|
|
|
`
|
|
|
|
);
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:24:45 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2020-11-21 19:48:49 +00:00
|
|
|
window.addEventListener("keyup", (e) => {
|
2020-11-21 14:24:45 +00:00
|
|
|
if (e.code === "Escape") {
|
|
|
|
this.open = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
updateHandlers(): void {
|
2020-11-21 11:27:19 +00:00
|
|
|
// Ensure links close the modal
|
2021-02-08 22:10:45 +00:00
|
|
|
this.shadowRoot?.querySelectorAll<HTMLAnchorElement>("a").forEach((a) => {
|
2020-11-29 18:40:28 +00:00
|
|
|
if (a.target == "_blank") {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-26 22:35:59 +00:00
|
|
|
// Make click on a close the modal
|
|
|
|
a.addEventListener("click", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.open = false;
|
|
|
|
});
|
|
|
|
});
|
2020-11-21 18:22:53 +00:00
|
|
|
// Make name field update slug field
|
2021-02-08 22:10:45 +00:00
|
|
|
this.shadowRoot?.querySelectorAll<HTMLInputElement>("input[name=name]").forEach((input) => {
|
2020-12-12 22:45:47 +00:00
|
|
|
const form = input.closest("form");
|
|
|
|
if (form === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const slugField = form.querySelector<HTMLInputElement>("input[name=slug]");
|
|
|
|
if (!slugField) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Only attach handler if the slug is already equal to the name
|
|
|
|
// if not, they are probably completely different and shouldn't update
|
|
|
|
// each other
|
|
|
|
if (convertToSlug(input.value) !== slugField.value) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-01 16:27:19 +00:00
|
|
|
input.addEventListener("input", () => {
|
2020-11-26 22:35:59 +00:00
|
|
|
slugField.value = convertToSlug(input.value);
|
|
|
|
});
|
|
|
|
});
|
2020-11-21 19:48:49 +00:00
|
|
|
// Ensure forms sends in AJAX
|
2021-02-08 22:10:45 +00:00
|
|
|
this.shadowRoot?.querySelectorAll<HTMLFormElement>("form").forEach((form) => {
|
2020-11-26 22:35:59 +00:00
|
|
|
form.addEventListener("submit", (e) => {
|
|
|
|
e.preventDefault();
|
2020-12-01 08:15:41 +00:00
|
|
|
const formData = new FormData(form);
|
2020-11-26 22:35:59 +00:00
|
|
|
fetch(this.href ? this.href : form.action, {
|
|
|
|
method: form.method,
|
|
|
|
body: formData,
|
|
|
|
redirect: "manual",
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return response.text();
|
2020-11-22 21:05:11 +00:00
|
|
|
})
|
2021-02-08 22:10:45 +00:00
|
|
|
.then((responseData) => {
|
|
|
|
if (responseData.indexOf("csrfmiddlewaretoken") !== -1) {
|
|
|
|
this.modal = responseData;
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug("authentik/modalbutton: re-showing form");
|
2020-11-26 22:35:59 +00:00
|
|
|
} else {
|
|
|
|
this.open = false;
|
2020-12-05 21:08:42 +00:00
|
|
|
console.debug("authentik/modalbutton: successful submit");
|
2020-11-26 22:35:59 +00:00
|
|
|
this.dispatchEvent(
|
2021-03-23 14:16:56 +00:00
|
|
|
new CustomEvent(EVENT_REFRESH, {
|
2020-11-26 22:35:59 +00:00
|
|
|
bubbles: true,
|
2020-12-12 18:39:09 +00:00
|
|
|
composed: true,
|
2020-11-26 22:35:59 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
2020-12-12 22:32:55 +00:00
|
|
|
showMessage({
|
2021-03-27 22:11:44 +00:00
|
|
|
level: MessageLevel.error,
|
2020-12-12 22:32:55 +00:00
|
|
|
message: "Unexpected error"
|
|
|
|
});
|
2021-02-20 17:28:11 +00:00
|
|
|
console.error(e);
|
2020-11-26 22:35:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-21 11:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
onClick(): void {
|
2020-11-22 20:35:52 +00:00
|
|
|
if (!this.href) {
|
|
|
|
this.updateHandlers();
|
|
|
|
this.open = true;
|
2021-03-29 09:57:53 +00:00
|
|
|
this.querySelectorAll("*").forEach(child => {
|
|
|
|
if ("requestUpdate" in child) {
|
|
|
|
(child as LitElement).requestUpdate();
|
|
|
|
}
|
|
|
|
});
|
2020-11-22 20:35:52 +00:00
|
|
|
} else {
|
|
|
|
const request = new Request(this.href);
|
|
|
|
fetch(request, {
|
|
|
|
mode: "same-origin",
|
2020-11-21 19:48:49 +00:00
|
|
|
})
|
2021-02-08 22:10:45 +00:00
|
|
|
.then((response) => response.text())
|
|
|
|
.then((responseData) => {
|
|
|
|
this.modal = responseData;
|
2020-11-22 20:35:52 +00:00
|
|
|
this.open = true;
|
2020-12-05 21:08:42 +00:00
|
|
|
this.querySelectorAll<SpinnerButton>("ak-spinner-button").forEach((sb) => {
|
2020-11-29 17:10:12 +00:00
|
|
|
sb.setDone(PRIMARY_CLASS);
|
|
|
|
});
|
2020-11-22 20:35:52 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2020-12-12 22:32:55 +00:00
|
|
|
showMessage({
|
2021-03-27 22:11:44 +00:00
|
|
|
level: MessageLevel.error,
|
2020-12-12 22:32:55 +00:00
|
|
|
message: "Unexpected error"
|
|
|
|
});
|
2021-02-20 17:28:11 +00:00
|
|
|
console.error(e);
|
2020-11-22 20:35:52 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-21 10:28:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 00:43:12 +00:00
|
|
|
renderModalInner(): TemplateResult {
|
|
|
|
return html`${unsafeHTML(this.modal)}`;
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
renderModal(): TemplateResult {
|
2020-11-20 21:08:00 +00:00
|
|
|
return html`<div class="pf-c-backdrop">
|
|
|
|
<div class="pf-l-bullseye">
|
2020-11-21 19:48:49 +00:00
|
|
|
<div
|
2020-11-22 20:04:09 +00:00
|
|
|
class="pf-c-modal-box pf-m-lg"
|
2020-11-21 19:48:49 +00:00
|
|
|
role="dialog"
|
|
|
|
aria-modal="true"
|
|
|
|
aria-labelledby="modal-md-title"
|
|
|
|
aria-describedby="modal-md-description"
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
@click=${() => (this.open = false)}
|
|
|
|
class="pf-c-button pf-m-plain"
|
|
|
|
type="button"
|
|
|
|
aria-label="Close dialog"
|
|
|
|
>
|
2020-11-21 10:28:00 +00:00
|
|
|
<i class="fas fa-times" aria-hidden="true"></i>
|
|
|
|
</button>
|
2021-03-18 00:43:12 +00:00
|
|
|
${this.renderModalInner()}
|
2020-11-20 21:08:00 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
|
|
|
return html` <slot name="trigger" @click=${() => this.onClick()}></slot>
|
2020-11-21 10:28:00 +00:00
|
|
|
${this.open ? this.renderModal() : ""}`;
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
2021-02-08 22:10:45 +00:00
|
|
|
|
|
|
|
updated(): void {
|
|
|
|
this.updateHandlers();
|
|
|
|
}
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|