@cedx/base 0.10.1 → 0.12.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.
Files changed (45) hide show
  1. package/ReadMe.md +1 -1
  2. package/lib/Data/Pagination.d.ts +3 -3
  3. package/lib/Data/Pagination.js +10 -10
  4. package/lib/UI/Components/KeyboardAccelerator.d.ts +11 -0
  5. package/lib/UI/Components/KeyboardAccelerator.d.ts.map +1 -1
  6. package/lib/UI/Components/KeyboardAccelerator.js +1 -1
  7. package/lib/UI/Components/OfflineIndicator.d.ts +0 -4
  8. package/lib/UI/Components/OfflineIndicator.d.ts.map +1 -1
  9. package/lib/UI/Components/OfflineIndicator.js +5 -11
  10. package/lib/UI/Components/TabActivator.d.ts +2 -2
  11. package/lib/UI/Components/TabActivator.d.ts.map +1 -1
  12. package/lib/UI/Components/TabActivator.js +6 -6
  13. package/lib/UI/Components/ThemeDropdown.d.ts +16 -0
  14. package/lib/UI/Components/ThemeDropdown.d.ts.map +1 -1
  15. package/lib/UI/Components/ThemeDropdown.js +73 -29
  16. package/lib/UI/Components/Toast.d.ts +74 -0
  17. package/lib/UI/Components/Toast.d.ts.map +1 -0
  18. package/lib/UI/Components/Toast.js +225 -0
  19. package/lib/UI/ElementExtensions.d.ts +13 -0
  20. package/lib/UI/ElementExtensions.d.ts.map +1 -0
  21. package/lib/UI/ElementExtensions.js +18 -0
  22. package/lib/UI/Position.d.ts +46 -0
  23. package/lib/UI/Position.d.ts.map +1 -0
  24. package/lib/UI/Position.js +41 -0
  25. package/lib/UI/Size.d.ts +34 -0
  26. package/lib/UI/Size.d.ts.map +1 -0
  27. package/lib/UI/Size.js +29 -0
  28. package/lib/UI/Variant.d.ts +26 -0
  29. package/lib/UI/Variant.d.ts.map +1 -0
  30. package/lib/UI/Variant.js +21 -0
  31. package/lib/UI/ViewportScroller.js +1 -1
  32. package/package.json +5 -5
  33. package/src/Client/Data/Pagination.ts +11 -11
  34. package/src/Client/UI/Components/KeyboardAccelerator.ts +14 -1
  35. package/src/Client/UI/Components/MessageBox.old +242 -0
  36. package/src/Client/UI/Components/OfflineIndicator.ts +5 -13
  37. package/src/Client/UI/Components/TabActivator.ts +7 -7
  38. package/src/Client/UI/Components/ThemeDropdown.ts +79 -26
  39. package/src/Client/UI/Components/Toast.ts +252 -0
  40. package/src/Client/UI/Components/Toaster.old +0 -0
  41. package/src/Client/UI/ElementExtensions.ts +19 -0
  42. package/src/Client/UI/Position.ts +55 -0
  43. package/src/Client/UI/Size.ts +40 -0
  44. package/src/Client/UI/Variant.ts +30 -0
  45. package/src/Client/UI/ViewportScroller.ts +1 -1
@@ -0,0 +1,252 @@
1
+ import {Duration} from "@cedx/base/Duration.js";
2
+ import {Context, getIcon} from "@cedx/base/UI/Context.js";
3
+ import {Toast as BootstrapToast} from "bootstrap";
4
+
5
+ /**
6
+ * Manages the notification messages.
7
+ */
8
+ export class Toast extends HTMLElement {
9
+
10
+ /**
11
+ * The list of observed attributes.
12
+ */
13
+ static readonly observedAttributes = ["caption", "context", "culture", "icon"];
14
+
15
+ /**
16
+ * The time units.
17
+ */
18
+ static readonly #timeUnits: Intl.RelativeTimeFormatUnit[] = ["second", "minute", "hour"];
19
+
20
+ /**
21
+ * The formatter used to format the relative time.
22
+ */
23
+ #formatter!: Intl.RelativeTimeFormat;
24
+
25
+ /**
26
+ * The toast header.
27
+ */
28
+ readonly #header = this.querySelector(".toast-header")!;
29
+
30
+ /**
31
+ * The time at which this component was initially shown.
32
+ */
33
+ #initialTime = Date.now();
34
+
35
+ /**
36
+ * The timer identifier.
37
+ */
38
+ #timer = 0;
39
+
40
+ /**
41
+ * The underlying Bootstrap toast.
42
+ */
43
+ #toast!: BootstrapToast;
44
+
45
+ /**
46
+ * Registers the component.
47
+ */
48
+ static {
49
+ customElements.define("toaster-item", this);
50
+ }
51
+
52
+ /**
53
+ * Value indicating whether to apply a fade transition.
54
+ */
55
+ get animation(): boolean {
56
+ return this.hasAttribute("animation");
57
+ }
58
+ set animation(value: boolean) {
59
+ if (value) this.setAttribute("animation", "");
60
+ else this.removeAttribute("animation");
61
+ }
62
+
63
+ /**
64
+ * Value indicating whether to automatically hide the notification.
65
+ */
66
+ get autoHide(): boolean {
67
+ return this.hasAttribute("autohide");
68
+ }
69
+ set autoHide(value: boolean) {
70
+ if (value) this.setAttribute("autohide", "");
71
+ else this.removeAttribute("autohide");
72
+ }
73
+
74
+ /**
75
+ * The title displayed in the header.
76
+ */
77
+ get caption(): string {
78
+ return (this.getAttribute("caption") ?? "").trim();
79
+ }
80
+ set caption(value: string) {
81
+ this.setAttribute("caption", value);
82
+ }
83
+
84
+ /**
85
+ * A contextual modifier.
86
+ */
87
+ get context(): Context {
88
+ const value = this.getAttribute("context") as Context;
89
+ return Object.values(Context).includes(value) ? value : Context.Info;
90
+ }
91
+ set context(value: Context) {
92
+ this.setAttribute("context", value);
93
+ }
94
+
95
+ /**
96
+ * The culture used to format the relative time.
97
+ */
98
+ get culture(): Intl.Locale {
99
+ const value = this.getAttribute("culture") ?? "";
100
+ return new Intl.Locale(value.trim() || navigator.language);
101
+ }
102
+ set culture(value: Intl.Locale) {
103
+ this.setAttribute("culture", value.toString());
104
+ }
105
+
106
+ /**
107
+ * The delay, in milliseconds, to hide the notification.
108
+ */
109
+ get delay(): number {
110
+ const value = Number(this.getAttribute("delay"));
111
+ return Math.max(1, Number.isNaN(value) ? 5_000 : value);
112
+ }
113
+ set delay(value: number) {
114
+ this.setAttribute("delay", value.toString());
115
+ }
116
+
117
+ /**
118
+ * The time elapsed since this component was initially shown, in milliseconds.
119
+ */
120
+ get elapsedTime(): number {
121
+ return Date.now() - this.#initialTime;
122
+ }
123
+
124
+ /**
125
+ * The icon displayed next to the caption.
126
+ */
127
+ get icon(): string {
128
+ const value = this.getAttribute("icon") ?? "";
129
+ return value.trim() || getIcon(Context.Info);
130
+ }
131
+ set icon(value: string) {
132
+ this.setAttribute("icon", value);
133
+ }
134
+
135
+ /**
136
+ * Method invoked when an attribute has been changed.
137
+ * @param attribute The attribute name.
138
+ * @param oldValue The previous attribute value.
139
+ * @param newValue The new attribute value.
140
+ */
141
+ attributeChangedCallback(attribute: string, oldValue: string|null, newValue: string|null): void {
142
+ if (newValue != oldValue) switch (attribute) {
143
+ case "caption":
144
+ this.#updateCaption(newValue ?? "");
145
+ break;
146
+ case "context":
147
+ this.#updateContext(Object.values(Context).includes(newValue as Context) ? newValue as Context : Context.Info);
148
+ break;
149
+ case "culture":
150
+ this.#formatter = new Intl.RelativeTimeFormat((newValue ?? "").trim() || navigator.language, {style: "long"});
151
+ break;
152
+ case "icon":
153
+ this.#updateIcon(newValue ?? "")
154
+ break;
155
+ // No default
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Method invoked when this component is connected.
161
+ */
162
+ connectedCallback(): void {
163
+ const toast = this.querySelector(".toast")!;
164
+ toast.addEventListener("hidden.bs.toast", () => clearInterval(this.#timer));
165
+ toast.addEventListener("show.bs.toast", () => this.#timer = window.setInterval(this.#updateElapsedTime, Duration.Second));
166
+
167
+ const {animation, autoHide: autohide, delay} = this;
168
+ this.#toast = new BootstrapToast(toast, {animation, autohide, delay});
169
+ }
170
+
171
+ /**
172
+ * Method invoked when this component is disconnected.
173
+ */
174
+ disconnectedCallback(): void {
175
+ clearInterval(this.#timer);
176
+ this.#toast.dispose();
177
+ }
178
+
179
+ /**
180
+ * Hides this toast.
181
+ */
182
+ hide(): void {
183
+ this.#toast.hide();
184
+ }
185
+
186
+ /**
187
+ * Shows this toast.
188
+ */
189
+ show(): void {
190
+ if (!this.#toast.isShown()) {
191
+ this.#initialTime = Date.now();
192
+ this.#updateElapsedTime();
193
+ }
194
+
195
+ this.#toast.show();
196
+ }
197
+
198
+ /**
199
+ * Formats the specified elapsed time.
200
+ * @param elapsed The elapsed time, in seconds.
201
+ * @returns The formated time.
202
+ */
203
+ #formatTime(elapsed: number): string {
204
+ let index = 0;
205
+ while (elapsed > 60 && index < Toast.#timeUnits.length) {
206
+ elapsed /= 60;
207
+ index++;
208
+ }
209
+
210
+ return this.#formatter.format(Math.ceil(-elapsed), Toast.#timeUnits[index]);
211
+ }
212
+
213
+ /**
214
+ * Updates the title displayed in the header.
215
+ * @param value The new value.
216
+ */
217
+ #updateCaption(value: string): void {
218
+ this.#header.querySelector("b")!.textContent = value.trim();
219
+ }
220
+
221
+ /**
222
+ * Updates the title displayed in the header.
223
+ * @param value The new value.
224
+ */
225
+ #updateContext(value: Context): void {
226
+ const contexts = Object.values(Context);
227
+
228
+ let {classList} = this.#header;
229
+ classList.remove(...contexts.map(context => `toast-header-${context}`));
230
+ classList.add(`toast-header-${value}`);
231
+
232
+ ({classList} = this.#header.querySelector(".icon")!);
233
+ classList.remove(...contexts.map(context => `text-${context}`));
234
+ classList.add(`text-${value}`);
235
+ }
236
+
237
+ /**
238
+ * Updates the label corresponding to the elapsed time.
239
+ */
240
+ readonly #updateElapsedTime: () => void = () => {
241
+ const {elapsedTime} = this;
242
+ this.#header.querySelector("small")!.textContent = elapsedTime > 0 ? this.#formatTime(elapsedTime / Duration.Second) : "";
243
+ };
244
+
245
+ /**
246
+ * Updates the icon displayed next to the caption.
247
+ * @param value The new value.
248
+ */
249
+ #updateIcon(value: string): void {
250
+ this.#header.querySelector(".icon")!.textContent = value.trim() || getIcon(Context.Info);
251
+ }
252
+ }
File without changes
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Creates a document fragment from the specified HTML string.
3
+ * @param childContent The HTML string providing the child content.
4
+ * @returns The document fragment corresponding to the specified HTML string.
5
+ */
6
+ export function createDocumentFragment(childContent: string): DocumentFragment {
7
+ const template = document.createElement("template");
8
+ template.innerHTML = childContent;
9
+ return template.content;
10
+ }
11
+
12
+ /**
13
+ * Returns a promise that resolves when the specified element has finished all its animations.
14
+ * @param element The target element.
15
+ * @returns The element animations.
16
+ */
17
+ export function waitForAnimations(element: Element): Promise<Array<PromiseSettledResult<Animation>>> {
18
+ return Promise.allSettled(element.getAnimations().map(animation => animation.finished));
19
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Defines the placement of an element.
3
+ */
4
+ export const Position = Object.freeze({
5
+
6
+ /**
7
+ * Top left.
8
+ */
9
+ TopLeft: "TopLeft",
10
+
11
+ /**
12
+ * Top center.
13
+ */
14
+ TopCenter: "TopCenter",
15
+
16
+ /**
17
+ * Top right.
18
+ */
19
+ TopRight: "TopRight",
20
+
21
+ /**
22
+ * Middle left.
23
+ */
24
+ MiddleLeft: "MiddleLeft",
25
+
26
+ /**
27
+ * Middle center.
28
+ */
29
+ MiddleCenter: "MiddleCenter",
30
+
31
+ /**
32
+ * Middle right.
33
+ */
34
+ MiddleRight: "MiddleRight",
35
+
36
+ /**
37
+ * Bottom left.
38
+ */
39
+ BottomLeft: "BottomLeft",
40
+
41
+ /**
42
+ * Bottom center.
43
+ */
44
+ BottomCenter: "BottomCenter",
45
+
46
+ /**
47
+ * Bottom right.
48
+ */
49
+ BottomRight: "BottomRight"
50
+ });
51
+
52
+ /**
53
+ * Defines the placement of an element.
54
+ */
55
+ export type Position = typeof Position[keyof typeof Position];
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Defines the size of a component.
3
+ */
4
+ export const Size = Object.freeze({
5
+
6
+ /**
7
+ * An extra small size.
8
+ */
9
+ ExtraSmall: "xs",
10
+
11
+ /**
12
+ * A small size.
13
+ */
14
+ Small: "sm",
15
+
16
+ /**
17
+ * A medium size.
18
+ */
19
+ Medium: "md",
20
+
21
+ /**
22
+ * A large size.
23
+ */
24
+ Large: "lg",
25
+
26
+ /**
27
+ * An extra large size.
28
+ */
29
+ ExtraLarge: "xl",
30
+
31
+ /**
32
+ * An extra extra large size.
33
+ */
34
+ ExtraExtraLarge: "xxl"
35
+ });
36
+
37
+ /**
38
+ * Defines the size of a component.
39
+ */
40
+ export type Size = typeof Size[keyof typeof Size];
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Defines tone variants.
3
+ */
4
+ export const Variant = Object.freeze({
5
+
6
+ /**
7
+ * A dark variant.
8
+ */
9
+ Dark: "dark",
10
+
11
+ /**
12
+ * A light variant.
13
+ */
14
+ Light: "light",
15
+
16
+ /**
17
+ * A primary variant.
18
+ */
19
+ Primary: "primary",
20
+
21
+ /**
22
+ * A secondary variant.
23
+ */
24
+ Secondary: "secondary"
25
+ });
26
+
27
+ /**
28
+ * Defines tone variants.
29
+ */
30
+ export type Variant = typeof Variant[keyof typeof Variant];
@@ -44,7 +44,7 @@ export class ViewportScroller {
44
44
  this.#scrollOffset += (navbar?.offsetHeight ?? 0);
45
45
  }
46
46
 
47
- const actionBar = document.body.querySelector<HTMLElement>("action-bar, .action-bar");
47
+ const actionBar = document.body.querySelector<HTMLElement>("action-bar");
48
48
  return this.#scrollOffset + (actionBar?.offsetHeight ?? 0);
49
49
  }
50
50