| 19 | } |
| 20 | |
| 21 | static override async bind(): Promise<void> { |
| 22 | const engine = this.engine; |
| 23 | // Bind the click event on data-do elements. This property is used for |
| 24 | // every choice button. |
| 25 | this.engine.on('click', '[data-choice]:not([disabled])', function (this: HTMLElement, event: Event) { |
| 26 | engine.debug.debug('Registered Click on Choice Button'); |
| 27 | event.stopImmediatePropagation(); |
| 28 | event.stopPropagation(); |
| 29 | event.preventDefault(); |
| 30 | |
| 31 | let doAction = this.dataset.do; |
| 32 | |
| 33 | // Check that the data property was not created with |
| 34 | // a null property |
| 35 | if (doAction == 'null') { |
| 36 | Choice.blocking = false; |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Remove all the choices |
| 41 | engine.element().find('choice-container').remove(); |
| 42 | |
| 43 | const choice = this.dataset.choice; |
| 44 | |
| 45 | const currentChoice = (engine.global('_CurrentChoice') as unknown[]).pop() as { Choice: Record<string, { Do: string; Timer?: unknown; onChosen?: () => void }> } | undefined; |
| 46 | const current = currentChoice?.Choice; |
| 47 | |
| 48 | if (current && typeof current.Timer !== 'undefined') { |
| 49 | const timer = (engine.global('_ChoiceTimer') as unknown[]).pop() as HTMLElement & { props?: { timer: ReturnType<typeof setTimeout> }; element?: () => { remove: () => void } } | undefined; |
| 50 | (engine.global('_choice_pending_rollback') as boolean[]).pop(); |
| 51 | if (typeof timer !== 'undefined' && timer.props) { |
| 52 | clearTimeout(timer.props.timer); |
| 53 | if (timer.parentNode !== null && timer.element) { |
| 54 | timer.element().remove(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const currentChoiceExists = current && typeof choice !== 'undefined' && typeof current[choice] !== 'undefined'; |
| 60 | |
| 61 | if (currentChoiceExists) { |
| 62 | doAction = current[choice].Do; |
| 63 | } |
| 64 | |
| 65 | const run = async () => { |
| 66 | if (typeof doAction === 'string' && typeof choice === 'string') { |
| 67 | const result = await engine.run(doAction); |
| 68 | |
| 69 | Choice.blocking = false; |
| 70 | engine.history('choice').push(choice); |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | if (currentChoiceExists && typeof current[choice].onChosen === 'function') { |
| 77 | Util.callAsync(current[choice].onChosen, engine).then(() => { |
| 78 | return run(); |