cx 25.6.3 → 25.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/dist/charts.css +250 -250
- package/dist/charts.js +109 -109
- package/dist/data.js +19 -19
- package/dist/hooks.js +1 -1
- package/dist/manifest.js +744 -744
- package/dist/svg.js +25 -25
- package/dist/ui.js +84 -84
- package/dist/util.js +6 -9
- package/dist/widgets.css +6 -6
- package/dist/widgets.js +516 -511
- package/package.json +1 -1
- package/src/charts/Legend.d.ts +45 -45
- package/src/charts/LegendEntry.js +128 -128
- package/src/charts/LegendEntry.scss +27 -27
- package/src/charts/Marker.d.ts +1 -1
- package/src/charts/MarkerLine.d.ts +25 -27
- package/src/charts/PieChart.d.ts +92 -92
- package/src/charts/RangeMarker.js +159 -159
- package/src/charts/axis/Axis.d.ts +113 -113
- package/src/charts/axis/Axis.js +280 -280
- package/src/charts/axis/CategoryAxis.d.ts +30 -30
- package/src/charts/axis/CategoryAxis.js +241 -241
- package/src/charts/axis/NumericAxis.js +351 -351
- package/src/charts/axis/Stack.js +55 -55
- package/src/charts/axis/TimeAxis.js +611 -611
- package/src/charts/helpers/SnapPointFinder.js +69 -69
- package/src/data/Binding.spec.js +69 -69
- package/src/data/ExposedValueView.d.ts +19 -19
- package/src/data/Expression.js +229 -229
- package/src/data/Expression.spec.js +229 -229
- package/src/data/StringTemplate.js +92 -92
- package/src/data/StringTemplate.spec.js +132 -132
- package/src/data/StructuredSelector.js +132 -132
- package/src/data/getAccessor.spec.js +11 -11
- package/src/data/getSelector.js +49 -49
- package/src/hooks/createLocalStorageRef.d.ts +3 -3
- package/src/hooks/createLocalStorageRef.js +20 -20
- package/src/index.scss +6 -6
- package/src/ui/Culture.d.ts +57 -57
- package/src/ui/Culture.js +139 -139
- package/src/ui/FocusManager.js +171 -171
- package/src/ui/Format.js +108 -108
- package/src/ui/HoverSync.js +147 -147
- package/src/ui/Repeater.d.ts +61 -61
- package/src/ui/index.d.ts +42 -42
- package/src/ui/layout/ContentPlaceholder.d.ts +19 -19
- package/src/ui/layout/ContentPlaceholder.js +105 -105
- package/src/ui/layout/ContentPlaceholder.spec.js +579 -579
- package/src/ui/layout/LabelsTopLayout.js +134 -134
- package/src/util/Format.js +270 -270
- package/src/util/date/encodeDate.d.ts +1 -1
- package/src/util/date/encodeDate.js +8 -8
- package/src/util/date/encodeDateWithTimezoneOffset.d.ts +1 -1
- package/src/util/date/index.d.ts +11 -11
- package/src/util/date/index.js +11 -11
- package/src/util/date/parseDateInvariant.d.ts +3 -3
- package/src/util/date/parseDateInvariant.js +20 -20
- package/src/util/getSearchQueryPredicate.js +59 -59
- package/src/util/index.d.ts +51 -51
- package/src/util/index.js +54 -54
- package/src/util/isFunction.d.ts +1 -1
- package/src/util/isValidIdentifierName.d.ts +1 -1
- package/src/util/isValidIdentifierName.js +5 -5
- package/src/util/isValidIdentifierName.spec.js +33 -33
- package/src/util/scss/add-rules.scss +38 -38
- package/src/widgets/CxCredit.scss +37 -37
- package/src/widgets/HighlightedSearchText.js +36 -36
- package/src/widgets/HighlightedSearchText.scss +18 -18
- package/src/widgets/List.scss +91 -91
- package/src/widgets/Sandbox.d.ts +18 -18
- package/src/widgets/Sandbox.js +65 -65
- package/src/widgets/drag-drop/DropZone.js +214 -214
- package/src/widgets/form/Calendar.js +618 -618
- package/src/widgets/form/Calendar.scss +196 -196
- package/src/widgets/form/Checkbox.scss +127 -127
- package/src/widgets/form/ColorField.js +397 -397
- package/src/widgets/form/ColorField.scss +96 -96
- package/src/widgets/form/ColorPicker.scss +283 -283
- package/src/widgets/form/DateTimeField.js +576 -576
- package/src/widgets/form/DateTimePicker.js +392 -392
- package/src/widgets/form/LookupField.d.ts +179 -179
- package/src/widgets/form/LookupField.scss +219 -219
- package/src/widgets/form/NumberField.js +459 -459
- package/src/widgets/form/NumberField.scss +61 -61
- package/src/widgets/form/Radio.scss +121 -121
- package/src/widgets/form/Select.scss +99 -99
- package/src/widgets/form/Slider.scss +118 -118
- package/src/widgets/form/Switch.scss +140 -140
- package/src/widgets/form/TextArea.scss +43 -43
- package/src/widgets/form/TextField.js +290 -290
- package/src/widgets/form/TextField.scss +55 -55
- package/src/widgets/form/UploadButton.d.ts +34 -34
- package/src/widgets/form/variables.scss +353 -353
- package/src/widgets/grid/Grid.d.ts +442 -442
- package/src/widgets/grid/Grid.js +28 -23
- package/src/widgets/grid/GridRow.js +228 -228
- package/src/widgets/grid/TreeNode.d.ts +23 -23
- package/src/widgets/grid/TreeNode.scss +88 -88
- package/src/widgets/grid/variables.scss +133 -133
- package/src/widgets/nav/LinkButton.js +128 -128
- package/src/widgets/nav/Menu.scss +74 -74
- package/src/widgets/overlay/Dropdown.js +612 -612
- package/src/widgets/overlay/FlyweightTooltipTracker.js +39 -39
- package/src/widgets/overlay/Overlay.d.ts +73 -73
- package/src/widgets/overlay/Window.js +202 -202
- package/src/widgets/overlay/captureMouse.js +124 -124
- package/src/widgets/overlay/createHotPromiseWindowFactory.d.ts +18 -18
- package/src/widgets/overlay/createHotPromiseWindowFactory.js +56 -56
- package/src/widgets/overlay/index.d.ts +11 -11
- package/src/widgets/overlay/index.js +11 -11
- package/src/widgets/variables.scss +144 -144
|
@@ -1,612 +1,612 @@
|
|
|
1
|
-
import { Localization } from "../../ui/Localization";
|
|
2
|
-
import { ResizeManager } from "../../ui/ResizeManager";
|
|
3
|
-
import { Widget, VDOM } from "../../ui/Widget";
|
|
4
|
-
import { calculateNaturalElementHeight } from "../../util/calculateNaturalElementHeight";
|
|
5
|
-
import { closestParent, findFirst, isFocusable } from "../../util/DOM";
|
|
6
|
-
import { getTopLevelBoundingClientRect } from "../../util/getTopLevelBoundingClientRect";
|
|
7
|
-
import { isTouchDevice } from "../../util/isTouchDevice";
|
|
8
|
-
import { Overlay } from "./Overlay";
|
|
9
|
-
|
|
10
|
-
/*
|
|
11
|
-
Dropdown specific features:
|
|
12
|
-
- ability to position itself next to the target element
|
|
13
|
-
- monitor scrollable parents and updates it's position
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export class Dropdown extends Overlay {
|
|
17
|
-
init() {
|
|
18
|
-
if (this.trackMouse) {
|
|
19
|
-
this.trackMouseX = true;
|
|
20
|
-
this.trackMouseY = true;
|
|
21
|
-
}
|
|
22
|
-
if (this.autoFocus && !this.hasOwnProperty(this.focusable)) this.focusable = true;
|
|
23
|
-
super.init();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
declareData() {
|
|
27
|
-
return super.declareData(...arguments, {
|
|
28
|
-
placement: undefined,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
initInstance(context, instance) {
|
|
33
|
-
instance.mousePosition = this.mousePosition;
|
|
34
|
-
instance.parentPositionChangeEvent = context.parentPositionChangeEvent;
|
|
35
|
-
super.initInstance(context, instance);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
explore(context, instance) {
|
|
39
|
-
context.push("lastDropdown", instance);
|
|
40
|
-
super.explore(context, instance);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
exploreCleanup(context, instance) {
|
|
44
|
-
context.pop("lastDropdown");
|
|
45
|
-
super.exploreCleanup(context, instance);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
overlayDidMount(instance, component) {
|
|
49
|
-
super.overlayDidMount(instance, component);
|
|
50
|
-
var scrollableParents = (component.scrollableParents = [window]);
|
|
51
|
-
component.updateDropdownPosition = (e) => this.updateDropdownPosition(instance, component);
|
|
52
|
-
|
|
53
|
-
instance.initialScreenPosition = null;
|
|
54
|
-
|
|
55
|
-
var el = instance.relatedElement.parentElement;
|
|
56
|
-
while (el) {
|
|
57
|
-
scrollableParents.push(el);
|
|
58
|
-
el = el.parentElement;
|
|
59
|
-
}
|
|
60
|
-
scrollableParents.forEach((el) => {
|
|
61
|
-
el.addEventListener("scroll", component.updateDropdownPosition);
|
|
62
|
-
});
|
|
63
|
-
component.offResize = ResizeManager.subscribe(component.updateDropdownPosition);
|
|
64
|
-
|
|
65
|
-
if (this.onDropdownDidMount) instance.invoke("onDropdownDidMount", instance, component);
|
|
66
|
-
|
|
67
|
-
if (this.pipeValidateDropdownPosition)
|
|
68
|
-
instance.invoke("pipeValidateDropdownPosition", component.updateDropdownPosition, instance);
|
|
69
|
-
|
|
70
|
-
if (instance.parentPositionChangeEvent)
|
|
71
|
-
component.offParentPositionChange = instance.parentPositionChangeEvent.subscribe(
|
|
72
|
-
component.updateDropdownPosition,
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
overlayDidUpdate(instance, component) {
|
|
77
|
-
this.updateDropdownPosition(instance, component);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
overlayWillUnmount(instance, component) {
|
|
81
|
-
var { scrollableParents } = component;
|
|
82
|
-
if (scrollableParents) {
|
|
83
|
-
scrollableParents.forEach((el) => {
|
|
84
|
-
el.removeEventListener("scroll", component.updateDropdownPosition);
|
|
85
|
-
});
|
|
86
|
-
delete component.scrollableParents;
|
|
87
|
-
delete component.updateDropdownPosition;
|
|
88
|
-
}
|
|
89
|
-
if (component.offResize) component.offResize();
|
|
90
|
-
|
|
91
|
-
if (this.pipeValidateDropdownPosition) instance.invoke("pipeValidateDropdownPosition", null, instance);
|
|
92
|
-
|
|
93
|
-
if (component.offParentPositionChange) component.offParentPositionChange();
|
|
94
|
-
|
|
95
|
-
delete component.parentBounds;
|
|
96
|
-
delete component.initialScreenPosition;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
dismissAfterScroll(data, instance, component) {
|
|
100
|
-
if (this.onDismissAfterScroll && instance.invoke("onDismissAfterScroll", data, instance, component) === false)
|
|
101
|
-
return;
|
|
102
|
-
if (instance.dismiss) instance.dismiss();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
updateDropdownPosition(instance, component) {
|
|
106
|
-
var { el, initialScreenPosition } = component;
|
|
107
|
-
var { data, relatedElement } = instance;
|
|
108
|
-
var parentBounds = getTopLevelBoundingClientRect(relatedElement);
|
|
109
|
-
|
|
110
|
-
//getBoundingClientRect() will return an empty rect if the element is hidden or removed
|
|
111
|
-
if (parentBounds.left == 0 && parentBounds.top == 0 && parentBounds.bottom == 0 && parentBounds.right == 0) {
|
|
112
|
-
if (!component.parentBounds) return;
|
|
113
|
-
parentBounds = component.parentBounds;
|
|
114
|
-
} else component.parentBounds = parentBounds;
|
|
115
|
-
|
|
116
|
-
if (this.trackMouseX && instance.mousePosition) {
|
|
117
|
-
parentBounds = {
|
|
118
|
-
top: parentBounds.top,
|
|
119
|
-
bottom: parentBounds.bottom,
|
|
120
|
-
left: instance.mousePosition.x,
|
|
121
|
-
right: instance.mousePosition.x,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (this.trackMouseY && instance.mousePosition) {
|
|
126
|
-
parentBounds = {
|
|
127
|
-
left: parentBounds.left,
|
|
128
|
-
right: parentBounds.right,
|
|
129
|
-
top: instance.mousePosition.y,
|
|
130
|
-
bottom: instance.mousePosition.y,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
let explode = this.pad && typeof this.elementExplode === "number" ? this.elementExplode : 0;
|
|
135
|
-
if (explode) {
|
|
136
|
-
parentBounds = {
|
|
137
|
-
left: Math.round(parentBounds.left - explode),
|
|
138
|
-
right: Math.round(parentBounds.right + explode),
|
|
139
|
-
top: Math.round(parentBounds.top - explode),
|
|
140
|
-
bottom: Math.round(parentBounds.bottom + explode),
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
var style = {};
|
|
145
|
-
if (this.matchWidth) style.minWidth = `${parentBounds.right - parentBounds.left}px`;
|
|
146
|
-
if (this.matchMaxWidth) style.maxWidth = `${parentBounds.right - parentBounds.left}px`;
|
|
147
|
-
|
|
148
|
-
var contentSize = this.measureNaturalDropdownSize(instance, component);
|
|
149
|
-
var placement = this.findOptimalPlacement(contentSize, parentBounds, data.placement, component.lastPlacement);
|
|
150
|
-
|
|
151
|
-
this.applyPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, false);
|
|
152
|
-
component.setCustomStyle(style);
|
|
153
|
-
this.setDirectionClass(component, placement);
|
|
154
|
-
|
|
155
|
-
if (this.constrain) {
|
|
156
|
-
//recheck content size for changes as sometimes when auto is used the size can change
|
|
157
|
-
let newContentSize = this.measureNaturalDropdownSize(instance, component);
|
|
158
|
-
if (newContentSize.width != contentSize.width || newContentSize.height != contentSize.height) {
|
|
159
|
-
let newStyle = {};
|
|
160
|
-
this.applyPositioningPlacementStyles(newStyle, placement, newContentSize, parentBounds, el, true);
|
|
161
|
-
component.setCustomStyle(newStyle);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (!initialScreenPosition) initialScreenPosition = component.initialScreenPosition = parentBounds;
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
(!this.trackMouseY && Math.abs(parentBounds.top - initialScreenPosition.top) > this.closeOnScrollDistance) ||
|
|
169
|
-
(!this.trackMouseX && Math.abs(parentBounds.left - initialScreenPosition.left) > this.closeOnScrollDistance)
|
|
170
|
-
)
|
|
171
|
-
this.dismissAfterScroll({ parentBounds, initialScreenPosition }, instance, component);
|
|
172
|
-
|
|
173
|
-
instance.positionChangeSubscribers.notify();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
applyFixedPositioningPlacementStyles(style, placement, contentSize, rel, el, noAuto) {
|
|
177
|
-
let viewport = getViewportRect(this.screenPadding);
|
|
178
|
-
style.position = "fixed";
|
|
179
|
-
|
|
180
|
-
if (placement.startsWith("down")) {
|
|
181
|
-
style.top = `${(this.cover ? rel.top : rel.bottom) + this.offset}px`;
|
|
182
|
-
let bottom = viewport.bottom - (rel.bottom + this.offset + contentSize.height);
|
|
183
|
-
style.bottom =
|
|
184
|
-
this.constrain && (noAuto || bottom < this.screenPadding + 10)
|
|
185
|
-
? Math.max(this.screenPadding, bottom) + "px"
|
|
186
|
-
: "auto";
|
|
187
|
-
} else if (placement.startsWith("up")) {
|
|
188
|
-
let top = rel.top - this.offset - contentSize.height - viewport.top;
|
|
189
|
-
style.top =
|
|
190
|
-
this.constrain && (noAuto || top < this.screenPadding + 10)
|
|
191
|
-
? Math.max(this.screenPadding, top) + "px"
|
|
192
|
-
: "auto";
|
|
193
|
-
style.bottom =
|
|
194
|
-
document.documentElement.offsetHeight - (this.cover ? rel.bottom : rel.top) + this.offset + "px";
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
switch (placement) {
|
|
198
|
-
case "down":
|
|
199
|
-
case "down-center":
|
|
200
|
-
style.right = "auto";
|
|
201
|
-
style.left = `${Math.round((rel.left + rel.right - el.offsetWidth) / 2)}px`;
|
|
202
|
-
break;
|
|
203
|
-
|
|
204
|
-
case "down-right":
|
|
205
|
-
style.right = "auto";
|
|
206
|
-
style.left = `${rel.left}px`;
|
|
207
|
-
break;
|
|
208
|
-
|
|
209
|
-
case "down-left":
|
|
210
|
-
style.right = `${document.documentElement.offsetWidth - rel.right}px`;
|
|
211
|
-
style.left = "auto";
|
|
212
|
-
break;
|
|
213
|
-
|
|
214
|
-
case "up":
|
|
215
|
-
case "up-center":
|
|
216
|
-
style.right = "auto";
|
|
217
|
-
style.left = `${Math.round((rel.left + rel.right - el.offsetWidth) / 2)}px`;
|
|
218
|
-
break;
|
|
219
|
-
|
|
220
|
-
case "up-right":
|
|
221
|
-
style.right = "auto";
|
|
222
|
-
style.left = `${rel.left}px`;
|
|
223
|
-
break;
|
|
224
|
-
|
|
225
|
-
case "up-left":
|
|
226
|
-
style.right = `${document.documentElement.offsetWidth - rel.right}px`;
|
|
227
|
-
style.left = "auto";
|
|
228
|
-
break;
|
|
229
|
-
|
|
230
|
-
case "right":
|
|
231
|
-
case "right-center":
|
|
232
|
-
style.top = `${Math.round((rel.top + rel.bottom - el.offsetHeight) / 2)}px`;
|
|
233
|
-
style.right = "auto";
|
|
234
|
-
style.bottom = "auto";
|
|
235
|
-
style.left = `${rel.right + this.offset}px`;
|
|
236
|
-
break;
|
|
237
|
-
|
|
238
|
-
case "right-down":
|
|
239
|
-
style.top = `${rel.top}px`;
|
|
240
|
-
style.right = "auto";
|
|
241
|
-
style.bottom = "auto";
|
|
242
|
-
style.left = `${rel.right + this.offset}px`;
|
|
243
|
-
break;
|
|
244
|
-
|
|
245
|
-
case "right-up":
|
|
246
|
-
style.top = "auto";
|
|
247
|
-
style.right = "auto";
|
|
248
|
-
style.bottom = `${document.documentElement.offsetHeight - rel.bottom}px`;
|
|
249
|
-
style.left = `${rel.right + this.offset}px`;
|
|
250
|
-
break;
|
|
251
|
-
|
|
252
|
-
case "left":
|
|
253
|
-
case "left-center":
|
|
254
|
-
style.top = `${Math.round((rel.top + rel.bottom - el.offsetHeight) / 2)}px`;
|
|
255
|
-
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
256
|
-
style.bottom = "auto";
|
|
257
|
-
style.left = "auto";
|
|
258
|
-
break;
|
|
259
|
-
|
|
260
|
-
case "left-down":
|
|
261
|
-
style.top = `${rel.top}px`;
|
|
262
|
-
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
263
|
-
style.bottom = "auto";
|
|
264
|
-
style.left = "auto";
|
|
265
|
-
break;
|
|
266
|
-
|
|
267
|
-
case "left-up":
|
|
268
|
-
style.top = "auto";
|
|
269
|
-
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
270
|
-
style.bottom = `${document.documentElement.offsetHeight - rel.bottom}px`;
|
|
271
|
-
style.left = "auto";
|
|
272
|
-
break;
|
|
273
|
-
|
|
274
|
-
case "screen-center":
|
|
275
|
-
let w = Math.min(contentSize.width, document.documentElement.offsetWidth - 2 * this.screenPadding);
|
|
276
|
-
let h = Math.min(contentSize.height, document.documentElement.offsetHeight - 2 * this.screenPadding);
|
|
277
|
-
style.top = `${Math.round((document.documentElement.offsetHeight - h) / 2)}px`;
|
|
278
|
-
style.right = `${Math.round((document.documentElement.offsetWidth - w) / 2)}px`;
|
|
279
|
-
style.bottom = `${Math.round((document.documentElement.offsetHeight - h) / 2)}px`;
|
|
280
|
-
style.left = `${Math.round((document.documentElement.offsetWidth - w) / 2)}px`;
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
applyAbsolutePositioningPlacementStyles(style, placement, contentSize, rel, el, noAuto) {
|
|
286
|
-
var viewport = getViewportRect(this.screenPadding);
|
|
287
|
-
|
|
288
|
-
style.position = "absolute";
|
|
289
|
-
|
|
290
|
-
if (placement.startsWith("down")) {
|
|
291
|
-
style.top = `${rel.bottom - rel.top + this.offset}px`;
|
|
292
|
-
let room = viewport.bottom - rel.bottom + this.offset;
|
|
293
|
-
style.bottom =
|
|
294
|
-
this.constrain && (noAuto || contentSize.height >= room - 10)
|
|
295
|
-
? `${-Math.min(room, contentSize.height)}px`
|
|
296
|
-
: "auto";
|
|
297
|
-
} else if (placement.startsWith("up")) {
|
|
298
|
-
let room = rel.top - this.offset - viewport.top;
|
|
299
|
-
style.top =
|
|
300
|
-
this.constrain && (noAuto || contentSize.height >= room - 10)
|
|
301
|
-
? `${-Math.min(room, contentSize.height)}px`
|
|
302
|
-
: "auto";
|
|
303
|
-
style.bottom = `${rel.bottom - rel.top - this.offset}px`;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
switch (placement) {
|
|
307
|
-
case "down":
|
|
308
|
-
case "down-center":
|
|
309
|
-
style.right = "auto";
|
|
310
|
-
style.left = `${Math.round((rel.right - rel.left - el.offsetWidth) / 2)}px`;
|
|
311
|
-
break;
|
|
312
|
-
|
|
313
|
-
case "down-right":
|
|
314
|
-
style.right = "auto";
|
|
315
|
-
style.left = `0`;
|
|
316
|
-
break;
|
|
317
|
-
|
|
318
|
-
case "down-left":
|
|
319
|
-
style.right = `0`;
|
|
320
|
-
style.left = "auto";
|
|
321
|
-
break;
|
|
322
|
-
|
|
323
|
-
case "up":
|
|
324
|
-
case "up-center":
|
|
325
|
-
style.right = "auto";
|
|
326
|
-
style.left = `${Math.round((rel.right - rel.left - el.offsetWidth) / 2)}px`;
|
|
327
|
-
break;
|
|
328
|
-
|
|
329
|
-
case "up-right":
|
|
330
|
-
style.right = "auto";
|
|
331
|
-
style.left = `0`;
|
|
332
|
-
break;
|
|
333
|
-
|
|
334
|
-
case "up-left":
|
|
335
|
-
style.right = `0`;
|
|
336
|
-
style.left = "auto";
|
|
337
|
-
break;
|
|
338
|
-
|
|
339
|
-
case "right":
|
|
340
|
-
case "right-center":
|
|
341
|
-
style.top = `${Math.round((rel.bottom - rel.top - el.offsetHeight) / 2)}px`;
|
|
342
|
-
style.right = "auto";
|
|
343
|
-
style.bottom = "auto";
|
|
344
|
-
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
345
|
-
break;
|
|
346
|
-
|
|
347
|
-
case "right-down":
|
|
348
|
-
style.top = `0`;
|
|
349
|
-
style.right = "auto";
|
|
350
|
-
style.bottom = "auto";
|
|
351
|
-
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
352
|
-
break;
|
|
353
|
-
|
|
354
|
-
case "right-up":
|
|
355
|
-
style.top = "auto";
|
|
356
|
-
style.right = "auto";
|
|
357
|
-
style.bottom = `0`;
|
|
358
|
-
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
359
|
-
break;
|
|
360
|
-
|
|
361
|
-
case "left":
|
|
362
|
-
case "left-center":
|
|
363
|
-
style.top = `${Math.round((rel.bottom - rel.top - el.offsetHeight) / 2)}px`;
|
|
364
|
-
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
365
|
-
style.bottom = "auto";
|
|
366
|
-
style.left = "auto";
|
|
367
|
-
break;
|
|
368
|
-
|
|
369
|
-
case "left-down":
|
|
370
|
-
style.top = `0`;
|
|
371
|
-
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
372
|
-
style.bottom = "auto";
|
|
373
|
-
style.left = "auto";
|
|
374
|
-
break;
|
|
375
|
-
|
|
376
|
-
case "left-up":
|
|
377
|
-
style.top = "auto";
|
|
378
|
-
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
379
|
-
style.bottom = `0`;
|
|
380
|
-
style.left = "auto";
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
applyPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto) {
|
|
386
|
-
switch (this.positioning) {
|
|
387
|
-
case "absolute":
|
|
388
|
-
this.applyAbsolutePositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
389
|
-
break;
|
|
390
|
-
|
|
391
|
-
case "auto":
|
|
392
|
-
if (isTouchDevice())
|
|
393
|
-
this.applyAbsolutePositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
394
|
-
else this.applyFixedPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
395
|
-
break;
|
|
396
|
-
|
|
397
|
-
default:
|
|
398
|
-
this.applyFixedPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
399
|
-
break;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
setDirectionClass(component, placement) {
|
|
404
|
-
var state = {
|
|
405
|
-
"place-left": false,
|
|
406
|
-
"place-right": false,
|
|
407
|
-
"place-up": false,
|
|
408
|
-
"place-down": false,
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
component.lastPlacement = placement;
|
|
412
|
-
|
|
413
|
-
component.setCSSState({
|
|
414
|
-
...state,
|
|
415
|
-
["place-" + placement]: true,
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
measureNaturalDropdownSize(instance, component) {
|
|
420
|
-
var { el } = component;
|
|
421
|
-
var size = {
|
|
422
|
-
width: el.offsetWidth,
|
|
423
|
-
height: this.constrain
|
|
424
|
-
? calculateNaturalElementHeight(el)
|
|
425
|
-
: el.offsetHeight - el.clientHeight + el.scrollHeight,
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
if (this.firstChildDefinesHeight && el.firstChild) {
|
|
429
|
-
size.height = el.firstChild.offsetHeight;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
if (this.firstChildDefinesWidth && el.firstChild) {
|
|
433
|
-
size.width = el.firstChild.offsetWidth;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
if (this.onMeasureNaturalContentSize) {
|
|
437
|
-
var more = instance.invoke("onMeasureNaturalContentSize", el, instance, component);
|
|
438
|
-
Object.assign(size, more);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return size;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
findOptimalPlacement(contentSize, target, placement, lastPlacement) {
|
|
445
|
-
var placementOrder = this.placementOrder.split(" ");
|
|
446
|
-
var best = lastPlacement || placement;
|
|
447
|
-
var first;
|
|
448
|
-
|
|
449
|
-
var score = {};
|
|
450
|
-
var viewport = getViewportRect();
|
|
451
|
-
|
|
452
|
-
for (var i = 0; i < placementOrder.length; i++) {
|
|
453
|
-
var p = placementOrder[i];
|
|
454
|
-
if (!first) first = p;
|
|
455
|
-
var parts = p.split("-");
|
|
456
|
-
|
|
457
|
-
var primary = parts[0];
|
|
458
|
-
var secondary = parts[1] || "center";
|
|
459
|
-
|
|
460
|
-
score[p] = 0;
|
|
461
|
-
var vertical = true;
|
|
462
|
-
|
|
463
|
-
switch (primary) {
|
|
464
|
-
case "down":
|
|
465
|
-
score[p] += 3 * Math.min(1, (viewport.bottom - target.bottom - this.offset) / contentSize.height);
|
|
466
|
-
break;
|
|
467
|
-
|
|
468
|
-
case "up":
|
|
469
|
-
score[p] += 3 * Math.min(1, (target.top - viewport.top - this.offset) / contentSize.height);
|
|
470
|
-
break;
|
|
471
|
-
|
|
472
|
-
case "right":
|
|
473
|
-
score[p] += target.right + contentSize.width + this.offset < viewport.right ? 3 : 0;
|
|
474
|
-
vertical = false;
|
|
475
|
-
break;
|
|
476
|
-
|
|
477
|
-
case "left":
|
|
478
|
-
score[p] += target.left - contentSize.width - this.offset >= viewport.left ? 3 : 0;
|
|
479
|
-
vertical = false;
|
|
480
|
-
break;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
switch (secondary) {
|
|
484
|
-
case "center":
|
|
485
|
-
if (vertical) {
|
|
486
|
-
score[p] += (target.right + target.left - contentSize.width) / 2 >= viewport.left ? 1 : 0;
|
|
487
|
-
score[p] += (target.right + target.left + contentSize.width) / 2 < viewport.right ? 1 : 0;
|
|
488
|
-
} else {
|
|
489
|
-
score[p] += (target.bottom + target.top - contentSize.height) / 2 >= viewport.top ? 1 : 0;
|
|
490
|
-
score[p] += (target.bottom + target.top + contentSize.height) / 2 < viewport.bottom ? 1 : 0;
|
|
491
|
-
}
|
|
492
|
-
break;
|
|
493
|
-
|
|
494
|
-
case "right":
|
|
495
|
-
score[p] += target.left + contentSize.width < viewport.right ? 2 : 0;
|
|
496
|
-
break;
|
|
497
|
-
|
|
498
|
-
case "left":
|
|
499
|
-
score[p] += target.right - contentSize.width >= viewport.left ? 2 : 0;
|
|
500
|
-
break;
|
|
501
|
-
|
|
502
|
-
case "up":
|
|
503
|
-
score[p] += target.bottom - contentSize.height >= viewport.top ? 2 : 0;
|
|
504
|
-
break;
|
|
505
|
-
|
|
506
|
-
case "down":
|
|
507
|
-
score[p] += target.top + contentSize.height < viewport.bottom ? 2 : 0;
|
|
508
|
-
break;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
if (!(best in score)) best = first;
|
|
513
|
-
|
|
514
|
-
for (var k in score) if (score[k] > score[best]) best = k;
|
|
515
|
-
|
|
516
|
-
if (this.touchFriendly && isTouchDevice() && score[best] < 5) return "screen-center";
|
|
517
|
-
|
|
518
|
-
return best;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
handleKeyDown(e, instance) {
|
|
522
|
-
switch (e.keyCode) {
|
|
523
|
-
case 27: //esc
|
|
524
|
-
var focusable = findFirst(instance.relatedElement, isFocusable);
|
|
525
|
-
if (focusable) focusable.focus();
|
|
526
|
-
e.stopPropagation();
|
|
527
|
-
e.preventDefault();
|
|
528
|
-
break;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
if (this.onKeyDown) instance.invoke("onKeyDown", e, instance);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
renderContents(context, instance) {
|
|
535
|
-
let { CSS, baseClass } = this;
|
|
536
|
-
let result = [super.renderContents(context, instance)];
|
|
537
|
-
if (this.arrow) {
|
|
538
|
-
result.push(
|
|
539
|
-
<div key="arrow-border" className={CSS.element(baseClass, "arrow-border")}></div>,
|
|
540
|
-
<div key="arrow-back" className={CSS.element(baseClass, "arrow-fill")}></div>,
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
return result;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
render(context, instance, key) {
|
|
547
|
-
let { CSS, baseClass } = this;
|
|
548
|
-
//if relatedElement is not provided, a beacon is rendered to and used to resolve a nearby element as a target
|
|
549
|
-
//if onResolveTarget doesn't provide another element, the beacon itself is used as a target
|
|
550
|
-
let beacon = null;
|
|
551
|
-
if (this.relatedElement) instance.relatedElement = this.relatedElement;
|
|
552
|
-
|
|
553
|
-
if (!this.relatedElement || instance.needsBeacon) {
|
|
554
|
-
beacon = (
|
|
555
|
-
<div
|
|
556
|
-
key={`${key}-beacon`}
|
|
557
|
-
className={CSS.element(baseClass, "beacon")}
|
|
558
|
-
ref={(el) => {
|
|
559
|
-
if (instance.relatedElement) return;
|
|
560
|
-
let target = el;
|
|
561
|
-
if (this.onResolveRelatedElement) target = instance.invoke("onResolveRelatedElement", el, instance);
|
|
562
|
-
else target = el.previousElementSibling;
|
|
563
|
-
if (!target) target = el;
|
|
564
|
-
if (target == el) instance.needsBeacon = true;
|
|
565
|
-
instance.relatedElement = target;
|
|
566
|
-
instance.setState({ dummy: {} });
|
|
567
|
-
}}
|
|
568
|
-
/>
|
|
569
|
-
);
|
|
570
|
-
}
|
|
571
|
-
return [beacon, instance.relatedElement && super.render(context, instance, key)];
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
getOverlayContainer() {
|
|
575
|
-
// this should be instance.relatedElement
|
|
576
|
-
if (this.relatedElement) {
|
|
577
|
-
let container = closestParent(this.relatedElement, (el) => el.dataset && el.dataset.focusableOverlayContainer);
|
|
578
|
-
if (container) return container;
|
|
579
|
-
}
|
|
580
|
-
return super.getOverlayContainer();
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
Dropdown.prototype.offset = 0;
|
|
585
|
-
Dropdown.prototype.baseClass = "dropdown";
|
|
586
|
-
Dropdown.prototype.matchWidth = true;
|
|
587
|
-
Dropdown.prototype.matchMaxWidth = false;
|
|
588
|
-
Dropdown.prototype.placementOrder = "up down right left";
|
|
589
|
-
Dropdown.prototype.placement = null; //default placement
|
|
590
|
-
Dropdown.prototype.constrain = false;
|
|
591
|
-
Dropdown.prototype.positioning = "fixed";
|
|
592
|
-
Dropdown.prototype.touchFriendly = false;
|
|
593
|
-
Dropdown.prototype.arrow = false;
|
|
594
|
-
Dropdown.prototype.pad = false;
|
|
595
|
-
Dropdown.prototype.elementExplode = 0;
|
|
596
|
-
Dropdown.prototype.closeOnScrollDistance = 50;
|
|
597
|
-
Dropdown.prototype.screenPadding = 5;
|
|
598
|
-
Dropdown.prototype.firstChildDefinesHeight = false;
|
|
599
|
-
Dropdown.prototype.firstChildDefinesWidth = false;
|
|
600
|
-
Dropdown.prototype.cover = false;
|
|
601
|
-
|
|
602
|
-
Widget.alias("dropdown", Dropdown);
|
|
603
|
-
Localization.registerPrototype("cx/widgets/Dropdown", Dropdown);
|
|
604
|
-
|
|
605
|
-
function getViewportRect(padding = 0) {
|
|
606
|
-
return {
|
|
607
|
-
left: padding,
|
|
608
|
-
top: padding,
|
|
609
|
-
right: document.documentElement.offsetWidth - padding,
|
|
610
|
-
bottom: document.documentElement.offsetHeight - padding,
|
|
611
|
-
};
|
|
612
|
-
}
|
|
1
|
+
import { Localization } from "../../ui/Localization";
|
|
2
|
+
import { ResizeManager } from "../../ui/ResizeManager";
|
|
3
|
+
import { Widget, VDOM } from "../../ui/Widget";
|
|
4
|
+
import { calculateNaturalElementHeight } from "../../util/calculateNaturalElementHeight";
|
|
5
|
+
import { closestParent, findFirst, isFocusable } from "../../util/DOM";
|
|
6
|
+
import { getTopLevelBoundingClientRect } from "../../util/getTopLevelBoundingClientRect";
|
|
7
|
+
import { isTouchDevice } from "../../util/isTouchDevice";
|
|
8
|
+
import { Overlay } from "./Overlay";
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
Dropdown specific features:
|
|
12
|
+
- ability to position itself next to the target element
|
|
13
|
+
- monitor scrollable parents and updates it's position
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export class Dropdown extends Overlay {
|
|
17
|
+
init() {
|
|
18
|
+
if (this.trackMouse) {
|
|
19
|
+
this.trackMouseX = true;
|
|
20
|
+
this.trackMouseY = true;
|
|
21
|
+
}
|
|
22
|
+
if (this.autoFocus && !this.hasOwnProperty(this.focusable)) this.focusable = true;
|
|
23
|
+
super.init();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declareData() {
|
|
27
|
+
return super.declareData(...arguments, {
|
|
28
|
+
placement: undefined,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
initInstance(context, instance) {
|
|
33
|
+
instance.mousePosition = this.mousePosition;
|
|
34
|
+
instance.parentPositionChangeEvent = context.parentPositionChangeEvent;
|
|
35
|
+
super.initInstance(context, instance);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
explore(context, instance) {
|
|
39
|
+
context.push("lastDropdown", instance);
|
|
40
|
+
super.explore(context, instance);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
exploreCleanup(context, instance) {
|
|
44
|
+
context.pop("lastDropdown");
|
|
45
|
+
super.exploreCleanup(context, instance);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
overlayDidMount(instance, component) {
|
|
49
|
+
super.overlayDidMount(instance, component);
|
|
50
|
+
var scrollableParents = (component.scrollableParents = [window]);
|
|
51
|
+
component.updateDropdownPosition = (e) => this.updateDropdownPosition(instance, component);
|
|
52
|
+
|
|
53
|
+
instance.initialScreenPosition = null;
|
|
54
|
+
|
|
55
|
+
var el = instance.relatedElement.parentElement;
|
|
56
|
+
while (el) {
|
|
57
|
+
scrollableParents.push(el);
|
|
58
|
+
el = el.parentElement;
|
|
59
|
+
}
|
|
60
|
+
scrollableParents.forEach((el) => {
|
|
61
|
+
el.addEventListener("scroll", component.updateDropdownPosition);
|
|
62
|
+
});
|
|
63
|
+
component.offResize = ResizeManager.subscribe(component.updateDropdownPosition);
|
|
64
|
+
|
|
65
|
+
if (this.onDropdownDidMount) instance.invoke("onDropdownDidMount", instance, component);
|
|
66
|
+
|
|
67
|
+
if (this.pipeValidateDropdownPosition)
|
|
68
|
+
instance.invoke("pipeValidateDropdownPosition", component.updateDropdownPosition, instance);
|
|
69
|
+
|
|
70
|
+
if (instance.parentPositionChangeEvent)
|
|
71
|
+
component.offParentPositionChange = instance.parentPositionChangeEvent.subscribe(
|
|
72
|
+
component.updateDropdownPosition,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
overlayDidUpdate(instance, component) {
|
|
77
|
+
this.updateDropdownPosition(instance, component);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
overlayWillUnmount(instance, component) {
|
|
81
|
+
var { scrollableParents } = component;
|
|
82
|
+
if (scrollableParents) {
|
|
83
|
+
scrollableParents.forEach((el) => {
|
|
84
|
+
el.removeEventListener("scroll", component.updateDropdownPosition);
|
|
85
|
+
});
|
|
86
|
+
delete component.scrollableParents;
|
|
87
|
+
delete component.updateDropdownPosition;
|
|
88
|
+
}
|
|
89
|
+
if (component.offResize) component.offResize();
|
|
90
|
+
|
|
91
|
+
if (this.pipeValidateDropdownPosition) instance.invoke("pipeValidateDropdownPosition", null, instance);
|
|
92
|
+
|
|
93
|
+
if (component.offParentPositionChange) component.offParentPositionChange();
|
|
94
|
+
|
|
95
|
+
delete component.parentBounds;
|
|
96
|
+
delete component.initialScreenPosition;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
dismissAfterScroll(data, instance, component) {
|
|
100
|
+
if (this.onDismissAfterScroll && instance.invoke("onDismissAfterScroll", data, instance, component) === false)
|
|
101
|
+
return;
|
|
102
|
+
if (instance.dismiss) instance.dismiss();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
updateDropdownPosition(instance, component) {
|
|
106
|
+
var { el, initialScreenPosition } = component;
|
|
107
|
+
var { data, relatedElement } = instance;
|
|
108
|
+
var parentBounds = getTopLevelBoundingClientRect(relatedElement);
|
|
109
|
+
|
|
110
|
+
//getBoundingClientRect() will return an empty rect if the element is hidden or removed
|
|
111
|
+
if (parentBounds.left == 0 && parentBounds.top == 0 && parentBounds.bottom == 0 && parentBounds.right == 0) {
|
|
112
|
+
if (!component.parentBounds) return;
|
|
113
|
+
parentBounds = component.parentBounds;
|
|
114
|
+
} else component.parentBounds = parentBounds;
|
|
115
|
+
|
|
116
|
+
if (this.trackMouseX && instance.mousePosition) {
|
|
117
|
+
parentBounds = {
|
|
118
|
+
top: parentBounds.top,
|
|
119
|
+
bottom: parentBounds.bottom,
|
|
120
|
+
left: instance.mousePosition.x,
|
|
121
|
+
right: instance.mousePosition.x,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (this.trackMouseY && instance.mousePosition) {
|
|
126
|
+
parentBounds = {
|
|
127
|
+
left: parentBounds.left,
|
|
128
|
+
right: parentBounds.right,
|
|
129
|
+
top: instance.mousePosition.y,
|
|
130
|
+
bottom: instance.mousePosition.y,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let explode = this.pad && typeof this.elementExplode === "number" ? this.elementExplode : 0;
|
|
135
|
+
if (explode) {
|
|
136
|
+
parentBounds = {
|
|
137
|
+
left: Math.round(parentBounds.left - explode),
|
|
138
|
+
right: Math.round(parentBounds.right + explode),
|
|
139
|
+
top: Math.round(parentBounds.top - explode),
|
|
140
|
+
bottom: Math.round(parentBounds.bottom + explode),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
var style = {};
|
|
145
|
+
if (this.matchWidth) style.minWidth = `${parentBounds.right - parentBounds.left}px`;
|
|
146
|
+
if (this.matchMaxWidth) style.maxWidth = `${parentBounds.right - parentBounds.left}px`;
|
|
147
|
+
|
|
148
|
+
var contentSize = this.measureNaturalDropdownSize(instance, component);
|
|
149
|
+
var placement = this.findOptimalPlacement(contentSize, parentBounds, data.placement, component.lastPlacement);
|
|
150
|
+
|
|
151
|
+
this.applyPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, false);
|
|
152
|
+
component.setCustomStyle(style);
|
|
153
|
+
this.setDirectionClass(component, placement);
|
|
154
|
+
|
|
155
|
+
if (this.constrain) {
|
|
156
|
+
//recheck content size for changes as sometimes when auto is used the size can change
|
|
157
|
+
let newContentSize = this.measureNaturalDropdownSize(instance, component);
|
|
158
|
+
if (newContentSize.width != contentSize.width || newContentSize.height != contentSize.height) {
|
|
159
|
+
let newStyle = {};
|
|
160
|
+
this.applyPositioningPlacementStyles(newStyle, placement, newContentSize, parentBounds, el, true);
|
|
161
|
+
component.setCustomStyle(newStyle);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!initialScreenPosition) initialScreenPosition = component.initialScreenPosition = parentBounds;
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
(!this.trackMouseY && Math.abs(parentBounds.top - initialScreenPosition.top) > this.closeOnScrollDistance) ||
|
|
169
|
+
(!this.trackMouseX && Math.abs(parentBounds.left - initialScreenPosition.left) > this.closeOnScrollDistance)
|
|
170
|
+
)
|
|
171
|
+
this.dismissAfterScroll({ parentBounds, initialScreenPosition }, instance, component);
|
|
172
|
+
|
|
173
|
+
instance.positionChangeSubscribers.notify();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
applyFixedPositioningPlacementStyles(style, placement, contentSize, rel, el, noAuto) {
|
|
177
|
+
let viewport = getViewportRect(this.screenPadding);
|
|
178
|
+
style.position = "fixed";
|
|
179
|
+
|
|
180
|
+
if (placement.startsWith("down")) {
|
|
181
|
+
style.top = `${(this.cover ? rel.top : rel.bottom) + this.offset}px`;
|
|
182
|
+
let bottom = viewport.bottom - (rel.bottom + this.offset + contentSize.height);
|
|
183
|
+
style.bottom =
|
|
184
|
+
this.constrain && (noAuto || bottom < this.screenPadding + 10)
|
|
185
|
+
? Math.max(this.screenPadding, bottom) + "px"
|
|
186
|
+
: "auto";
|
|
187
|
+
} else if (placement.startsWith("up")) {
|
|
188
|
+
let top = rel.top - this.offset - contentSize.height - viewport.top;
|
|
189
|
+
style.top =
|
|
190
|
+
this.constrain && (noAuto || top < this.screenPadding + 10)
|
|
191
|
+
? Math.max(this.screenPadding, top) + "px"
|
|
192
|
+
: "auto";
|
|
193
|
+
style.bottom =
|
|
194
|
+
document.documentElement.offsetHeight - (this.cover ? rel.bottom : rel.top) + this.offset + "px";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
switch (placement) {
|
|
198
|
+
case "down":
|
|
199
|
+
case "down-center":
|
|
200
|
+
style.right = "auto";
|
|
201
|
+
style.left = `${Math.round((rel.left + rel.right - el.offsetWidth) / 2)}px`;
|
|
202
|
+
break;
|
|
203
|
+
|
|
204
|
+
case "down-right":
|
|
205
|
+
style.right = "auto";
|
|
206
|
+
style.left = `${rel.left}px`;
|
|
207
|
+
break;
|
|
208
|
+
|
|
209
|
+
case "down-left":
|
|
210
|
+
style.right = `${document.documentElement.offsetWidth - rel.right}px`;
|
|
211
|
+
style.left = "auto";
|
|
212
|
+
break;
|
|
213
|
+
|
|
214
|
+
case "up":
|
|
215
|
+
case "up-center":
|
|
216
|
+
style.right = "auto";
|
|
217
|
+
style.left = `${Math.round((rel.left + rel.right - el.offsetWidth) / 2)}px`;
|
|
218
|
+
break;
|
|
219
|
+
|
|
220
|
+
case "up-right":
|
|
221
|
+
style.right = "auto";
|
|
222
|
+
style.left = `${rel.left}px`;
|
|
223
|
+
break;
|
|
224
|
+
|
|
225
|
+
case "up-left":
|
|
226
|
+
style.right = `${document.documentElement.offsetWidth - rel.right}px`;
|
|
227
|
+
style.left = "auto";
|
|
228
|
+
break;
|
|
229
|
+
|
|
230
|
+
case "right":
|
|
231
|
+
case "right-center":
|
|
232
|
+
style.top = `${Math.round((rel.top + rel.bottom - el.offsetHeight) / 2)}px`;
|
|
233
|
+
style.right = "auto";
|
|
234
|
+
style.bottom = "auto";
|
|
235
|
+
style.left = `${rel.right + this.offset}px`;
|
|
236
|
+
break;
|
|
237
|
+
|
|
238
|
+
case "right-down":
|
|
239
|
+
style.top = `${rel.top}px`;
|
|
240
|
+
style.right = "auto";
|
|
241
|
+
style.bottom = "auto";
|
|
242
|
+
style.left = `${rel.right + this.offset}px`;
|
|
243
|
+
break;
|
|
244
|
+
|
|
245
|
+
case "right-up":
|
|
246
|
+
style.top = "auto";
|
|
247
|
+
style.right = "auto";
|
|
248
|
+
style.bottom = `${document.documentElement.offsetHeight - rel.bottom}px`;
|
|
249
|
+
style.left = `${rel.right + this.offset}px`;
|
|
250
|
+
break;
|
|
251
|
+
|
|
252
|
+
case "left":
|
|
253
|
+
case "left-center":
|
|
254
|
+
style.top = `${Math.round((rel.top + rel.bottom - el.offsetHeight) / 2)}px`;
|
|
255
|
+
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
256
|
+
style.bottom = "auto";
|
|
257
|
+
style.left = "auto";
|
|
258
|
+
break;
|
|
259
|
+
|
|
260
|
+
case "left-down":
|
|
261
|
+
style.top = `${rel.top}px`;
|
|
262
|
+
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
263
|
+
style.bottom = "auto";
|
|
264
|
+
style.left = "auto";
|
|
265
|
+
break;
|
|
266
|
+
|
|
267
|
+
case "left-up":
|
|
268
|
+
style.top = "auto";
|
|
269
|
+
style.right = `${document.documentElement.offsetWidth - rel.left + this.offset}px`;
|
|
270
|
+
style.bottom = `${document.documentElement.offsetHeight - rel.bottom}px`;
|
|
271
|
+
style.left = "auto";
|
|
272
|
+
break;
|
|
273
|
+
|
|
274
|
+
case "screen-center":
|
|
275
|
+
let w = Math.min(contentSize.width, document.documentElement.offsetWidth - 2 * this.screenPadding);
|
|
276
|
+
let h = Math.min(contentSize.height, document.documentElement.offsetHeight - 2 * this.screenPadding);
|
|
277
|
+
style.top = `${Math.round((document.documentElement.offsetHeight - h) / 2)}px`;
|
|
278
|
+
style.right = `${Math.round((document.documentElement.offsetWidth - w) / 2)}px`;
|
|
279
|
+
style.bottom = `${Math.round((document.documentElement.offsetHeight - h) / 2)}px`;
|
|
280
|
+
style.left = `${Math.round((document.documentElement.offsetWidth - w) / 2)}px`;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
applyAbsolutePositioningPlacementStyles(style, placement, contentSize, rel, el, noAuto) {
|
|
286
|
+
var viewport = getViewportRect(this.screenPadding);
|
|
287
|
+
|
|
288
|
+
style.position = "absolute";
|
|
289
|
+
|
|
290
|
+
if (placement.startsWith("down")) {
|
|
291
|
+
style.top = `${rel.bottom - rel.top + this.offset}px`;
|
|
292
|
+
let room = viewport.bottom - rel.bottom + this.offset;
|
|
293
|
+
style.bottom =
|
|
294
|
+
this.constrain && (noAuto || contentSize.height >= room - 10)
|
|
295
|
+
? `${-Math.min(room, contentSize.height)}px`
|
|
296
|
+
: "auto";
|
|
297
|
+
} else if (placement.startsWith("up")) {
|
|
298
|
+
let room = rel.top - this.offset - viewport.top;
|
|
299
|
+
style.top =
|
|
300
|
+
this.constrain && (noAuto || contentSize.height >= room - 10)
|
|
301
|
+
? `${-Math.min(room, contentSize.height)}px`
|
|
302
|
+
: "auto";
|
|
303
|
+
style.bottom = `${rel.bottom - rel.top - this.offset}px`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
switch (placement) {
|
|
307
|
+
case "down":
|
|
308
|
+
case "down-center":
|
|
309
|
+
style.right = "auto";
|
|
310
|
+
style.left = `${Math.round((rel.right - rel.left - el.offsetWidth) / 2)}px`;
|
|
311
|
+
break;
|
|
312
|
+
|
|
313
|
+
case "down-right":
|
|
314
|
+
style.right = "auto";
|
|
315
|
+
style.left = `0`;
|
|
316
|
+
break;
|
|
317
|
+
|
|
318
|
+
case "down-left":
|
|
319
|
+
style.right = `0`;
|
|
320
|
+
style.left = "auto";
|
|
321
|
+
break;
|
|
322
|
+
|
|
323
|
+
case "up":
|
|
324
|
+
case "up-center":
|
|
325
|
+
style.right = "auto";
|
|
326
|
+
style.left = `${Math.round((rel.right - rel.left - el.offsetWidth) / 2)}px`;
|
|
327
|
+
break;
|
|
328
|
+
|
|
329
|
+
case "up-right":
|
|
330
|
+
style.right = "auto";
|
|
331
|
+
style.left = `0`;
|
|
332
|
+
break;
|
|
333
|
+
|
|
334
|
+
case "up-left":
|
|
335
|
+
style.right = `0`;
|
|
336
|
+
style.left = "auto";
|
|
337
|
+
break;
|
|
338
|
+
|
|
339
|
+
case "right":
|
|
340
|
+
case "right-center":
|
|
341
|
+
style.top = `${Math.round((rel.bottom - rel.top - el.offsetHeight) / 2)}px`;
|
|
342
|
+
style.right = "auto";
|
|
343
|
+
style.bottom = "auto";
|
|
344
|
+
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
345
|
+
break;
|
|
346
|
+
|
|
347
|
+
case "right-down":
|
|
348
|
+
style.top = `0`;
|
|
349
|
+
style.right = "auto";
|
|
350
|
+
style.bottom = "auto";
|
|
351
|
+
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
352
|
+
break;
|
|
353
|
+
|
|
354
|
+
case "right-up":
|
|
355
|
+
style.top = "auto";
|
|
356
|
+
style.right = "auto";
|
|
357
|
+
style.bottom = `0`;
|
|
358
|
+
style.left = `${rel.right - rel.left + this.offset}px`;
|
|
359
|
+
break;
|
|
360
|
+
|
|
361
|
+
case "left":
|
|
362
|
+
case "left-center":
|
|
363
|
+
style.top = `${Math.round((rel.bottom - rel.top - el.offsetHeight) / 2)}px`;
|
|
364
|
+
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
365
|
+
style.bottom = "auto";
|
|
366
|
+
style.left = "auto";
|
|
367
|
+
break;
|
|
368
|
+
|
|
369
|
+
case "left-down":
|
|
370
|
+
style.top = `0`;
|
|
371
|
+
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
372
|
+
style.bottom = "auto";
|
|
373
|
+
style.left = "auto";
|
|
374
|
+
break;
|
|
375
|
+
|
|
376
|
+
case "left-up":
|
|
377
|
+
style.top = "auto";
|
|
378
|
+
style.right = `${rel.right - rel.left + this.offset}px`;
|
|
379
|
+
style.bottom = `0`;
|
|
380
|
+
style.left = "auto";
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
applyPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto) {
|
|
386
|
+
switch (this.positioning) {
|
|
387
|
+
case "absolute":
|
|
388
|
+
this.applyAbsolutePositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
389
|
+
break;
|
|
390
|
+
|
|
391
|
+
case "auto":
|
|
392
|
+
if (isTouchDevice())
|
|
393
|
+
this.applyAbsolutePositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
394
|
+
else this.applyFixedPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
395
|
+
break;
|
|
396
|
+
|
|
397
|
+
default:
|
|
398
|
+
this.applyFixedPositioningPlacementStyles(style, placement, contentSize, parentBounds, el, noAuto);
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
setDirectionClass(component, placement) {
|
|
404
|
+
var state = {
|
|
405
|
+
"place-left": false,
|
|
406
|
+
"place-right": false,
|
|
407
|
+
"place-up": false,
|
|
408
|
+
"place-down": false,
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
component.lastPlacement = placement;
|
|
412
|
+
|
|
413
|
+
component.setCSSState({
|
|
414
|
+
...state,
|
|
415
|
+
["place-" + placement]: true,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
measureNaturalDropdownSize(instance, component) {
|
|
420
|
+
var { el } = component;
|
|
421
|
+
var size = {
|
|
422
|
+
width: el.offsetWidth,
|
|
423
|
+
height: this.constrain
|
|
424
|
+
? calculateNaturalElementHeight(el)
|
|
425
|
+
: el.offsetHeight - el.clientHeight + el.scrollHeight,
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
if (this.firstChildDefinesHeight && el.firstChild) {
|
|
429
|
+
size.height = el.firstChild.offsetHeight;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (this.firstChildDefinesWidth && el.firstChild) {
|
|
433
|
+
size.width = el.firstChild.offsetWidth;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (this.onMeasureNaturalContentSize) {
|
|
437
|
+
var more = instance.invoke("onMeasureNaturalContentSize", el, instance, component);
|
|
438
|
+
Object.assign(size, more);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return size;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
findOptimalPlacement(contentSize, target, placement, lastPlacement) {
|
|
445
|
+
var placementOrder = this.placementOrder.split(" ");
|
|
446
|
+
var best = lastPlacement || placement;
|
|
447
|
+
var first;
|
|
448
|
+
|
|
449
|
+
var score = {};
|
|
450
|
+
var viewport = getViewportRect();
|
|
451
|
+
|
|
452
|
+
for (var i = 0; i < placementOrder.length; i++) {
|
|
453
|
+
var p = placementOrder[i];
|
|
454
|
+
if (!first) first = p;
|
|
455
|
+
var parts = p.split("-");
|
|
456
|
+
|
|
457
|
+
var primary = parts[0];
|
|
458
|
+
var secondary = parts[1] || "center";
|
|
459
|
+
|
|
460
|
+
score[p] = 0;
|
|
461
|
+
var vertical = true;
|
|
462
|
+
|
|
463
|
+
switch (primary) {
|
|
464
|
+
case "down":
|
|
465
|
+
score[p] += 3 * Math.min(1, (viewport.bottom - target.bottom - this.offset) / contentSize.height);
|
|
466
|
+
break;
|
|
467
|
+
|
|
468
|
+
case "up":
|
|
469
|
+
score[p] += 3 * Math.min(1, (target.top - viewport.top - this.offset) / contentSize.height);
|
|
470
|
+
break;
|
|
471
|
+
|
|
472
|
+
case "right":
|
|
473
|
+
score[p] += target.right + contentSize.width + this.offset < viewport.right ? 3 : 0;
|
|
474
|
+
vertical = false;
|
|
475
|
+
break;
|
|
476
|
+
|
|
477
|
+
case "left":
|
|
478
|
+
score[p] += target.left - contentSize.width - this.offset >= viewport.left ? 3 : 0;
|
|
479
|
+
vertical = false;
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
switch (secondary) {
|
|
484
|
+
case "center":
|
|
485
|
+
if (vertical) {
|
|
486
|
+
score[p] += (target.right + target.left - contentSize.width) / 2 >= viewport.left ? 1 : 0;
|
|
487
|
+
score[p] += (target.right + target.left + contentSize.width) / 2 < viewport.right ? 1 : 0;
|
|
488
|
+
} else {
|
|
489
|
+
score[p] += (target.bottom + target.top - contentSize.height) / 2 >= viewport.top ? 1 : 0;
|
|
490
|
+
score[p] += (target.bottom + target.top + contentSize.height) / 2 < viewport.bottom ? 1 : 0;
|
|
491
|
+
}
|
|
492
|
+
break;
|
|
493
|
+
|
|
494
|
+
case "right":
|
|
495
|
+
score[p] += target.left + contentSize.width < viewport.right ? 2 : 0;
|
|
496
|
+
break;
|
|
497
|
+
|
|
498
|
+
case "left":
|
|
499
|
+
score[p] += target.right - contentSize.width >= viewport.left ? 2 : 0;
|
|
500
|
+
break;
|
|
501
|
+
|
|
502
|
+
case "up":
|
|
503
|
+
score[p] += target.bottom - contentSize.height >= viewport.top ? 2 : 0;
|
|
504
|
+
break;
|
|
505
|
+
|
|
506
|
+
case "down":
|
|
507
|
+
score[p] += target.top + contentSize.height < viewport.bottom ? 2 : 0;
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (!(best in score)) best = first;
|
|
513
|
+
|
|
514
|
+
for (var k in score) if (score[k] > score[best]) best = k;
|
|
515
|
+
|
|
516
|
+
if (this.touchFriendly && isTouchDevice() && score[best] < 5) return "screen-center";
|
|
517
|
+
|
|
518
|
+
return best;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
handleKeyDown(e, instance) {
|
|
522
|
+
switch (e.keyCode) {
|
|
523
|
+
case 27: //esc
|
|
524
|
+
var focusable = findFirst(instance.relatedElement, isFocusable);
|
|
525
|
+
if (focusable) focusable.focus();
|
|
526
|
+
e.stopPropagation();
|
|
527
|
+
e.preventDefault();
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (this.onKeyDown) instance.invoke("onKeyDown", e, instance);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
renderContents(context, instance) {
|
|
535
|
+
let { CSS, baseClass } = this;
|
|
536
|
+
let result = [super.renderContents(context, instance)];
|
|
537
|
+
if (this.arrow) {
|
|
538
|
+
result.push(
|
|
539
|
+
<div key="arrow-border" className={CSS.element(baseClass, "arrow-border")}></div>,
|
|
540
|
+
<div key="arrow-back" className={CSS.element(baseClass, "arrow-fill")}></div>,
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
render(context, instance, key) {
|
|
547
|
+
let { CSS, baseClass } = this;
|
|
548
|
+
//if relatedElement is not provided, a beacon is rendered to and used to resolve a nearby element as a target
|
|
549
|
+
//if onResolveTarget doesn't provide another element, the beacon itself is used as a target
|
|
550
|
+
let beacon = null;
|
|
551
|
+
if (this.relatedElement) instance.relatedElement = this.relatedElement;
|
|
552
|
+
|
|
553
|
+
if (!this.relatedElement || instance.needsBeacon) {
|
|
554
|
+
beacon = (
|
|
555
|
+
<div
|
|
556
|
+
key={`${key}-beacon`}
|
|
557
|
+
className={CSS.element(baseClass, "beacon")}
|
|
558
|
+
ref={(el) => {
|
|
559
|
+
if (instance.relatedElement) return;
|
|
560
|
+
let target = el;
|
|
561
|
+
if (this.onResolveRelatedElement) target = instance.invoke("onResolveRelatedElement", el, instance);
|
|
562
|
+
else target = el.previousElementSibling;
|
|
563
|
+
if (!target) target = el;
|
|
564
|
+
if (target == el) instance.needsBeacon = true;
|
|
565
|
+
instance.relatedElement = target;
|
|
566
|
+
instance.setState({ dummy: {} });
|
|
567
|
+
}}
|
|
568
|
+
/>
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
return [beacon, instance.relatedElement && super.render(context, instance, key)];
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
getOverlayContainer() {
|
|
575
|
+
// this should be instance.relatedElement
|
|
576
|
+
if (this.relatedElement) {
|
|
577
|
+
let container = closestParent(this.relatedElement, (el) => el.dataset && el.dataset.focusableOverlayContainer);
|
|
578
|
+
if (container) return container;
|
|
579
|
+
}
|
|
580
|
+
return super.getOverlayContainer();
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
Dropdown.prototype.offset = 0;
|
|
585
|
+
Dropdown.prototype.baseClass = "dropdown";
|
|
586
|
+
Dropdown.prototype.matchWidth = true;
|
|
587
|
+
Dropdown.prototype.matchMaxWidth = false;
|
|
588
|
+
Dropdown.prototype.placementOrder = "up down right left";
|
|
589
|
+
Dropdown.prototype.placement = null; //default placement
|
|
590
|
+
Dropdown.prototype.constrain = false;
|
|
591
|
+
Dropdown.prototype.positioning = "fixed";
|
|
592
|
+
Dropdown.prototype.touchFriendly = false;
|
|
593
|
+
Dropdown.prototype.arrow = false;
|
|
594
|
+
Dropdown.prototype.pad = false;
|
|
595
|
+
Dropdown.prototype.elementExplode = 0;
|
|
596
|
+
Dropdown.prototype.closeOnScrollDistance = 50;
|
|
597
|
+
Dropdown.prototype.screenPadding = 5;
|
|
598
|
+
Dropdown.prototype.firstChildDefinesHeight = false;
|
|
599
|
+
Dropdown.prototype.firstChildDefinesWidth = false;
|
|
600
|
+
Dropdown.prototype.cover = false;
|
|
601
|
+
|
|
602
|
+
Widget.alias("dropdown", Dropdown);
|
|
603
|
+
Localization.registerPrototype("cx/widgets/Dropdown", Dropdown);
|
|
604
|
+
|
|
605
|
+
function getViewportRect(padding = 0) {
|
|
606
|
+
return {
|
|
607
|
+
left: padding,
|
|
608
|
+
top: padding,
|
|
609
|
+
right: document.documentElement.offsetWidth - padding,
|
|
610
|
+
bottom: document.documentElement.offsetHeight - padding,
|
|
611
|
+
};
|
|
612
|
+
}
|