2020-11-21 19:48:49 +00:00
|
|
|
import { LitElement, html, customElement, property } from "lit-element";
|
2020-10-16 14:36:18 +00:00
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
enum ResponseType {
|
2020-11-21 19:43:05 +00:00
|
|
|
redirect = "redirect",
|
2020-11-21 19:48:49 +00:00
|
|
|
template = "template",
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
2020-10-16 14:36:18 +00:00
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
interface Response {
|
|
|
|
type: ResponseType;
|
|
|
|
to?: string;
|
|
|
|
body?: string;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:43:05 +00:00
|
|
|
@customElement("pb-flow-shell-card")
|
2020-11-20 21:08:00 +00:00
|
|
|
export class FlowShellCard extends LitElement {
|
|
|
|
@property()
|
|
|
|
flowBodyUrl: string = "";
|
|
|
|
|
|
|
|
@property()
|
2020-11-21 11:27:19 +00:00
|
|
|
flowBody?: string;
|
2020-10-16 14:36:18 +00:00
|
|
|
|
|
|
|
createRenderRoot() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
firstUpdated() {
|
2020-11-21 19:48:49 +00:00
|
|
|
fetch(this.flowBodyUrl)
|
|
|
|
.then((r) => {
|
2020-11-27 17:37:56 +00:00
|
|
|
if (r.status === 404) {
|
|
|
|
// Fallback when the flow does not exist, just redirect to the root
|
|
|
|
window.location.pathname = "/";
|
|
|
|
} else if (!r.ok) {
|
2020-11-21 19:48:49 +00:00
|
|
|
throw Error(r.statusText);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
})
|
|
|
|
.then((r) => {
|
|
|
|
return r.json();
|
|
|
|
})
|
|
|
|
.then((r) => {
|
|
|
|
this.updateCard(r);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
// Catch JSON or Update errors
|
|
|
|
this.errorMessage(e);
|
|
|
|
});
|
2020-10-16 14:36:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
async updateCard(data: Response) {
|
2020-10-16 14:36:18 +00:00
|
|
|
switch (data.type) {
|
2020-11-20 21:08:00 +00:00
|
|
|
case ResponseType.redirect:
|
|
|
|
window.location.assign(data.to!);
|
2020-10-16 14:36:18 +00:00
|
|
|
break;
|
2020-11-20 21:08:00 +00:00
|
|
|
case ResponseType.template:
|
2020-10-16 14:36:18 +00:00
|
|
|
this.flowBody = data.body;
|
|
|
|
await this.requestUpdate();
|
|
|
|
this.checkAutofocus();
|
|
|
|
this.loadFormCode();
|
|
|
|
this.setFormSubmitHandlers();
|
2020-11-26 17:36:26 +00:00
|
|
|
break;
|
2020-10-16 14:36:18 +00:00
|
|
|
default:
|
2020-11-26 22:35:59 +00:00
|
|
|
console.debug(`passbook/flows: unexpected data type ${data.type}`);
|
2020-10-16 14:36:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-21 19:48:49 +00:00
|
|
|
}
|
2020-10-16 14:36:18 +00:00
|
|
|
|
|
|
|
loadFormCode() {
|
2020-11-21 19:48:49 +00:00
|
|
|
this.querySelectorAll("script").forEach((script) => {
|
2020-10-16 14:36:18 +00:00
|
|
|
let newScript = document.createElement("script");
|
|
|
|
newScript.src = script.src;
|
|
|
|
document.head.appendChild(newScript);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
checkAutofocus() {
|
2020-11-20 21:08:00 +00:00
|
|
|
const autofocusElement = <HTMLElement>this.querySelector("[autofocus]");
|
2020-10-16 14:36:18 +00:00
|
|
|
if (autofocusElement !== null) {
|
|
|
|
autofocusElement.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
updateFormAction(form: HTMLFormElement) {
|
2020-10-16 14:36:18 +00:00
|
|
|
for (let index = 0; index < form.elements.length; index++) {
|
2020-11-20 21:08:00 +00:00
|
|
|
const element = <HTMLInputElement>form.elements[index];
|
2020-10-16 14:36:18 +00:00
|
|
|
if (element.value === form.action) {
|
2020-11-26 16:23:29 +00:00
|
|
|
console.debug(
|
2020-11-21 19:48:49 +00:00
|
|
|
"passbook/flows: Found Form action URL in form elements, not changing form action."
|
|
|
|
);
|
2020-10-16 14:36:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
form.action = this.flowBodyUrl;
|
2020-11-26 22:35:59 +00:00
|
|
|
console.debug(`passbook/flows: updated form.action ${this.flowBodyUrl}`);
|
2020-10-16 14:36:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
checkAutosubmit(form: HTMLFormElement) {
|
2020-10-16 14:36:18 +00:00
|
|
|
if ("autosubmit" in form.attributes) {
|
|
|
|
return form.submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setFormSubmitHandlers() {
|
2020-11-21 19:48:49 +00:00
|
|
|
this.querySelectorAll("form").forEach((form) => {
|
2020-11-26 22:35:59 +00:00
|
|
|
console.debug(`passbook/flows: Checking for autosubmit attribute ${form}`);
|
2020-10-16 14:36:18 +00:00
|
|
|
this.checkAutosubmit(form);
|
2020-11-26 16:23:29 +00:00
|
|
|
console.debug(`passbook/flows: Setting action for form ${form}`);
|
2020-10-16 14:36:18 +00:00
|
|
|
this.updateFormAction(form);
|
2020-11-26 16:23:29 +00:00
|
|
|
console.debug(`passbook/flows: Adding handler for form ${form}`);
|
2020-11-21 19:48:49 +00:00
|
|
|
form.addEventListener("submit", (e) => {
|
2020-10-16 14:36:18 +00:00
|
|
|
e.preventDefault();
|
|
|
|
let formData = new FormData(form);
|
|
|
|
this.flowBody = undefined;
|
|
|
|
fetch(this.flowBodyUrl, {
|
2020-11-21 19:48:49 +00:00
|
|
|
method: "post",
|
2020-10-16 14:36:18 +00:00
|
|
|
body: formData,
|
2020-11-21 19:48:49 +00:00
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
this.updateCard(data);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.errorMessage(e);
|
|
|
|
});
|
2020-10-16 14:36:18 +00:00
|
|
|
});
|
|
|
|
form.classList.add("pb-flow-wrapped");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:08:00 +00:00
|
|
|
errorMessage(error: string) {
|
2020-10-26 09:52:13 +00:00
|
|
|
this.flowBody = `
|
|
|
|
<style>
|
|
|
|
.pb-exception {
|
|
|
|
font-family: monospace;
|
|
|
|
overflow-x: scroll;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<header class="pf-c-login__main-header">
|
|
|
|
<h1 class="pf-c-title pf-m-3xl">
|
|
|
|
Whoops!
|
|
|
|
</h1>
|
|
|
|
</header>
|
|
|
|
<div class="pf-c-login__main-body">
|
|
|
|
<h3>
|
|
|
|
Something went wrong! Please try again later.
|
|
|
|
</h3>
|
|
|
|
<pre class="pb-exception">${error}</pre>
|
|
|
|
</div>`;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:36:18 +00:00
|
|
|
loading() {
|
2020-11-21 19:48:49 +00:00
|
|
|
return html` <div class="pf-c-login__main-body pb-loading">
|
2020-11-26 22:35:59 +00:00
|
|
|
<span class="pf-c-spinner" role="progressbar" aria-valuetext="Loading...">
|
2020-11-21 19:48:49 +00:00
|
|
|
<span class="pf-c-spinner__clipper"></span>
|
|
|
|
<span class="pf-c-spinner__lead-ball"></span>
|
|
|
|
<span class="pf-c-spinner__tail-ball"></span>
|
|
|
|
</span>
|
|
|
|
</div>`;
|
2020-10-16 14:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-11-21 19:43:05 +00:00
|
|
|
if (this.flowBody) {
|
2020-11-21 19:48:49 +00:00
|
|
|
return html(<TemplateStringsArray>(<unknown>[this.flowBody]));
|
2020-10-16 14:36:18 +00:00
|
|
|
}
|
|
|
|
return this.loading();
|
|
|
|
}
|
|
|
|
}
|