@olenbetong/appframe-core 2.6.0 → 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
@@ -3,7 +3,7 @@ import "./errorHandler.js";
3
3
  import { expose } from "./expose.js";
4
4
  import { getLocalizedString } from "./getLocalizedString.js";
5
5
  import { localize } from "./localize.js";
6
- import { logActivity } from "./logActivity.js";
6
+ import { logActivity, startActivity } from "./logActivity.js";
7
7
  export { default as mitt } from "mitt";
8
8
  export { toLocalISODate, toLocalISODateTime } from "./date.js";
9
9
  export { downloadBlob } from "./downloadBlob.js";
@@ -17,10 +17,12 @@ export { has } from "./has.js";
17
17
  export { invariant } from "./invariant.js";
18
18
  export { localize } from "./localize.js";
19
19
  export { default as localStorage } from "./localStorage.js";
20
- export { logActivity } from "./logActivity.js";
20
+ 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;
@@ -42,6 +44,7 @@ export interface AfCore {
42
44
  expose: typeof expose;
43
45
  localize: typeof localize;
44
46
  logActivity: typeof logActivity;
47
+ startActivity: typeof startActivity;
45
48
  };
46
49
  controls?: {
47
50
  alert?: (message: string, callback?: () => void) => void;
package/es/index.js CHANGED
@@ -11,8 +11,9 @@ export { has } from "./has.js";
11
11
  export { invariant } from "./invariant.js";
12
12
  export { localize } from "./localize.js";
13
13
  export { default as localStorage } from "./localStorage.js";
14
- export { logActivity } from "./logActivity.js";
14
+ 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.0";
18
+ export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
19
+ export const version = "2.7.0";
@@ -1 +1,17 @@
1
- export declare function logActivity(action: string, parameters: Record<string, any>, timeElapsed: number): Promise<void>;
1
+ /**
2
+ * Logs an activity to the website activity log
3
+ *
4
+ * @param activity name of the activity we are logging
5
+ * @param parameters A dictionary of parameters used when doing the activity
6
+ * @param timeElapsed Number of milliseconds elapsed while executing
7
+ */
8
+ export declare function logActivity(activity: string, parameters: Record<string, any>, timeElapsed?: number): Promise<void>;
9
+ /**
10
+ * Records the current time, and returns a function that logs the activity with the
11
+ * time elapsed when it is called.
12
+ *
13
+ * @param activity Name of the activity we are logging
14
+ * @param parameters A dictionary of parameters used when doing the activity
15
+ * @returns A function that should be called when the activity is completed to log it to the website log
16
+ */
17
+ export declare function startActivity(activity: string, parameters: Record<string, any>): () => Promise<void>;
package/es/logActivity.js CHANGED
@@ -1,11 +1,18 @@
1
- export async function logActivity(action, parameters, timeElapsed) {
1
+ /**
2
+ * Logs an activity to the website activity log
3
+ *
4
+ * @param activity name of the activity we are logging
5
+ * @param parameters A dictionary of parameters used when doing the activity
6
+ * @param timeElapsed Number of milliseconds elapsed while executing
7
+ */
8
+ export async function logActivity(activity, parameters, timeElapsed = 0) {
2
9
  try {
3
10
  let response = await fetch("/api/logging/log-activity", {
4
11
  method: "POST",
5
12
  body: JSON.stringify({
6
13
  article: af.article.id,
7
14
  timeElapsed,
8
- action,
15
+ action: activity,
9
16
  parameters,
10
17
  }),
11
18
  headers: { "Content-Type": "application/json", Accept: "application/json" },
@@ -22,3 +29,15 @@ export async function logActivity(action, parameters, timeElapsed) {
22
29
  }
23
30
  catch (error) { }
24
31
  }
32
+ /**
33
+ * Records the current time, and returns a function that logs the activity with the
34
+ * time elapsed when it is called.
35
+ *
36
+ * @param activity Name of the activity we are logging
37
+ * @param parameters A dictionary of parameters used when doing the activity
38
+ * @returns A function that should be called when the activity is completed to log it to the website log
39
+ */
40
+ export function startActivity(activity, parameters) {
41
+ let start = performance.now();
42
+ return () => logActivity(activity, parameters, performance.now() - start);
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.6.0",
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": "49054954b937cf12215f72bda93dc2acd3c89846"
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
@@ -3,7 +3,7 @@ import "./errorHandler.js";
3
3
  import { expose } from "./expose.js";
4
4
  import { getLocalizedString } from "./getLocalizedString.js";
5
5
  import { localize } from "./localize.js";
6
- import { logActivity } from "./logActivity.js";
6
+ import { logActivity, startActivity } from "./logActivity.js";
7
7
 
8
8
  export { default as mitt } from "mitt";
9
9
  export { toLocalISODate, toLocalISODateTime } from "./date.js";
@@ -18,10 +18,12 @@ export { has } from "./has.js";
18
18
  export { invariant } from "./invariant.js";
19
19
  export { localize } from "./localize.js";
20
20
  export { default as localStorage } from "./localStorage.js";
21
- export { logActivity } from "./logActivity.js";
21
+ 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
 
@@ -48,6 +50,7 @@ export interface AfCore {
48
50
  expose: typeof expose;
49
51
  localize: typeof localize;
50
52
  logActivity: typeof logActivity;
53
+ startActivity: typeof startActivity;
51
54
  };
52
55
  controls?: {
53
56
  alert?: (message: string, callback?: () => void) => void;
@@ -1,11 +1,18 @@
1
- export async function logActivity(action: string, parameters: Record<string, any>, timeElapsed: number) {
1
+ /**
2
+ * Logs an activity to the website activity log
3
+ *
4
+ * @param activity name of the activity we are logging
5
+ * @param parameters A dictionary of parameters used when doing the activity
6
+ * @param timeElapsed Number of milliseconds elapsed while executing
7
+ */
8
+ export async function logActivity(activity: string, parameters: Record<string, any>, timeElapsed: number = 0) {
2
9
  try {
3
10
  let response = await fetch("/api/logging/log-activity", {
4
11
  method: "POST",
5
12
  body: JSON.stringify({
6
13
  article: af.article.id,
7
14
  timeElapsed,
8
- action,
15
+ action: activity,
9
16
  parameters,
10
17
  }),
11
18
  headers: { "Content-Type": "application/json", Accept: "application/json" },
@@ -21,3 +28,16 @@ export async function logActivity(action: string, parameters: Record<string, any
21
28
  }
22
29
  } catch (error) {}
23
30
  }
31
+
32
+ /**
33
+ * Records the current time, and returns a function that logs the activity with the
34
+ * time elapsed when it is called.
35
+ *
36
+ * @param activity Name of the activity we are logging
37
+ * @param parameters A dictionary of parameters used when doing the activity
38
+ * @returns A function that should be called when the activity is completed to log it to the website log
39
+ */
40
+ export function startActivity(activity: string, parameters: Record<string, any>) {
41
+ let start = performance.now();
42
+ return () => logActivity(activity, parameters, performance.now() - start);
43
+ }