@fluid-topics/ft-wc-utils 1.3.0 → 1.3.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.
@@ -5,26 +5,32 @@ export declare class ClearCacheEvent extends CustomEvent<{
5
5
  }> {
6
6
  constructor(clearedKeys: string[]);
7
7
  }
8
- export type CachedContent<T = any> = undefined | CancelablePromise<T> | T | Error;
9
- export declare class CacheRegistry<T = any> extends WithEventBus {
10
- private loaders;
8
+ type CachedContentStatus = "REGISTERED" | "LOADING" | "RESOLVED" | "ERROR";
9
+ export type CachedContent = {
10
+ loader: () => Promise<any>;
11
+ final: boolean;
12
+ status: CachedContentStatus;
13
+ promise?: CancelablePromise<any>;
14
+ value?: any;
15
+ error?: Error;
16
+ clearTimeout?: number;
17
+ };
18
+ export declare class CacheRegistry extends WithEventBus {
11
19
  private content;
12
- private clearTimeouts;
13
- private finalContent;
14
- register(key: string, loader: () => Promise<T>): void;
15
- registerFinal(key: string, loader: () => Promise<T>): void;
16
- clearAll(dispatchEvent?: boolean): void;
20
+ register(key: string, loader: () => Promise<any>, final?: boolean): void;
21
+ registerFinal(key: string, loader: () => Promise<any>): void;
22
+ clearAll(dispatchEvent?: boolean): string[];
17
23
  clear(key: string, dispatchEvent?: boolean): string | undefined;
18
24
  private forceClear;
19
25
  private clearClearTimeout;
20
- set(key: string, value: T): void;
21
- setFinal(key: string, value: T): void;
22
- get<U = T>(key: string, loader?: () => Promise<U>, ttl?: number): Promise<U>;
23
- private isResolvedValue;
24
- getNow<U = T>(key: string): U | undefined;
26
+ set(key: string, value: any, final?: boolean): void;
27
+ setFinal(key: string, value: any): void;
28
+ get<U = any>(key: string, loader?: () => Promise<U>, ttl?: number): Promise<U>;
29
+ isRegistered(key: string): boolean;
30
+ getNow<U = any>(key: string): U | undefined;
25
31
  has(key: string): boolean;
26
32
  resolvedKeys(): Array<string>;
27
- resolvedValues(): Array<T>;
33
+ resolvedValues<U = any>(): Array<U>;
28
34
  keys(): Array<string>;
29
- values(): Array<CachedContent<T>>;
30
35
  }
36
+ export {};
@@ -1,4 +1,4 @@
1
- import { cancelable, CancelablePromise } from "./CancelablePromise";
1
+ import { cancelable } from "./CancelablePromise";
2
2
  import { WithEventBus } from "./events";
3
3
  export class ClearCacheEvent extends CustomEvent {
4
4
  constructor(clearedKeys) {
@@ -8,22 +8,21 @@ export class ClearCacheEvent extends CustomEvent {
8
8
  export class CacheRegistry extends WithEventBus {
9
9
  constructor() {
10
10
  super(...arguments);
11
- this.loaders = {};
12
11
  this.content = {};
13
- this.clearTimeouts = {};
14
- this.finalContent = new Set();
15
12
  }
16
- register(key, loader) {
17
- this.loaders[key] = loader;
18
- this.finalContent.delete(key);
13
+ register(key, loader, final = false) {
14
+ var _a;
15
+ const content = this.content[key];
16
+ const status = (_a = content === null || content === void 0 ? void 0 : content.status) !== null && _a !== void 0 ? _a : "REGISTERED";
17
+ this.content[key] = { ...content, loader, final, status };
19
18
  }
20
19
  registerFinal(key, loader) {
21
- this.loaders[key] = loader;
22
- this.finalContent.add(key);
20
+ this.forceClear(key); // Force the final content to be reloaded when the loader changes
21
+ this.register(key, loader, true);
23
22
  }
24
23
  clearAll(dispatchEvent = true) {
25
24
  const clearedKeys = [];
26
- for (let key in this.content) {
25
+ for (const key in this.content) {
27
26
  if (this.clear(key, false) != undefined) {
28
27
  clearedKeys.push(key);
29
28
  }
@@ -31,9 +30,10 @@ export class CacheRegistry extends WithEventBus {
31
30
  if (dispatchEvent && clearedKeys.length > 0) {
32
31
  this.dispatchEvent(new ClearCacheEvent(clearedKeys));
33
32
  }
33
+ return clearedKeys;
34
34
  }
35
35
  clear(key, dispatchEvent = true) {
36
- if (!this.finalContent.has(key)) {
36
+ if (this.content[key] && !this.content[key].final) {
37
37
  this.forceClear(key);
38
38
  if (dispatchEvent) {
39
39
  this.dispatchEvent(new ClearCacheEvent([key]));
@@ -43,73 +43,91 @@ export class CacheRegistry extends WithEventBus {
43
43
  return undefined;
44
44
  }
45
45
  forceClear(key) {
46
- this.clearClearTimeout(key);
47
- if (this.content[key] instanceof CancelablePromise) {
48
- this.content[key].cancel();
46
+ var _a;
47
+ const content = this.content[key];
48
+ if (content) {
49
+ this.clearClearTimeout(key);
50
+ (_a = content.promise) === null || _a === void 0 ? void 0 : _a.cancel();
51
+ this.content[key] = {
52
+ loader: content.loader,
53
+ final: content.final,
54
+ status: "REGISTERED",
55
+ };
49
56
  }
50
- delete this.content[key];
51
57
  }
52
58
  clearClearTimeout(key) {
53
- if (this.clearTimeouts[key] != null) {
54
- window.clearTimeout(this.clearTimeouts[key]);
55
- delete this.clearTimeouts[key];
59
+ var _a, _b, _c;
60
+ if (((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.clearTimeout) != null) {
61
+ window.clearTimeout((_b = this.content[key]) === null || _b === void 0 ? void 0 : _b.clearTimeout);
62
+ (_c = this.content[key]) === null || _c === void 0 ? true : delete _c.clearTimeout;
56
63
  }
57
64
  }
58
- set(key, value) {
65
+ set(key, value, final = false) {
59
66
  this.forceClear(key);
60
- this.register(key, async () => value);
61
- this.content[key] = value;
67
+ const loader = async () => value;
68
+ const status = "RESOLVED";
69
+ this.content[key] = { loader, final, status, value };
62
70
  }
63
71
  setFinal(key, value) {
64
- this.forceClear(key);
65
- this.registerFinal(key, async () => value);
66
- this.content[key] = value;
72
+ return this.set(key, value, true);
67
73
  }
68
74
  async get(key, loader, ttl) {
69
- if (this.content[key] === undefined) {
70
- loader = loader !== null && loader !== void 0 ? loader : this.loaders[key];
71
- if (loader == undefined) {
75
+ if (!this.isRegistered(key)) {
76
+ if (!loader) {
72
77
  throw new Error("Unknown cache key " + key);
73
78
  }
74
- const cancelablePromise = cancelable(loader());
75
- this.content[key] = cancelablePromise;
76
- return cancelablePromise
77
- .then(v => {
78
- this.content[key] = v;
79
- if (ttl != null) {
80
- this.clearClearTimeout(key);
81
- this.clearTimeouts[key] = window.setTimeout(() => this.clear(key), ttl);
82
- }
83
- return v;
84
- });
79
+ this.register(key, loader);
85
80
  }
86
- if (this.content[key] instanceof Error) {
87
- throw this.content[key];
81
+ const content = this.content[key];
82
+ switch (content.status) {
83
+ case "ERROR":
84
+ throw content.error;
85
+ case "LOADING":
86
+ return content.promise;
87
+ case "RESOLVED":
88
+ return content.value;
89
+ case "REGISTERED":
90
+ loader = loader !== null && loader !== void 0 ? loader : content.loader;
91
+ content.status = "LOADING";
92
+ content.promise = cancelable(loader());
93
+ return content.promise
94
+ .then(value => {
95
+ content.status = "RESOLVED";
96
+ content.value = value;
97
+ if (ttl != null) {
98
+ this.clearClearTimeout(key);
99
+ content.clearTimeout = window.setTimeout(() => this.clear(key), ttl);
100
+ }
101
+ return value;
102
+ })
103
+ .catch(error => {
104
+ var _a;
105
+ if (!((_a = content.promise) === null || _a === void 0 ? void 0 : _a.isCanceled)) {
106
+ content.status = "ERROR";
107
+ content.error = error;
108
+ }
109
+ throw error;
110
+ });
88
111
  }
89
- return this.content[key];
90
112
  }
91
- isResolvedValue(value) {
92
- return value != null && !(value instanceof Promise) && !(value instanceof Error);
113
+ isRegistered(key) {
114
+ return this.content[key] != undefined;
93
115
  }
94
116
  getNow(key) {
95
- if (this.isResolvedValue(this.content[key])) {
96
- return this.content[key];
97
- }
98
- return undefined;
117
+ var _a;
118
+ return (_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.value;
99
119
  }
100
120
  has(key) {
101
- return this.content[key] != null;
121
+ var _a, _b;
122
+ return ((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.status) === "RESOLVED" || ((_b = this.content[key]) === null || _b === void 0 ? void 0 : _b.status) === "LOADING";
102
123
  }
103
124
  resolvedKeys() {
104
- return Object.keys(this.content).filter(key => this.isResolvedValue(this.content[key]));
125
+ return Object.keys(this.content).filter(key => { var _a; return ((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.status) === "RESOLVED"; });
105
126
  }
106
127
  resolvedValues() {
107
- return Object.values(this.content).filter(value => this.isResolvedValue(value));
128
+ return this.resolvedKeys().map(key => this.content[key].value);
108
129
  }
109
130
  keys() {
110
131
  return Object.keys(this.content);
111
132
  }
112
- values() {
113
- return Object.values(this.content);
114
- }
115
133
  }
@@ -72,7 +72,7 @@ export class SearchPlaceConverter {
72
72
  params.append(SearchPlaceQueryParams.QUERY, request.query);
73
73
  }
74
74
  // Only extract non-negative valueFilter since they are not supported by URLSearchParams
75
- const valueFilters = (_c = (_b = request.metadataFilters) === null || _b === void 0 ? void 0 : _b.filter(f => { var _a; return ((_a = f.valueFilter) === null || _a === void 0 ? void 0 : _a.negative) === false; })) !== null && _c !== void 0 ? _c : [];
75
+ const valueFilters = (_c = (_b = request.metadataFilters) === null || _b === void 0 ? void 0 : _b.filter(f => f.valueFilter && !f.valueFilter.negative)) !== null && _c !== void 0 ? _c : [];
76
76
  if (valueFilters.length > 0) {
77
77
  const serializedValues = valueFilters.map(f => {
78
78
  var _a, _b;
@@ -109,7 +109,7 @@ export class SearchPlaceConverter {
109
109
  params.append(SearchPlaceQueryParams.PER_PAGE, String(request.paging.perPage));
110
110
  }
111
111
  const otherQueryParams = (_m = request.otherQueryParams) !== null && _m !== void 0 ? _m : {};
112
- for (let key in otherQueryParams) {
112
+ for (const key in otherQueryParams) {
113
113
  params.append(key, otherQueryParams[key]);
114
114
  }
115
115
  // Use encodeURIComponent instead of URLSearchParams.toString() to encode the result
@@ -148,7 +148,7 @@ export class SearchPlaceConverter {
148
148
  const scope = (_c = params.get(SearchPlaceQueryParams.SCOPE)) !== null && _c !== void 0 ? _c : "default";
149
149
  const perPage = +((_d = params.get(SearchPlaceQueryParams.PER_PAGE)) !== null && _d !== void 0 ? _d : this.defaultPerPage);
150
150
  const otherQueryParams = {};
151
- for (let key of params.keys()) {
151
+ for (const key of params.keys()) {
152
152
  if (!isSearchPlaceQueryParams(key)) {
153
153
  otherQueryParams[key] = params.get(key);
154
154
  }
@@ -163,7 +163,7 @@ export class SearchPlaceConverter {
163
163
  facets: [],
164
164
  paging: {
165
165
  page: 1,
166
- perPage: minmax(1, isNaN(perPage) ? this.defaultPerPage : perPage, 1000)
166
+ perPage: minmax(1, isNaN(perPage) ? this.defaultPerPage : perPage, 1000),
167
167
  },
168
168
  otherQueryParams,
169
169
  };
@@ -177,7 +177,7 @@ export class SearchPlaceConverter {
177
177
  if ((_b = params.has(SearchPlaceQueryParams.PERIOD)) !== null && _b !== void 0 ? _b : "") {
178
178
  legacyFilters = [
179
179
  ...legacyFilters,
180
- ...this.parseLegacyPeriodFilter(((_c = params.get(SearchPlaceQueryParams.PERIOD)) !== null && _c !== void 0 ? _c : "").toUpperCase())
180
+ ...this.parseLegacyPeriodFilter(((_c = params.get(SearchPlaceQueryParams.PERIOD)) !== null && _c !== void 0 ? _c : "").toUpperCase()),
181
181
  ];
182
182
  }
183
183
  return [
@@ -203,8 +203,8 @@ export class SearchPlaceConverter {
203
203
  values: oneFilterSplit[1].split("_")
204
204
  .map(unquote)
205
205
  .map(value => this.unescapeFilterValue(value)),
206
- negative: false
207
- }
206
+ negative: false,
207
+ },
208
208
  }));
209
209
  }
210
210
  parseDateFilters(s) {
@@ -215,8 +215,8 @@ export class SearchPlaceConverter {
215
215
  .map(oneFilterSplit => ({
216
216
  key: oneFilterSplit[0],
217
217
  dateFilter: {
218
- type: FtDateFilterType[oneFilterSplit[1].toUpperCase()]
219
- }
218
+ type: FtDateFilterType[oneFilterSplit[1].toUpperCase()],
219
+ },
220
220
  }));
221
221
  }
222
222
  parseRangeFilters(s) {
@@ -228,24 +228,24 @@ export class SearchPlaceConverter {
228
228
  key: oneFilterSplit[0],
229
229
  rangeFilter: {
230
230
  from: oneFilterSplit[1].split("_")[0],
231
- to: oneFilterSplit[1].split("_")[1]
232
- }
231
+ to: oneFilterSplit[1].split("_")[1],
232
+ },
233
233
  }));
234
234
  }
235
235
  escapeFilters(s) {
236
- for (let [c, escaped, superEscaped] of this.filtersEscapeMapping) {
236
+ for (const [c, escaped, _] of this.filtersEscapeMapping) {
237
237
  s = s.replaceAll(c, escaped);
238
238
  }
239
239
  return s;
240
240
  }
241
241
  superEscapeFilters(s) {
242
- for (let [c, escaped, superEscaped] of this.filtersEscapeMapping) {
242
+ for (const [_, escaped, superEscaped] of this.filtersEscapeMapping) {
243
243
  s = s.replaceAll(escaped, superEscaped);
244
244
  }
245
245
  return s;
246
246
  }
247
247
  unescapeFilterValue(value) {
248
- for (let [c, escaped, superEscaped] of this.filtersEscapeMapping) {
248
+ for (const [c, _, superEscaped] of this.filtersEscapeMapping) {
249
249
  value = value.replace(superEscaped, c);
250
250
  }
251
251
  return value;
@@ -256,9 +256,9 @@ export class SearchPlaceConverter {
256
256
  {
257
257
  key: "ft:lastEdition",
258
258
  dateFilter: {
259
- type: FtDateFilterType[period]
260
- }
261
- }
259
+ type: FtDateFilterType[period],
260
+ },
261
+ },
262
262
  ];
263
263
  }
264
264
  if (period.startsWith("CUSTOM_")) {
@@ -268,9 +268,9 @@ export class SearchPlaceConverter {
268
268
  key: "ft:lastEdition",
269
269
  rangeFilter: {
270
270
  from: from,
271
- to: to
272
- }
273
- }
271
+ to: to,
272
+ },
273
+ },
274
274
  ];
275
275
  }
276
276
  return [];
@@ -1,6 +1,7 @@
1
1
  import { Constructor } from "../generic-types";
2
2
  import { ValidationRule } from "./ValidationRules";
3
3
  import { FtLitElement } from "../FtLitElement";
4
+ import { TemplateResult } from "lit";
4
5
  export type FtInputInterface<V> = {
5
6
  warningRules: Array<ValidationRule<V>>;
6
7
  errorRules: Array<ValidationRule<V>>;
@@ -23,6 +24,8 @@ export type FtInputInterface<V> = {
23
24
  onFocus(): void;
24
25
  onBlur(): void;
25
26
  triggerValidation(): void;
27
+ renderMessagesForScreenReader(): TemplateResult;
28
+ messagesForScreenReaderElementId: string;
26
29
  };
27
30
  export declare enum ValidateOn {
28
31
  blur = "blur",
@@ -4,7 +4,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { property, state, } from "lit/decorators.js";
7
+ import { property, state } from "lit/decorators.js";
8
+ import { repeat } from "lit/directives/repeat.js";
9
+ import { when } from "lit/directives/when.js";
10
+ import { html } from "lit";
8
11
  export var ValidateOn;
9
12
  (function (ValidateOn) {
10
13
  ValidateOn["blur"] = "blur";
@@ -67,6 +70,25 @@ export function toFtInput(ElementClass) {
67
70
  this.status = this.resolveStatus();
68
71
  this.dispatchEvent(new InputValidationEvent(this.status));
69
72
  }
73
+ get messagesForScreenReaderElementId() {
74
+ return "sr-helper-text-list";
75
+ }
76
+ renderMessagesForScreenReader() {
77
+ return html `
78
+ <ul aria-live="polite" id="${this.messagesForScreenReaderElementId}"
79
+ style="position:absolute;left:-10000px;top:auto;width:1px;height:1px;overflow:hidden;list-style:none;">
80
+ ${repeat(this.errorMessages, (v) => html `
81
+ <li>${v}</li>
82
+ `)}
83
+ ${repeat(this.warningMessages, (v) => html `
84
+ <li>${v}</li>
85
+ `)}
86
+ ${when((!this.errorMessages.length && !this.warningMessages.length && this.helperText), () => html `
87
+ <li>${this.helperText}</li>
88
+ `)}
89
+ </ul>
90
+ `;
91
+ }
70
92
  resolveStatus() {
71
93
  return this.forceStatus ? this.forceStatus :
72
94
  this.errorMessages.length ? Status.error :