| 
									
										
										
										
											2022-12-02 16:46:14 +01:00
										 |  |  | import keyboardActionsService from "../../services/keyboard_actions.js"; | 
					
						
							|  |  |  | import AbstractButtonWidget from "./abstract_button.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let actions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | keyboardActionsService.getActions().then(as => actions = as); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class CommandButtonWidget extends AbstractButtonWidget { | 
					
						
							|  |  |  |     doRender() { | 
					
						
							|  |  |  |         super.doRender(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.settings.command) { | 
					
						
							|  |  |  |             this.$widget.on("click", () => { | 
					
						
							| 
									
										
										
										
											2024-09-03 11:28:50 +02:00
										 |  |  |                 this.tooltip.hide(); | 
					
						
							| 
									
										
										
										
											2022-12-02 16:46:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 this.triggerCommand(this._command); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             console.warn(`Button widget '${this.componentId}' has no defined command`, this.settings); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getTitle() { | 
					
						
							|  |  |  |         const title = super.getTitle(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const action = actions.find(act => act.actionName === this._command); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (action && action.effectiveShortcuts.length > 0) { | 
					
						
							|  |  |  |             return `${title} (${action.effectiveShortcuts.join(", ")})`; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return title; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onClick(handler) { | 
					
						
							|  |  |  |         this.settings.onClick = handler; | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param {function|string} command | 
					
						
							| 
									
										
										
										
											2023-01-15 19:46:32 +01:00
										 |  |  |      * @returns {this} | 
					
						
							| 
									
										
										
										
											2022-12-02 16:46:14 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     command(command) { | 
					
						
							|  |  |  |         this.settings.command = command; | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     get _command() { | 
					
						
							|  |  |  |         return typeof this.settings.command === "function" | 
					
						
							|  |  |  |             ? this.settings.command() | 
					
						
							|  |  |  |             : this.settings.command; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |