2020-12-01 16:27:19 +00:00
|
|
|
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
|
2020-11-29 17:10:12 +00:00
|
|
|
// @ts-ignore
|
2021-03-17 16:11:39 +00:00
|
|
|
import GlobalsStyle from "@patternfly/patternfly/patternfly-base.css";
|
2020-11-29 17:10:12 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import ButtonStyle from "@patternfly/patternfly/components/Button/button.css";
|
|
|
|
// @ts-ignore
|
|
|
|
import SpinnerStyle from "@patternfly/patternfly/components/Spinner/spinner.css";
|
2021-02-21 21:01:48 +00:00
|
|
|
import { SpinnerSize } from "../Spinner";
|
2021-02-27 16:26:20 +00:00
|
|
|
import { PRIMARY_CLASS, PROGRESS_CLASS } from "../../constants";
|
|
|
|
import { ColorStyles } from "../../common/styles";
|
2020-11-29 17:10:12 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-spinner-button")
|
2020-11-29 17:10:12 +00:00
|
|
|
export class SpinnerButton extends LitElement {
|
2020-12-02 14:44:40 +00:00
|
|
|
@property({type: Boolean})
|
2020-11-29 17:10:12 +00:00
|
|
|
isRunning = false;
|
|
|
|
|
2020-11-29 17:46:45 +00:00
|
|
|
@property()
|
|
|
|
form?: string;
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2020-11-29 17:10:12 +00:00
|
|
|
return [
|
|
|
|
GlobalsStyle,
|
|
|
|
ButtonStyle,
|
|
|
|
SpinnerStyle,
|
|
|
|
ColorStyles,
|
|
|
|
css`
|
|
|
|
button {
|
|
|
|
/* Have to use !important here, as buttons with pf-m-progress have transition already */
|
|
|
|
transition: all var(--pf-c-button--m-progress--TransitionDuration) ease 0s !important;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.classList.add(PRIMARY_CLASS);
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
setLoading(): void {
|
2020-11-29 17:10:12 +00:00
|
|
|
this.isRunning = true;
|
|
|
|
this.classList.add(PROGRESS_CLASS);
|
|
|
|
this.requestUpdate();
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
setDone(statusClass: string): void {
|
2020-11-29 17:10:12 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
this.classList.remove(PROGRESS_CLASS);
|
|
|
|
this.classList.replace(PRIMARY_CLASS, statusClass);
|
|
|
|
this.requestUpdate();
|
|
|
|
setTimeout(() => {
|
|
|
|
this.classList.replace(statusClass, PRIMARY_CLASS);
|
|
|
|
this.requestUpdate();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
callAction(): void {
|
2020-11-29 17:10:12 +00:00
|
|
|
if (this.isRunning === true) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-29 17:46:45 +00:00
|
|
|
if (this.form) {
|
2021-03-02 19:37:23 +00:00
|
|
|
// Since the form= attribute is only used within a modal button,
|
|
|
|
// we can assume the form is always two levels up
|
2021-03-03 22:01:47 +00:00
|
|
|
this.parentElement?.parentElement?.querySelector<HTMLFormElement>(`#${this.form}`)?.dispatchEvent(new Event("submit", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
}));
|
2020-11-29 17:46:45 +00:00
|
|
|
}
|
2020-11-29 17:10:12 +00:00
|
|
|
this.setLoading();
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
2020-11-29 17:10:12 +00:00
|
|
|
return html`<button
|
|
|
|
class="pf-c-button pf-m-progress ${this.classList.toString()}"
|
|
|
|
@click=${() => this.callAction()}
|
|
|
|
>
|
|
|
|
${this.isRunning
|
2021-02-19 17:43:57 +00:00
|
|
|
? html` <span class="pf-c-button__progress">
|
2021-02-21 21:01:48 +00:00
|
|
|
<ak-spinner size=${SpinnerSize.Medium}></ak-spinner>
|
2021-02-19 17:43:57 +00:00
|
|
|
</span>`
|
|
|
|
: ""}
|
2020-11-29 17:10:12 +00:00
|
|
|
<slot></slot>
|
|
|
|
</button>`;
|
|
|
|
}
|
|
|
|
}
|