From d4569411f238cd25e1c5a5de5ae6a43b9ad774d0 Mon Sep 17 00:00:00 2001 From: Ofek Shilon Date: Sat, 17 Aug 2024 22:16:57 +0300 Subject: [PATCH] fix `Alert` --- static/utils.ts | 15 --------------- static/widgets/alert.ts | 13 ++++++++++--- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/static/utils.ts b/static/utils.ts index 81decfae973..f1ec9b6c726 100644 --- a/static/utils.ts +++ b/static/utils.ts @@ -40,21 +40,6 @@ export function updateAndCalcTopBarHeight(domRoot: JQuery, topBar: JQuery, hidea return topBarHeight; } -/** - * Subscribe and unsubscribe the event listener. - * - * @param {JQuery} element - * @param {string} eventName - * @param {(event:JQuery.Event)=>void} callback - * @returns void - */ -export function toggleEventListener(element: JQuery, eventName: string, callback: (event: JQuery.Event) => void): void { - element.on(eventName, (event: JQuery.Event) => { - callback(event); - element.off(eventName); - }); -} - export function formatDateTimeWithSpaces(d: Date) { const t = x => x.slice(-2); // Hopefully some day we can use the temporal api to make this less of a pain diff --git a/static/widgets/alert.ts b/static/widgets/alert.ts index 19c658caaca..d00fff13adb 100644 --- a/static/widgets/alert.ts +++ b/static/widgets/alert.ts @@ -25,7 +25,6 @@ import $ from 'jquery'; import {AlertAskOptions, AlertEnterTextOptions, AlertNotifyOptions} from './alert.interfaces.js'; -import {toggleEventListener} from '../utils.js'; export class Alert { yesHandler: ((answer?: string | string[] | number) => void) | null = null; @@ -42,6 +41,14 @@ export class Alert { }); } + private toggleEventListener(element: JQuery, eventName: string, callback: (event: JQuery.Event) => void): void { + element.on(eventName, (event: JQuery.Event) => { + callback(event); + element.off(eventName); + this.yesHandler = null; + this.noHandler = null; + }); + } /** * Display an alert with a title and a body */ @@ -140,13 +147,13 @@ export class Alert { modal.find('.modal-body .question').html(question); const yesButton = modal.find('.modal-footer .yes'); - toggleEventListener(yesButton, 'click', () => { + this.toggleEventListener(yesButton, 'click', () => { const answer = modal.find('.question-answer'); this.yesHandler?.(answer.val()); }); const noButton = modal.find('.modal-footer .no'); - toggleEventListener(noButton, 'click', () => { + this.toggleEventListener(noButton, 'click', () => { this.noHandler?.(); });