@mediusinc/mng-commons-data-api 5.2.0-rc.1 → 5.2.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  import { inject, Injector } from '@angular/core';
2
- import { TableviewInputBuilder, TableviewDescriptorInst, TableviewDataProviderInst } from '@mediusinc/mng-commons/tableview/api';
2
+ import { TableviewInputBuilder, TableviewDescriptorInst, TableviewDataProviderInst, FieldInputDescriptor } from '@mediusinc/mng-commons/tableview/api';
3
+ import { enumModelGeneric, EnumDescriptor } from '@mediusinc/mng-commons/model';
4
+ import { CommonsInternalError, fromAngularDateFormatToPrime } from '@mediusinc/mng-commons/core';
3
5
 
4
6
  /**
5
7
  * Creates a tableview with a given model, service and sorts and filters extracted from Data API Get All request params, service, and optional build function.
@@ -17,9 +19,681 @@ function tableviewWithGetAllParams(model, service, params, buildFn) {
17
19
  return builder.build();
18
20
  }
19
21
 
22
+ /**
23
+ * <em>Experimental:</em> Emulates an enum model object from enum schema.
24
+ *
25
+ * @experimental
26
+ *
27
+ * @param {SchemaEnum<Enum>} enumSchema - The schema of the enum.
28
+ *
29
+ * @returns {EnumDescriptor<Enum>} - The generated enum model descriptor.
30
+ */
31
+ function enumModelFromSchema(enumSchema) {
32
+ const enumObj = {};
33
+ enumSchema.values
34
+ .map((v, idx) => ({ value: v, constant: enumSchema.constants[idx] }))
35
+ .forEach(({ value, constant }) => {
36
+ const valueAsStrOrInt = value;
37
+ enumObj[constant] = valueAsStrOrInt;
38
+ if (typeof valueAsStrOrInt === 'number' && enumObj[valueAsStrOrInt] == null)
39
+ enumObj[valueAsStrOrInt] = constant;
40
+ });
41
+ return enumModelGeneric(enumObj, enumSchema.name.replace(/\d+$/, ''));
42
+ }
43
+
44
+ function fromSchemaCurrencyOptsToCurrency(opts) {
45
+ return {
46
+ currency: typeof opts === 'string' ? opts : opts.currency,
47
+ currencyDisplay: typeof opts === 'object' ? opts.currencyDisplay : undefined,
48
+ currencyProperty: typeof opts === 'object' ? opts.currencyProperty : undefined
49
+ };
50
+ }
51
+ function fromSchemaFilterNonArrayProperties(propertySchemas) {
52
+ return Object.keys(propertySchemas).filter(key => {
53
+ const propertySchema = propertySchemas[key];
54
+ return ['number', 'string', 'boolean', 'date', 'date-time', 'enum', 'object'].includes(propertySchema.type);
55
+ });
56
+ }
57
+
58
+ const defaultGetter = value => {
59
+ if (typeof value === 'object') {
60
+ const valueObj = value;
61
+ return (valueObj['title'] ??
62
+ valueObj['name'] ??
63
+ valueObj['naziv'] ??
64
+ valueObj['code'] ??
65
+ valueObj['sifra'] ??
66
+ valueObj['id'] ??
67
+ valueObj['uuid'] ??
68
+ valueObj[Object.keys(valueObj)[0]]);
69
+ }
70
+ else {
71
+ return value;
72
+ }
73
+ };
74
+ /**
75
+ * <em>Experimental:</em> Builder for adding columns to table.
76
+ *
77
+ * @experimental
78
+ */
79
+ class SchemaColumnsBuilder {
80
+ constructor(descriptor, schema, opts) {
81
+ this.descriptor = descriptor;
82
+ this.schema = schema;
83
+ this.opts = opts;
84
+ }
85
+ mergeColumnsOpts(property, opts) {
86
+ return {
87
+ ...this.opts,
88
+ columnType: this.opts?.columnTypes?.[property],
89
+ getter: opts?.getters?.[property] ?? this.opts?.getters?.[property],
90
+ enumModel: opts?.enumModels?.[property] ?? this.opts?.enumModels?.[property],
91
+ enumSchema: opts?.enumSchemas?.[property] ?? this.opts?.enumSchemas?.[property],
92
+ ...opts
93
+ };
94
+ }
95
+ mergeColumnOpts(property, opts) {
96
+ return {
97
+ ...this.opts,
98
+ columnType: this.opts?.columnTypes?.[property],
99
+ getter: this.opts?.getters?.[property],
100
+ enumModel: this.opts?.enumModels?.[property],
101
+ enumSchema: this.opts?.enumSchemas?.[property],
102
+ ...opts
103
+ };
104
+ }
105
+ /**
106
+ * <em>Experimental:</em> Adds a single column via {addColumnFromSchema}.
107
+ *
108
+ * @experimental
109
+ *
110
+ * @param {Property} property - The property to add the column for.
111
+ * @param {ColumnFromSchemaOptsType<SchModel[Property], SchModel, any, Model>} opts - Additional options.
112
+ *
113
+ * @return {ColumnDescriptor<NonNullable<Model[Property]>, Model, Model[Property]>} The added column descriptor.
114
+ */
115
+ add(property, opts) {
116
+ return addColumnFromSchema(this.descriptor, property, this.schema.properties[property], this.mergeColumnOpts(property, opts));
117
+ }
118
+ /**
119
+ * <em>Experimental:</em> Adds all columns from the non-array schema properties via {addColumnFromSchema}.
120
+ *
121
+ * @experimental
122
+ *
123
+ * @param {ColumnsFromSchemaOptsType<SchModel>} opts - Additional options.
124
+ *
125
+ * @return {this} - The current instance of the builder.
126
+ */
127
+ withAddAll(opts) {
128
+ this.addColumns(undefined, opts);
129
+ return this;
130
+ }
131
+ withAdd(propOrOpts, ...properties) {
132
+ if (typeof propOrOpts === 'object') {
133
+ this.addColumns(properties.length === 0 ? undefined : properties, propOrOpts);
134
+ }
135
+ else {
136
+ this.addColumns([propOrOpts, ...properties]);
137
+ }
138
+ return this;
139
+ }
140
+ /**
141
+ * <em>Experimental:</em> Adds a single column via {addColumnFromSchema} where property schema is manually provided.
142
+ *
143
+ * @experimental
144
+ *
145
+ * @param {Property} property - The property to add the column for.
146
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
147
+ * @param {ColumnFromSchemaOptsType} opts - Additional options.
148
+ *
149
+ * @return {ColumnDescriptor<NonNullable<Model[Property]>, Model, Model[Property]>} The added column descriptor.
150
+ */
151
+ addFromSchema(property, schemaProperty, opts) {
152
+ return addColumnFromSchema(this.descriptor, property, schemaProperty, this.mergeColumnOpts(property, opts));
153
+ }
154
+ addColumns(properties, opts) {
155
+ const addProperties = properties ?? fromSchemaFilterNonArrayProperties(this.schema.properties);
156
+ if (addProperties) {
157
+ addProperties.forEach(property => {
158
+ addColumnFromSchema(this.descriptor, property, this.schema.properties[property], this.mergeColumnsOpts(property, opts));
159
+ });
160
+ }
161
+ }
162
+ }
163
+ /**
164
+ * <em>Experimental:</em> Creates builder for adding columns to descriptor based on provided model schema.
165
+ *
166
+ * @experimental
167
+ *
168
+ * @param {TableviewDescriptorInst<Model, any, any, any> | TableDescriptorInst<Model, any, any>} descriptor - Descriptor to add columns to.
169
+ * @param {SchemaModel<SchModel>} schema - The schema with metadata about properties for fields.
170
+ * @param {ColumnsFromSchemaOptsType<SchModel>} [opts] - Additional options.
171
+ *
172
+ * @return {SchemaColumnsBuilder<Model, SchModel>} - The column schema builder instance.
173
+ */
174
+ function columnsFromSchema(descriptor, schema, opts) {
175
+ return new SchemaColumnsBuilder(descriptor, schema, opts);
176
+ }
177
+ /**
178
+ * <em>Experimental:</em> Adds a column to table descriptor based on the given schema property and options.
179
+ *
180
+ * @param {TableviewDescriptorInst<Model, any, any, any> | TableDescriptorInst<Model, any, any>} descriptor - The table view descriptor or table descriptor to add the column to.
181
+ * @param {Property} property - The property key of the model to create the column for.
182
+ * @param {SchemaProperty} propertySchema - The schema property for the property.
183
+ * @param {ColumnFromSchemaOptsType<Model[Property], Model, any, Model>} [opts] - The options for the column.
184
+ *
185
+ * @return {ColumnDescriptor<NonNullable<Model[Property]>, Model, Model[Property]>} - The added column descriptor.
186
+ */
187
+ function addColumnFromSchema(descriptor, property, propertySchema, opts) {
188
+ const propertyType = opts?.columnType ?? propertySchema?.type;
189
+ const column = descriptor.addColumn(property);
190
+ // Handle different property types
191
+ switch (propertyType) {
192
+ case 'number':
193
+ if (opts?.currency) {
194
+ column.asCurrency(fromSchemaCurrencyOptsToCurrency(opts.currency));
195
+ }
196
+ else {
197
+ column.asNumber();
198
+ }
199
+ break;
200
+ case 'boolean':
201
+ column.asBoolean(undefined, undefined, true);
202
+ break;
203
+ case 'date':
204
+ column.asDate(opts?.dateFormat ?? 'dd.MM.YYYY');
205
+ break;
206
+ case 'enum':
207
+ // eslint-disable-next-line no-case-declarations
208
+ const enumModel = opts?.enumSchema ? enumModelFromSchema(opts.enumSchema) : opts?.enumModel;
209
+ if (enumModel) {
210
+ column.asEnumUnsafe(enumModel);
211
+ }
212
+ break;
213
+ case 'date-time':
214
+ column.asDate(opts?.dateTimeFormat ?? 'dd.MM.YYYY HH:mm');
215
+ break;
216
+ case 'currency':
217
+ column.asCurrency(opts?.currency ? fromSchemaCurrencyOptsToCurrency(opts?.currency) : undefined);
218
+ break;
219
+ case 'object':
220
+ column.withGetter(defaultGetter);
221
+ break;
222
+ }
223
+ // Add optional filter, sort and getters
224
+ if (opts?.getter)
225
+ column.withGetter(opts?.getter);
226
+ return column;
227
+ }
228
+
229
+ /**
230
+ * <em>Experimental:</em> Builder for adding fields to editor.
231
+ *
232
+ * @experimental
233
+ */
234
+ class SchemaFieldsBuilder {
235
+ constructor(descriptor, schema, opts) {
236
+ this.descriptor = descriptor;
237
+ this.schema = schema;
238
+ this.opts = opts;
239
+ }
240
+ mergeFieldsInputOpts(property, opts) {
241
+ return {
242
+ ...this.opts,
243
+ fieldType: opts?.fieldTypes?.[property] ?? this.opts?.fieldTypes?.[property],
244
+ enumModel: opts?.enumModels?.[property] ?? this.opts?.enumModels?.[property],
245
+ enumSchema: opts?.enumSchemas?.[property] ?? this.opts?.enumSchemas?.[property],
246
+ ...opts
247
+ };
248
+ }
249
+ mergeFieldInputOpts(property, opts) {
250
+ return {
251
+ ...this.opts,
252
+ fieldType: this.opts?.fieldTypes?.[property],
253
+ enumModel: this.opts?.enumModels?.[property],
254
+ enumSchema: this.opts?.enumSchemas?.[property],
255
+ ...opts
256
+ };
257
+ }
258
+ /**
259
+ * <em>Experimental:</em> Adds a single input field via {addFieldInputFromSchema}.
260
+ *
261
+ * @experimental
262
+ *
263
+ * @param {Property} property - The property to add the field for.
264
+ * @param {FieldInputFromSchemaOptsType<SchModel>} opts - Additional options.
265
+ *
266
+ * @return {FieldInputDescriptor<Model, NonNullable<Model[Property]>, Model[Property], Parent>} The added field descriptor.
267
+ */
268
+ add(property, opts) {
269
+ return addFieldInputFromSchema(this.descriptor, property, this.schema.properties[property], this.mergeFieldInputOpts(property, opts));
270
+ }
271
+ /**
272
+ * <em>Experimental:</em> Adds all input fields for the non-array properties via {addFieldInputFromSchema}.
273
+ *
274
+ * @experimental
275
+ *
276
+ * @param {FieldsFromSchemaOptsType<SchModel>} opts - Additional options.
277
+ *
278
+ * @return {this} - The current instance of the builder.
279
+ */
280
+ withAddAll(opts) {
281
+ this.addFields(undefined, opts);
282
+ return this;
283
+ }
284
+ withAdd(propOrOpts, ...properties) {
285
+ if (typeof propOrOpts === 'object') {
286
+ this.addFields(properties.length === 0 ? undefined : properties, propOrOpts);
287
+ }
288
+ else {
289
+ this.addFields(propOrOpts === undefined ? properties : [propOrOpts, ...properties]);
290
+ }
291
+ return this;
292
+ }
293
+ addFields(properties, opts) {
294
+ const addProperties = properties ?? fromSchemaFilterNonArrayProperties(this.schema.properties);
295
+ addProperties.forEach(property => {
296
+ addFieldInputFromSchema(this.descriptor, property, this.schema.properties[property], this.mergeFieldsInputOpts(property, opts));
297
+ });
298
+ }
299
+ /**
300
+ * <em>Experimental:</em> Adds a single input field via {addFieldInputFromSchema} where property schema is manually provided.
301
+ *
302
+ * @experimental
303
+ *
304
+ * @param {Property} property - The property to add the field for.
305
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
306
+ * @param {FieldInputFromSchemaOptsType<Model>} opts - Additional options.
307
+ *
308
+ * @return {FieldInputDescriptor<Model, NonNullable<Model[Property]>, Model[Property], Parent>} The added field descriptor.
309
+ */
310
+ addFromSchema(property, schemaProperty, opts) {
311
+ return addFieldInputFromSchema(this.descriptor, property, schemaProperty, this.mergeFieldInputOpts(property, opts));
312
+ }
313
+ /**
314
+ * <em>Experimental:</em> Adds an enum lookup field via {addFieldEnumLookupFromSchema}.
315
+ *
316
+ * @experimental
317
+ *
318
+ * @param {Property} property - The property to add the enum lookup field for.
319
+ * @param {SchemaEnum<Enum> | EnumDescriptor<Enum>} [enumOpt] - The enum schema or the enum model descriptor. If non provided, builder opts will be checked.
320
+ *
321
+ * @returns {FieldLookupEnumDescriptor<Enum, Model, Parent>} - The added field descriptor.
322
+ *
323
+ * @throws {CommonsInternalError} - If no enum metadata is found for the property either from parameter or from builder's opts.
324
+ */
325
+ addEnum(property, enumOpt) {
326
+ if (!enumOpt) {
327
+ enumOpt = this.opts?.enumModels?.[property] ?? this.opts?.enumSchemas?.[property];
328
+ }
329
+ if (!enumOpt) {
330
+ throw new CommonsInternalError(`Enum lookup field for property ${property} cannot be added because enum metadata was not provided.`);
331
+ }
332
+ return addFieldEnumLookupFromSchema(this.descriptor, property, enumOpt, this.schema.properties[property]);
333
+ }
334
+ /**
335
+ * <em>Experimental:</em> Adds an enum lookup field via {addFieldEnumLookupFromSchema} where property schema is manually provided.
336
+ *
337
+ * @experimental
338
+ *
339
+ * @param {Property} property - The property to add the enum lookup field for.
340
+ * @param {SchemaEnum<Enum> | EnumDescriptor<Enum>} [enumOpt] - The enum schema or the enum model descriptor. If non provided, builder opts will be checked.
341
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
342
+ *
343
+ * @returns {FieldLookupEnumDescriptor<Enum, Model, Parent>} - The added field descriptor.
344
+ *
345
+ * @throws {CommonsInternalError} - If no enum metadata is found for the property either from parameter or from builder's opts.
346
+ */
347
+ addEnumFromSchema(property, enumOpt, schemaProperty) {
348
+ if (!enumOpt) {
349
+ enumOpt = this.opts?.enumModels?.[property] ?? this.opts?.enumSchemas?.[property];
350
+ }
351
+ if (!enumOpt) {
352
+ throw new CommonsInternalError(`Enum lookup field for property ${property} cannot be added because enum metadata was not provided.`);
353
+ }
354
+ return addFieldEnumLookupFromSchema(this.descriptor, property, enumOpt, schemaProperty);
355
+ }
356
+ /**
357
+ * <em>Experimental:</em> Adds a lookup field.
358
+ * {EditorDescriptorInt.addFieldLookup} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
359
+ *
360
+ * @experimental
361
+ *
362
+ * @param {Property} property - The property to create the lookup field for.
363
+ *
364
+ * @return The created lookup field.
365
+ */
366
+ addLookup(property) {
367
+ const field = this.descriptor.addFieldLookup(property);
368
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
369
+ return field;
370
+ }
371
+ /**
372
+ * <em>Experimental:</em> Adds a lookup field where property schema is manually provided.
373
+ * {EditorDescriptorInt.addFieldLookup} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
374
+ *
375
+ * @experimental
376
+ *
377
+ * @param {Property} property - The property to create the lookup field for.
378
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
379
+ *
380
+ * @return The created lookup field.
381
+ */
382
+ addLookupFromSchema(property, schemaProperty) {
383
+ const field = this.descriptor.addFieldLookup(property);
384
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
385
+ return field;
386
+ }
387
+ /**
388
+ * <em>Experimental:</em> Adds a lookup field.
389
+ * {EditorDescriptorInt.addFieldLookupWithOptionsValueProperty} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
390
+ *
391
+ * @experimental
392
+ *
393
+ * @param {Property} property - The property to create the lookup field for.
394
+ * @param {TypeDescriptor<LookupModel>} type - Type of lookup model.
395
+ * @param {LookupValueProperty} lookupOptionsValueProperty - Value property from lookup model.
396
+ *
397
+ * @return The created lookup field.
398
+ */
399
+ addLookupWithOptionsValueProperty(property, type, lookupOptionsValueProperty) {
400
+ const field = this.descriptor.addFieldLookupWithOptionsValueProperty(property, type, lookupOptionsValueProperty);
401
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
402
+ return field;
403
+ }
404
+ /**
405
+ * <em>Experimental:</em> Adds a lookup field where property schema is manually provided.
406
+ * {EditorDescriptorInt.addFieldLookupWithOptionsValueProperty} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
407
+ *
408
+ * @experimental
409
+ *
410
+ * @param {Property} property - The property to create the lookup field for.
411
+ * @param {TypeDescriptor<LookupModel>} type - Type of lookup model.
412
+ * @param {LookupValueProperty} lookupOptionsValueProperty - Value property from lookup model.
413
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
414
+ *
415
+ * @return The created lookup field.
416
+ */
417
+ addLookupWithOptionsValuePropertyFromSchema(property, type, lookupOptionsValueProperty, schemaProperty) {
418
+ const field = this.descriptor.addFieldLookupWithOptionsValueProperty(property, type, lookupOptionsValueProperty);
419
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
420
+ return field;
421
+ }
422
+ /**
423
+ * <em>Experimental:</em> Adds a lookup field.
424
+ * {EditorDescriptorInt.addFieldLookupWithProvider} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
425
+ *
426
+ * @experimental
427
+ *
428
+ * @param {Property} property - The property to create the lookup field for.
429
+ * @param {FieldLookupProviderType<LookupModel, Service>} provider - Lookup provider.
430
+ *
431
+ * @return The created lookup field.
432
+ */
433
+ addLookupWithProvider(property, provider) {
434
+ const field = this.descriptor.addFieldLookupWithProvider(property, provider);
435
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
436
+ return field;
437
+ }
438
+ /**
439
+ * <em>Experimental:</em> Adds a lookup field where property schema is manually provided.
440
+ * {EditorDescriptorInt.addFieldLookupWithProvider} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
441
+ *
442
+ * @experimental
443
+ *
444
+ * @param {Property} property - The property to create the lookup field for.
445
+ * @param {FieldLookupProviderType<LookupModel, Service>} provider - Lookup provider.
446
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
447
+ *
448
+ * @return The created lookup field.
449
+ */
450
+ addLookupWithProviderFromSchema(property, provider, schemaProperty) {
451
+ const field = this.descriptor.addFieldLookupWithProvider(property, provider);
452
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
453
+ return field;
454
+ }
455
+ /**
456
+ * <em>Experimental:</em> Adds a lookup field.
457
+ * {EditorDescriptorInt.addFieldLookupWithProviderAndOptionsValueProperty} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
458
+ *
459
+ * @experimental
460
+ *
461
+ * @param {Property} property - The property to create the lookup field for.
462
+ * @param {FieldLookupProviderType<LookupModel, Service>} provider - Lookup provider.
463
+ * @param {TypeDescriptor<LookupModel>} type - Type of lookup model.
464
+ * @param {LookupValueProperty} lookupOptionsValueProperty - Value property from lookup model.
465
+ *
466
+ * @return The created lookup field.
467
+ */
468
+ addLookupWithProviderAndOptionsValueProperty(property, provider, type, lookupOptionsValueProperty) {
469
+ const field = this.descriptor.addFieldLookupWithProviderAndOptionsValueProperty(property, provider, type, lookupOptionsValueProperty);
470
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
471
+ return field;
472
+ }
473
+ /**
474
+ * <em>Experimental:</em> Adds a lookup field where property schema is manually provided.
475
+ * {EditorDescriptorInt.addFieldLookupWithProviderAndOptionsValueProperty} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
476
+ *
477
+ * @experimental
478
+ *
479
+ * @param {Property} property - The property to create the lookup field for.
480
+ * @param {FieldLookupProviderType<LookupModel, Service>} provider - Lookup provider.
481
+ * @param {TypeDescriptor<LookupModel>} type - Type of lookup model.
482
+ * @param {LookupValueProperty} lookupOptionsValueProperty - Value property from lookup model.
483
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
484
+ *
485
+ * @return The created lookup field.
486
+ */
487
+ addLookupWithProviderAndOptionsValuePropertyFromSchema(property, provider, type, lookupOptionsValueProperty, schemaProperty) {
488
+ const field = this.descriptor.addFieldLookupWithProviderAndOptionsValueProperty(property, provider, type, lookupOptionsValueProperty);
489
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
490
+ return field;
491
+ }
492
+ /**
493
+ * <em>Experimental:</em> Adds a one-to-many editor field.
494
+ * {EditorDescriptorInt.addFieldManyEditor} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
495
+ *
496
+ * @experimental
497
+ *
498
+ * @param {Property} property - The property to create the many editor field for.
499
+ * @param {TableviewDescriptorInst<FieldModel, any, any, Model>} tableviewDescriptor - Tableview descriptor.
500
+ *
501
+ * @return The created one-to-many editor field.
502
+ */
503
+ addManyEditor(property, tableviewDescriptor) {
504
+ const field = this.descriptor.addFieldManyEditor(property, tableviewDescriptor);
505
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
506
+ return field;
507
+ }
508
+ /**
509
+ * <em>Experimental:</em> Adds an one-to-many editor field where property schema is manually provided.
510
+ * {EditorDescriptorInt.addFieldManyEditor} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
511
+ *
512
+ * @experimental
513
+ *
514
+ * @param {Property} property - The property to create the many editor field for.
515
+ * @param {TableviewDescriptorInst<FieldModel, any, any, Model>} tableviewDescriptor - Tableview descriptor.
516
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
517
+ *
518
+ * @return The created one-to-many editor field.
519
+ */
520
+ addManyEditorFromSchema(property, tableviewDescriptor, schemaProperty) {
521
+ const field = this.descriptor.addFieldManyEditor(property, tableviewDescriptor);
522
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
523
+ return field;
524
+ }
525
+ /**
526
+ * <em>Experimental:</em> Adds a many-to-many editor field.
527
+ * {EditorDescriptorInt.addFieldManyToManyEditor} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
528
+ *
529
+ * @experimental
530
+ *
531
+ * @param {Property} property - The property to create the many editor field for.
532
+ * @param {TableDescriptorInst<FieldModel>} mainTableDescriptor - Main table descriptor.
533
+ * @param {TableDescriptorInst<FieldModel>} lookupTableDescriptor - Lookup table descriptor.
534
+ * @param {ITableDataProvider<FieldModel, ServiceType>} lookupDataProvider - Lookup data provider.
535
+ *
536
+ * @return The created many-to-many editor field.
537
+ */
538
+ addManyToManyEditor(property, mainTableDescriptor, lookupTableDescriptor, lookupDataProvider) {
539
+ const field = this.descriptor.addFieldManyToManyEditor(property, mainTableDescriptor, lookupTableDescriptor, lookupDataProvider);
540
+ setFieldBasicOptionsFromSchema(field, this.schema.properties[property]);
541
+ return field;
542
+ }
543
+ /**
544
+ * <em>Experimental:</em> Adds a many-to-many editor field where property schema is manually provided.
545
+ * {EditorDescriptorInt.addFieldManyToManyEditor} is used to create field and ${setFieldBasicOptionsFromSchema} to add basic field's options from schema.
546
+ *
547
+ * @experimental
548
+ *
549
+ * @param {Property} property - The property to create the many editor field for.
550
+ * @param {TableDescriptorInst<FieldModel>} mainTableDescriptor - Main table descriptor.
551
+ * @param {TableDescriptorInst<FieldModel>} lookupTableDescriptor - Lookup table descriptor.
552
+ * @param {ITableDataProvider<FieldModel, ServiceType>} lookupDataProvider - Lookup data provider.
553
+ * @param {SchemaProperty} schemaProperty - Manually provided schema property.
554
+ *
555
+ * @return The created many-to-many editor field.
556
+ */
557
+ addManyToManyEditorFromSchema(property, mainTableDescriptor, lookupTableDescriptor, lookupDataProvider, schemaProperty) {
558
+ const field = this.descriptor.addFieldManyToManyEditor(property, mainTableDescriptor, lookupTableDescriptor, lookupDataProvider);
559
+ setFieldBasicOptionsFromSchema(field, schemaProperty);
560
+ return field;
561
+ }
562
+ }
563
+ /**
564
+ * <em>Experimental:</em> Creates builder for adding fields to descriptor based on provided model schema.
565
+ *
566
+ * @experimental
567
+ *
568
+ * @param {TableviewDescriptorInst<Model, any, any, Parent> | EditorDescriptorInst<Model>} descriptor - Descriptor to add fields to.
569
+ * @param {SchemaModel<SchModel>} schema - The schema with metadata about properties for fields.
570
+ * @param {FieldsFromSchemaOptsType<SchModel>} [opts] - Additional options.
571
+ *
572
+ * @return {SchemaFieldsBuilder<Model, Parent, SchModel>} - The SchemaFieldsBuilder object containing the extracted fields.
573
+ */
574
+ function fieldsFromSchema(descriptor, schema, opts) {
575
+ return new SchemaFieldsBuilder(descriptor, schema, opts);
576
+ }
577
+ /**
578
+ * <em>Experimental:</em> Adds a field input to editor descriptor based on the given schema property and options.
579
+ *
580
+ * @experimental
581
+ *
582
+ * @param {TableviewDescriptorInst | EditorDescriptorInst} descriptor - Descriptor to add field to.
583
+ * @param {keyof Model} property - The property of the model.
584
+ * @param {SchemaProperty} propertySchema - The property's schema for the field.
585
+ * @param {FieldInputFromSchemaOptsType<Model>} [opts] - Additional options.
586
+ *
587
+ * @return {FieldInputDescriptor<Model, NonNullable<Model[Property]>, Model[Property], Parent>} - The generated field input descriptor.
588
+ */
589
+ function addFieldInputFromSchema(descriptor, property, propertySchema, opts) {
590
+ const propertyType = opts?.fieldType ?? propertySchema.type;
591
+ const field = new FieldInputDescriptor(descriptor instanceof TableviewDescriptorInst ? descriptor.detailsEditor : descriptor, property);
592
+ if (descriptor instanceof TableviewDescriptorInst) {
593
+ descriptor.detailsEditor.addFieldDescriptor(field);
594
+ descriptor.editEditor.addFieldDescriptor(field);
595
+ descriptor.addEditor.addFieldDescriptor(field);
596
+ }
597
+ else {
598
+ descriptor.addFieldDescriptor(field);
599
+ }
600
+ // Handle different property types
601
+ switch (propertyType) {
602
+ case 'number':
603
+ if (opts?.currency) {
604
+ field.asCurrency(fromSchemaCurrencyOptsToCurrency(opts.currency));
605
+ }
606
+ else {
607
+ field.asNumber();
608
+ }
609
+ break;
610
+ case 'currency':
611
+ field.asCurrency(opts?.currency ? fromSchemaCurrencyOptsToCurrency(opts.currency) : undefined);
612
+ break;
613
+ case 'boolean':
614
+ field.asSwitch();
615
+ break;
616
+ case 'text':
617
+ case 'string':
618
+ field.asText();
619
+ break;
620
+ case 'textarea':
621
+ field.asTextarea();
622
+ break;
623
+ case 'date':
624
+ field.asDatePicker({ format: opts?.dateFormat ? fromAngularDateFormatToPrime(opts.dateFormat) : undefined });
625
+ break;
626
+ case 'date-time':
627
+ field.asDatePicker({ showTime: true, format: opts?.dateTimeFormat ? fromAngularDateFormatToPrime(opts.dateTimeFormat) : undefined });
628
+ break;
629
+ case 'enum':
630
+ // eslint-disable-next-line no-case-declarations
631
+ const enumModel = opts?.enumSchema ? enumModelFromSchema(opts.enumSchema) : opts?.enumModel;
632
+ if (enumModel) {
633
+ field.asRadioFromEnum(enumModel);
634
+ }
635
+ else {
636
+ field.asText();
637
+ }
638
+ break;
639
+ case 'hidden':
640
+ field.asHidden();
641
+ break;
642
+ }
643
+ // Handle different property validations
644
+ if (propertyType === 'number' || propertyType === 'currency') {
645
+ field.withNumberValidation({
646
+ min: propertySchema.exclusiveMinimum && propertySchema.minimum != undefined ? propertySchema.minimum + (propertySchema.isInteger ? 1 : 0.0001) : propertySchema.minimum,
647
+ max: propertySchema.exclusiveMaximum && propertySchema.maximum != undefined ? propertySchema.maximum - (propertySchema.isInteger ? 1 : 0.0001) : propertySchema.maximum
648
+ });
649
+ }
650
+ if (propertyType === 'string' || propertyType === 'textarea' || propertyType === 'text') {
651
+ field.withTextValidation({
652
+ minLength: propertySchema.minLength,
653
+ maxLength: propertySchema.maxLength,
654
+ pattern: propertySchema.pattern ? new RegExp(propertySchema.pattern.slice(1, -1)) : undefined
655
+ });
656
+ if (propertyType !== 'textarea' && propertySchema.isEmail) {
657
+ field.withTextValidationEmail();
658
+ }
659
+ }
660
+ setFieldBasicOptionsFromSchema(field, propertySchema);
661
+ return field;
662
+ }
663
+ /**
664
+ * <em>Experimental:</em> Adds a field enum lookup to editor descriptor based on the given schema property and options.
665
+ *
666
+ * @experimental
667
+ *
668
+ * @param {TableviewDescriptorInst<Model, any, any, Parent> | EditorDescriptorInst<Model>} descriptor - Descriptor to add field to.
669
+ * @param {keyof Model} property - The property of the model.
670
+ * @param {SchemaEnum<Enum> | EnumDescriptor<Enum>} enumOpt - The enum schema or enum model descriptor.
671
+ * @param {SchemaProperty} propertySchema - The property's schema for the field.
672
+ *
673
+ * @returns {FieldLookupEnum<Enum>} - The field with enumeration values.
674
+ */
675
+ function addFieldEnumLookupFromSchema(descriptor, property, enumOpt, propertySchema) {
676
+ const enumDescriptor = enumOpt instanceof EnumDescriptor ? enumOpt : enumModelFromSchema(enumOpt);
677
+ const field = descriptor.addFieldLookupEnumUnsafe(property, enumDescriptor);
678
+ setFieldBasicOptionsFromSchema(field, propertySchema);
679
+ return field;
680
+ }
681
+ /**
682
+ * <em>Experimental:</em> Sets basic options for a field based on a given property schema.
683
+ *
684
+ * @param {AFieldDescriptor<FieldModel, EditorModel, FieldValue, ParentEditorModel>} field - The field to set options for.
685
+ * @param {SchemaProperty} propertySchema - The property schema to base the options on.
686
+ */
687
+ function setFieldBasicOptionsFromSchema(field, propertySchema) {
688
+ if (propertySchema.required)
689
+ field.withRequired();
690
+ if (propertySchema.isReadOnly)
691
+ field.withDisabled();
692
+ }
693
+
20
694
  /**
21
695
  * Generated bundle index. Do not edit.
22
696
  */
23
697
 
24
- export { tableviewWithGetAllParams };
698
+ export { SchemaColumnsBuilder, SchemaFieldsBuilder, addColumnFromSchema, addFieldEnumLookupFromSchema, addFieldInputFromSchema, columnsFromSchema, enumModelFromSchema, fieldsFromSchema, setFieldBasicOptionsFromSchema, tableviewWithGetAllParams };
25
699
  //# sourceMappingURL=mediusinc-mng-commons-data-api-tableview.mjs.map