@olenbetong/appframe-core 2.7.1 → 2.7.3

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,7 +20,11 @@ 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");
@@ -30,7 +34,12 @@ export async function openAlertDialog({ buttons, content, className }) {
30
34
  if (!$dialog || !$content || !$menu)
31
35
  throw Error("Invalid alert dialog template");
32
36
  if (className) {
33
- $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);
34
43
  }
35
44
  $content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
36
45
  for (let { label, value, primary } of buttons) {
@@ -46,34 +55,47 @@ export async function openAlertDialog({ buttons, content, className }) {
46
55
  }
47
56
  document.body.appendChild($dialog);
48
57
  $dialog.showModal();
49
- await waitForDialog($dialog);
50
- let result = $dialog.returnValue || "cancel";
51
- document.body.removeChild($dialog);
52
- 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;
53
74
  }
54
75
  export var ConfirmButtons;
55
76
  (function (ConfirmButtons) {
56
77
  ConfirmButtons[ConfirmButtons["YesNo"] = 0] = "YesNo";
57
78
  ConfirmButtons[ConfirmButtons["OkCancel"] = 1] = "OkCancel";
58
79
  })(ConfirmButtons || (ConfirmButtons = {}));
59
- export async function confirm(prompt, button = ConfirmButtons.YesNo) {
60
- let buttons = [
61
- { label: getLocalizedString("Yes"), value: "yes", primary: true },
62
- { label: getLocalizedString("No"), value: "no" },
63
- ];
64
- if (button == ConfirmButtons.OkCancel) {
65
- buttons = [
80
+ export async function confirm(prompt, options = {}) {
81
+ let { buttons: optionButtons = ConfirmButtons.YesNo, title } = options;
82
+ let buttons = optionButtons === ConfirmButtons.OkCancel
83
+ ? [
66
84
  { label: getLocalizedString("OK"), value: "yes", primary: true },
67
85
  { label: getLocalizedString("Cancel"), value: "no" },
86
+ ]
87
+ : [
88
+ { label: getLocalizedString("Yes"), value: "yes", primary: true },
89
+ { label: getLocalizedString("No"), value: "no" },
68
90
  ];
69
- }
70
- let result = await openAlertDialog({ content: prompt, buttons, className: "alert-dialog-confirm" });
91
+ let result = await openAlertDialog({ content: prompt, buttons, className: "alert-dialog-confirm", title });
71
92
  return result === "yes";
72
93
  }
73
- export async function alert(prompt) {
94
+ export async function alert(prompt, options = {}) {
74
95
  await openAlertDialog({
75
96
  content: prompt,
76
97
  className: "alert-dialog-alert",
77
98
  buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
99
+ ...options,
78
100
  });
79
101
  }
package/es/index.d.ts CHANGED
@@ -1,7 +1,9 @@
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";
6
+ import afLocalStorage from "./localStorage.js";
5
7
  import { localize } from "./localize.js";
6
8
  import { logActivity, startActivity } from "./logActivity.js";
7
9
  export { default as mitt } from "mitt";
@@ -16,13 +18,13 @@ export { getLocalizedString } from "./getLocalizedString.js";
16
18
  export { has } from "./has.js";
17
19
  export { invariant } from "./invariant.js";
18
20
  export { localize } from "./localize.js";
19
- export { default as localStorage } from "./localStorage.js";
21
+ export { afLocalStorage as localStorage };
20
22
  export { logActivity, startActivity } from "./logActivity.js";
21
23
  export { default as SessionHandler } from "./SessionHandler.js";
22
24
  export { UserSession };
23
25
  export { getSettings, saveSettings } from "./settings.js";
24
26
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
25
- export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
27
+ export type { AlertDialogButton, AlertDialogOptions, AlertOptions, ConfirmOptions } from "./alert.js";
26
28
  export declare const version: string;
27
29
  declare global {
28
30
  var __VERSION__: string;
@@ -43,8 +45,12 @@ export interface AfCore {
43
45
  getLocalizedString: typeof getLocalizedString;
44
46
  expose: typeof expose;
45
47
  localize: typeof localize;
48
+ localStorage: typeof afLocalStorage;
46
49
  logActivity: typeof logActivity;
47
50
  startActivity: typeof startActivity;
51
+ alert: typeof alert;
52
+ confirm: typeof confirm;
53
+ openAlertDialog: typeof openAlertDialog;
48
54
  };
49
55
  controls?: {
50
56
  alert?: (message: string, callback?: () => void) => void;
package/es/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { UserSession } from "./UserSession.js";
2
2
  import "./errorHandler.js";
3
+ import afLocalStorage from "./localStorage.js";
3
4
  export { default as mitt } from "mitt";
4
5
  export { toLocalISODate, toLocalISODateTime } from "./date.js";
5
6
  export { downloadBlob } from "./downloadBlob.js";
@@ -10,10 +11,10 @@ export { getLocalizedString } from "./getLocalizedString.js";
10
11
  export { has } from "./has.js";
11
12
  export { invariant } from "./invariant.js";
12
13
  export { localize } from "./localize.js";
13
- export { default as localStorage } from "./localStorage.js";
14
+ export { afLocalStorage as localStorage };
14
15
  export { logActivity, startActivity } from "./logActivity.js";
15
16
  export { default as SessionHandler } from "./SessionHandler.js";
16
17
  export { UserSession };
17
18
  export { getSettings, saveSettings } from "./settings.js";
18
19
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
19
- export const version = "2.7.1";
20
+ export const version = "2.7.3";
@@ -1,5 +1,5 @@
1
1
  declare class LocalStorage {
2
- isPersisted(): boolean;
2
+ isPersisted: () => boolean;
3
3
  getLocalStorageKey(key: string, global?: boolean): string;
4
4
  get(key: string, defaultValue: unknown, global?: boolean): any;
5
5
  set(key: string, value: unknown, global?: boolean): void;
@@ -2,8 +2,8 @@ function getLocalKey(key) {
2
2
  return [af?.article?.id || "", "common", key].join(".");
3
3
  }
4
4
  class LocalStorage {
5
- isPersisted() {
6
- return true;
5
+ constructor() {
6
+ this.isPersisted = () => true;
7
7
  }
8
8
  getLocalStorageKey(key, global) {
9
9
  return global ? ["common", key].join(".") : getLocalKey(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.7.1",
3
+ "version": "2.7.3",
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": "45942718b312253e1113e5ef8177bf5b753fb898"
51
+ "gitHead": "a98679d30bbd34f2507d87b84502fef1c1e96e52"
52
52
  }
package/src/alert.ts CHANGED
@@ -34,9 +34,17 @@ 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
 
@@ -47,7 +55,13 @@ export async function openAlertDialog({ buttons, content, className }: AlertDial
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,27 +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("Yes"), value: "yes", primary: true },
84
- { label: getLocalizedString("No"), value: "no" },
85
- ];
86
-
87
- if (button == ConfirmButtons.OkCancel) {
88
- buttons = [
89
- { label: getLocalizedString("OK"), value: "yes", primary: true },
90
- { label: getLocalizedString("Cancel"), value: "no" },
91
- ];
92
- }
108
+ export type ConfirmOptions = {
109
+ buttons?: ConfirmButtons;
110
+ title?: string;
111
+ };
93
112
 
94
- let result = await openAlertDialog({ content: prompt, buttons, className: "alert-dialog-confirm" });
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 });
95
127
  return result === "yes";
96
128
  }
97
129
 
98
- 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> {
99
135
  await openAlertDialog({
100
136
  content: prompt,
101
137
  className: "alert-dialog-alert",
102
138
  buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
139
+ ...options,
103
140
  });
104
141
  }
package/src/index.ts CHANGED
@@ -1,7 +1,9 @@
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";
6
+ import afLocalStorage from "./localStorage.js";
5
7
  import { localize } from "./localize.js";
6
8
  import { logActivity, startActivity } from "./logActivity.js";
7
9
 
@@ -17,13 +19,13 @@ export { getLocalizedString } from "./getLocalizedString.js";
17
19
  export { has } from "./has.js";
18
20
  export { invariant } from "./invariant.js";
19
21
  export { localize } from "./localize.js";
20
- export { default as localStorage } from "./localStorage.js";
22
+ export { afLocalStorage as localStorage };
21
23
  export { logActivity, startActivity } from "./logActivity.js";
22
24
  export { default as SessionHandler } from "./SessionHandler.js";
23
25
  export { UserSession };
24
26
  export { getSettings, saveSettings } from "./settings.js";
25
27
  export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
26
- export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
28
+ export type { AlertDialogButton, AlertDialogOptions, AlertOptions, ConfirmOptions } from "./alert.js";
27
29
 
28
30
  export const version = __VERSION__;
29
31
 
@@ -49,8 +51,12 @@ export interface AfCore {
49
51
  getLocalizedString: typeof getLocalizedString;
50
52
  expose: typeof expose;
51
53
  localize: typeof localize;
54
+ localStorage: typeof afLocalStorage;
52
55
  logActivity: typeof logActivity;
53
56
  startActivity: typeof startActivity;
57
+ alert: typeof alert;
58
+ confirm: typeof confirm;
59
+ openAlertDialog: typeof openAlertDialog;
54
60
  };
55
61
  controls?: {
56
62
  alert?: (message: string, callback?: () => void) => void;
@@ -3,9 +3,7 @@ function getLocalKey(key: string): string {
3
3
  }
4
4
 
5
5
  class LocalStorage {
6
- isPersisted() {
7
- return true;
8
- }
6
+ isPersisted = () => true;
9
7
 
10
8
  getLocalStorageKey(key: string, global?: boolean) {
11
9
  return global ? ["common", key].join(".") : getLocalKey(key);