cx 24.3.2 → 24.3.3

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.
@@ -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
+ }
@@ -1,72 +1,72 @@
1
- import { Record } from "../core";
2
- import { RenderingContext } from "./RenderingContext";
3
- import { View } from "../data/View";
4
- import { Widget } from "./Widget";
5
-
6
- export class Instance<ViewModel = any, Controller = any> {
7
- store: View<ViewModel>;
8
- data: Record;
9
- widget: Widget;
10
- key: number;
11
- id: string;
12
- controller: Controller;
13
- parentOptions: any;
14
- children?: Instance[];
15
-
16
- constructor(widget: Widget, key: string | number, parent?: Instance, store?: View);
17
-
18
- setStore(store: View): void;
19
-
20
- init(context: RenderingContext): void;
21
-
22
- explore(context: RenderingContext): boolean;
23
-
24
- prepare(context: RenderingContext): void;
25
-
26
- cleanup(context: RenderingContext): void;
27
-
28
- render(context: RenderingContext, keyPrefix?: string): JSX.Element | void;
29
-
30
- setState(state: Cx.Record): void;
31
-
32
- set(prop: string, value: any, internal?: boolean);
33
-
34
- definePropertySetter(prop: string, setter: (value: any) => void): boolean;
35
-
36
- /**
37
- * @protected
38
- * @param prop
39
- * @param value
40
- * @returns {boolean}
41
- */
42
- protected doSet(prop: string, value: any): boolean;
43
-
44
- replaceState(state: Cx.Config);
45
-
46
- getInstanceCache(): InstanceCache;
47
-
48
- clearChildrenCache();
49
-
50
- getChild(context: RenderingContext | null, widget: Widget, keyPrefix?: string, store?: View): Instance;
51
-
52
- getDetachedChild(widget: Widget, key: number | string, store?: View): Instance;
53
-
54
- // TODO: check return type
55
- prepareRenderCleanupChild(widget: Widget, store?: View, keyPrefix?: string, options?: object): JSX.Element | void;
56
-
57
- getJsxEventProps(): Cx.Config;
58
-
59
- nestedDataSet(key: string, value: any, dataConfig: Cx.Config): boolean;
60
-
61
- invokeControllerMethod(methodName: string, ...args: any[]);
62
- }
63
-
64
- export class InstanceCache {
65
- constructor(parent: Instance);
66
-
67
- getChild(widget: Widget, store: View, keyPrefix?: string): Instance;
68
-
69
- mark();
70
-
71
- sweep();
72
- }
1
+ import { Record } from "../core";
2
+ import { RenderingContext } from "./RenderingContext";
3
+ import { View } from "../data/View";
4
+ import { Widget } from "./Widget";
5
+
6
+ export class Instance<ViewModel = any, Controller = any> {
7
+ store: View<ViewModel>;
8
+ data: Record;
9
+ widget: Widget;
10
+ key: number;
11
+ id: string;
12
+ controller: Controller;
13
+ parentOptions: any;
14
+ children?: Instance[];
15
+
16
+ constructor(widget: Widget, key: string | number, parent?: Instance, store?: View);
17
+
18
+ setStore(store: View): void;
19
+
20
+ init(context: RenderingContext): void;
21
+
22
+ explore(context: RenderingContext): boolean;
23
+
24
+ prepare(context: RenderingContext): void;
25
+
26
+ cleanup(context: RenderingContext): void;
27
+
28
+ render(context: RenderingContext, keyPrefix?: string): JSX.Element | void;
29
+
30
+ setState(state: Cx.Record): void;
31
+
32
+ set(prop: string, value: any, internal?: boolean);
33
+
34
+ definePropertySetter(prop: string, setter: (value: any) => void): boolean;
35
+
36
+ /**
37
+ * @protected
38
+ * @param prop
39
+ * @param value
40
+ * @returns {boolean}
41
+ */
42
+ protected doSet(prop: string, value: any): boolean;
43
+
44
+ replaceState(state: Cx.Config);
45
+
46
+ getInstanceCache(): InstanceCache;
47
+
48
+ clearChildrenCache();
49
+
50
+ getChild(context: RenderingContext | null, widget: Widget, keyPrefix?: string, store?: View): Instance;
51
+
52
+ getDetachedChild(widget: Widget, key: number | string, store?: View): Instance;
53
+
54
+ // TODO: check return type
55
+ prepareRenderCleanupChild(widget: Widget, store?: View, keyPrefix?: string, options?: object): JSX.Element | void;
56
+
57
+ getJsxEventProps(): Cx.Config;
58
+
59
+ nestedDataSet(key: string, value: any, dataConfig: Cx.Config): boolean;
60
+
61
+ invokeControllerMethod(methodName: string, ...args: any[]);
62
+ }
63
+
64
+ export class InstanceCache {
65
+ constructor(parent: Instance);
66
+
67
+ getChild(widget: Widget, store: View, keyPrefix?: string): Instance;
68
+
69
+ mark();
70
+
71
+ sweep();
72
+ }
@@ -1,88 +1,88 @@
1
- import { Widget, VDOM } from "../../ui/Widget";
2
- import { HtmlElement } from "../HtmlElement";
3
- import { FocusManager } from "../../ui/FocusManager";
4
- import { isArray } from "../../util/isArray";
5
- import { coalesce } from "../../util/coalesce";
6
-
7
- export class Label extends HtmlElement {
8
- declareData() {
9
- super.declareData(...arguments, {
10
- required: undefined,
11
- disabled: undefined,
12
- htmlFor: undefined,
13
- });
14
- }
15
-
16
- prepareData(context, instance) {
17
- let { data } = instance;
18
- data.stateMods = {
19
- ...data.stateMods,
20
- disabled: data.disabled,
21
- };
22
- data._disabled = data.disabled;
23
- super.prepareData(context, instance);
24
- }
25
-
26
- explore(context, instance) {
27
- let { data } = instance;
28
-
29
- if (!data.htmlFor) data.htmlFor = context.lastFieldId;
30
-
31
- data.disabled = data.stateMods.disabled = coalesce(
32
- context.parentStrict ? context.parentDisabled : null,
33
- data._disabled,
34
- context.parentDisabled
35
- );
36
-
37
- data.asterisk = context.parentAsterisk || this.asterisk;
38
-
39
- if (instance.cache("disabled", data.disabled) || instance.cache("asterisk", data.asterisk)) {
40
- instance.markShouldUpdate(context);
41
- this.prepareCSS(context, instance);
42
- }
43
-
44
- super.explore(context, instance);
45
- }
46
-
47
- isValidHtmlAttribute(attrName) {
48
- switch (attrName) {
49
- case "asterisk":
50
- case "required":
51
- return false;
52
- }
53
- return super.isValidHtmlAttribute(attrName);
54
- }
55
-
56
- attachProps(context, instance, props) {
57
- super.attachProps(context, instance, props);
58
-
59
- let { data } = instance;
60
-
61
- if (data.htmlFor) {
62
- props.htmlFor = data.htmlFor;
63
-
64
- if (!props.onClick)
65
- props.onClick = () => {
66
- //additional focus for LookupFields which are not input based
67
- let el = document.getElementById(instance.data.htmlFor);
68
- if (el) FocusManager.focusFirst(el);
69
- };
70
- }
71
-
72
- if (!props.id && data.htmlFor) props.id = `${data.htmlFor}-label`;
73
-
74
- if (data.required && data.asterisk) {
75
- if (!isArray(props.children)) props.children = [props.children];
76
- props.children.push(" ");
77
- props.children.push(
78
- <span key="asterisk" className={this.CSS.element(this.baseClass, "asterisk")}>
79
- *
80
- </span>
81
- );
82
- }
83
- }
84
- }
85
-
86
- Label.prototype.baseClass = "label";
87
- Label.prototype.tag = "label";
88
- Label.prototype.asterisk = false;
1
+ import { Widget, VDOM } from "../../ui/Widget";
2
+ import { HtmlElement } from "../HtmlElement";
3
+ import { FocusManager } from "../../ui/FocusManager";
4
+ import { isArray } from "../../util/isArray";
5
+ import { coalesce } from "../../util/coalesce";
6
+
7
+ export class Label extends HtmlElement {
8
+ declareData() {
9
+ super.declareData(...arguments, {
10
+ required: undefined,
11
+ disabled: undefined,
12
+ htmlFor: undefined,
13
+ });
14
+ }
15
+
16
+ prepareData(context, instance) {
17
+ let { data } = instance;
18
+ data.stateMods = {
19
+ ...data.stateMods,
20
+ disabled: data.disabled,
21
+ };
22
+ data._disabled = data.disabled;
23
+ super.prepareData(context, instance);
24
+ }
25
+
26
+ explore(context, instance) {
27
+ let { data } = instance;
28
+
29
+ if (!data.htmlFor) data.htmlFor = context.lastFieldId;
30
+
31
+ data.disabled = data.stateMods.disabled = coalesce(
32
+ context.parentStrict ? context.parentDisabled : null,
33
+ data._disabled,
34
+ context.parentDisabled
35
+ );
36
+
37
+ data.asterisk = context.parentAsterisk || this.asterisk;
38
+
39
+ if (instance.cache("disabled", data.disabled) || instance.cache("asterisk", data.asterisk)) {
40
+ instance.markShouldUpdate(context);
41
+ this.prepareCSS(context, instance);
42
+ }
43
+
44
+ super.explore(context, instance);
45
+ }
46
+
47
+ isValidHtmlAttribute(attrName) {
48
+ switch (attrName) {
49
+ case "asterisk":
50
+ case "required":
51
+ return false;
52
+ }
53
+ return super.isValidHtmlAttribute(attrName);
54
+ }
55
+
56
+ attachProps(context, instance, props) {
57
+ super.attachProps(context, instance, props);
58
+
59
+ let { data } = instance;
60
+
61
+ if (data.htmlFor) {
62
+ props.htmlFor = data.htmlFor;
63
+
64
+ if (!props.onClick)
65
+ props.onClick = () => {
66
+ //additional focus for LookupFields which are not input based
67
+ let el = document.getElementById(instance.data.htmlFor);
68
+ if (el) FocusManager.focusFirst(el);
69
+ };
70
+ }
71
+
72
+ if (!props.id && data.htmlFor) props.id = `${data.htmlFor}-label`;
73
+
74
+ if (data.required && data.asterisk) {
75
+ if (!isArray(props.children)) props.children = [props.children];
76
+ props.children.push(" ");
77
+ props.children.push(
78
+ <span key="asterisk" className={this.CSS.element(this.baseClass, "asterisk")}>
79
+ *
80
+ </span>
81
+ );
82
+ }
83
+ }
84
+ }
85
+
86
+ Label.prototype.baseClass = "label";
87
+ Label.prototype.tag = "label";
88
+ Label.prototype.asterisk = false;