Skip to content

Commit

Permalink
fix Alert
Browse files Browse the repository at this point in the history
  • Loading branch information
OfekShilon committed Aug 17, 2024
1 parent 4f57931 commit d456941
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
15 changes: 0 additions & 15 deletions static/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions static/widgets/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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?.();
});

Expand Down

0 comments on commit d456941

Please sign in to comment.