]> BookStack Code Mirror - bookstack/blob - resources/js/components/loading-button.ts
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / resources / js / components / loading-button.ts
1 import {Component} from "./component.js";
2 import {showLoading} from "../services/dom";
3 import {el} from "../wysiwyg/utils/dom";
4
5 /**
6  * Loading button.
7  * Shows a loading indicator and disables the button when the button is clicked,
8  * or when the form attached to the button is submitted.
9  */
10 export class LoadingButton extends Component {
11
12     protected button!: HTMLButtonElement;
13     protected loadingEl: HTMLDivElement|null = null;
14
15     setup() {
16         this.button = this.$el as HTMLButtonElement;
17         const form = this.button.form;
18
19         const action = () => {
20             setTimeout(() => this.showLoadingState(), 10)
21         };
22
23         this.button.addEventListener('click', action);
24         if (form) {
25             form.addEventListener('submit', action);
26         }
27     }
28
29     showLoadingState() {
30         this.button.disabled = true;
31
32         if (!this.loadingEl) {
33             this.loadingEl = el('div', {class: 'inline block'}) as HTMLDivElement;
34             showLoading(this.loadingEl);
35             this.button.after(this.loadingEl);
36         }
37     }
38 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.