| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							|  |  |  | import { Store, UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import { UIElement } from "../UIElement" | 
					
						
							|  |  |  | import { VariableUiElement } from "./VariableUIElement" | 
					
						
							|  |  |  | import Lazy from "./Lazy" | 
					
						
							|  |  |  | import Loading from "./Loading" | 
					
						
							| 
									
										
										
										
											2023-01-17 18:31:51 +01:00
										 |  |  | import SubtleButtonSvelte from "./SubtleButton.svelte" | 
					
						
							|  |  |  | import SvelteUIElement from "./SvelteUIElement" | 
					
						
							| 
									
										
										
										
											2020-07-29 16:46:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | export class SubtleButton extends UIElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly imageUrl: string | BaseUIElement | 
					
						
							|  |  |  |     private readonly message: string | BaseUIElement | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |     private readonly options: { | 
					
						
							|  |  |  |         url?: string | Store<string> | 
					
						
							|  |  |  |         newTab?: boolean | 
					
						
							| 
									
										
										
										
											2023-03-09 23:19:49 +01:00
										 |  |  |         imgSize?: string | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |         extraClasses?: string | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-30 00:17:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         imageUrl: string | BaseUIElement, | 
					
						
							|  |  |  |         message: string | BaseUIElement, | 
					
						
							|  |  |  |         options: { | 
					
						
							|  |  |  |             url?: string | Store<string> | 
					
						
							|  |  |  |             newTab?: boolean | 
					
						
							| 
									
										
										
										
											2023-03-09 23:19:49 +01:00
										 |  |  |             imgSize?: "h-11 w-11" | string | 
					
						
							| 
									
										
										
										
											2023-01-21 23:58:14 +01:00
										 |  |  |             extraClasses?: string | 
					
						
							| 
									
										
										
										
											2023-01-17 18:31:51 +01:00
										 |  |  |         } = {} | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     ) { | 
					
						
							|  |  |  |         super() | 
					
						
							|  |  |  |         this.imageUrl = imageUrl | 
					
						
							|  |  |  |         this.message = message | 
					
						
							|  |  |  |         this.options = options | 
					
						
							| 
									
										
										
										
											2021-05-30 00:17:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 19:09:24 +01:00
										 |  |  |     protected InnerRender(): string | BaseUIElement { | 
					
						
							| 
									
										
										
										
											2023-01-17 18:31:51 +01:00
										 |  |  |         return new SvelteUIElement(SubtleButtonSvelte, { | 
					
						
							|  |  |  |             imageUrl: this?.imageUrl ?? undefined, | 
					
						
							|  |  |  |             message: this?.message ?? "", | 
					
						
							|  |  |  |             options: this?.options ?? {}, | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2020-07-29 16:46:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |     public OnClickWithLoading( | 
					
						
							|  |  |  |         loadingText: BaseUIElement | string, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         action: () => Promise<void> | 
					
						
							|  |  |  |     ): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |         const state = new UIEventSource<"idle" | "running">("idle") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const button = this | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         button.onClick(async () => { | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |             state.setData("running") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 await action() | 
					
						
							|  |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |                 console.error(e) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             } finally { | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |                 state.setData("idle") | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const loading = new Lazy(() => new Loading(loadingText)) | 
					
						
							| 
									
										
										
										
											2022-11-02 14:44:06 +01:00
										 |  |  |         return new VariableUiElement( | 
					
						
							|  |  |  |             state.map((st) => { | 
					
						
							|  |  |  |                 if (st === "idle") { | 
					
						
							|  |  |  |                     return button | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return loading | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-02-16 01:28:23 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |