@olenbetong/appframe-core 2.6.1 → 2.7.0

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.js ADDED
@@ -0,0 +1,81 @@
1
+ import { getLocalizedString } from "./getLocalizedString.js";
2
+ const template = `
3
+ <dialog class="alert-dialog">
4
+ <form method="dialog">
5
+ <article></article>
6
+ <footer>
7
+ <menu></menu>
8
+ </footer>
9
+ </form>
10
+ </dialog>
11
+ `;
12
+ if (globalThis.document && !document.querySelector("#AlertDialogTemplate")) {
13
+ let $template = document.createElement("template");
14
+ $template.id = "AlertDialogTemplate";
15
+ $template.innerHTML = template;
16
+ document.body.append($template);
17
+ }
18
+ export function waitForDialog(dialog) {
19
+ return new Promise((resolve) => {
20
+ dialog.addEventListener("close", resolve, { once: true });
21
+ });
22
+ }
23
+ export async function openAlertDialog({ buttons, content, className }) {
24
+ let $template = document.querySelector("#AlertDialogTemplate");
25
+ if (!$template)
26
+ throw Error("No alert dialog template found");
27
+ let $dialog = $template.content.firstElementChild?.cloneNode(true);
28
+ let $content = $dialog?.querySelector("article");
29
+ let $menu = $dialog?.querySelector("footer menu");
30
+ console.log({ $dialog, $content, $menu });
31
+ if (!$dialog || !$content || !$menu)
32
+ throw Error("Invalid alert dialog template");
33
+ if (className) {
34
+ $dialog.classList.add(className);
35
+ }
36
+ $content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
37
+ for (let { label, value, primary } of buttons) {
38
+ let listItem = document.createElement("li");
39
+ let buttonElem = document.createElement("button");
40
+ buttonElem.value = value;
41
+ if (primary) {
42
+ buttonElem.classList.add("primary");
43
+ }
44
+ buttonElem.textContent = label;
45
+ listItem.appendChild(buttonElem);
46
+ $menu.appendChild(buttonElem);
47
+ }
48
+ document.body.appendChild($dialog);
49
+ $dialog.showModal();
50
+ await waitForDialog($dialog);
51
+ let result = $dialog.returnValue || "cancel";
52
+ document.body.removeChild($dialog);
53
+ return result;
54
+ }
55
+ export var ConfirmButtons;
56
+ (function (ConfirmButtons) {
57
+ ConfirmButtons[ConfirmButtons["YesNo"] = 0] = "YesNo";
58
+ ConfirmButtons[ConfirmButtons["OkCancel"] = 1] = "OkCancel";
59
+ })(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" },
69
+ { label: getLocalizedString("OK"), value: "yes", primary: true },
70
+ ];
71
+ }
72
+ let result = await openAlertDialog({ content: prompt, buttons });
73
+ return result === "yes";
74
+ }
75
+ export async function alert(prompt) {
76
+ await openAlertDialog({
77
+ content: prompt,
78
+ className: "alert",
79
+ buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
80
+ });
81
+ }
package/es/index.d.ts CHANGED
@@ -21,6 +21,8 @@ export { logActivity, startActivity } from "./logActivity.js";
21
21
  export { default as SessionHandler } from "./SessionHandler.js";
22
22
  export { UserSession };
23
23
  export { getSettings, saveSettings } from "./settings.js";
24
+ export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
25
+ export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
24
26
  export declare const version: string;
25
27
  declare global {
26
28
  var __VERSION__: string;
package/es/index.js CHANGED
@@ -15,4 +15,5 @@ export { logActivity, startActivity } from "./logActivity.js";
15
15
  export { default as SessionHandler } from "./SessionHandler.js";
16
16
  export { UserSession };
17
17
  export { getSettings, saveSettings } from "./settings.js";
18
- export const version = "2.6.1";
18
+ export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
19
+ export const version = "2.7.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.6.1",
3
+ "version": "2.7.0",
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": "523363eb14f80f32006f78ec85f65fecb2bdc1b5"
51
+ "gitHead": "33c7f963b43ac6c4993b1da34d5761663e4c1546"
52
52
  }
package/src/alert.ts ADDED
@@ -0,0 +1,106 @@
1
+ import { getLocalizedString } from "./getLocalizedString.js";
2
+
3
+ const template = `
4
+ <dialog class="alert-dialog">
5
+ <form method="dialog">
6
+ <article></article>
7
+ <footer>
8
+ <menu></menu>
9
+ </footer>
10
+ </form>
11
+ </dialog>
12
+ `;
13
+
14
+ if (globalThis.document && !document.querySelector("#AlertDialogTemplate")) {
15
+ let $template = document.createElement("template");
16
+ $template.id = "AlertDialogTemplate";
17
+ $template.innerHTML = template;
18
+ document.body.append($template);
19
+ }
20
+
21
+ export function waitForDialog(dialog: HTMLDialogElement) {
22
+ return new Promise((resolve) => {
23
+ dialog.addEventListener("close", resolve, { once: true });
24
+ });
25
+ }
26
+
27
+ export type AlertDialogButton = {
28
+ label: string;
29
+ value: string;
30
+ primary?: boolean;
31
+ };
32
+
33
+ export type AlertDialogOptions = {
34
+ buttons: AlertDialogButton[];
35
+ content: string | HTMLElement;
36
+ className?: string;
37
+ };
38
+
39
+ export async function openAlertDialog({ buttons, content, className }: AlertDialogOptions) {
40
+ let $template = document.querySelector<HTMLTemplateElement>("#AlertDialogTemplate");
41
+ if (!$template) throw Error("No alert dialog template found");
42
+
43
+ let $dialog = $template.content.firstElementChild?.cloneNode(true) as HTMLDialogElement | null;
44
+ let $content = $dialog?.querySelector("article");
45
+ let $menu = $dialog?.querySelector("footer menu");
46
+ console.log({ $dialog, $content, $menu });
47
+ if (!$dialog || !$content || !$menu) throw Error("Invalid alert dialog template");
48
+
49
+ if (className) {
50
+ $dialog.classList.add(className);
51
+ }
52
+
53
+ $content.appendChild(typeof content === "string" ? document.createTextNode(content) : content);
54
+ for (let { label, value, primary } of buttons) {
55
+ let listItem = document.createElement("li");
56
+ let buttonElem = document.createElement("button");
57
+ buttonElem.value = value;
58
+ if (primary) {
59
+ buttonElem.classList.add("primary");
60
+ }
61
+ buttonElem.textContent = label;
62
+ listItem.appendChild(buttonElem);
63
+ $menu.appendChild(buttonElem);
64
+ }
65
+
66
+ document.body.appendChild($dialog);
67
+ $dialog.showModal();
68
+
69
+ await waitForDialog($dialog);
70
+ let result = $dialog.returnValue || "cancel";
71
+ document.body.removeChild($dialog);
72
+
73
+ return result;
74
+ }
75
+
76
+ export enum ConfirmButtons {
77
+ YesNo,
78
+ OkCancel,
79
+ }
80
+
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
+ }
95
+
96
+ let result = await openAlertDialog({ content: prompt, buttons });
97
+ return result === "yes";
98
+ }
99
+
100
+ export async function alert(prompt: string): Promise<void> {
101
+ await openAlertDialog({
102
+ content: prompt,
103
+ className: "alert",
104
+ buttons: [{ label: getLocalizedString("OK"), value: "ok", primary: true }],
105
+ });
106
+ }
package/src/index.ts CHANGED
@@ -22,6 +22,8 @@ export { logActivity, startActivity } from "./logActivity.js";
22
22
  export { default as SessionHandler } from "./SessionHandler.js";
23
23
  export { UserSession };
24
24
  export { getSettings, saveSettings } from "./settings.js";
25
+ export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
26
+ export type { AlertDialogButton, AlertDialogOptions } from "./alert.js";
25
27
 
26
28
  export const version = __VERSION__;
27
29