@finos/legend-application 8.0.2 → 9.0.1

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 (123) hide show
  1. package/lib/components/LegendApplicationComponentFrameworkProvider.d.ts +4 -2
  2. package/lib/components/LegendApplicationComponentFrameworkProvider.d.ts.map +1 -1
  3. package/lib/components/LegendApplicationComponentFrameworkProvider.js +7 -4
  4. package/lib/components/LegendApplicationComponentFrameworkProvider.js.map +1 -1
  5. package/lib/components/NotificationManager.js +1 -1
  6. package/lib/components/NotificationManager.js.map +1 -1
  7. package/lib/components/WebApplicationNavigatorProvider.d.ts.map +1 -1
  8. package/lib/components/WebApplicationNavigatorProvider.js +1 -1
  9. package/lib/components/WebApplicationNavigatorProvider.js.map +1 -1
  10. package/lib/components/{shared/execution-plan-viewer → execution-plan-viewer}/ExecutionPlanViewer.d.ts +1 -1
  11. package/lib/components/execution-plan-viewer/ExecutionPlanViewer.d.ts.map +1 -0
  12. package/lib/components/{shared/execution-plan-viewer → execution-plan-viewer}/ExecutionPlanViewer.js +3 -3
  13. package/lib/components/execution-plan-viewer/ExecutionPlanViewer.js.map +1 -0
  14. package/lib/components/{shared/execution-plan-viewer → execution-plan-viewer}/SQLExecutionNodeViewer.d.ts +1 -1
  15. package/lib/components/execution-plan-viewer/SQLExecutionNodeViewer.d.ts.map +1 -0
  16. package/lib/components/{shared/execution-plan-viewer → execution-plan-viewer}/SQLExecutionNodeViewer.js +2 -2
  17. package/lib/components/execution-plan-viewer/SQLExecutionNodeViewer.js.map +1 -0
  18. package/lib/components/shared/DocumentationLink.d.ts +5 -0
  19. package/lib/components/shared/DocumentationLink.d.ts.map +1 -1
  20. package/lib/components/shared/DocumentationLink.js +12 -2
  21. package/lib/components/shared/DocumentationLink.js.map +1 -1
  22. package/lib/components/shared/{PackageableElementOptionRenderer.d.ts → PackageableElementOptionLabel.d.ts} +1 -1
  23. package/lib/components/shared/PackageableElementOptionLabel.d.ts.map +1 -0
  24. package/lib/components/shared/{PackageableElementOptionRenderer.js → PackageableElementOptionLabel.js} +5 -5
  25. package/lib/components/shared/PackageableElementOptionLabel.js.map +1 -0
  26. package/lib/components/shared/TextInputEditor.d.ts.map +1 -1
  27. package/lib/components/shared/TextInputEditor.js +1 -2
  28. package/lib/components/shared/TextInputEditor.js.map +1 -1
  29. package/lib/index.css +2 -2
  30. package/lib/index.css.map +1 -1
  31. package/lib/index.d.ts +8 -9
  32. package/lib/index.d.ts.map +1 -1
  33. package/lib/index.js +8 -9
  34. package/lib/index.js.map +1 -1
  35. package/lib/stores/ApplicationStore.d.ts +2 -0
  36. package/lib/stores/ApplicationStore.d.ts.map +1 -1
  37. package/lib/stores/ApplicationStore.js +13 -10
  38. package/lib/stores/ApplicationStore.js.map +1 -1
  39. package/lib/stores/AssistantService.js +1 -1
  40. package/lib/stores/AssistantService.js.map +1 -1
  41. package/lib/stores/{shared/ExecutionPlanState.d.ts → ExecutionPlanState.d.ts} +1 -1
  42. package/lib/stores/ExecutionPlanState.d.ts.map +1 -0
  43. package/lib/stores/{shared/ExecutionPlanState.js → ExecutionPlanState.js} +0 -0
  44. package/lib/stores/ExecutionPlanState.js.map +1 -0
  45. package/lib/stores/LegendApplicationDocumentation.d.ts +2 -1
  46. package/lib/stores/LegendApplicationDocumentation.d.ts.map +1 -1
  47. package/lib/stores/LegendApplicationDocumentation.js +1 -0
  48. package/lib/stores/LegendApplicationDocumentation.js.map +1 -1
  49. package/lib/stores/WebApplicationNavigator.d.ts +62 -30
  50. package/lib/stores/WebApplicationNavigator.d.ts.map +1 -1
  51. package/lib/stores/WebApplicationNavigator.js +80 -15
  52. package/lib/stores/WebApplicationNavigator.js.map +1 -1
  53. package/lib/{components/ApplicationTestID.js → stores/WebApplicationRouter.d.ts} +4 -5
  54. package/lib/stores/WebApplicationRouter.d.ts.map +1 -0
  55. package/{src/components/ApplicationTestID.ts → lib/stores/WebApplicationRouter.js} +4 -4
  56. package/lib/stores/WebApplicationRouter.js.map +1 -0
  57. package/package.json +9 -11
  58. package/src/components/LegendApplicationComponentFrameworkProvider.tsx +18 -14
  59. package/src/components/NotificationManager.tsx +1 -1
  60. package/src/components/WebApplicationNavigatorProvider.tsx +1 -1
  61. package/src/components/{shared/execution-plan-viewer → execution-plan-viewer}/ExecutionPlanViewer.tsx +3 -3
  62. package/src/components/{shared/execution-plan-viewer → execution-plan-viewer}/SQLExecutionNodeViewer.tsx +3 -3
  63. package/src/components/shared/DocumentationLink.tsx +25 -1
  64. package/src/components/shared/{PackageableElementOptionRenderer.tsx → PackageableElementOptionLabel.tsx} +4 -4
  65. package/src/components/shared/TextInputEditor.tsx +1 -2
  66. package/src/index.ts +9 -9
  67. package/src/stores/ApplicationStore.ts +15 -12
  68. package/src/stores/AssistantService.ts +1 -1
  69. package/src/stores/{shared/ExecutionPlanState.ts → ExecutionPlanState.ts} +1 -1
  70. package/src/stores/LegendApplicationDocumentation.ts +1 -0
  71. package/src/stores/WebApplicationNavigator.ts +149 -39
  72. package/{lib/components/ApplicationTestID.d.ts → src/stores/WebApplicationRouter.ts} +12 -4
  73. package/tsconfig.json +7 -16
  74. package/lib/components/ApplicationTestID.d.ts.map +0 -1
  75. package/lib/components/ApplicationTestID.js.map +0 -1
  76. package/lib/components/shared/BasicValueSpecificationEditor.d.ts +0 -52
  77. package/lib/components/shared/BasicValueSpecificationEditor.d.ts.map +0 -1
  78. package/lib/components/shared/BasicValueSpecificationEditor.js +0 -323
  79. package/lib/components/shared/BasicValueSpecificationEditor.js.map +0 -1
  80. package/lib/components/shared/CustomDatePicker.d.ts +0 -38
  81. package/lib/components/shared/CustomDatePicker.d.ts.map +0 -1
  82. package/lib/components/shared/CustomDatePicker.js +0 -616
  83. package/lib/components/shared/CustomDatePicker.js.map +0 -1
  84. package/lib/components/shared/LambdaEditor.d.ts +0 -92
  85. package/lib/components/shared/LambdaEditor.d.ts.map +0 -1
  86. package/lib/components/shared/LambdaEditor.js +0 -434
  87. package/lib/components/shared/LambdaEditor.js.map +0 -1
  88. package/lib/components/shared/LambdaParameterValuesEditor.d.ts +0 -25
  89. package/lib/components/shared/LambdaParameterValuesEditor.d.ts.map +0 -1
  90. package/lib/components/shared/LambdaParameterValuesEditor.js +0 -52
  91. package/lib/components/shared/LambdaParameterValuesEditor.js.map +0 -1
  92. package/lib/components/shared/PackageableElementOptionRenderer.d.ts.map +0 -1
  93. package/lib/components/shared/PackageableElementOptionRenderer.js.map +0 -1
  94. package/lib/components/shared/execution-plan-viewer/ExecutionPlanViewer.d.ts.map +0 -1
  95. package/lib/components/shared/execution-plan-viewer/ExecutionPlanViewer.js.map +0 -1
  96. package/lib/components/shared/execution-plan-viewer/SQLExecutionNodeViewer.d.ts.map +0 -1
  97. package/lib/components/shared/execution-plan-viewer/SQLExecutionNodeViewer.js.map +0 -1
  98. package/lib/stores/CJS__Fuse.cjs +0 -35
  99. package/lib/stores/CJS__Fuse.cjs.map +0 -1
  100. package/lib/stores/CJS__Fuse.d.cts +0 -28
  101. package/lib/stores/CJS__Fuse.d.cts.map +0 -1
  102. package/lib/stores/shared/ExecutionPlanState.d.ts.map +0 -1
  103. package/lib/stores/shared/ExecutionPlanState.js.map +0 -1
  104. package/lib/stores/shared/LambdaEditorState.d.ts +0 -40
  105. package/lib/stores/shared/LambdaEditorState.d.ts.map +0 -1
  106. package/lib/stores/shared/LambdaEditorState.js +0 -81
  107. package/lib/stores/shared/LambdaEditorState.js.map +0 -1
  108. package/lib/stores/shared/LambdaParameterState.d.ts +0 -62
  109. package/lib/stores/shared/LambdaParameterState.d.ts.map +0 -1
  110. package/lib/stores/shared/LambdaParameterState.js +0 -160
  111. package/lib/stores/shared/LambdaParameterState.js.map +0 -1
  112. package/lib/stores/shared/ValueSpecificationModifierHelper.d.ts +0 -27
  113. package/lib/stores/shared/ValueSpecificationModifierHelper.d.ts.map +0 -1
  114. package/lib/stores/shared/ValueSpecificationModifierHelper.js +0 -49
  115. package/lib/stores/shared/ValueSpecificationModifierHelper.js.map +0 -1
  116. package/src/components/shared/BasicValueSpecificationEditor.tsx +0 -828
  117. package/src/components/shared/CustomDatePicker.tsx +0 -1292
  118. package/src/components/shared/LambdaEditor.tsx +0 -854
  119. package/src/components/shared/LambdaParameterValuesEditor.tsx +0 -118
  120. package/src/stores/CJS__Fuse.cts +0 -28
  121. package/src/stores/shared/LambdaEditorState.ts +0 -118
  122. package/src/stores/shared/LambdaParameterState.ts +0 -253
  123. package/src/stores/shared/ValueSpecificationModifierHelper.ts +0 -104
@@ -1,1292 +0,0 @@
1
- /**
2
- * Copyright (c) 2020-present, Goldman Sachs
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import {
18
- type SelectComponent,
19
- BasePopover,
20
- BaseRadioGroup,
21
- CustomSelectorInput,
22
- } from '@finos/legend-art';
23
- import {
24
- type PureModel,
25
- type Enum,
26
- type Type,
27
- type ValueSpecification,
28
- PRIMITIVE_TYPE,
29
- SimpleFunctionExpression,
30
- InstanceValue,
31
- GenericType,
32
- PrimitiveInstanceValue,
33
- GenericTypeExplicitReference,
34
- EnumValueExplicitReference,
35
- EnumValueInstanceValue,
36
- matchFunctionName,
37
- TYPICAL_MULTIPLICITY_TYPE,
38
- SUPPORTED_FUNCTIONS,
39
- DAY_OF_WEEK,
40
- buildPrimitiveInstanceValue,
41
- DURATION_UNIT,
42
- } from '@finos/legend-graph';
43
- import {
44
- guaranteeNonNullable,
45
- parseNumber,
46
- returnUndefOnError,
47
- UnsupportedOperationError,
48
- } from '@finos/legend-shared';
49
- import { useEffect, useRef, useState } from 'react';
50
- import {
51
- genericType_setRawType,
52
- instanceValue_changeValue,
53
- } from '../../stores/shared/ValueSpecificationModifierHelper.js';
54
-
55
- enum CUSTOM_DATE_PICKER_OPTION {
56
- ABSOLUTE_DATE = 'Absolute Date',
57
- ABSOLUTE_TIME = 'Absolute Time',
58
- TODAY = 'Today',
59
- NOW = 'Now',
60
- YESTERDAY = 'Yesterday',
61
- ONE_YEAR_AGO = 'One Year Ago',
62
- ONE_MONTH_AGO = 'One Month Ago',
63
- ONE_WEEK_AGO = 'One Week Ago',
64
- CUSTOM_DATE = 'Custom Date',
65
- PREVIOUS_DAY_OF_WEEK = 'Previous ... of Week',
66
- FIRST_DAY_OF = 'First day of...',
67
- LATEST_DATE = 'Latest Date',
68
- }
69
-
70
- enum CUSTOM_DATE_OPTION_UNIT {
71
- DAYS = 'Day(s)',
72
- WEEKS = 'Week(s)',
73
- MONTHS = 'Month(s)',
74
- YEARS = 'Year(s)',
75
- }
76
-
77
- enum CUSTOM_DATE_FIRST_DAY_OF_UNIT {
78
- WEEK = 'Week',
79
- MONTH = 'Month',
80
- QUARTER = 'Quarter',
81
- YEAR = 'Year',
82
- }
83
-
84
- enum CUSTOM_DATE_DAY_OF_WEEK {
85
- MONDAY = 'Monday',
86
- TUESDAY = 'Tuesday',
87
- WENDNESDAY = 'Wednesday',
88
- THURSDAY = 'Thursday',
89
- FRIDAY = 'Friday',
90
- SATURDAY = 'Saturday',
91
- SUNDAY = 'Sunday',
92
- }
93
-
94
- enum CUSTOM_DATE_OPTION_DIRECTION {
95
- BEFORE = 'Before',
96
- AFTER = 'After',
97
- }
98
-
99
- enum CUSTOM_DATE_OPTION_REFERENCE_MOMENT {
100
- TODAY = 'Today',
101
- NOW = 'Now',
102
- FIRST_DAY_OF_YEAR = 'Start of Year',
103
- FIRST_DAY_OF_QUARTER = 'Start of Quarter',
104
- FIRST_DAY_OF_MONTH = 'Start of Month',
105
- FIRST_DAY_OF_WEEK = 'Start of Week',
106
- }
107
-
108
- /**
109
- * DatePickerOption is the base class being used to display and generate the corresponding pure date function.
110
- */
111
- class DatePickerOption {
112
- /**
113
- * label is the text that shows up in the valueSpecification box.
114
- */
115
- label: string;
116
- /**
117
- * value is the selected date option in date-dropdown.
118
- */
119
- value: string;
120
-
121
- constructor(label: string, value: string) {
122
- this.label = label;
123
- this.value = value;
124
- }
125
- }
126
-
127
- class CustomDateOption extends DatePickerOption {
128
- /**
129
- * duration is the amount of time span that will be adjusted.
130
- */
131
- duration: number;
132
- /**
133
- * unit represents the time duration unit, e.g. year, week, etc.
134
- */
135
- unit: CUSTOM_DATE_OPTION_UNIT | undefined;
136
- /**
137
- * direction means the direction in which time adjustment will go to.
138
- */
139
- direction: CUSTOM_DATE_OPTION_DIRECTION | undefined;
140
- /**
141
- * referenceMoment is the date which adjustment starts from.
142
- */
143
- referenceMoment: CUSTOM_DATE_OPTION_REFERENCE_MOMENT | undefined;
144
-
145
- constructor(
146
- label: string,
147
- value: string,
148
- duration: number,
149
- unit: CUSTOM_DATE_OPTION_UNIT | undefined,
150
- direction: CUSTOM_DATE_OPTION_DIRECTION | undefined,
151
- referenceMoment: CUSTOM_DATE_OPTION_REFERENCE_MOMENT | undefined,
152
- ) {
153
- super(label, value);
154
- this.duration = duration;
155
- this.unit = unit;
156
- this.direction = direction;
157
- this.referenceMoment = referenceMoment;
158
- }
159
-
160
- generateDisplayLabel(): string {
161
- return [
162
- this.duration,
163
- this.unit,
164
- this.direction,
165
- this.referenceMoment,
166
- ].join(' ');
167
- }
168
-
169
- updateLabel(): void {
170
- this.label = this.generateDisplayLabel();
171
- }
172
- }
173
-
174
- class CustomFirstDayOfOption extends DatePickerOption {
175
- /**
176
- * unit: time unit, e.g. Week, Month, etc.
177
- */
178
- unit: CUSTOM_DATE_FIRST_DAY_OF_UNIT | undefined;
179
-
180
- constructor(label: string, unit: CUSTOM_DATE_FIRST_DAY_OF_UNIT | undefined) {
181
- super(label, CUSTOM_DATE_PICKER_OPTION.FIRST_DAY_OF);
182
- this.unit = unit;
183
- }
184
- }
185
-
186
- class CustomPreviousDayOfWeekOption extends DatePickerOption {
187
- /**
188
- * day: which day in the week will be selected.
189
- */
190
- day: CUSTOM_DATE_DAY_OF_WEEK;
191
-
192
- constructor(label: string, day: CUSTOM_DATE_DAY_OF_WEEK) {
193
- super(label, CUSTOM_DATE_PICKER_OPTION.PREVIOUS_DAY_OF_WEEK);
194
- this.day = day;
195
- }
196
- }
197
-
198
- const reservedCustomDateOptions: CustomDateOption[] = [
199
- new CustomDateOption(
200
- 'Yesterday',
201
- CUSTOM_DATE_PICKER_OPTION.YESTERDAY,
202
- 1,
203
- CUSTOM_DATE_OPTION_UNIT.DAYS,
204
- CUSTOM_DATE_OPTION_DIRECTION.BEFORE,
205
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY,
206
- ),
207
- new CustomDateOption(
208
- 'One Week Ago',
209
- CUSTOM_DATE_PICKER_OPTION.ONE_WEEK_AGO,
210
- 1,
211
- CUSTOM_DATE_OPTION_UNIT.WEEKS,
212
- CUSTOM_DATE_OPTION_DIRECTION.BEFORE,
213
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY,
214
- ),
215
- new CustomDateOption(
216
- 'One Month Ago',
217
- CUSTOM_DATE_PICKER_OPTION.ONE_MONTH_AGO,
218
- 1,
219
- CUSTOM_DATE_OPTION_UNIT.MONTHS,
220
- CUSTOM_DATE_OPTION_DIRECTION.BEFORE,
221
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY,
222
- ),
223
- new CustomDateOption(
224
- 'One Year Ago',
225
- CUSTOM_DATE_PICKER_OPTION.ONE_YEAR_AGO,
226
- 1,
227
- CUSTOM_DATE_OPTION_UNIT.YEARS,
228
- CUSTOM_DATE_OPTION_DIRECTION.BEFORE,
229
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY,
230
- ),
231
- ];
232
-
233
- /**
234
- * Generate pure date functions based on the DatePickerOption.
235
- */
236
- const buildPureDateFunctionExpression = (
237
- datePickerOption: DatePickerOption,
238
- graph: PureModel,
239
- ): SimpleFunctionExpression => {
240
- const multiplicityOne = graph.getTypicalMultiplicity(
241
- TYPICAL_MULTIPLICITY_TYPE.ONE,
242
- );
243
- const strictDate = graph.getPrimitiveType(PRIMITIVE_TYPE.STRICTDATE);
244
- const date = graph.getPrimitiveType(PRIMITIVE_TYPE.DATE);
245
- const dateTime = graph.getPrimitiveType(PRIMITIVE_TYPE.DATETIME);
246
- if (datePickerOption instanceof CustomPreviousDayOfWeekOption) {
247
- const previousFridaySFE = new SimpleFunctionExpression(
248
- SUPPORTED_FUNCTIONS.PREVIOUS_DAY_OF_WEEK,
249
- multiplicityOne,
250
- );
251
- previousFridaySFE.genericType = GenericTypeExplicitReference.create(
252
- new GenericType(date),
253
- );
254
- const dayOfWeekEnumIntanceValue = new EnumValueInstanceValue(
255
- GenericTypeExplicitReference.create(
256
- new GenericType(graph.getType(DAY_OF_WEEK)),
257
- ),
258
- multiplicityOne,
259
- );
260
- dayOfWeekEnumIntanceValue.values.push(
261
- EnumValueExplicitReference.create(
262
- guaranteeNonNullable(
263
- graph
264
- .getEnumeration(DAY_OF_WEEK)
265
- .values.filter((e) => e.name === datePickerOption.day)[0],
266
- ),
267
- ),
268
- );
269
- previousFridaySFE.parametersValues.push(dayOfWeekEnumIntanceValue);
270
- return previousFridaySFE;
271
- } else if (datePickerOption instanceof CustomFirstDayOfOption) {
272
- switch (datePickerOption.unit) {
273
- case CUSTOM_DATE_FIRST_DAY_OF_UNIT.YEAR: {
274
- const firstDayOfYearSFE = new SimpleFunctionExpression(
275
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_YEAR,
276
- multiplicityOne,
277
- );
278
- firstDayOfYearSFE.genericType = GenericTypeExplicitReference.create(
279
- new GenericType(date),
280
- );
281
- return firstDayOfYearSFE;
282
- }
283
- case CUSTOM_DATE_FIRST_DAY_OF_UNIT.QUARTER: {
284
- const firstDayOfQuarterSFE = new SimpleFunctionExpression(
285
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_QUARTER,
286
- multiplicityOne,
287
- );
288
- firstDayOfQuarterSFE.genericType = GenericTypeExplicitReference.create(
289
- new GenericType(strictDate),
290
- );
291
- return firstDayOfQuarterSFE;
292
- }
293
- case CUSTOM_DATE_FIRST_DAY_OF_UNIT.MONTH: {
294
- const firstDayOfMonthSFE = new SimpleFunctionExpression(
295
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_MONTH,
296
- multiplicityOne,
297
- );
298
- firstDayOfMonthSFE.genericType = GenericTypeExplicitReference.create(
299
- new GenericType(date),
300
- );
301
- return firstDayOfMonthSFE;
302
- }
303
- case CUSTOM_DATE_FIRST_DAY_OF_UNIT.WEEK: {
304
- const firstDayOfWeekSFE = new SimpleFunctionExpression(
305
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_WEEK,
306
- multiplicityOne,
307
- );
308
- firstDayOfWeekSFE.genericType = GenericTypeExplicitReference.create(
309
- new GenericType(date),
310
- );
311
- return firstDayOfWeekSFE;
312
- }
313
- default:
314
- throw new UnsupportedOperationError(
315
- `Can't build expression for 'First Day Of ...' date picker option for unit '${datePickerOption.unit}'`,
316
- );
317
- }
318
- } else {
319
- switch (datePickerOption.value) {
320
- case CUSTOM_DATE_PICKER_OPTION.TODAY: {
321
- const todaySFE = new SimpleFunctionExpression(
322
- SUPPORTED_FUNCTIONS.TODAY,
323
- multiplicityOne,
324
- );
325
- todaySFE.genericType = GenericTypeExplicitReference.create(
326
- new GenericType(strictDate),
327
- );
328
- return todaySFE;
329
- }
330
- case CUSTOM_DATE_PICKER_OPTION.NOW: {
331
- const nowSFE = new SimpleFunctionExpression(
332
- SUPPORTED_FUNCTIONS.NOW,
333
- multiplicityOne,
334
- );
335
- nowSFE.genericType = GenericTypeExplicitReference.create(
336
- new GenericType(dateTime),
337
- );
338
- return nowSFE;
339
- }
340
- case CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_YEAR: {
341
- const firstDayOfYearSFE = new SimpleFunctionExpression(
342
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_YEAR,
343
- multiplicityOne,
344
- );
345
- firstDayOfYearSFE.genericType = GenericTypeExplicitReference.create(
346
- new GenericType(date),
347
- );
348
- return firstDayOfYearSFE;
349
- }
350
- case CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_QUARTER: {
351
- const firstDayOfQuarterSFE = new SimpleFunctionExpression(
352
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_QUARTER,
353
- multiplicityOne,
354
- );
355
- firstDayOfQuarterSFE.genericType = GenericTypeExplicitReference.create(
356
- new GenericType(strictDate),
357
- );
358
- return firstDayOfQuarterSFE;
359
- }
360
- case CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_MONTH: {
361
- const firstDayOfMonthSFE = new SimpleFunctionExpression(
362
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_MONTH,
363
- multiplicityOne,
364
- );
365
- firstDayOfMonthSFE.genericType = GenericTypeExplicitReference.create(
366
- new GenericType(date),
367
- );
368
- return firstDayOfMonthSFE;
369
- }
370
- case CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_WEEK: {
371
- const firstDayOfWeekSFE = new SimpleFunctionExpression(
372
- SUPPORTED_FUNCTIONS.FIRST_DAY_OF_WEEK,
373
- multiplicityOne,
374
- );
375
- firstDayOfWeekSFE.genericType = GenericTypeExplicitReference.create(
376
- new GenericType(date),
377
- );
378
- return firstDayOfWeekSFE;
379
- }
380
- default:
381
- throw new UnsupportedOperationError(
382
- `Can't build expression for date picker option '${datePickerOption.value}'`,
383
- );
384
- }
385
- }
386
- };
387
-
388
- /**
389
- * Generate the enum value of type Pure Enum, DURATION_UNIT, based on the input string.
390
- */
391
- const buildPureDurationEnumValue = (
392
- unitString: string,
393
- graph: PureModel,
394
- ): Enum => {
395
- const durationUnitEnum = graph.getEnumeration(DURATION_UNIT);
396
- const targetPureDurationEnumValue = durationUnitEnum.values.filter(
397
- (e) =>
398
- e.name ===
399
- Object.keys(CUSTOM_DATE_OPTION_UNIT).filter(
400
- (key) =>
401
- CUSTOM_DATE_OPTION_UNIT[
402
- key as keyof typeof CUSTOM_DATE_OPTION_UNIT
403
- ] === unitString,
404
- )[0],
405
- )[0];
406
- return (
407
- targetPureDurationEnumValue ??
408
- guaranteeNonNullable(durationUnitEnum.values[0])
409
- );
410
- };
411
-
412
- /**
413
- * Generate the pure date ajust() function based on the CustomDateOption.
414
- */
415
- const buildPureAdjustDateFunction = (
416
- customDateOption: CustomDateOption,
417
- graph: PureModel,
418
- ): SimpleFunctionExpression => {
419
- const multiplicityOne = graph.getTypicalMultiplicity(
420
- TYPICAL_MULTIPLICITY_TYPE.ONE,
421
- );
422
- const dateAdjustSimpleFunctionExpression = new SimpleFunctionExpression(
423
- SUPPORTED_FUNCTIONS.ADJUST,
424
- multiplicityOne,
425
- );
426
- dateAdjustSimpleFunctionExpression.parametersValues.push(
427
- buildPureDateFunctionExpression(
428
- new DatePickerOption(
429
- guaranteeNonNullable(customDateOption.referenceMoment),
430
- guaranteeNonNullable(customDateOption.referenceMoment),
431
- ),
432
- graph,
433
- ),
434
- );
435
- if (customDateOption.direction === CUSTOM_DATE_OPTION_DIRECTION.BEFORE) {
436
- const minusFunc = new SimpleFunctionExpression(
437
- SUPPORTED_FUNCTIONS.MINUS,
438
- multiplicityOne,
439
- );
440
- minusFunc.parametersValues.push(
441
- buildPrimitiveInstanceValue(
442
- graph,
443
- PRIMITIVE_TYPE.INTEGER,
444
- customDateOption.duration,
445
- ),
446
- );
447
- dateAdjustSimpleFunctionExpression.parametersValues.push(minusFunc);
448
- } else {
449
- const adjustmentInstanceValue = buildPrimitiveInstanceValue(
450
- graph,
451
- PRIMITIVE_TYPE.INTEGER,
452
- customDateOption.duration,
453
- );
454
- dateAdjustSimpleFunctionExpression.parametersValues.push(
455
- adjustmentInstanceValue,
456
- );
457
- }
458
- const durationUnitEnumIntanceValue = new EnumValueInstanceValue(
459
- GenericTypeExplicitReference.create(
460
- new GenericType(graph.getType(DURATION_UNIT)),
461
- ),
462
- multiplicityOne,
463
- );
464
- durationUnitEnumIntanceValue.values.push(
465
- EnumValueExplicitReference.create(
466
- guaranteeNonNullable(
467
- buildPureDurationEnumValue(
468
- guaranteeNonNullable(customDateOption.unit),
469
- graph,
470
- ),
471
- ),
472
- ),
473
- );
474
- dateAdjustSimpleFunctionExpression.parametersValues.push(
475
- durationUnitEnumIntanceValue,
476
- );
477
- dateAdjustSimpleFunctionExpression.genericType =
478
- GenericTypeExplicitReference.create(
479
- new GenericType(graph.getPrimitiveType(PRIMITIVE_TYPE.DATE)),
480
- );
481
- return dateAdjustSimpleFunctionExpression;
482
- };
483
-
484
- /**
485
- * Generate the value of CustomDateOption.duration from the pure date adjust() function.
486
- */
487
- const buildCustomDateOptionDurationValue = (
488
- pureDateAdjustFunction: SimpleFunctionExpression,
489
- ): number => {
490
- const durationParam = pureDateAdjustFunction.parametersValues[1];
491
- return durationParam instanceof PrimitiveInstanceValue
492
- ? (durationParam.values[0] as number)
493
- : durationParam instanceof SimpleFunctionExpression &&
494
- matchFunctionName(durationParam.functionName, SUPPORTED_FUNCTIONS.MINUS)
495
- ? durationParam.parametersValues[0] instanceof PrimitiveInstanceValue
496
- ? (durationParam.parametersValues[0].values[0] as number)
497
- : 0
498
- : 0;
499
- };
500
-
501
- /**
502
- * Generate the value of CustomDateOption.direction from the pure date adjust() function.
503
- */
504
- const buildCustomDateOptionDirectionValue = (
505
- pureDateAdjustFunction: SimpleFunctionExpression,
506
- ): CUSTOM_DATE_OPTION_DIRECTION =>
507
- pureDateAdjustFunction.parametersValues[1] instanceof
508
- SimpleFunctionExpression &&
509
- matchFunctionName(
510
- pureDateAdjustFunction.parametersValues[1].functionName,
511
- SUPPORTED_FUNCTIONS.MINUS,
512
- )
513
- ? CUSTOM_DATE_OPTION_DIRECTION.BEFORE
514
- : CUSTOM_DATE_OPTION_DIRECTION.AFTER;
515
-
516
- /**
517
- * Generate the value of CustomDateOption.unit from the pure date adjust() function.
518
- */
519
- const buildCustomDateOptionUnitValue = (
520
- valueSpecification: SimpleFunctionExpression,
521
- ): CUSTOM_DATE_OPTION_UNIT =>
522
- guaranteeNonNullable(
523
- Object.keys(CUSTOM_DATE_OPTION_UNIT)
524
- .filter(
525
- (key) =>
526
- key ===
527
- (valueSpecification.parametersValues[2] as EnumValueInstanceValue)
528
- .values[0]?.value.name,
529
- )
530
- .map(
531
- (key) =>
532
- CUSTOM_DATE_OPTION_UNIT[key as keyof typeof CUSTOM_DATE_OPTION_UNIT],
533
- )[0],
534
- );
535
-
536
- /**
537
- * Generate the value of CustomDateOption.moment from the pure date adjust() function.
538
- */
539
- const buildCustomDateOptionReferenceMomentValue = (
540
- pureDateAjustFunction: SimpleFunctionExpression,
541
- ): CUSTOM_DATE_OPTION_REFERENCE_MOMENT => {
542
- const funcName = (
543
- pureDateAjustFunction.parametersValues[0] as SimpleFunctionExpression
544
- ).functionName;
545
- switch (funcName) {
546
- case SUPPORTED_FUNCTIONS.TODAY:
547
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY;
548
- case SUPPORTED_FUNCTIONS.NOW:
549
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.NOW;
550
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_YEAR:
551
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_YEAR;
552
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_QUARTER:
553
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_QUARTER;
554
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_MONTH:
555
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_MONTH;
556
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_WEEK:
557
- return CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_WEEK;
558
- default:
559
- throw new UnsupportedOperationError(
560
- `Can't build custom date option reference moment '${funcName}'`,
561
- );
562
- }
563
- };
564
-
565
- /**
566
- * Build CustomDateOption based on the pure date adjust() function.
567
- * Transform CustomDateOption if it matches any preserved custom adjust date functions. e.g. One Month Ago..
568
- */
569
- const buildCustomDateOption = (
570
- valueSpecification: SimpleFunctionExpression | PrimitiveInstanceValue,
571
- ): CustomDateOption => {
572
- if (
573
- valueSpecification instanceof SimpleFunctionExpression &&
574
- matchFunctionName(
575
- valueSpecification.functionName,
576
- SUPPORTED_FUNCTIONS.ADJUST,
577
- )
578
- ) {
579
- const customDateOption = new CustomDateOption(
580
- '',
581
- CUSTOM_DATE_PICKER_OPTION.CUSTOM_DATE,
582
- buildCustomDateOptionDurationValue(valueSpecification),
583
- buildCustomDateOptionUnitValue(valueSpecification),
584
- buildCustomDateOptionDirectionValue(valueSpecification),
585
- buildCustomDateOptionReferenceMomentValue(valueSpecification),
586
- );
587
- const matchedPreservedCustomAdjustDates = reservedCustomDateOptions.filter(
588
- (t) =>
589
- t.generateDisplayLabel() === customDateOption.generateDisplayLabel(),
590
- );
591
- if (matchedPreservedCustomAdjustDates.length > 0) {
592
- customDateOption.label = guaranteeNonNullable(
593
- matchedPreservedCustomAdjustDates[0]?.label,
594
- );
595
- customDateOption.value = guaranteeNonNullable(
596
- matchedPreservedCustomAdjustDates[0]?.value,
597
- );
598
- return customDateOption;
599
- }
600
- customDateOption.updateLabel();
601
- return customDateOption;
602
- }
603
- return new CustomDateOption('', '', 0, undefined, undefined, undefined);
604
- };
605
-
606
- /**
607
- * Build DatePickerOption from pure date functions or PrimitiveInstanceValue
608
- */
609
- const buildDatePickerOption = (
610
- valueSpecification: SimpleFunctionExpression | PrimitiveInstanceValue,
611
- ): DatePickerOption => {
612
- if (valueSpecification instanceof SimpleFunctionExpression) {
613
- switch (valueSpecification.functionName) {
614
- case SUPPORTED_FUNCTIONS.TODAY:
615
- return new DatePickerOption(
616
- CUSTOM_DATE_PICKER_OPTION.TODAY,
617
- CUSTOM_DATE_PICKER_OPTION.TODAY,
618
- );
619
- case SUPPORTED_FUNCTIONS.NOW:
620
- return new DatePickerOption(
621
- CUSTOM_DATE_PICKER_OPTION.NOW,
622
- CUSTOM_DATE_PICKER_OPTION.NOW,
623
- );
624
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_YEAR:
625
- return new CustomFirstDayOfOption(
626
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_YEAR,
627
- CUSTOM_DATE_FIRST_DAY_OF_UNIT.YEAR,
628
- );
629
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_QUARTER:
630
- return new CustomFirstDayOfOption(
631
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_QUARTER,
632
- CUSTOM_DATE_FIRST_DAY_OF_UNIT.QUARTER,
633
- );
634
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_MONTH:
635
- return new CustomFirstDayOfOption(
636
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_MONTH,
637
- CUSTOM_DATE_FIRST_DAY_OF_UNIT.MONTH,
638
- );
639
- case SUPPORTED_FUNCTIONS.FIRST_DAY_OF_WEEK:
640
- return new CustomFirstDayOfOption(
641
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.FIRST_DAY_OF_WEEK,
642
- CUSTOM_DATE_FIRST_DAY_OF_UNIT.WEEK,
643
- );
644
- case SUPPORTED_FUNCTIONS.PREVIOUS_DAY_OF_WEEK:
645
- return new CustomPreviousDayOfWeekOption(
646
- `Previous ${
647
- (valueSpecification.parametersValues[0] as EnumValueInstanceValue)
648
- .values[0]?.value.name
649
- }`,
650
- (valueSpecification.parametersValues[0] as EnumValueInstanceValue)
651
- .values[0]?.value.name as CUSTOM_DATE_DAY_OF_WEEK,
652
- );
653
-
654
- case SUPPORTED_FUNCTIONS.ADJUST:
655
- return buildCustomDateOption(valueSpecification);
656
- default:
657
- return new DatePickerOption('', '');
658
- }
659
- } else {
660
- return valueSpecification.genericType.value.rawType.path ===
661
- PRIMITIVE_TYPE.LATESTDATE
662
- ? new DatePickerOption(
663
- CUSTOM_DATE_PICKER_OPTION.LATEST_DATE,
664
- CUSTOM_DATE_PICKER_OPTION.LATEST_DATE,
665
- )
666
- : new DatePickerOption(
667
- valueSpecification.values[0] as string,
668
- valueSpecification.genericType.value.rawType.path ===
669
- PRIMITIVE_TYPE.DATETIME
670
- ? CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_TIME
671
- : CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_DATE,
672
- );
673
- }
674
- };
675
-
676
- const AbsoluteDateValueSpecificationEditor: React.FC<{
677
- valueSpecification: SimpleFunctionExpression | PrimitiveInstanceValue;
678
- graph: PureModel;
679
- setValueSpecification: (val: ValueSpecification) => void;
680
- setDatePickerOption: (datePickerOption: DatePickerOption) => void;
681
- }> = (props) => {
682
- const {
683
- valueSpecification,
684
- graph,
685
- setValueSpecification,
686
- setDatePickerOption,
687
- } = props;
688
- const inputRef = useRef<HTMLInputElement>(null);
689
- const absoluteDateValue =
690
- valueSpecification instanceof SimpleFunctionExpression
691
- ? ''
692
- : (valueSpecification.values[0] as string);
693
- const updateAbsoluteDateValue: React.ChangeEventHandler<HTMLInputElement> = (
694
- event,
695
- ) => {
696
- if (valueSpecification instanceof SimpleFunctionExpression) {
697
- setValueSpecification(
698
- buildPrimitiveInstanceValue(
699
- graph,
700
- PRIMITIVE_TYPE.STRICTDATE,
701
- event.target.value,
702
- ),
703
- );
704
- } else if (valueSpecification instanceof InstanceValue) {
705
- instanceValue_changeValue(valueSpecification, event.target.value, 0);
706
- if (
707
- valueSpecification.genericType.value.rawType.path !==
708
- PRIMITIVE_TYPE.STRICTDATE
709
- ) {
710
- genericType_setRawType(
711
- valueSpecification.genericType.value,
712
- graph.getPrimitiveType(PRIMITIVE_TYPE.STRICTDATE),
713
- );
714
- }
715
- setValueSpecification(valueSpecification);
716
- }
717
- setDatePickerOption(
718
- new DatePickerOption(
719
- event.target.value,
720
- CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_DATE,
721
- ),
722
- );
723
- };
724
-
725
- useEffect(() => {
726
- inputRef.current?.focus();
727
- }, []);
728
-
729
- return (
730
- <div className="value-spec-editor__date-picker__absolute-date">
731
- <input
732
- ref={inputRef}
733
- className="panel__content__form__section__input value-spec-editor__date-picker__absolute-date__input input--dark"
734
- type="date"
735
- spellCheck={false}
736
- value={absoluteDateValue}
737
- onChange={updateAbsoluteDateValue}
738
- />
739
- </div>
740
- );
741
- };
742
-
743
- const AbsoluteTimeValueSpecificationEditor: React.FC<{
744
- valueSpecification: SimpleFunctionExpression | PrimitiveInstanceValue;
745
- graph: PureModel;
746
- setValueSpecification: (val: ValueSpecification) => void;
747
- setDatePickerOption: (datePickerOption: DatePickerOption) => void;
748
- }> = (props) => {
749
- const {
750
- valueSpecification,
751
- graph,
752
- setValueSpecification,
753
- setDatePickerOption,
754
- } = props;
755
- const inputRef = useRef<HTMLInputElement>(null);
756
- const absoluteTimeValue =
757
- valueSpecification instanceof SimpleFunctionExpression
758
- ? ''
759
- : (valueSpecification.values[0] as string);
760
- const updateAbsoluteTimeValue: React.ChangeEventHandler<HTMLInputElement> = (
761
- event,
762
- ) => {
763
- if (valueSpecification instanceof SimpleFunctionExpression) {
764
- setValueSpecification(
765
- buildPrimitiveInstanceValue(
766
- graph,
767
- PRIMITIVE_TYPE.DATETIME,
768
- event.target.value,
769
- ),
770
- );
771
- } else {
772
- instanceValue_changeValue(valueSpecification, event.target.value, 0);
773
- if (
774
- valueSpecification.genericType.value.rawType.path !==
775
- PRIMITIVE_TYPE.DATETIME
776
- ) {
777
- genericType_setRawType(
778
- valueSpecification.genericType.value,
779
- graph.getPrimitiveType(PRIMITIVE_TYPE.DATETIME),
780
- );
781
- }
782
- setValueSpecification(valueSpecification);
783
- }
784
- setDatePickerOption(
785
- new DatePickerOption(
786
- event.target.value,
787
- CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_TIME,
788
- ),
789
- );
790
- };
791
-
792
- useEffect(() => {
793
- inputRef.current?.focus();
794
- }, []);
795
-
796
- return (
797
- <div className="value-spec-editor__date-picker__absolute-date">
798
- <input
799
- ref={inputRef}
800
- className="panel__content__form__section__input value-spec-editor__date-picker__absolute-date__input input--dark"
801
- // Despite its name this would actually allow us to register time in UTC
802
- // See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local#setting_timezones
803
- type="datetime-local"
804
- spellCheck={false}
805
- value={absoluteTimeValue}
806
- onChange={updateAbsoluteTimeValue}
807
- />
808
- </div>
809
- );
810
- };
811
-
812
- const CustomDateInstanceValueEditor: React.FC<{
813
- customDateOptionValue: CustomDateOption;
814
- graph: PureModel;
815
- setValueSpecification: (val: ValueSpecification) => void;
816
- setDatePickerOption: (datePickerOption: DatePickerOption) => void;
817
- }> = (props) => {
818
- const {
819
- customDateOptionValue,
820
- graph,
821
- setValueSpecification,
822
- setDatePickerOption,
823
- } = props;
824
- const inputRef = useRef<HTMLInputElement>(null);
825
- const [durationValue, setDurationValue] = useState(
826
- customDateOptionValue.duration,
827
- );
828
- const [unitValue, setUnitValue] = useState(
829
- customDateOptionValue.unit ?? CUSTOM_DATE_OPTION_UNIT.DAYS,
830
- );
831
- const [directionValue, setDirectionValue] = useState(
832
- customDateOptionValue.direction ?? CUSTOM_DATE_OPTION_DIRECTION.BEFORE,
833
- );
834
- const [referenceMomentValue, setReferenceMomentValueValue] = useState(
835
- customDateOptionValue.referenceMoment ??
836
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT.TODAY,
837
- );
838
- const changeValue = (
839
- latestDurationValue: number,
840
- latestUnitValue: string,
841
- latestDirectionValue: string,
842
- latestReferenceMomentValue: string,
843
- ): void => {
844
- if (
845
- latestDurationValue !== 0 &&
846
- latestUnitValue !== '' &&
847
- latestDirectionValue !== '' &&
848
- latestReferenceMomentValue !== ''
849
- ) {
850
- const dateOption = new CustomDateOption(
851
- CUSTOM_DATE_PICKER_OPTION.CUSTOM_DATE,
852
- CUSTOM_DATE_PICKER_OPTION.CUSTOM_DATE,
853
- latestDurationValue,
854
- latestUnitValue as CUSTOM_DATE_OPTION_UNIT,
855
- latestDirectionValue as CUSTOM_DATE_OPTION_DIRECTION,
856
- latestReferenceMomentValue as CUSTOM_DATE_OPTION_REFERENCE_MOMENT,
857
- );
858
- setValueSpecification(buildPureAdjustDateFunction(dateOption, graph));
859
- const matchedPreservedCustomAdjustDates =
860
- reservedCustomDateOptions.filter(
861
- (t) => t.generateDisplayLabel() === dateOption.generateDisplayLabel(),
862
- );
863
- if (matchedPreservedCustomAdjustDates.length > 0) {
864
- dateOption.label = guaranteeNonNullable(
865
- matchedPreservedCustomAdjustDates[0]?.label,
866
- );
867
- dateOption.value = guaranteeNonNullable(
868
- matchedPreservedCustomAdjustDates[0]?.value,
869
- );
870
- } else {
871
- dateOption.updateLabel();
872
- }
873
- setDatePickerOption(dateOption);
874
- }
875
- };
876
- const changeDurationValue: React.ChangeEventHandler<HTMLInputElement> = (
877
- event,
878
- ) => {
879
- const duration =
880
- event.target.value !== ''
881
- ? returnUndefOnError(() => parseNumber(event.target.value)) ?? 0
882
- : 0;
883
- setDurationValue(duration);
884
- changeValue(duration, unitValue, directionValue, referenceMomentValue);
885
- };
886
-
887
- useEffect(() => {
888
- inputRef.current?.focus();
889
- }, []);
890
-
891
- return (
892
- <div className="value-spec-editor__date-picker__custom-date">
893
- <div className="value-spec-editor__date-picker__custom-date__input">
894
- <input
895
- ref={inputRef}
896
- className="value-spec-editor__date-picker__custom-date__input-text-editor input--dark"
897
- spellCheck={false}
898
- value={durationValue}
899
- type="number"
900
- onChange={changeDurationValue}
901
- />
902
- </div>
903
- <div className="value-spec-editor__date-picker__custom-date__input">
904
- <CustomSelectorInput
905
- placeholder="Unit"
906
- className="value-spec-editor__date-picker__custom-date__input-dropdown"
907
- options={Object.values(CUSTOM_DATE_OPTION_UNIT).map((t) => ({
908
- value: t.toString(),
909
- label: t.toString(),
910
- }))}
911
- onChange={(val: {
912
- label: string;
913
- value: CUSTOM_DATE_OPTION_UNIT;
914
- }): void => {
915
- setUnitValue(val.value);
916
- changeValue(
917
- durationValue,
918
- val.value,
919
- directionValue,
920
- referenceMomentValue,
921
- );
922
- }}
923
- value={{ value: unitValue, label: unitValue }}
924
- darkMode={true}
925
- />
926
- </div>
927
- <div className="value-spec-editor__date-picker__custom-date__input">
928
- <CustomSelectorInput
929
- className="value-spec-editor__date-picker__custom-date__input-dropdown"
930
- options={Object.values(CUSTOM_DATE_OPTION_DIRECTION).map((t) => ({
931
- value: t.toString(),
932
- label: t.toString(),
933
- }))}
934
- onChange={(val: {
935
- label: string;
936
- value: CUSTOM_DATE_OPTION_DIRECTION;
937
- }): void => {
938
- setDirectionValue(val.value);
939
- changeValue(
940
- durationValue,
941
- unitValue,
942
- val.value,
943
- referenceMomentValue,
944
- );
945
- }}
946
- value={{ value: directionValue, label: directionValue }}
947
- darkMode={true}
948
- />
949
- </div>
950
- <div className="value-spec-editor__date-picker__custom-date__input">
951
- <CustomSelectorInput
952
- className="value-spec-editor__date-picker__custom-date__input-dropdown"
953
- options={Object.values(CUSTOM_DATE_OPTION_REFERENCE_MOMENT).map(
954
- (t) => ({
955
- value: t.toString(),
956
- label: t.toString(),
957
- }),
958
- )}
959
- onChange={(val: {
960
- label: string;
961
- value: CUSTOM_DATE_OPTION_REFERENCE_MOMENT;
962
- }): void => {
963
- setReferenceMomentValueValue(val.value);
964
- changeValue(durationValue, unitValue, directionValue, val.value);
965
- }}
966
- value={{ value: referenceMomentValue, label: referenceMomentValue }}
967
- darkMode={true}
968
- />
969
- </div>
970
- </div>
971
- );
972
- };
973
-
974
- const CustomFirstDayOfValueSpecificationEditor: React.FC<{
975
- customDateAdjustOptionValue: DatePickerOption;
976
- graph: PureModel;
977
- setValueSpecification: (val: ValueSpecification) => void;
978
- setDatePickerOption: (datePickerOption: DatePickerOption) => void;
979
- }> = (props) => {
980
- const {
981
- customDateAdjustOptionValue,
982
- graph,
983
- setValueSpecification,
984
- setDatePickerOption,
985
- } = props;
986
- const selectorRef = useRef<SelectComponent>(null);
987
- const [unitValue, setUnitValue] = useState(
988
- customDateAdjustOptionValue instanceof CustomFirstDayOfOption
989
- ? (customDateAdjustOptionValue.unit as string)
990
- : null,
991
- );
992
- const changeValue = (latestUnitValue: string): void => {
993
- if (latestUnitValue !== '') {
994
- const targetUnitValue = Object.values(
995
- CUSTOM_DATE_OPTION_REFERENCE_MOMENT,
996
- ).filter((moment) => moment.toString().includes(latestUnitValue));
997
- const startDayOfDateOption =
998
- targetUnitValue.length > 0
999
- ? new CustomFirstDayOfOption(
1000
- guaranteeNonNullable(targetUnitValue[0]?.toString()),
1001
- latestUnitValue as CUSTOM_DATE_FIRST_DAY_OF_UNIT,
1002
- )
1003
- : new CustomFirstDayOfOption('', undefined);
1004
- setValueSpecification(
1005
- buildPureDateFunctionExpression(startDayOfDateOption, graph),
1006
- );
1007
- setDatePickerOption(startDayOfDateOption);
1008
- }
1009
- };
1010
-
1011
- useEffect(() => {
1012
- selectorRef.current?.focus();
1013
- }, []);
1014
-
1015
- return (
1016
- <div className="value-spec-editor__date-picker__custom-date">
1017
- <div className="value-spec-editor__date-picker__custom-date__input">
1018
- <CustomSelectorInput
1019
- ref={selectorRef}
1020
- placeholder="Choose a unit..."
1021
- className="value-spec-editor__date-picker__custom-date__input-dropdown value-spec-editor__date-picker__custom-date__input-dropdown--full"
1022
- options={Object.values(CUSTOM_DATE_FIRST_DAY_OF_UNIT).map((t) => ({
1023
- value: t.toString(),
1024
- label: t.toString(),
1025
- }))}
1026
- onChange={(val: { label: string; value: string } | null): void => {
1027
- if (val) {
1028
- setUnitValue(val.value);
1029
- changeValue(val.value);
1030
- }
1031
- }}
1032
- value={unitValue ? { value: unitValue, label: unitValue } : null}
1033
- darkMode={true}
1034
- />
1035
- </div>
1036
- </div>
1037
- );
1038
- };
1039
-
1040
- const CustomPreviousDayOfWeekValueSpecificationEditor: React.FC<{
1041
- customDateAdjustOptionValue: DatePickerOption;
1042
- graph: PureModel;
1043
- setValueSpecification: (val: ValueSpecification) => void;
1044
- setDatePickerOption: (datePickerOption: DatePickerOption) => void;
1045
- }> = (props) => {
1046
- const {
1047
- customDateAdjustOptionValue,
1048
- graph,
1049
- setValueSpecification,
1050
- setDatePickerOption,
1051
- } = props;
1052
- const selectorRef = useRef<SelectComponent>(null);
1053
- const [dayOfWeekValue, setDayOfWeekValue] = useState(
1054
- customDateAdjustOptionValue instanceof CustomPreviousDayOfWeekOption
1055
- ? (customDateAdjustOptionValue.day as string)
1056
- : null,
1057
- );
1058
- const changeValue = (latestDurationUnitValue: string): void => {
1059
- if (latestDurationUnitValue !== '') {
1060
- const previousDayOfWeekDateOption = new CustomPreviousDayOfWeekOption(
1061
- `Previous ${latestDurationUnitValue}`,
1062
- latestDurationUnitValue as CUSTOM_DATE_DAY_OF_WEEK,
1063
- );
1064
- setValueSpecification(
1065
- buildPureDateFunctionExpression(previousDayOfWeekDateOption, graph),
1066
- );
1067
- setDatePickerOption(previousDayOfWeekDateOption);
1068
- }
1069
- };
1070
-
1071
- useEffect(() => {
1072
- selectorRef.current?.focus();
1073
- }, []);
1074
-
1075
- return (
1076
- <div className="value-spec-editor__date-picker__custom-date">
1077
- <div className="value-spec-editor__date-picker__custom-date__input">
1078
- <CustomSelectorInput
1079
- ref={selectorRef}
1080
- placeholder="Choose a day..."
1081
- className="value-spec-editor__date-picker__custom-date__input-dropdown value-spec-editor__date-picker__custom-date__input-dropdown--full"
1082
- options={Object.values(CUSTOM_DATE_DAY_OF_WEEK).map((t) => ({
1083
- value: t.toString(),
1084
- label: t.toString(),
1085
- }))}
1086
- onChange={(val: { label: string; value: string } | null): void => {
1087
- if (val) {
1088
- setDayOfWeekValue(val.value);
1089
- changeValue(val.value);
1090
- }
1091
- }}
1092
- value={
1093
- dayOfWeekValue
1094
- ? { value: dayOfWeekValue, label: dayOfWeekValue }
1095
- : null
1096
- }
1097
- darkMode={true}
1098
- />
1099
- </div>
1100
- </div>
1101
- );
1102
- };
1103
-
1104
- export const CustomDatePicker: React.FC<{
1105
- valueSpecification: PrimitiveInstanceValue | SimpleFunctionExpression;
1106
- graph: PureModel;
1107
- typeCheckOption: {
1108
- expectedType: Type;
1109
- /**
1110
- * Indicates if a strict type-matching will happen.
1111
- * Sometimes, auto-boxing allow some rooms to wiggle,
1112
- * for example we can assign a Float to an Integer, a
1113
- * Date to a DateTime. With this flag set to `true`
1114
- * we will not allow this.
1115
- *
1116
- * For example, if `match=true`, it means that options in the
1117
- * date-capability-dropdown which are not returning type DateTime
1118
- * will be filtered out.
1119
- */
1120
- match?: boolean;
1121
- };
1122
- setValueSpecification: (val: ValueSpecification) => void;
1123
- }> = (props) => {
1124
- const { valueSpecification, setValueSpecification, graph, typeCheckOption } =
1125
- props;
1126
- // For some cases where types need to be matched strictly.
1127
- // Some options need to be filtered out for DateTime.
1128
- const targetDateOptionsEnum = typeCheckOption.match
1129
- ? Object.values([
1130
- CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_TIME,
1131
- CUSTOM_DATE_PICKER_OPTION.NOW,
1132
- ])
1133
- : Object.values(CUSTOM_DATE_PICKER_OPTION);
1134
- const [datePickerOption, setDatePickerOption] = useState(
1135
- buildDatePickerOption(valueSpecification),
1136
- );
1137
-
1138
- const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
1139
- const openCustomDatePickerPopover = (
1140
- event: React.MouseEvent<HTMLButtonElement>,
1141
- ): void => {
1142
- setAnchorEl(event.currentTarget);
1143
- };
1144
- const handleEnter = (): void => {
1145
- setDatePickerOption(buildDatePickerOption(valueSpecification));
1146
- };
1147
- const closeCustomDatePickerPopover = (): void => {
1148
- setDatePickerOption(buildDatePickerOption(valueSpecification));
1149
- setAnchorEl(null);
1150
- };
1151
- const handleDatePickerOptionChange = (
1152
- event: React.ChangeEvent<HTMLInputElement>,
1153
- ): void => {
1154
- const chosenDatePickerOption = new DatePickerOption(
1155
- (event.target as HTMLInputElement).value,
1156
- (event.target as HTMLInputElement).value,
1157
- );
1158
- if (
1159
- CUSTOM_DATE_PICKER_OPTION.LATEST_DATE === chosenDatePickerOption.value
1160
- ) {
1161
- setValueSpecification(
1162
- buildPrimitiveInstanceValue(
1163
- graph,
1164
- PRIMITIVE_TYPE.LATESTDATE,
1165
- event.target.value,
1166
- ),
1167
- );
1168
- } else if (
1169
- // Elements in this list will trigger children date components
1170
- ![
1171
- CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_DATE,
1172
- CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_TIME,
1173
- CUSTOM_DATE_PICKER_OPTION.CUSTOM_DATE,
1174
- CUSTOM_DATE_PICKER_OPTION.FIRST_DAY_OF,
1175
- CUSTOM_DATE_PICKER_OPTION.PREVIOUS_DAY_OF_WEEK,
1176
- ].includes(chosenDatePickerOption.value as CUSTOM_DATE_PICKER_OPTION)
1177
- ) {
1178
- const theReservedCustomDateOption = reservedCustomDateOptions.filter(
1179
- (d) => d.value === chosenDatePickerOption.value,
1180
- );
1181
- theReservedCustomDateOption.length > 0
1182
- ? setValueSpecification(
1183
- buildPureAdjustDateFunction(
1184
- guaranteeNonNullable(theReservedCustomDateOption[0]),
1185
- graph,
1186
- ),
1187
- )
1188
- : setValueSpecification(
1189
- buildPureDateFunctionExpression(chosenDatePickerOption, graph),
1190
- );
1191
- }
1192
- setDatePickerOption(chosenDatePickerOption);
1193
- };
1194
- const renderChildrenDateComponents = (): React.ReactNode => {
1195
- switch (datePickerOption.value) {
1196
- case CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_DATE:
1197
- return (
1198
- <AbsoluteDateValueSpecificationEditor
1199
- graph={graph}
1200
- valueSpecification={valueSpecification}
1201
- setValueSpecification={setValueSpecification}
1202
- setDatePickerOption={setDatePickerOption}
1203
- />
1204
- );
1205
- case CUSTOM_DATE_PICKER_OPTION.ABSOLUTE_TIME:
1206
- return (
1207
- <AbsoluteTimeValueSpecificationEditor
1208
- graph={graph}
1209
- valueSpecification={valueSpecification}
1210
- setValueSpecification={setValueSpecification}
1211
- setDatePickerOption={setDatePickerOption}
1212
- />
1213
- );
1214
- case CUSTOM_DATE_PICKER_OPTION.CUSTOM_DATE:
1215
- return (
1216
- <CustomDateInstanceValueEditor
1217
- graph={graph}
1218
- customDateOptionValue={buildCustomDateOption(valueSpecification)}
1219
- setValueSpecification={setValueSpecification}
1220
- setDatePickerOption={setDatePickerOption}
1221
- />
1222
- );
1223
- case CUSTOM_DATE_PICKER_OPTION.FIRST_DAY_OF:
1224
- return (
1225
- <CustomFirstDayOfValueSpecificationEditor
1226
- graph={graph}
1227
- customDateAdjustOptionValue={buildDatePickerOption(
1228
- valueSpecification,
1229
- )}
1230
- setValueSpecification={setValueSpecification}
1231
- setDatePickerOption={setDatePickerOption}
1232
- />
1233
- );
1234
- case CUSTOM_DATE_PICKER_OPTION.PREVIOUS_DAY_OF_WEEK:
1235
- return (
1236
- <CustomPreviousDayOfWeekValueSpecificationEditor
1237
- graph={graph}
1238
- customDateAdjustOptionValue={buildDatePickerOption(
1239
- valueSpecification,
1240
- )}
1241
- setValueSpecification={setValueSpecification}
1242
- setDatePickerOption={setDatePickerOption}
1243
- />
1244
- );
1245
- default:
1246
- return null;
1247
- }
1248
- };
1249
-
1250
- // make sure the date picker label is updated when the value is reset or changed somehow
1251
- useEffect(() => {
1252
- setDatePickerOption(buildDatePickerOption(valueSpecification));
1253
- }, [valueSpecification]);
1254
-
1255
- return (
1256
- <>
1257
- <button
1258
- className="value-spec-editor__date-picker__trigger"
1259
- title="Click to edit and pick from more date options"
1260
- onClick={openCustomDatePickerPopover}
1261
- >
1262
- {datePickerOption.label}
1263
- </button>
1264
- <BasePopover
1265
- open={Boolean(anchorEl)}
1266
- TransitionProps={{
1267
- onEnter: handleEnter,
1268
- }}
1269
- anchorEl={anchorEl}
1270
- onClose={closeCustomDatePickerPopover}
1271
- anchorOrigin={{
1272
- vertical: 'bottom',
1273
- horizontal: 'center',
1274
- }}
1275
- transformOrigin={{
1276
- vertical: 'top',
1277
- horizontal: 'center',
1278
- }}
1279
- >
1280
- <BaseRadioGroup
1281
- className="value-spec-editor__date-picker__options"
1282
- value={datePickerOption.value}
1283
- onChange={handleDatePickerOptionChange}
1284
- row={true}
1285
- options={targetDateOptionsEnum}
1286
- size={2}
1287
- />
1288
- {renderChildrenDateComponents()}
1289
- </BasePopover>
1290
- </>
1291
- );
1292
- };