@olenbetong/appframe-core 2.7.0 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/alert.d.ts CHANGED
@@ -8,11 +8,20 @@ export type AlertDialogOptions = {
8
8
  buttons: AlertDialogButton[];
9
9
  content: string | HTMLElement;
10
10
  className?: string;
11
+ id?: string;
12
+ title?: string;
11
13
  };
12
- export declare function openAlertDialog({ buttons, content, className }: AlertDialogOptions): Promise<string>;
14
+ export declare function openAlertDialog({ buttons, content, className, id, title }: AlertDialogOptions): Promise<string | undefined>;
13
15
  export declare enum ConfirmButtons {
14
16
  YesNo = 0,
15
17
  OkCancel = 1
16
18
  }
17
- export declare function confirm(prompt: string, button?: ConfirmButtons): Promise<boolean>;
18
- export declare function alert(prompt: string): Promise<void>;
19
+ export type ConfirmOptions = {
20
+ buttons?: ConfirmButtons;
21
+ title?: string;
22
+ };
23
+ export declare function confirm(prompt: string, options?: ConfirmOptions): Promise<boolean>;
24
+ export type AlertOptions = {
25
+ title?: string;
26
+ };
27
+ export declare function alert(prompt: string, options?: AlertOptions): Promise<void>;
package/es/alert.js CHANGED
@@ -20,18 +20,26 @@ export function waitForDialog(dialog) {
20
20
  dialog.addEventListener("close", resolve, { once: true });
21
21
  });
22
22
  }
23
- export async function openAlertDialog({ buttons, content, className }) {
23
+ const alertDialogPromiseMap = new Map();
24
+ export async function openAlertDialog({ buttons, content, className, id, title }) {
25
+ if (id && alertDialogPromiseMap.has(id)) {
26
+ return await alertDialogPromiseMap.get(id);
27
+ }
24
28
  let $template = document.querySelector("#AlertDialogTemplate");
25
29
  if (!$template)
26
30
  throw Error("No alert dialog template found");
27
31
  let $dialog = $template.content.firstElementChild?.cloneNode(true);
28
32
  let $content = $dialog?.querySelector("article");
29
33
  let $menu = $dialog?.querySelector("footer menu");
30
- console.log({ $dialog, $content, $menu });
31
34
  if (!$dialog || !$content || !$menu)
32
35
  throw Error("Invalid alert dialog template");
33
36
  if (className) {
34
- $dialog.classList.add(className);
37
+ className.split(/\s+/).forEach((className) => $dialog?.classList.add(className));
38
+ }
39
+ if (title) {
40
+ let $header = document.createElement("header");
41
+ $header.append(title);
42
+ $content.parentNode?.insertBefore($header, $content);
35
43
  }
36
44
  $content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
37
45
  for (let { label, value, primary } of buttons) {
@@ -47,35 +55,47 @@ export async function openAlertDialog({ buttons, content, className }) {
47
55
  }
48
56
  document.body.appendChild($dialog);
49
57
  $dialog.showModal();
50
- await waitForDialog($dialog);
51
- let result = $dialog.returnValue || "cancel";
52
- document.body.removeChild($dialog);
53
- return result;
58
+ let dialogPromise = waitForDialog($dialog)
59
+ .then(() => {
60
+ if (id && alertDialogPromiseMap.has(id)) {
61
+ alertDialogPromiseMap.delete(id);
62
+ }
63
+ })
64
+ .then(() => {
65
+ let result = $dialog?.returnValue || "cancel";
66
+ if ($dialog)
67
+ document.body.removeChild($dialog);
68
+ return result;
69
+ });
70
+ if (id) {
71
+ alertDialogPromiseMap.set(id, dialogPromise);
72
+ }
73
+ return await dialogPromise;
54
74
  }
55
75
  export var ConfirmButtons;
56
76
  (function (ConfirmButtons) {
57
77
  ConfirmButtons[ConfirmButtons["YesNo"] = 0] = "YesNo";
58
78
  ConfirmButtons[ConfirmButtons["OkCancel"] = 1] = "OkCancel";
59
79
  })(ConfirmButtons || (ConfirmButtons = {}));
60
- export async function confirm(prompt, button = ConfirmButtons.YesNo) {
61
- let buttons = [
62
- { label: getLocalizedString("No"), value: "no" },
63
- { label: getLocalizedString("Yes"), value: "yes", primary: true },
64
- ];
65
- console.log(button);
66
- if (button == ConfirmButtons.OkCancel) {
67
- buttons = [
68
- { label: getLocalizedString("Cancel"), value: "no" },
80
+ export async function confirm(prompt, options = {}) {
81
+ let { buttons: optionButtons = ConfirmButtons.YesNo, title } = options;
82
+ let buttons = optionButtons === ConfirmButtons.OkCancel
83
+ ? [
69
84
  { label: getLocalizedString("OK"), value: "yes", primary: true },
85
+ { label: getLocalizedString("Cancel"), value: "no" },
86
+ ]
87
+ : [
88
+ { label: getLocalizedString("Yes"), value: "yes", primary: true },
89
+ { label: getLocalizedString("No"), value: "no" },
70
90
  ];
71
- }
72
- let result = await openAlertDialog({ content: prompt, buttons });
91
+ let result = await openAlertDialog({ content: prompt, buttons, className: "alert-dialog-confirm", title });
73
92
  return result === "yes";
74
93
  }
75
- export async function alert(prompt) {
94
+ export async function alert(prompt, options = {}) {
76
95
  await openAlertDialog({
77
96
  content: prompt,
78
- className: "alert",
97
+ className: "alert-dialog-alert",
79
98
  buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
99
+ ...options,
80
100
  });
81
101
  }
package/es/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { UserSession } from "./UserSession.js";
2
+ import { alert, confirm, openAlertDialog } from "./alert.js";
2
3
  import "./errorHandler.js";
3
4
  import { expose } from "./expose.js";
4
5
  import { getLocalizedString } from "./getLocalizedString.js";
@@ -22,7 +23,7 @@ export { default as SessionHandler } from "./SessionHandler.js";
22
23
  export { UserSession };
23
24
  export { getSettings, saveSettings } from "./settings.js";
24
25
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
25
- export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
26
+ export type { AlertDialogButton, AlertDialogOptions, AlertOptions, ConfirmOptions } from "./alert.js";
26
27
  export declare const version: string;
27
28
  declare global {
28
29
  var __VERSION__: string;
@@ -45,6 +46,9 @@ export interface AfCore {
45
46
  localize: typeof localize;
46
47
  logActivity: typeof logActivity;
47
48
  startActivity: typeof startActivity;
49
+ alert: typeof alert;
50
+ confirm: typeof confirm;
51
+ openAlertDialog: typeof openAlertDialog;
48
52
  };
49
53
  controls?: {
50
54
  alert?: (message: string, callback?: () => void) => void;
package/es/index.js CHANGED
@@ -16,4 +16,4 @@ export { default as SessionHandler } from "./SessionHandler.js";
16
16
  export { UserSession };
17
17
  export { getSettings, saveSettings } from "./settings.js";
18
18
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
19
- export const version = "2.7.0";
19
+ export const version = "2.7.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.7.0",
3
+ "version": "2.7.2",
4
4
  "description": "Javascript libraries to utilize the Appframe framework",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -48,5 +48,5 @@
48
48
  "devDependencies": {
49
49
  "chalk": "^5.1.2"
50
50
  },
51
- "gitHead": "33c7f963b43ac6c4993b1da34d5761663e4c1546"
51
+ "gitHead": "4ba1cde385d7df1be5a90c7594af2e2ff2f662dd"
52
52
  }
package/src/alert.ts CHANGED
@@ -34,20 +34,34 @@ export type AlertDialogOptions = {
34
34
  buttons: AlertDialogButton[];
35
35
  content: string | HTMLElement;
36
36
  className?: string;
37
+ id?: string;
38
+ title?: string;
37
39
  };
38
40
 
39
- export async function openAlertDialog({ buttons, content, className }: AlertDialogOptions) {
41
+ const alertDialogPromiseMap = new Map<string, Promise<string>>();
42
+
43
+ export async function openAlertDialog({ buttons, content, className, id, title }: AlertDialogOptions) {
44
+ if (id && alertDialogPromiseMap.has(id)) {
45
+ return await alertDialogPromiseMap.get(id);
46
+ }
47
+
40
48
  let $template = document.querySelector<HTMLTemplateElement>("#AlertDialogTemplate");
41
49
  if (!$template) throw Error("No alert dialog template found");
42
50
 
43
51
  let $dialog = $template.content.firstElementChild?.cloneNode(true) as HTMLDialogElement | null;
44
52
  let $content = $dialog?.querySelector("article");
45
53
  let $menu = $dialog?.querySelector("footer menu");
46
- console.log({ $dialog, $content, $menu });
54
+
47
55
  if (!$dialog || !$content || !$menu) throw Error("Invalid alert dialog template");
48
56
 
49
57
  if (className) {
50
- $dialog.classList.add(className);
58
+ className.split(/\s+/).forEach((className) => $dialog?.classList.add(className));
59
+ }
60
+
61
+ if (title) {
62
+ let $header = document.createElement("header");
63
+ $header.append(title);
64
+ $content.parentNode?.insertBefore($header, $content);
51
65
  }
52
66
 
53
67
  $content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
@@ -66,11 +80,24 @@ export async function openAlertDialog({ buttons, content, className }: AlertDial
66
80
  document.body.appendChild($dialog);
67
81
  $dialog.showModal();
68
82
 
69
- await waitForDialog($dialog);
70
- let result = $dialog.returnValue || "cancel";
71
- document.body.removeChild($dialog);
83
+ let dialogPromise = waitForDialog($dialog)
84
+ .then(() => {
85
+ if (id && alertDialogPromiseMap.has(id)) {
86
+ alertDialogPromiseMap.delete(id);
87
+ }
88
+ })
89
+ .then(() => {
90
+ let result = $dialog?.returnValue || "cancel";
91
+ if ($dialog) document.body.removeChild($dialog);
92
+
93
+ return result;
94
+ });
95
+
96
+ if (id) {
97
+ alertDialogPromiseMap.set(id, dialogPromise);
98
+ }
72
99
 
73
- return result;
100
+ return await dialogPromise;
74
101
  }
75
102
 
76
103
  export enum ConfirmButtons {
@@ -78,29 +105,37 @@ export enum ConfirmButtons {
78
105
  OkCancel,
79
106
  }
80
107
 
81
- export async function confirm(prompt: string, button: ConfirmButtons = ConfirmButtons.YesNo): Promise<boolean> {
82
- let buttons: AlertDialogButton[] = [
83
- { label: getLocalizedString("No"), value: "no" },
84
- { label: getLocalizedString("Yes"), value: "yes", primary: true },
85
- ];
86
-
87
- console.log(button);
88
-
89
- if (button == ConfirmButtons.OkCancel) {
90
- buttons = [
91
- { label: getLocalizedString("Cancel"), value: "no" },
92
- { label: getLocalizedString("OK"), value: "yes", primary: true },
93
- ];
94
- }
108
+ export type ConfirmOptions = {
109
+ buttons?: ConfirmButtons;
110
+ title?: string;
111
+ };
95
112
 
96
- let result = await openAlertDialog({ content: prompt, buttons });
113
+ export async function confirm(prompt: string, options: ConfirmOptions = {}): Promise<boolean> {
114
+ let { buttons: optionButtons = ConfirmButtons.YesNo, title } = options;
115
+ let buttons: AlertDialogButton[] =
116
+ optionButtons === ConfirmButtons.OkCancel
117
+ ? [
118
+ { label: getLocalizedString("OK"), value: "yes", primary: true },
119
+ { label: getLocalizedString("Cancel"), value: "no" },
120
+ ]
121
+ : [
122
+ { label: getLocalizedString("Yes"), value: "yes", primary: true },
123
+ { label: getLocalizedString("No"), value: "no" },
124
+ ];
125
+
126
+ let result = await openAlertDialog({ content: prompt, buttons, className: "alert-dialog-confirm", title });
97
127
  return result === "yes";
98
128
  }
99
129
 
100
- export async function alert(prompt: string): Promise<void> {
130
+ export type AlertOptions = {
131
+ title?: string;
132
+ };
133
+
134
+ export async function alert(prompt: string, options: AlertOptions = {}): Promise<void> {
101
135
  await openAlertDialog({
102
136
  content: prompt,
103
- className: "alert",
137
+ className: "alert-dialog-alert",
104
138
  buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
139
+ ...options,
105
140
  });
106
141
  }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { UserSession } from "./UserSession.js";
2
+ import { alert, confirm, openAlertDialog } from "./alert.js";
2
3
  import "./errorHandler.js";
3
4
  import { expose } from "./expose.js";
4
5
  import { getLocalizedString } from "./getLocalizedString.js";
@@ -23,7 +24,7 @@ export { default as SessionHandler } from "./SessionHandler.js";
23
24
  export { UserSession };
24
25
  export { getSettings, saveSettings } from "./settings.js";
25
26
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
26
- export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
27
+ export type { AlertDialogButton, AlertDialogOptions, AlertOptions, ConfirmOptions } from "./alert.js";
27
28
 
28
29
  export const version = __VERSION__;
29
30
 
@@ -51,6 +52,9 @@ export interface AfCore {
51
52
  localize: typeof localize;
52
53
  logActivity: typeof logActivity;
53
54
  startActivity: typeof startActivity;
55
+ alert: typeof alert;
56
+ confirm: typeof confirm;
57
+ openAlertDialog: typeof openAlertDialog;
54
58
  };
55
59
  controls?: {
56
60
  alert?: (message: string, callback?: () => void) => void;