cx 24.0.2 → 24.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/data.js +15 -26
  2. package/dist/manifest.js +525 -522
  3. package/dist/ui.js +1 -1
  4. package/dist/widgets.js +162 -77
  5. package/locale/de-de.js +6 -1
  6. package/locale/en-us.js +5 -1
  7. package/locale/es-es.js +6 -1
  8. package/locale/fr-fr.js +7 -2
  9. package/locale/nl-nl.js +4 -5
  10. package/locale/pt-pt.js +12 -1
  11. package/locale/sr-latn-ba.js +6 -2
  12. package/package.json +1 -1
  13. package/src/charts/ColorMap.js +4 -6
  14. package/src/charts/axis/Axis.d.ts +96 -96
  15. package/src/charts/axis/Axis.js +252 -252
  16. package/src/data/Expression.js +212 -212
  17. package/src/data/Expression.spec.js +174 -174
  18. package/src/data/StringTemplate.spec.js +105 -105
  19. package/src/data/StructuredSelector.d.ts +1 -1
  20. package/src/data/ops/updateTree.js +1 -1
  21. package/src/data/ops/updateTree.spec.js +16 -14
  22. package/src/ui/Controller.d.ts +182 -182
  23. package/src/ui/Culture.d.ts +0 -3
  24. package/src/ui/DataProxy.d.ts +1 -0
  25. package/src/ui/DataProxy.js +2 -2
  26. package/src/ui/FocusManager.js +171 -171
  27. package/src/ui/Format.js +87 -87
  28. package/src/ui/Instance.d.ts +72 -72
  29. package/src/ui/Localization.js +0 -2
  30. package/src/ui/Rescope.js +2 -2
  31. package/src/ui/Text.js +2 -4
  32. package/src/ui/adapter/DataAdapter.js +7 -12
  33. package/src/ui/adapter/GroupAdapter.d.ts +22 -3
  34. package/src/ui/adapter/TreeAdapter.d.ts +23 -3
  35. package/src/ui/keyboardShortcuts.js +4 -5
  36. package/src/ui/selection/KeySelection.d.ts +1 -1
  37. package/src/ui/selection/PropertySelection.d.ts +1 -1
  38. package/src/ui/selection/PropertySelection.js +2 -4
  39. package/src/ui/selection/Selection.d.ts +1 -1
  40. package/src/widgets/form/ColorField.js +14 -9
  41. package/src/widgets/form/ColorPicker.scss +275 -275
  42. package/src/widgets/form/ColorPicker.variables.scss +22 -22
  43. package/src/widgets/form/DateTimeField.d.ts +86 -86
  44. package/src/widgets/form/DateTimeField.js +573 -572
  45. package/src/widgets/form/Field.js +24 -9
  46. package/src/widgets/form/FieldIcon.js +42 -0
  47. package/src/widgets/form/Label.js +88 -88
  48. package/src/widgets/form/LookupField.d.ts +173 -174
  49. package/src/widgets/form/LookupField.js +1130 -1131
  50. package/src/widgets/form/MonthField.d.ts +37 -38
  51. package/src/widgets/form/MonthField.js +16 -15
  52. package/src/widgets/form/NumberField.d.ts +2 -2
  53. package/src/widgets/form/NumberField.js +13 -13
  54. package/src/widgets/form/Select.d.ts +31 -35
  55. package/src/widgets/form/Select.js +7 -12
  56. package/src/widgets/form/TextArea.js +10 -6
  57. package/src/widgets/form/TextField.d.ts +2 -2
  58. package/src/widgets/form/TextField.js +17 -14
  59. package/src/widgets/form/UploadButton.d.ts +34 -34
  60. package/src/widgets/form/index.js +1 -2
  61. package/src/widgets/grid/Grid.d.ts +5 -2
  62. package/src/widgets/overlay/Dropdown.d.ts +1 -0
@@ -1,572 +1,573 @@
1
- import { Widget, VDOM, getContent } from "../../ui/Widget";
2
- import { Cx } from "../../ui/Cx";
3
- import { Field, getFieldTooltip } from "./Field";
4
- import { DateTimePicker } from "./DateTimePicker";
5
- import { Calendar } from "./Calendar";
6
- import { Culture } from "../../ui/Culture";
7
- import { isTouchEvent } from "../../util/isTouchEvent";
8
- import { isTouchDevice } from "../../util/isTouchDevice";
9
- import { Dropdown } from "../overlay/Dropdown";
10
- import { StringTemplate } from "../../data/StringTemplate";
11
- import { zeroTime } from "../../util/date/zeroTime";
12
- import { dateDiff } from "../../util/date/dateDiff";
13
- import {
14
- tooltipParentWillReceiveProps,
15
- tooltipParentWillUnmount,
16
- tooltipMouseMove,
17
- tooltipMouseLeave,
18
- tooltipParentDidMount,
19
- } from "../overlay/tooltip-ops";
20
- import { KeyCode } from "../../util/KeyCode";
21
- import { Localization } from "../../ui/Localization";
22
- import DropdownIcon from "../icons/drop-down";
23
- import { Icon } from "../Icon";
24
- import ClearIcon from "../icons/clear";
25
- import { stopPropagation } from "../../util/eventCallbacks";
26
- import { Format } from "../../util/Format";
27
- import { TimeList } from "./TimeList";
28
- import { autoFocus } from "../autoFocus";
29
-
30
- export class DateTimeField extends Field {
31
- declareData() {
32
- super.declareData(
33
- {
34
- value: this.emptyValue,
35
- disabled: undefined,
36
- readOnly: undefined,
37
- enabled: undefined,
38
- placeholder: undefined,
39
- required: undefined,
40
- minValue: undefined,
41
- minExclusive: undefined,
42
- maxValue: undefined,
43
- maxExclusive: undefined,
44
- format: undefined,
45
- icon: undefined,
46
- autoOpen: undefined,
47
- },
48
- ...arguments
49
- );
50
- }
51
-
52
- init() {
53
- if (typeof this.hideClear !== "undefined") this.showClear = !this.hideClear;
54
-
55
- if (this.alwaysShowClear) this.showClear = true;
56
-
57
- if (!this.format) {
58
- switch (this.segment) {
59
- case "datetime":
60
- this.format = "datetime;YYYYMMddhhmm";
61
- break;
62
-
63
- case "time":
64
- this.format = "time;hhmm";
65
- break;
66
-
67
- case "date":
68
- this.format = "date;yyyyMMMdd";
69
- break;
70
- }
71
- }
72
- super.init();
73
- }
74
-
75
- prepareData(context, instance) {
76
- let { data } = instance;
77
-
78
- if (data.value) {
79
- let date = new Date(data.value);
80
- if (isNaN(date.getTime())) data.formatted = String(data.value);
81
- else {
82
- // handle utc edge cases
83
- if (this.segment == "date") date = zeroTime(date);
84
- data.formatted = Format.value(date, data.format);
85
- }
86
- data.date = date;
87
- } else data.formatted = "";
88
-
89
- if (data.refDate) data.refDate = zeroTime(new Date(data.refDate));
90
-
91
- if (data.maxValue) data.maxValue = new Date(data.maxValue);
92
-
93
- if (data.minValue) data.minValue = new Date(data.minValue);
94
-
95
- if (this.segment == "date") {
96
- if (data.minValue) data.minValue = zeroTime(data.minValue);
97
-
98
- if (data.maxValue) data.maxValue = zeroTime(data.maxValue);
99
- }
100
-
101
- instance.lastDropdown = context.lastDropdown;
102
-
103
- super.prepareData(context, instance);
104
- }
105
-
106
- validate(context, instance) {
107
- super.validate(context, instance);
108
- var { data, widget } = instance;
109
- if (!data.error && data.date) {
110
- if (isNaN(data.date)) data.error = this.inputErrorText;
111
- else {
112
- let d;
113
- if (data.maxValue) {
114
- d = dateDiff(data.date, data.maxValue);
115
- if (d > 0) data.error = StringTemplate.format(this.maxValueErrorText, data.maxValue);
116
- else if (d == 0 && data.maxExclusive)
117
- data.error = StringTemplate.format(this.maxExclusiveErrorText, data.maxValue);
118
- }
119
- if (data.minValue) {
120
- d = dateDiff(data.date, data.minValue);
121
- if (d < 0) data.error = StringTemplate.format(this.minValueErrorText, data.minValue);
122
- else if (d == 0 && data.minExclusive)
123
- data.error = StringTemplate.format(this.minExclusiveErrorText, data.minValue);
124
- }
125
- if (widget.disabledDaysOfWeek) {
126
- if (widget.disabledDaysOfWeek.includes(data.date.getDay()))
127
- data.error = this.disabledDaysOfWeekErrorText;
128
- }
129
- }
130
- }
131
- }
132
-
133
- renderInput(context, instance, key) {
134
- return (
135
- <DateTimeInput
136
- key={key}
137
- instance={instance}
138
- data={instance.data}
139
- picker={{
140
- value: this.value,
141
- minValue: this.minValue,
142
- maxValue: this.maxValue,
143
- minExclusive: this.minExclusive,
144
- maxExclusive: this.maxExclusive,
145
- maxValueErrorText: this.maxValueErrorText,
146
- maxExclusiveErrorText: this.maxExclusiveErrorText,
147
- minValueErrorText: this.minValueErrorText,
148
- minExclusiveErrorText: this.minExclusiveErrorText,
149
- }}
150
- label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
151
- help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
152
- />
153
- );
154
- }
155
-
156
- formatValue(context, { data }) {
157
- return data.value ? data.formatted : null;
158
- }
159
-
160
- parseDate(date, instance) {
161
- if (!date) return null;
162
- if (date instanceof Date) return date;
163
- if (this.onParseInput) {
164
- let result = instance.invoke("onParseInput", date, instance);
165
- if (result !== undefined) return result;
166
- }
167
- date = Culture.getDateTimeCulture().parse(date, { useCurrentDateForDefaults: true });
168
- return date;
169
- }
170
- }
171
-
172
- DateTimeField.prototype.baseClass = "datetimefield";
173
- DateTimeField.prototype.maxValueErrorText = "Select {0:d} or before.";
174
- DateTimeField.prototype.maxExclusiveErrorText = "Select a date before {0:d}.";
175
- DateTimeField.prototype.minValueErrorText = "Select {0:d} or later.";
176
- DateTimeField.prototype.minExclusiveErrorText = "Select a date after {0:d}.";
177
- DateTimeField.prototype.inputErrorText = "Invalid date entered.";
178
- DateTimeField.prototype.disabledDaysOfWeekErrorText = "Selected day of week is not allowed.";
179
-
180
- DateTimeField.prototype.suppressErrorsUntilVisited = true;
181
- DateTimeField.prototype.icon = "calendar";
182
- DateTimeField.prototype.showClear = true;
183
- DateTimeField.prototype.alwaysShowClear = false;
184
- DateTimeField.prototype.reactOn = "enter blur";
185
- DateTimeField.prototype.segment = "datetime";
186
- DateTimeField.prototype.picker = "auto";
187
- DateTimeField.prototype.disabledDaysOfWeek = null;
188
- DateTimeField.prototype.focusInputFirst = false;
189
-
190
- Widget.alias("datetimefield", DateTimeField);
191
- Localization.registerPrototype("cx/widgets/DateTimeField", DateTimeField);
192
-
193
- class DateTimeInput extends VDOM.Component {
194
- constructor(props) {
195
- super(props);
196
- props.instance.component = this;
197
- this.state = {
198
- dropdownOpen: false,
199
- focus: false,
200
- };
201
- }
202
-
203
- getDropdown() {
204
- if (this.dropdown) return this.dropdown;
205
-
206
- let { widget, lastDropdown } = this.props.instance;
207
-
208
- let pickerConfig;
209
-
210
- switch (widget.picker) {
211
- case "calendar":
212
- pickerConfig = {
213
- type: Calendar,
214
- partial: widget.partial,
215
- encoding: widget.encoding,
216
- disabledDaysOfWeek: widget.disabledDaysOfWeek,
217
- focusable: !widget.focusInputFirst,
218
- };
219
- break;
220
-
221
- case "list":
222
- pickerConfig = {
223
- type: TimeList,
224
- style: "height: 300px",
225
- encoding: widget.encoding,
226
- step: widget.step,
227
- format: widget.format,
228
- scrollSelectionIntoView: true,
229
- };
230
- break;
231
-
232
- default:
233
- pickerConfig = {
234
- type: DateTimePicker,
235
- segment: widget.segment,
236
- encoding: widget.encoding,
237
- showSeconds: widget.showSeconds,
238
- };
239
- break;
240
- }
241
-
242
- let dropdown = {
243
- scrollTracking: true,
244
- inline: !isTouchDevice() || !!lastDropdown,
245
- matchWidth: false,
246
- placementOrder: "down down-right down-left up up-right up-left",
247
- touchFriendly: true,
248
- firstChildDefinesHeight: true,
249
- firstChildDefinesWidth: true,
250
- ...widget.dropdownOptions,
251
- type: Dropdown,
252
- relatedElement: this.input,
253
- onFocusOut: (e) => {
254
- this.closeDropdown(e);
255
- },
256
- onMouseDown: stopPropagation,
257
- items: {
258
- ...pickerConfig,
259
- ...this.props.picker,
260
- autoFocus: !widget.focusInputFirst,
261
- tabIndex: widget.focusInputFirst ? -1 : 0,
262
- onKeyDown: (e) => this.onKeyDown(e),
263
- onSelect: (e, calendar, date) => {
264
- e.stopPropagation();
265
- e.preventDefault();
266
- let touch = isTouchEvent(e);
267
- this.closeDropdown(e, () => {
268
- if (date) {
269
- // If a blur event occurs before we re-render the input,
270
- // the old input value is parsed and written to the store.
271
- // We want to prevent that by eagerly updating the input value.
272
- // This can happen if the date field is within a menu.
273
- let newFormattedValue = Format.value(date, this.props.data.format);
274
- this.input.value = newFormattedValue;
275
- }
276
- if (!touch) this.input.focus();
277
- });
278
- },
279
- },
280
- };
281
-
282
- return (this.dropdown = Widget.create(dropdown));
283
- }
284
-
285
- render() {
286
- let { instance, label, help } = this.props;
287
- let { data, widget, state } = instance;
288
- let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
289
-
290
- let insideButton, icon;
291
-
292
- if (!data.readOnly && !data.disabled) {
293
- if (
294
- widget.showClear &&
295
- (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
296
- )
297
- insideButton = (
298
- <div
299
- className={CSS.element(baseClass, "clear")}
300
- onMouseDown={(e) => {
301
- e.preventDefault();
302
- e.stopPropagation();
303
- }}
304
- onClick={(e) => this.onClearClick(e)}
305
- >
306
- <ClearIcon className={CSS.element(baseClass, "icon")} />
307
- </div>
308
- );
309
- else
310
- insideButton = (
311
- <div className={CSS.element(baseClass, "right-icon")}>
312
- <DropdownIcon className={CSS.element(baseClass, "icon")} />
313
- </div>
314
- );
315
- }
316
-
317
- if (data.icon) {
318
- icon = (
319
- <div className={CSS.element(baseClass, "left-icon")}>
320
- {Icon.render(data.icon, { className: CSS.element(baseClass, "icon") })}
321
- </div>
322
- );
323
- }
324
-
325
- let dropdown = false;
326
- if (this.state.dropdownOpen)
327
- dropdown = (
328
- <Cx
329
- widget={this.getDropdown()}
330
- parentInstance={instance}
331
- options={{ name: "datefield-dropdown" }}
332
- subscribe
333
- />
334
- );
335
-
336
- let empty = this.input ? !this.input.value : data.empty;
337
-
338
- return (
339
- <div
340
- className={CSS.expand(
341
- data.classNames,
342
- CSS.state({
343
- visited: state.visited,
344
- focus: this.state.focus || this.state.dropdownOpen,
345
- icon: !!icon,
346
- empty: empty && !data.placeholder,
347
- error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
348
- })
349
- )}
350
- style={data.style}
351
- onMouseDown={this.onMouseDown.bind(this)}
352
- onTouchStart={stopPropagation}
353
- >
354
- <input
355
- id={data.id}
356
- ref={(el) => {
357
- this.input = el;
358
- }}
359
- type="text"
360
- className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
361
- style={data.inputStyle}
362
- defaultValue={data.formatted}
363
- disabled={data.disabled}
364
- readOnly={data.readOnly}
365
- tabIndex={data.tabIndex}
366
- placeholder={data.placeholder}
367
- {...data.inputAttrs}
368
- onInput={(e) => this.onChange(e, "input")}
369
- onChange={(e) => this.onChange(e, "change")}
370
- onKeyDown={(e) => this.onKeyDown(e)}
371
- onBlur={(e) => {
372
- this.onBlur(e);
373
- }}
374
- onFocus={(e) => {
375
- this.onFocus(e);
376
- }}
377
- onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
378
- onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
379
- />
380
- {icon}
381
- {insideButton}
382
- {dropdown}
383
- {label}
384
- {help}
385
- </div>
386
- );
387
- }
388
-
389
- onMouseDown(e) {
390
- e.stopPropagation();
391
-
392
- if (this.state.dropdownOpen) {
393
- this.closeDropdown(e);
394
- } else {
395
- this.openDropdownOnFocus = true;
396
- }
397
-
398
- //icon click
399
- if (e.target !== this.input) {
400
- e.preventDefault();
401
-
402
- //the field should not focus only in case when dropdown will open and autofocus
403
- if (this.props.instance.widget.focusInputFirst || this.state.dropdownOpen) this.input.focus();
404
-
405
- if (this.state.dropdownOpen) this.closeDropdown(e);
406
- else this.openDropdown(e);
407
- }
408
- }
409
-
410
- onFocus(e) {
411
- let { instance } = this.props;
412
- let { widget } = instance;
413
- if (widget.trackFocus) {
414
- this.setState({
415
- focus: true,
416
- });
417
- }
418
- if (this.openDropdownOnFocus || widget.focusInputFirst) this.openDropdown(e);
419
- }
420
-
421
- onKeyDown(e) {
422
- let { instance } = this.props;
423
- if (instance.widget.handleKeyDown(e, instance) === false) return;
424
-
425
- switch (e.keyCode) {
426
- case KeyCode.enter:
427
- this.onChange(e, "enter");
428
- break;
429
-
430
- case KeyCode.esc:
431
- if (this.state.dropdownOpen) {
432
- e.stopPropagation();
433
- this.closeDropdown(e, () => {
434
- this.input.focus();
435
- });
436
- }
437
- break;
438
-
439
- case KeyCode.left:
440
- case KeyCode.right:
441
- e.stopPropagation();
442
- break;
443
-
444
- case KeyCode.down:
445
- this.openDropdown(e);
446
- e.stopPropagation();
447
- e.preventDefault();
448
- break;
449
- }
450
- }
451
-
452
- onBlur(e) {
453
- if (!this.state.dropdownOpen) this.props.instance.setState({ visited: true });
454
- else if (this.props.instance.widget.focusInputFirst) this.closeDropdown(e);
455
- if (this.state.focus)
456
- this.setState({
457
- focus: false,
458
- });
459
- this.onChange(e, "blur");
460
- }
461
-
462
- closeDropdown(e, callback) {
463
- if (this.state.dropdownOpen) {
464
- if (this.scrollableParents)
465
- this.scrollableParents.forEach((el) => {
466
- el.removeEventListener("scroll", this.updateDropdownPosition);
467
- });
468
-
469
- this.setState({ dropdownOpen: false }, callback);
470
- this.props.instance.setState({ visited: true });
471
- } else if (callback) callback();
472
- }
473
-
474
- openDropdown() {
475
- let { data } = this.props.instance;
476
- this.openDropdownOnFocus = false;
477
-
478
- if (!this.state.dropdownOpen && !(data.disabled || data.readOnly)) {
479
- this.setState({
480
- dropdownOpen: true,
481
- dropdownOpenTime: Date.now(),
482
- });
483
- }
484
- }
485
-
486
- onClearClick(e) {
487
- this.setValue(null);
488
- e.stopPropagation();
489
- e.preventDefault();
490
- }
491
-
492
- UNSAFE_componentWillReceiveProps(props) {
493
- let { data, state } = props.instance;
494
- if (data.formatted !== this.input.value && (data.formatted !== this.props.data.formatted || !state.inputError)) {
495
- this.input.value = data.formatted || "";
496
- props.instance.setState({
497
- inputError: false,
498
- });
499
- }
500
-
501
- tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(this.props.instance));
502
- }
503
-
504
- componentDidMount() {
505
- tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
506
- autoFocus(this.input, this);
507
- if (this.props.data.autoOpen) this.openDropdown();
508
- }
509
-
510
- componentDidUpdate() {
511
- autoFocus(this.input, this);
512
- }
513
-
514
- componentWillUnmount() {
515
- tooltipParentWillUnmount(this.props.instance);
516
- }
517
-
518
- onChange(e, eventType) {
519
- let { instance, data } = this.props;
520
- let { widget } = instance;
521
-
522
- if (data.disabled || data.readOnly) return;
523
-
524
- if (widget.reactOn.indexOf(eventType) === -1) return;
525
-
526
- if (eventType == "enter") instance.setState({ visited: true });
527
-
528
- this.setValue(e.target.value, data.value);
529
- }
530
-
531
- setValue(text, baseValue) {
532
- let { instance, data } = this.props;
533
- let { widget } = instance;
534
-
535
- let date = widget.parseDate(text, instance);
536
-
537
- instance.setState({
538
- inputError: isNaN(date) && widget.inputErrorText,
539
- });
540
-
541
- if (!isNaN(date)) {
542
- let mixed = new Date(baseValue);
543
- if (date && baseValue && !isNaN(mixed) && widget.partial) {
544
- switch (widget.segment) {
545
- case "date":
546
- mixed.setFullYear(date.getFullYear());
547
- mixed.setMonth(date.getMonth());
548
- mixed.setDate(date.getDate());
549
- break;
550
-
551
- case "time":
552
- mixed.setHours(date.getHours());
553
- mixed.setMinutes(date.getMinutes());
554
- mixed.setSeconds(date.getSeconds());
555
- break;
556
-
557
- default:
558
- mixed = date;
559
- break;
560
- }
561
-
562
- date = mixed;
563
- }
564
-
565
- let encode = widget.encoding || Culture.getDefaultDateEncoding();
566
-
567
- let value = date ? encode(date) : widget.emptyValue;
568
-
569
- if (!instance.set("value", value)) this.input.value = value ? Format.value(date, data.format) : "";
570
- }
571
- }
572
- }
1
+ import { Widget, VDOM, getContent } from "../../ui/Widget";
2
+ import { Cx } from "../../ui/Cx";
3
+ import { Field, getFieldTooltip } from "./Field";
4
+ import { DateTimePicker } from "./DateTimePicker";
5
+ import { Calendar } from "./Calendar";
6
+ import { Culture } from "../../ui/Culture";
7
+ import { isTouchEvent } from "../../util/isTouchEvent";
8
+ import { isTouchDevice } from "../../util/isTouchDevice";
9
+ import { Dropdown } from "../overlay/Dropdown";
10
+ import { StringTemplate } from "../../data/StringTemplate";
11
+ import { zeroTime } from "../../util/date/zeroTime";
12
+ import { dateDiff } from "../../util/date/dateDiff";
13
+ import {
14
+ tooltipParentWillReceiveProps,
15
+ tooltipParentWillUnmount,
16
+ tooltipMouseMove,
17
+ tooltipMouseLeave,
18
+ tooltipParentDidMount,
19
+ } from "../overlay/tooltip-ops";
20
+ import { KeyCode } from "../../util/KeyCode";
21
+ import { Localization } from "../../ui/Localization";
22
+ import DropdownIcon from "../icons/drop-down";
23
+ import { Icon } from "../Icon";
24
+ import ClearIcon from "../icons/clear";
25
+ import { stopPropagation } from "../../util/eventCallbacks";
26
+ import { Format } from "../../util/Format";
27
+ import { TimeList } from "./TimeList";
28
+ import { autoFocus } from "../autoFocus";
29
+ import { getActiveElement } from "../../util";
30
+
31
+ export class DateTimeField extends Field {
32
+ declareData() {
33
+ super.declareData(
34
+ {
35
+ value: this.emptyValue,
36
+ disabled: undefined,
37
+ readOnly: undefined,
38
+ enabled: undefined,
39
+ placeholder: undefined,
40
+ required: undefined,
41
+ minValue: undefined,
42
+ minExclusive: undefined,
43
+ maxValue: undefined,
44
+ maxExclusive: undefined,
45
+ format: undefined,
46
+ icon: undefined,
47
+ autoOpen: undefined,
48
+ },
49
+ ...arguments,
50
+ );
51
+ }
52
+
53
+ init() {
54
+ if (typeof this.hideClear !== "undefined") this.showClear = !this.hideClear;
55
+
56
+ if (this.alwaysShowClear) this.showClear = true;
57
+
58
+ if (!this.format) {
59
+ switch (this.segment) {
60
+ case "datetime":
61
+ this.format = "datetime;YYYYMMddhhmm";
62
+ break;
63
+
64
+ case "time":
65
+ this.format = "time;hhmm";
66
+ break;
67
+
68
+ case "date":
69
+ this.format = "date;yyyyMMMdd";
70
+ break;
71
+ }
72
+ }
73
+ super.init();
74
+ }
75
+
76
+ prepareData(context, instance) {
77
+ let { data } = instance;
78
+
79
+ if (data.value) {
80
+ let date = new Date(data.value);
81
+ if (isNaN(date.getTime())) data.formatted = String(data.value);
82
+ else {
83
+ // handle utc edge cases
84
+ if (this.segment == "date") date = zeroTime(date);
85
+ data.formatted = Format.value(date, data.format);
86
+ }
87
+ data.date = date;
88
+ } else data.formatted = "";
89
+
90
+ if (data.refDate) data.refDate = zeroTime(new Date(data.refDate));
91
+
92
+ if (data.maxValue) data.maxValue = new Date(data.maxValue);
93
+
94
+ if (data.minValue) data.minValue = new Date(data.minValue);
95
+
96
+ if (this.segment == "date") {
97
+ if (data.minValue) data.minValue = zeroTime(data.minValue);
98
+
99
+ if (data.maxValue) data.maxValue = zeroTime(data.maxValue);
100
+ }
101
+
102
+ instance.lastDropdown = context.lastDropdown;
103
+
104
+ super.prepareData(context, instance);
105
+ }
106
+
107
+ validate(context, instance) {
108
+ super.validate(context, instance);
109
+ var { data, widget } = instance;
110
+ if (!data.error && data.date) {
111
+ if (isNaN(data.date)) data.error = this.inputErrorText;
112
+ else {
113
+ let d;
114
+ if (data.maxValue) {
115
+ d = dateDiff(data.date, data.maxValue);
116
+ if (d > 0) data.error = StringTemplate.format(this.maxValueErrorText, data.maxValue);
117
+ else if (d == 0 && data.maxExclusive)
118
+ data.error = StringTemplate.format(this.maxExclusiveErrorText, data.maxValue);
119
+ }
120
+ if (data.minValue) {
121
+ d = dateDiff(data.date, data.minValue);
122
+ if (d < 0) data.error = StringTemplate.format(this.minValueErrorText, data.minValue);
123
+ else if (d == 0 && data.minExclusive)
124
+ data.error = StringTemplate.format(this.minExclusiveErrorText, data.minValue);
125
+ }
126
+ if (widget.disabledDaysOfWeek) {
127
+ if (widget.disabledDaysOfWeek.includes(data.date.getDay()))
128
+ data.error = this.disabledDaysOfWeekErrorText;
129
+ }
130
+ }
131
+ }
132
+ }
133
+
134
+ renderInput(context, instance, key) {
135
+ return (
136
+ <DateTimeInput
137
+ key={key}
138
+ instance={instance}
139
+ data={instance.data}
140
+ picker={{
141
+ value: this.value,
142
+ minValue: this.minValue,
143
+ maxValue: this.maxValue,
144
+ minExclusive: this.minExclusive,
145
+ maxExclusive: this.maxExclusive,
146
+ maxValueErrorText: this.maxValueErrorText,
147
+ maxExclusiveErrorText: this.maxExclusiveErrorText,
148
+ minValueErrorText: this.minValueErrorText,
149
+ minExclusiveErrorText: this.minExclusiveErrorText,
150
+ }}
151
+ label={this.labelPlacement && getContent(this.renderLabel(context, instance, "label"))}
152
+ help={this.helpPlacement && getContent(this.renderHelp(context, instance, "help"))}
153
+ icon={getContent(this.renderIcon(context, instance, "icon"))}
154
+ />
155
+ );
156
+ }
157
+
158
+ formatValue(context, { data }) {
159
+ return data.value ? data.formatted : null;
160
+ }
161
+
162
+ parseDate(date, instance) {
163
+ if (!date) return null;
164
+ if (date instanceof Date) return date;
165
+ if (this.onParseInput) {
166
+ let result = instance.invoke("onParseInput", date, instance);
167
+ if (result !== undefined) return result;
168
+ }
169
+ date = Culture.getDateTimeCulture().parse(date, { useCurrentDateForDefaults: true });
170
+ return date;
171
+ }
172
+ }
173
+
174
+ DateTimeField.prototype.baseClass = "datetimefield";
175
+ DateTimeField.prototype.maxValueErrorText = "Select {0:d} or before.";
176
+ DateTimeField.prototype.maxExclusiveErrorText = "Select a date before {0:d}.";
177
+ DateTimeField.prototype.minValueErrorText = "Select {0:d} or later.";
178
+ DateTimeField.prototype.minExclusiveErrorText = "Select a date after {0:d}.";
179
+ DateTimeField.prototype.inputErrorText = "Invalid date entered.";
180
+ DateTimeField.prototype.disabledDaysOfWeekErrorText = "Selected day of week is not allowed.";
181
+
182
+ DateTimeField.prototype.suppressErrorsUntilVisited = true;
183
+ DateTimeField.prototype.icon = "calendar";
184
+ DateTimeField.prototype.showClear = true;
185
+ DateTimeField.prototype.alwaysShowClear = false;
186
+ DateTimeField.prototype.reactOn = "enter blur";
187
+ DateTimeField.prototype.segment = "datetime";
188
+ DateTimeField.prototype.picker = "auto";
189
+ DateTimeField.prototype.disabledDaysOfWeek = null;
190
+ DateTimeField.prototype.focusInputFirst = false;
191
+
192
+ Widget.alias("datetimefield", DateTimeField);
193
+ Localization.registerPrototype("cx/widgets/DateTimeField", DateTimeField);
194
+
195
+ class DateTimeInput extends VDOM.Component {
196
+ constructor(props) {
197
+ super(props);
198
+ props.instance.component = this;
199
+ this.state = {
200
+ dropdownOpen: false,
201
+ focus: false,
202
+ };
203
+ }
204
+
205
+ getDropdown() {
206
+ if (this.dropdown) return this.dropdown;
207
+
208
+ let { widget, lastDropdown } = this.props.instance;
209
+
210
+ let pickerConfig;
211
+
212
+ switch (widget.picker) {
213
+ case "calendar":
214
+ pickerConfig = {
215
+ type: Calendar,
216
+ partial: widget.partial,
217
+ encoding: widget.encoding,
218
+ disabledDaysOfWeek: widget.disabledDaysOfWeek,
219
+ focusable: !widget.focusInputFirst,
220
+ };
221
+ break;
222
+
223
+ case "list":
224
+ pickerConfig = {
225
+ type: TimeList,
226
+ style: "height: 300px",
227
+ encoding: widget.encoding,
228
+ step: widget.step,
229
+ format: widget.format,
230
+ scrollSelectionIntoView: true,
231
+ };
232
+ break;
233
+
234
+ default:
235
+ pickerConfig = {
236
+ type: DateTimePicker,
237
+ segment: widget.segment,
238
+ encoding: widget.encoding,
239
+ showSeconds: widget.showSeconds,
240
+ };
241
+ break;
242
+ }
243
+
244
+ let dropdown = {
245
+ scrollTracking: true,
246
+ inline: !isTouchDevice() || !!lastDropdown,
247
+ matchWidth: false,
248
+ placementOrder: "down down-right down-left up up-right up-left",
249
+ touchFriendly: true,
250
+ firstChildDefinesHeight: true,
251
+ firstChildDefinesWidth: true,
252
+ ...widget.dropdownOptions,
253
+ type: Dropdown,
254
+ relatedElement: this.input,
255
+ onFocusOut: (e) => {
256
+ this.closeDropdown(e);
257
+ },
258
+ onMouseDown: stopPropagation,
259
+ items: {
260
+ ...pickerConfig,
261
+ ...this.props.picker,
262
+ autoFocus: !widget.focusInputFirst,
263
+ tabIndex: widget.focusInputFirst ? -1 : 0,
264
+ onKeyDown: (e) => this.onKeyDown(e),
265
+ onSelect: (e, calendar, date) => {
266
+ e.stopPropagation();
267
+ e.preventDefault();
268
+ let touch = isTouchEvent(e);
269
+ this.closeDropdown(e, () => {
270
+ if (date) {
271
+ // If a blur event occurs before we re-render the input,
272
+ // the old input value is parsed and written to the store.
273
+ // We want to prevent that by eagerly updating the input value.
274
+ // This can happen if the date field is within a menu.
275
+ let newFormattedValue = Format.value(date, this.props.data.format);
276
+ this.input.value = newFormattedValue;
277
+ }
278
+ if (!touch) this.input.focus();
279
+ });
280
+ },
281
+ },
282
+ };
283
+
284
+ return (this.dropdown = Widget.create(dropdown));
285
+ }
286
+
287
+ render() {
288
+ let { instance, label, help, icon: iconVDOM } = this.props;
289
+ let { data, widget, state } = instance;
290
+ let { CSS, baseClass, suppressErrorsUntilVisited } = widget;
291
+
292
+ let insideButton, icon;
293
+
294
+ if (!data.readOnly && !data.disabled) {
295
+ if (
296
+ widget.showClear &&
297
+ (((widget.alwaysShowClear || !data.required) && !data.empty) || instance.state.inputError)
298
+ )
299
+ insideButton = (
300
+ <div
301
+ className={CSS.element(baseClass, "clear")}
302
+ onMouseDown={(e) => {
303
+ e.preventDefault();
304
+ e.stopPropagation();
305
+ }}
306
+ onClick={(e) => this.onClearClick(e)}
307
+ >
308
+ <ClearIcon className={CSS.element(baseClass, "icon")} />
309
+ </div>
310
+ );
311
+ else
312
+ insideButton = (
313
+ <div className={CSS.element(baseClass, "right-icon")}>
314
+ <DropdownIcon className={CSS.element(baseClass, "icon")} />
315
+ </div>
316
+ );
317
+ }
318
+
319
+ if (iconVDOM) {
320
+ icon = <div className={CSS.element(baseClass, "left-icon")}>{iconVDOM}</div>;
321
+ }
322
+
323
+ let dropdown = false;
324
+ if (this.state.dropdownOpen)
325
+ dropdown = (
326
+ <Cx
327
+ widget={this.getDropdown()}
328
+ parentInstance={instance}
329
+ options={{ name: "datefield-dropdown" }}
330
+ subscribe
331
+ />
332
+ );
333
+
334
+ let empty = this.input ? !this.input.value : data.empty;
335
+
336
+ return (
337
+ <div
338
+ className={CSS.expand(
339
+ data.classNames,
340
+ CSS.state({
341
+ visited: state.visited,
342
+ focus: this.state.focus || this.state.dropdownOpen,
343
+ icon: !!icon,
344
+ empty: empty && !data.placeholder,
345
+ error: data.error && (state.visited || !suppressErrorsUntilVisited || !empty),
346
+ }),
347
+ )}
348
+ style={data.style}
349
+ onMouseDown={this.onMouseDown.bind(this)}
350
+ onTouchStart={stopPropagation}
351
+ >
352
+ <input
353
+ id={data.id}
354
+ ref={(el) => {
355
+ this.input = el;
356
+ }}
357
+ type="text"
358
+ className={CSS.expand(CSS.element(baseClass, "input"), data.inputClass)}
359
+ style={data.inputStyle}
360
+ defaultValue={data.formatted}
361
+ disabled={data.disabled}
362
+ readOnly={data.readOnly}
363
+ tabIndex={data.tabIndex}
364
+ placeholder={data.placeholder}
365
+ {...data.inputAttrs}
366
+ onInput={(e) => this.onChange(e.target.value, "input")}
367
+ onChange={(e) => this.onChange(e.target.value, "change")}
368
+ onKeyDown={(e) => this.onKeyDown(e)}
369
+ onBlur={(e) => {
370
+ this.onBlur(e);
371
+ }}
372
+ onFocus={(e) => {
373
+ this.onFocus(e);
374
+ }}
375
+ onMouseMove={(e) => tooltipMouseMove(e, ...getFieldTooltip(this.props.instance))}
376
+ onMouseLeave={(e) => tooltipMouseLeave(e, ...getFieldTooltip(this.props.instance))}
377
+ />
378
+ {icon}
379
+ {insideButton}
380
+ {dropdown}
381
+ {label}
382
+ {help}
383
+ </div>
384
+ );
385
+ }
386
+
387
+ onMouseDown(e) {
388
+ e.stopPropagation();
389
+
390
+ if (this.state.dropdownOpen) {
391
+ this.closeDropdown(e);
392
+ } else {
393
+ this.openDropdownOnFocus = true;
394
+ }
395
+
396
+ //icon click
397
+ if (e.target !== this.input) {
398
+ e.preventDefault();
399
+
400
+ //the field should not focus only in case when dropdown will open and autofocus
401
+ if (this.props.instance.widget.focusInputFirst || this.state.dropdownOpen) this.input.focus();
402
+
403
+ if (this.state.dropdownOpen) this.closeDropdown(e);
404
+ else this.openDropdown(e);
405
+ }
406
+ }
407
+
408
+ onFocus(e) {
409
+ let { instance } = this.props;
410
+ let { widget } = instance;
411
+ if (widget.trackFocus) {
412
+ this.setState({
413
+ focus: true,
414
+ });
415
+ }
416
+ if (this.openDropdownOnFocus || widget.focusInputFirst) this.openDropdown(e);
417
+ }
418
+
419
+ onKeyDown(e) {
420
+ let { instance } = this.props;
421
+ if (instance.widget.handleKeyDown(e, instance) === false) return;
422
+
423
+ switch (e.keyCode) {
424
+ case KeyCode.enter:
425
+ this.onChange(e.target.value, "enter");
426
+ break;
427
+
428
+ case KeyCode.esc:
429
+ if (this.state.dropdownOpen) {
430
+ e.stopPropagation();
431
+ this.closeDropdown(e, () => {
432
+ this.input.focus();
433
+ });
434
+ }
435
+ break;
436
+
437
+ case KeyCode.left:
438
+ case KeyCode.right:
439
+ e.stopPropagation();
440
+ break;
441
+
442
+ case KeyCode.down:
443
+ this.openDropdown(e);
444
+ e.stopPropagation();
445
+ e.preventDefault();
446
+ break;
447
+ }
448
+ }
449
+
450
+ onBlur(e) {
451
+ if (!this.state.dropdownOpen) this.props.instance.setState({ visited: true });
452
+ else if (this.props.instance.widget.focusInputFirst) this.closeDropdown(e);
453
+ if (this.state.focus)
454
+ this.setState({
455
+ focus: false,
456
+ });
457
+ this.onChange(e.target.value, "blur");
458
+ }
459
+
460
+ closeDropdown(e, callback) {
461
+ if (this.state.dropdownOpen) {
462
+ if (this.scrollableParents)
463
+ this.scrollableParents.forEach((el) => {
464
+ el.removeEventListener("scroll", this.updateDropdownPosition);
465
+ });
466
+
467
+ this.setState({ dropdownOpen: false }, callback);
468
+ this.props.instance.setState({ visited: true });
469
+ } else if (callback) callback();
470
+ }
471
+
472
+ openDropdown() {
473
+ let { data } = this.props.instance;
474
+ this.openDropdownOnFocus = false;
475
+
476
+ if (!this.state.dropdownOpen && !(data.disabled || data.readOnly)) {
477
+ this.setState({
478
+ dropdownOpen: true,
479
+ dropdownOpenTime: Date.now(),
480
+ });
481
+ }
482
+ }
483
+
484
+ onClearClick(e) {
485
+ this.setValue(null);
486
+ e.stopPropagation();
487
+ e.preventDefault();
488
+ }
489
+
490
+ UNSAFE_componentWillReceiveProps(props) {
491
+ let { data, state } = props.instance;
492
+ if (data.formatted !== this.input.value && (data.formatted !== this.props.data.formatted || !state.inputError)) {
493
+ this.input.value = data.formatted || "";
494
+ props.instance.setState({
495
+ inputError: false,
496
+ });
497
+ }
498
+
499
+ tooltipParentWillReceiveProps(this.input, ...getFieldTooltip(this.props.instance));
500
+ }
501
+
502
+ componentDidMount() {
503
+ tooltipParentDidMount(this.input, ...getFieldTooltip(this.props.instance));
504
+ autoFocus(this.input, this);
505
+ if (this.props.data.autoOpen) this.openDropdown();
506
+ }
507
+
508
+ componentDidUpdate() {
509
+ autoFocus(this.input, this);
510
+ }
511
+
512
+ componentWillUnmount() {
513
+ if (this.input == getActiveElement()) {
514
+ this.onChange(this.input.value, "blur");
515
+ }
516
+ tooltipParentWillUnmount(this.props.instance);
517
+ }
518
+
519
+ onChange(inputValue, eventType) {
520
+ let { instance, data } = this.props;
521
+ let { widget } = instance;
522
+
523
+ if (data.disabled || data.readOnly) return;
524
+
525
+ if (widget.reactOn.indexOf(eventType) === -1) return;
526
+
527
+ if (eventType == "enter") instance.setState({ visited: true });
528
+
529
+ this.setValue(inputValue, data.value);
530
+ }
531
+
532
+ setValue(text, baseValue) {
533
+ let { instance, data } = this.props;
534
+ let { widget } = instance;
535
+
536
+ let date = widget.parseDate(text, instance);
537
+
538
+ instance.setState({
539
+ inputError: isNaN(date) && widget.inputErrorText,
540
+ });
541
+
542
+ if (!isNaN(date)) {
543
+ let mixed = new Date(baseValue);
544
+ if (date && baseValue && !isNaN(mixed) && widget.partial) {
545
+ switch (widget.segment) {
546
+ case "date":
547
+ mixed.setFullYear(date.getFullYear());
548
+ mixed.setMonth(date.getMonth());
549
+ mixed.setDate(date.getDate());
550
+ break;
551
+
552
+ case "time":
553
+ mixed.setHours(date.getHours());
554
+ mixed.setMinutes(date.getMinutes());
555
+ mixed.setSeconds(date.getSeconds());
556
+ break;
557
+
558
+ default:
559
+ mixed = date;
560
+ break;
561
+ }
562
+
563
+ date = mixed;
564
+ }
565
+
566
+ let encode = widget.encoding || Culture.getDefaultDateEncoding();
567
+
568
+ let value = date ? encode(date) : widget.emptyValue;
569
+
570
+ if (!instance.set("value", value)) this.input.value = value ? Format.value(date, data.format) : "";
571
+ }
572
+ }
573
+ }