2021-08-03 15:52:21 +00:00
|
|
|
import {
|
|
|
|
css,
|
|
|
|
CSSResult,
|
|
|
|
customElement,
|
|
|
|
html,
|
|
|
|
LitElement,
|
|
|
|
property,
|
|
|
|
TemplateResult,
|
|
|
|
} from "lit-element";
|
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 PFCard from "@patternfly/patternfly/components/Card/card.css";
|
2021-04-10 21:27:57 +00:00
|
|
|
import PFTitle from "@patternfly/patternfly/components/Title/title.css";
|
2021-03-17 16:11:39 +00:00
|
|
|
import PFContent from "@patternfly/patternfly/components/Content/content.css";
|
2021-03-17 18:00:57 +00:00
|
|
|
import AKGlobal from "../../authentik.css";
|
2021-04-04 20:04:46 +00:00
|
|
|
import { PFSize } from "../Spinner";
|
2020-11-21 23:06:25 +00:00
|
|
|
|
2021-04-10 18:13:35 +00:00
|
|
|
export const MODAL_BUTTON_STYLES = css`
|
|
|
|
:host {
|
|
|
|
text-align: left;
|
|
|
|
font-size: var(--pf-global--FontSize--md);
|
|
|
|
}
|
|
|
|
.pf-c-modal-box.pf-m-lg {
|
|
|
|
overflow-y: auto;
|
|
|
|
}
|
|
|
|
.pf-c-modal-box > .pf-c-button + * {
|
|
|
|
margin-right: 0;
|
|
|
|
}
|
|
|
|
/* fix multiple selects height */
|
|
|
|
select[multiple] {
|
|
|
|
height: 15em;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
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()
|
2021-04-04 20:04:46 +00:00
|
|
|
size: PFSize = PFSize.Large;
|
2020-11-20 21:08:00 +00:00
|
|
|
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ type: Boolean })
|
2020-12-01 08:15:41 +00:00
|
|
|
open = false;
|
2020-11-21 10:28:00 +00:00
|
|
|
|
2021-08-10 21:26:17 +00:00
|
|
|
handlerBound = false;
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
PFBase,
|
|
|
|
PFButton,
|
|
|
|
PFModalBox,
|
|
|
|
PFForm,
|
|
|
|
PFTitle,
|
|
|
|
PFFormControl,
|
|
|
|
PFBullseye,
|
|
|
|
PFBackdrop,
|
|
|
|
PFPage,
|
|
|
|
PFCard,
|
|
|
|
PFContent,
|
|
|
|
AKGlobal,
|
|
|
|
MODAL_BUTTON_STYLES,
|
|
|
|
];
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 21:26:17 +00:00
|
|
|
connectedCallback(): void {
|
|
|
|
if (this.handlerBound) return;
|
2021-08-10 21:22:13 +00:00
|
|
|
window.addEventListener("keyup", this.keyUpHandler);
|
2021-08-10 21:26:17 +00:00
|
|
|
this.handlerBound = true;
|
2021-08-10 21:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 21:26:17 +00:00
|
|
|
keyUpHandler = (e: KeyboardEvent): void => {
|
2021-08-10 21:22:13 +00:00
|
|
|
if (e.code === "Escape") {
|
|
|
|
this.resetForms();
|
|
|
|
this.open = false;
|
|
|
|
}
|
2021-08-10 21:26:17 +00:00
|
|
|
};
|
2021-08-10 21:22:13 +00:00
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
2021-08-10 21:26:17 +00:00
|
|
|
super.disconnectedCallback();
|
|
|
|
window.removeEventListener("keyup", this.keyUpHandler);
|
2020-11-21 14:24:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 19:29:27 +00:00
|
|
|
resetForms(): void {
|
2021-08-03 15:52:21 +00:00
|
|
|
this.querySelectorAll<HTMLFormElement>("[slot=form]").forEach((form) => {
|
2021-06-21 20:48:47 +00:00
|
|
|
if ("resetForm" in form) {
|
|
|
|
form?.resetForm();
|
|
|
|
}
|
2021-03-29 19:29:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-03 12:34:46 +00:00
|
|
|
onClick(): void {
|
|
|
|
this.open = true;
|
2021-08-03 15:52:21 +00:00
|
|
|
this.querySelectorAll("*").forEach((child) => {
|
2021-04-03 12:34:46 +00:00
|
|
|
if ("requestUpdate" in child) {
|
|
|
|
(child as LitElement).requestUpdate();
|
2020-12-12 22:45:47 +00:00
|
|
|
}
|
2020-11-26 22:35:59 +00:00
|
|
|
});
|
2020-11-21 10:28:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 00:43:12 +00:00
|
|
|
renderModalInner(): TemplateResult {
|
2021-08-03 15:52:21 +00:00
|
|
|
return html`<slot name="modal"></slot>`;
|
2021-03-18 00:43:12 +00:00
|
|
|
}
|
|
|
|
|
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">
|
2021-08-03 15:52:21 +00:00
|
|
|
<div class="pf-c-modal-box ${this.size}" role="dialog" aria-modal="true">
|
2020-11-21 19:48:49 +00:00
|
|
|
<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
|
|
|
}
|
|
|
|
}
|