cx 24.4.6 → 24.4.7

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/src/ui/Culture.js CHANGED
@@ -1,129 +1,129 @@
1
- import { NumberCulture, DateTimeCulture } from "intl-io";
2
- import { Localization } from "./Localization";
3
- import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
4
- import { invalidateExpressionCache } from "../data/Expression";
5
- import { invalidateStringTemplateCache } from "../data/StringTemplate";
6
- import { defaultCompare } from "../data/defaultCompare";
7
- import { Console } from "../util/Console";
8
-
9
- let stack = [
10
- {
11
- culture: "en",
12
- numberCulture: null,
13
- dateTimeCulture: null,
14
- cache: {},
15
- defaultCurrency: "USD",
16
- dateEncoding: (date) => date.toISOString(),
17
- },
18
- ];
19
-
20
- export function getDefaultCulture() {
21
- return stack[0];
22
- }
23
-
24
- export function getCurrentCulture() {
25
- return stack[stack.length - 1];
26
- }
27
-
28
- export function getCurrentCultureCache() {
29
- return getCurrentCulture().cache;
30
- }
31
-
32
- export function pushCulture(cultureInfo) {
33
- stack.push(cultureInfo);
34
- }
35
-
36
- export function createCulture(cultureSpecs) {
37
- let current = getCurrentCulture();
38
- let info = {
39
- culture: current.culture,
40
- dateEncoding: current.dateEncoding,
41
- defaultCurrency: current.defaultCurrency,
42
- cache: {},
43
- };
44
- for (let key in cultureSpecs) {
45
- if (!cultureSpecs[key]) continue;
46
- info[key] = cultureSpecs[key];
47
- }
48
- return info;
49
- }
50
-
51
- export function popCulture(cultureSpecs) {
52
- if (stack.length == 1) throw new Error("Cannot pop the last culture object.");
53
- if (cultureSpecs && stack[stack.length - 1] !== cultureSpecs) {
54
- Console.warn("Popped culture object does not match the current one.");
55
- }
56
- return stack.pop();
57
- }
58
-
59
- export class Culture {
60
- static setCulture(cultureCode) {
61
- let cultureSpecs = getDefaultCulture();
62
- cultureSpecs.culture = cultureCode;
63
- cultureSpecs.cache = {};
64
- Localization.setCulture(cultureCode);
65
- this.invalidateCache();
66
- }
67
-
68
- static setNumberCulture(cultureCode) {
69
- let cultureSpecs = getDefaultCulture();
70
- cultureSpecs.numberCulture = cultureCode;
71
- delete cultureSpecs.cache.numberCulture;
72
- this.invalidateCache();
73
- }
74
-
75
- static setDateTimeCulture(cultureCode) {
76
- let cultureSpecs = getDefaultCulture();
77
- cultureSpecs.dateTimeCulture = cultureCode;
78
- delete cultureSpecs.cache.dateTimeCulture;
79
- this.invalidateCache();
80
- }
81
-
82
- static setDefaultCurrency(currencyCode) {
83
- let cultureSpecs = getDefaultCulture();
84
- cultureSpecs.defaultCurrency = currencyCode;
85
- this.invalidateCache();
86
- }
87
-
88
- static setDefaultDateEncoding(encoding) {
89
- let cultureSpecs = getDefaultCulture();
90
- cultureSpecs.dateEncoding = encoding;
91
- this.invalidateCache();
92
- }
93
-
94
- static invalidateCache() {
95
- GlobalCacheIdentifier.change();
96
- invalidateExpressionCache();
97
- invalidateStringTemplateCache();
98
- }
99
-
100
- static get defaultCurrency() {
101
- return getCurrentCulture().defaultCurrency;
102
- }
103
-
104
- static get culture() {
105
- return getCurrentCulture().culture;
106
- }
107
-
108
- static getNumberCulture() {
109
- let { cache, numberCulture, culture } = getCurrentCulture();
110
- if (!cache.numberCulture) cache.numberCulture = new NumberCulture(numberCulture ?? culture);
111
- return cache.numberCulture;
112
- }
113
-
114
- static getDateTimeCulture() {
115
- let { cache, dateTimeCulture, culture } = getCurrentCulture();
116
- if (!cache.dateTimeCulture) cache.dateTimeCulture = new DateTimeCulture(dateTimeCulture ?? culture);
117
- return cache.dateTimeCulture;
118
- }
119
-
120
- static getDefaultDateEncoding() {
121
- return getCurrentCulture().dateEncoding;
122
- }
123
-
124
- static getComparer(options) {
125
- let { culture } = getCurrentCulture();
126
- if (typeof Intl.Collator != "undefined") return new Intl.Collator(culture, options).compare;
127
- return defaultCompare;
128
- }
129
- }
1
+ import { NumberCulture, DateTimeCulture } from "intl-io";
2
+ import { Localization } from "./Localization";
3
+ import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
4
+ import { invalidateExpressionCache } from "../data/Expression";
5
+ import { invalidateStringTemplateCache } from "../data/StringTemplate";
6
+ import { defaultCompare } from "../data/defaultCompare";
7
+ import { Console } from "../util/Console";
8
+
9
+ let stack = [
10
+ {
11
+ culture: "en",
12
+ numberCulture: null,
13
+ dateTimeCulture: null,
14
+ cache: {},
15
+ defaultCurrency: "USD",
16
+ dateEncoding: (date) => date.toISOString(),
17
+ },
18
+ ];
19
+
20
+ export function getDefaultCulture() {
21
+ return stack[0];
22
+ }
23
+
24
+ export function getCurrentCulture() {
25
+ return stack[stack.length - 1];
26
+ }
27
+
28
+ export function getCurrentCultureCache() {
29
+ return getCurrentCulture().cache;
30
+ }
31
+
32
+ export function pushCulture(cultureInfo) {
33
+ stack.push(cultureInfo);
34
+ }
35
+
36
+ export function createCulture(cultureSpecs) {
37
+ let current = getCurrentCulture();
38
+ let info = {
39
+ culture: current.culture,
40
+ dateEncoding: current.dateEncoding,
41
+ defaultCurrency: current.defaultCurrency,
42
+ cache: {},
43
+ };
44
+ for (let key in cultureSpecs) {
45
+ if (!cultureSpecs[key]) continue;
46
+ info[key] = cultureSpecs[key];
47
+ }
48
+ return info;
49
+ }
50
+
51
+ export function popCulture(cultureSpecs) {
52
+ if (stack.length == 1) throw new Error("Cannot pop the last culture object.");
53
+ if (cultureSpecs && stack[stack.length - 1] !== cultureSpecs) {
54
+ Console.warn("Popped culture object does not match the current one.");
55
+ }
56
+ return stack.pop();
57
+ }
58
+
59
+ export class Culture {
60
+ static setCulture(cultureCode) {
61
+ let cultureSpecs = getDefaultCulture();
62
+ cultureSpecs.culture = cultureCode;
63
+ cultureSpecs.cache = {};
64
+ Localization.setCulture(cultureCode);
65
+ this.invalidateCache();
66
+ }
67
+
68
+ static setNumberCulture(cultureCode) {
69
+ let cultureSpecs = getDefaultCulture();
70
+ cultureSpecs.numberCulture = cultureCode;
71
+ delete cultureSpecs.cache.numberCulture;
72
+ this.invalidateCache();
73
+ }
74
+
75
+ static setDateTimeCulture(cultureCode) {
76
+ let cultureSpecs = getDefaultCulture();
77
+ cultureSpecs.dateTimeCulture = cultureCode;
78
+ delete cultureSpecs.cache.dateTimeCulture;
79
+ this.invalidateCache();
80
+ }
81
+
82
+ static setDefaultCurrency(currencyCode) {
83
+ let cultureSpecs = getDefaultCulture();
84
+ cultureSpecs.defaultCurrency = currencyCode;
85
+ this.invalidateCache();
86
+ }
87
+
88
+ static setDefaultDateEncoding(encoding) {
89
+ let cultureSpecs = getDefaultCulture();
90
+ cultureSpecs.dateEncoding = encoding;
91
+ this.invalidateCache();
92
+ }
93
+
94
+ static invalidateCache() {
95
+ GlobalCacheIdentifier.change();
96
+ invalidateExpressionCache();
97
+ invalidateStringTemplateCache();
98
+ }
99
+
100
+ static get defaultCurrency() {
101
+ return getCurrentCulture().defaultCurrency;
102
+ }
103
+
104
+ static get culture() {
105
+ return getCurrentCulture().culture;
106
+ }
107
+
108
+ static getNumberCulture() {
109
+ let { cache, numberCulture, culture } = getCurrentCulture();
110
+ if (!cache.numberCulture) cache.numberCulture = new NumberCulture(numberCulture ?? culture);
111
+ return cache.numberCulture;
112
+ }
113
+
114
+ static getDateTimeCulture() {
115
+ let { cache, dateTimeCulture, culture } = getCurrentCulture();
116
+ if (!cache.dateTimeCulture) cache.dateTimeCulture = new DateTimeCulture(dateTimeCulture ?? culture);
117
+ return cache.dateTimeCulture;
118
+ }
119
+
120
+ static getDefaultDateEncoding() {
121
+ return getCurrentCulture().dateEncoding;
122
+ }
123
+
124
+ static getComparer(options) {
125
+ let { culture } = getCurrentCulture();
126
+ if (typeof Intl.Collator != "undefined") return new Intl.Collator(culture, options).compare;
127
+ return defaultCompare;
128
+ }
129
+ }
@@ -1,171 +1,171 @@
1
- import { isSelfOrDescendant, findFirst, findFirstChild, isFocusable, closestParent } from "../util/DOM";
2
- import { batchUpdates } from "./batchUpdates";
3
- import { SubscriberList } from "../util/SubscriberList";
4
- import { isTouchEvent } from "../util/isTouchEvent";
5
- import { getActiveElement } from "../util/getActiveElement";
6
-
7
- /*
8
- * Purpose of FocusManager is to provide focusout notifications.
9
- * IE and Firefox do not provide relatedTarget info in blur events which makes it impossible
10
- * to determine if focus went outside or stayed inside the component.
11
- */
12
-
13
- let subscribers = new SubscriberList(),
14
- timerInterval = 300,
15
- timerId = null;
16
-
17
- let lastActiveElement = null;
18
- let pending = false;
19
-
20
- export class FocusManager {
21
- static subscribe(callback) {
22
- let unsubscribe = subscribers.subscribe(callback);
23
- checkTimer();
24
- return unsubscribe;
25
- }
26
-
27
- static onFocusOut(el, callback) {
28
- let active = isSelfOrDescendant(el, getActiveElement());
29
- return this.subscribe((focusedEl) => {
30
- if (!active) active = isSelfOrDescendant(el, getActiveElement());
31
- else if (!isSelfOrDescendant(el, focusedEl)) {
32
- active = false;
33
- callback(focusedEl);
34
- }
35
- });
36
- }
37
-
38
- static oneFocusOut(el, callback) {
39
- this.nudge();
40
- let off = this.subscribe((focusedEl) => {
41
- if (!isSelfOrDescendant(el, focusedEl)) {
42
- callback(focusedEl);
43
- off();
44
- }
45
- });
46
- return off;
47
- }
48
-
49
- static nudge() {
50
- if (typeof document !== "undefined" && getActiveElement() !== lastActiveElement) {
51
- if (!pending) {
52
- pending = true;
53
- setTimeout(function () {
54
- pending = false;
55
- if (getActiveElement() !== lastActiveElement) {
56
- lastActiveElement = getActiveElement();
57
- batchUpdates(() => {
58
- subscribers.notify(lastActiveElement);
59
- });
60
- checkTimer();
61
- }
62
- }, 0);
63
- }
64
- }
65
- }
66
-
67
- static focus(el) {
68
- el.focus();
69
- this.nudge();
70
- }
71
-
72
- static focusFirst(el) {
73
- let focusable = findFirst(el, isFocusable);
74
- if (focusable) this.focus(focusable);
75
- return focusable;
76
- }
77
-
78
- static focusFirstChild(el) {
79
- let focusable = findFirstChild(el, isFocusable);
80
- if (focusable) this.focus(focusable);
81
- return focusable;
82
- }
83
-
84
- static focusNext(el) {
85
- let next = el,
86
- skip = true;
87
- do {
88
- if (!skip) {
89
- let focusable = this.focusFirst(next);
90
- if (focusable) return focusable;
91
- }
92
-
93
- if (next.nextSibling) {
94
- next = next.nextSibling;
95
- skip = false;
96
- } else {
97
- next = next.parentNode;
98
- skip = true;
99
- }
100
- } while (next);
101
- }
102
-
103
- static setInterval(interval) {
104
- timerInterval = interval;
105
- checkTimer();
106
- }
107
- }
108
-
109
- export function oneFocusOut(component, el, callback) {
110
- if (!component.oneFocusOut)
111
- component.oneFocusOut = FocusManager.oneFocusOut(el, (focus) => {
112
- delete component.oneFocusOut;
113
- callback(focus);
114
- });
115
- }
116
-
117
- export function offFocusOut(component) {
118
- if (component.oneFocusOut) {
119
- component.oneFocusOut();
120
- delete component.oneFocusOut;
121
- }
122
- }
123
-
124
- export function preventFocus(e) {
125
- //Focus can be prevented only on mousedown event. On touchstart this will not work
126
- //preventDefault cannot be used as it prevents scrolling
127
- if (e.type !== "mousedown") return;
128
-
129
- e.preventDefault();
130
-
131
- unfocusElement(e.currentTarget, false);
132
- }
133
-
134
- function checkTimer() {
135
- let shouldRun = !subscribers.isEmpty();
136
-
137
- if (shouldRun && !timerId)
138
- timerId = setInterval(() => {
139
- FocusManager.nudge();
140
- }, timerInterval);
141
-
142
- if (!shouldRun && timerId) {
143
- clearInterval(timerId);
144
- timerId = null;
145
- }
146
- }
147
-
148
- export function preventFocusOnTouch(e, force = false) {
149
- if (force || isTouchEvent()) preventFocus(e);
150
- }
151
-
152
- export function unfocusElement(target = null, unfocusParentOverlay = true) {
153
- const activeElement = getActiveElement();
154
- if (!target) target = activeElement;
155
-
156
- if (unfocusParentOverlay) {
157
- let focusableOverlayContainer = closestParent(target, (el) => el.dataset?.focusableOverlayContainer);
158
- if (focusableOverlayContainer) target = focusableOverlayContainer;
159
- }
160
-
161
- //find the closest focusable parent of the target element and focus it instead
162
- let focusableParent = closestParent(
163
- target,
164
- (el) => isFocusable(el) && (!unfocusParentOverlay || el.dataset?.focusableOverlayContainer)
165
- );
166
-
167
- if (focusableParent && focusableParent !== document.body) focusableParent.focus();
168
- else activeElement.blur();
169
-
170
- FocusManager.nudge();
171
- }
1
+ import { isSelfOrDescendant, findFirst, findFirstChild, isFocusable, closestParent } from "../util/DOM";
2
+ import { batchUpdates } from "./batchUpdates";
3
+ import { SubscriberList } from "../util/SubscriberList";
4
+ import { isTouchEvent } from "../util/isTouchEvent";
5
+ import { getActiveElement } from "../util/getActiveElement";
6
+
7
+ /*
8
+ * Purpose of FocusManager is to provide focusout notifications.
9
+ * IE and Firefox do not provide relatedTarget info in blur events which makes it impossible
10
+ * to determine if focus went outside or stayed inside the component.
11
+ */
12
+
13
+ let subscribers = new SubscriberList(),
14
+ timerInterval = 300,
15
+ timerId = null;
16
+
17
+ let lastActiveElement = null;
18
+ let pending = false;
19
+
20
+ export class FocusManager {
21
+ static subscribe(callback) {
22
+ let unsubscribe = subscribers.subscribe(callback);
23
+ checkTimer();
24
+ return unsubscribe;
25
+ }
26
+
27
+ static onFocusOut(el, callback) {
28
+ let active = isSelfOrDescendant(el, getActiveElement());
29
+ return this.subscribe((focusedEl) => {
30
+ if (!active) active = isSelfOrDescendant(el, getActiveElement());
31
+ else if (!isSelfOrDescendant(el, focusedEl)) {
32
+ active = false;
33
+ callback(focusedEl);
34
+ }
35
+ });
36
+ }
37
+
38
+ static oneFocusOut(el, callback) {
39
+ this.nudge();
40
+ let off = this.subscribe((focusedEl) => {
41
+ if (!isSelfOrDescendant(el, focusedEl)) {
42
+ callback(focusedEl);
43
+ off();
44
+ }
45
+ });
46
+ return off;
47
+ }
48
+
49
+ static nudge() {
50
+ if (typeof document !== "undefined" && getActiveElement() !== lastActiveElement) {
51
+ if (!pending) {
52
+ pending = true;
53
+ setTimeout(function () {
54
+ pending = false;
55
+ if (getActiveElement() !== lastActiveElement) {
56
+ lastActiveElement = getActiveElement();
57
+ batchUpdates(() => {
58
+ subscribers.notify(lastActiveElement);
59
+ });
60
+ checkTimer();
61
+ }
62
+ }, 0);
63
+ }
64
+ }
65
+ }
66
+
67
+ static focus(el) {
68
+ el.focus();
69
+ this.nudge();
70
+ }
71
+
72
+ static focusFirst(el) {
73
+ let focusable = findFirst(el, isFocusable);
74
+ if (focusable) this.focus(focusable);
75
+ return focusable;
76
+ }
77
+
78
+ static focusFirstChild(el) {
79
+ let focusable = findFirstChild(el, isFocusable);
80
+ if (focusable) this.focus(focusable);
81
+ return focusable;
82
+ }
83
+
84
+ static focusNext(el) {
85
+ let next = el,
86
+ skip = true;
87
+ do {
88
+ if (!skip) {
89
+ let focusable = this.focusFirst(next);
90
+ if (focusable) return focusable;
91
+ }
92
+
93
+ if (next.nextSibling) {
94
+ next = next.nextSibling;
95
+ skip = false;
96
+ } else {
97
+ next = next.parentNode;
98
+ skip = true;
99
+ }
100
+ } while (next);
101
+ }
102
+
103
+ static setInterval(interval) {
104
+ timerInterval = interval;
105
+ checkTimer();
106
+ }
107
+ }
108
+
109
+ export function oneFocusOut(component, el, callback) {
110
+ if (!component.oneFocusOut)
111
+ component.oneFocusOut = FocusManager.oneFocusOut(el, (focus) => {
112
+ delete component.oneFocusOut;
113
+ callback(focus);
114
+ });
115
+ }
116
+
117
+ export function offFocusOut(component) {
118
+ if (component.oneFocusOut) {
119
+ component.oneFocusOut();
120
+ delete component.oneFocusOut;
121
+ }
122
+ }
123
+
124
+ export function preventFocus(e) {
125
+ //Focus can be prevented only on mousedown event. On touchstart this will not work
126
+ //preventDefault cannot be used as it prevents scrolling
127
+ if (e.type !== "mousedown") return;
128
+
129
+ e.preventDefault();
130
+
131
+ unfocusElement(e.currentTarget, false);
132
+ }
133
+
134
+ function checkTimer() {
135
+ let shouldRun = !subscribers.isEmpty();
136
+
137
+ if (shouldRun && !timerId)
138
+ timerId = setInterval(() => {
139
+ FocusManager.nudge();
140
+ }, timerInterval);
141
+
142
+ if (!shouldRun && timerId) {
143
+ clearInterval(timerId);
144
+ timerId = null;
145
+ }
146
+ }
147
+
148
+ export function preventFocusOnTouch(e, force = false) {
149
+ if (force || isTouchEvent()) preventFocus(e);
150
+ }
151
+
152
+ export function unfocusElement(target = null, unfocusParentOverlay = true) {
153
+ const activeElement = getActiveElement();
154
+ if (!target) target = activeElement;
155
+
156
+ if (unfocusParentOverlay) {
157
+ let focusableOverlayContainer = closestParent(target, (el) => el.dataset?.focusableOverlayContainer);
158
+ if (focusableOverlayContainer) target = focusableOverlayContainer;
159
+ }
160
+
161
+ //find the closest focusable parent of the target element and focus it instead
162
+ let focusableParent = closestParent(
163
+ target,
164
+ (el) => isFocusable(el) && (!unfocusParentOverlay || el.dataset?.focusableOverlayContainer)
165
+ );
166
+
167
+ if (focusableParent && focusableParent !== document.body) focusableParent.focus();
168
+ else activeElement.blur();
169
+
170
+ FocusManager.nudge();
171
+ }