-
Notifications
You must be signed in to change notification settings - Fork 208
/
Fields.ts
976 lines (891 loc) · 32.1 KB
/
Fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Content
*/
import { assert, Id64String } from "@itwin/core-bentley";
import {
ClassInfo,
ClassInfoJSON,
CompressedClassInfoJSON,
NavigationPropertyInfo,
PropertyInfo,
PropertyInfoJSON,
RelatedClassInfo,
RelationshipPath,
RelationshipPathJSON,
StrippedRelationshipPath,
} from "../EC";
import { PresentationError, PresentationStatus } from "../Error";
import { RelationshipMeaning } from "../rules/content/modifiers/RelatedPropertiesSpecification";
import { CategoryDescription } from "./Category";
import { EditorDescription } from "./Editor";
import { Property, PropertyJSON } from "./Property";
import { RendererDescription } from "./Renderer";
import { TypeDescription } from "./TypeDescription";
/**
* Data structure for a [[Field]] serialized to JSON.
* @public
*/
export interface BaseFieldJSON {
category: string;
name: string;
label: string;
type: TypeDescription;
isReadonly: boolean;
priority: number;
renderer?: RendererDescription;
editor?: EditorDescription;
}
/**
* Data structure for a [[PropertiesField]] serialized to JSON.
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export interface PropertiesFieldJSON<TClassInfoJSON = ClassInfoJSON> extends BaseFieldJSON {
properties: PropertyJSON<TClassInfoJSON>[];
}
/**
* Data structure for a [[ArrayPropertiesField]] serialized to JSON.
* @public
*/
export interface ArrayPropertiesFieldJSON<TClassInfoJSON = ClassInfo> extends PropertiesFieldJSON<TClassInfoJSON> {
itemsField: PropertiesFieldJSON<TClassInfoJSON>;
}
/**
* Data structure for a [[StructPropertiesField]] serialized to JSON.
* @public
*/
export interface StructPropertiesFieldJSON<TClassInfoJSON = ClassInfo> extends PropertiesFieldJSON<TClassInfoJSON> {
memberFields: PropertiesFieldJSON<TClassInfoJSON>[];
}
/**
* Data structure for a [[NestedContentField]] serialized to JSON.
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export interface NestedContentFieldJSON<TClassInfoJSON = ClassInfoJSON> extends BaseFieldJSON {
contentClassInfo: TClassInfoJSON;
pathToPrimaryClass: RelationshipPathJSON<TClassInfoJSON>;
relationshipMeaning?: RelationshipMeaning;
/** @beta */
actualPrimaryClassIds?: Id64String[];
autoExpand?: boolean;
nestedFields: FieldJSON<TClassInfoJSON>[];
}
/**
* JSON representation of a [[Field]]
* @public
*/
// eslint-disable-next-line deprecation/deprecation
export type FieldJSON<TClassInfoJSON = ClassInfoJSON> =
| BaseFieldJSON
| PropertiesFieldJSON<TClassInfoJSON>
| ArrayPropertiesFieldJSON<TClassInfoJSON>
| StructPropertiesFieldJSON<TClassInfoJSON>
| NestedContentFieldJSON<TClassInfoJSON>;
/** Is supplied field a properties field. */
function isPropertiesField(field: FieldJSON): field is PropertiesFieldJSON<any>;
function isPropertiesField(field: Field): field is PropertiesField;
function isPropertiesField(field: FieldJSON | Field) {
return !!(field as any).properties;
}
/** Is supplied field an array properties field. */
function isArrayPropertiesField(field: FieldJSON): field is ArrayPropertiesFieldJSON<any>;
function isArrayPropertiesField(field: Field): field is ArrayPropertiesField;
function isArrayPropertiesField(field: FieldJSON | Field) {
return !!(field as ArrayPropertiesFieldJSON).itemsField;
}
/** Is supplied field an array properties field. */
function isStructPropertiesField(field: FieldJSON): field is StructPropertiesFieldJSON<any>;
function isStructPropertiesField(field: Field): field is StructPropertiesField;
function isStructPropertiesField(field: FieldJSON | Field) {
return !!(field as StructPropertiesFieldJSON).memberFields;
}
/** Is supplied field a nested content field. */
function isNestedContentField(field: FieldJSON): field is NestedContentFieldJSON<any>;
function isNestedContentField(field: Field): field is NestedContentField;
function isNestedContentField(field: FieldJSON | Field) {
return !!(field as any).nestedFields;
}
/**
* Describes a single content field. A field is usually represented as a grid column
* or a property pane row.
*
* @public
*/
export class Field {
/** Category information */
public category: CategoryDescription;
/** Unique name */
public name: string;
/** Display label */
public label: string;
/** Description of this field's values data type */
public type: TypeDescription;
/** Are values in this field read-only */
public isReadonly: boolean;
/** Priority of the field. Higher priority fields should appear first in the UI */
public priority: number;
/** Property renderer used to render values of this field */
public renderer?: RendererDescription;
/** Property editor used to edit values of this field */
public editor?: EditorDescription;
/** Parent field */
private _parent?: NestedContentField;
/**
* Creates an instance of Field.
* @param category Category information
* @param name Unique name
* @param label Display label
* @param type Description of this field's values data type
* @param isReadonly Are values in this field read-only
* @param priority Priority of the field
* @param editor Property editor used to edit values of this field
* @param renderer Property renderer used to render values of this field
*/
public constructor(
category: CategoryDescription,
name: string,
label: string,
type: TypeDescription,
isReadonly: boolean,
priority: number,
editor?: EditorDescription,
renderer?: RendererDescription,
) {
this.category = category;
this.name = name;
this.label = label;
this.type = type;
this.isReadonly = isReadonly;
this.priority = priority;
this.editor = editor;
this.renderer = renderer;
}
/**
* Is this a [[PropertiesField]]
*/
public isPropertiesField(): this is PropertiesField {
return isPropertiesField(this);
}
/**
* Is this a [[NestedContentField]]
*/
public isNestedContentField(): this is NestedContentField {
return isNestedContentField(this);
}
/**
* Get parent
*/
public get parent(): NestedContentField | undefined {
return this._parent;
}
public clone() {
const clone = new Field(this.category, this.name, this.label, this.type, this.isReadonly, this.priority, this.editor, this.renderer);
clone.rebuildParentship(this.parent);
return clone;
}
/** Serialize this object to JSON */
public toJSON(): FieldJSON {
return {
category: this.category.name,
name: this.name,
label: this.label,
type: this.type,
isReadonly: this.isReadonly,
priority: this.priority,
renderer: this.renderer,
editor: this.editor,
};
}
/** Serialize this object to compressed JSON */
public toCompressedJSON(_classesMap: { [id: string]: CompressedClassInfoJSON }): FieldJSON<string> {
return this.toJSON();
}
/** Deserialize [[Field]] from JSON */
public static fromJSON(json: FieldJSON | undefined, categories: CategoryDescription[]): Field | undefined {
if (!json) {
return undefined;
}
if (isPropertiesField(json)) {
return PropertiesField.fromJSON(json, categories);
}
if (isNestedContentField(json)) {
// eslint-disable-next-line deprecation/deprecation
return NestedContentField.fromJSON(json, categories);
}
const field = Object.create(Field.prototype);
return Object.assign(field, json, {
category: Field.getCategoryFromFieldJson(json, categories),
});
}
/** Deserialize a [[Field]] from compressed JSON. */
public static fromCompressedJSON(
json: FieldJSON<string> | undefined,
classesMap: { [id: string]: CompressedClassInfoJSON },
categories: CategoryDescription[],
): Field | undefined {
if (!json) {
return undefined;
}
if (isPropertiesField(json)) {
return PropertiesField.fromCompressedJSON(json, classesMap, categories);
}
if (isNestedContentField(json)) {
return NestedContentField.fromCompressedJSON(json, classesMap, categories);
}
const field = Object.create(Field.prototype);
return Object.assign(field, json, {
category: Field.getCategoryFromFieldJson(json, categories),
});
}
protected static getCategoryFromFieldJson(fieldJson: FieldJSON, categories: CategoryDescription[]): CategoryDescription {
const category = categories.find((c) => c.name === fieldJson.category);
if (!category) {
throw new PresentationError(PresentationStatus.InvalidArgument, `Invalid content field category`);
}
return category;
}
/** Resets field's parent. */
public resetParentship(): void {
this._parent = undefined;
}
/** Sets provided [[NestedContentField]] as parent of this field. */
public rebuildParentship(parentField?: NestedContentField): void {
this._parent = parentField;
}
/**
* Get descriptor for this field.
* @public
*/
public getFieldDescriptor(): FieldDescriptor {
return {
type: FieldDescriptorType.Name,
fieldName: this.name,
} as NamedFieldDescriptor;
}
/**
* Checks if this field matches given field descriptor
* @see [[getFieldDescriptor]]
* @beta
*/
public matchesDescriptor(descriptor: FieldDescriptor) {
return FieldDescriptor.isNamed(descriptor) && descriptor.fieldName === this.name;
}
}
/**
* Describes a content field that's based on one or more similar
* EC properties.
*
* @public
*/
export class PropertiesField extends Field {
/** A list of properties this field is created from */
public properties: Property[];
/**
* Creates an instance of PropertiesField.
* @param category Category information
* @param name Unique name
* @param label Display label
* @param type Description of this field's values data type
* @param isReadonly Are values in this field read-only
* @param priority Priority of the field
* @param properties A list of properties this field is created from
* @param editor Property editor used to edit values of this field
* @param renderer Property renderer used to render values of this field
*/
public constructor(
category: CategoryDescription,
name: string,
label: string,
description: TypeDescription,
isReadonly: boolean,
priority: number,
properties: Property[],
editor?: EditorDescription,
renderer?: RendererDescription,
) {
super(category, name, label, description, isReadonly, priority, editor, renderer);
this.properties = properties;
}
/** Is this a an array property field */
public isArrayPropertiesField(): this is ArrayPropertiesField {
return false;
}
/** Is this a an struct property field */
public isStructPropertiesField(): this is StructPropertiesField {
return false;
}
public override clone() {
const clone = new PropertiesField(
this.category,
this.name,
this.label,
this.type,
this.isReadonly,
this.priority,
this.properties,
this.editor,
this.renderer,
);
clone.rebuildParentship(this.parent);
return clone;
}
/** Serialize this object to JSON */
public override toJSON(): PropertiesFieldJSON {
return {
...super.toJSON(),
properties: this.properties,
};
}
/** Serialize this object to compressed JSON */
public override toCompressedJSON(classesMap: { [id: string]: CompressedClassInfoJSON }): PropertiesFieldJSON<string> {
return {
...super.toCompressedJSON(classesMap),
properties: this.properties.map((property) => Property.toCompressedJSON(property, classesMap)),
};
}
/** Deserialize [[PropertiesField]] from JSON */
public static override fromJSON(json: PropertiesFieldJSON | undefined, categories: CategoryDescription[]): PropertiesField | undefined {
if (!json) {
return undefined;
}
if (isArrayPropertiesField(json)) {
return ArrayPropertiesField.fromJSON(json, categories);
}
if (isStructPropertiesField(json)) {
return StructPropertiesField.fromJSON(json, categories);
}
const field = Object.create(PropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
});
}
/**
* Deserialize a [[PropertiesField]] from compressed JSON.
* @public
*/
public static override fromCompressedJSON(
json: PropertiesFieldJSON<Id64String>,
classesMap: { [id: string]: CompressedClassInfoJSON },
categories: CategoryDescription[],
): PropertiesField | undefined {
if (isArrayPropertiesField(json)) {
return ArrayPropertiesField.fromCompressedJSON(json, classesMap, categories);
}
if (isStructPropertiesField(json)) {
return StructPropertiesField.fromCompressedJSON(json, classesMap, categories);
}
const field = Object.create(PropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
properties: json.properties.map((propertyJson) => fromCompressedPropertyJSON(propertyJson, classesMap)),
});
}
/**
* Get descriptor for this field.
* @public
*/
public override getFieldDescriptor(): FieldDescriptor {
const pathFromPropertyToSelectClass = new Array<RelatedClassInfo>();
let currAncestor = this.parent;
while (currAncestor) {
pathFromPropertyToSelectClass.push(...currAncestor.pathToPrimaryClass);
currAncestor = currAncestor.parent;
}
return {
type: FieldDescriptorType.Properties,
pathFromSelectToPropertyClass: RelationshipPath.strip(RelationshipPath.reverse(pathFromPropertyToSelectClass)),
properties: this.properties.map((p) => ({
class: p.property.classInfo.name,
name: p.property.name,
})),
} as PropertiesFieldDescriptor;
}
/**
* Checks if this field matches given field descriptor
* @see [[getFieldDescriptor]]
* @beta
*/
public override matchesDescriptor(descriptor: FieldDescriptor) {
if (!FieldDescriptor.isProperties(descriptor)) {
return false;
}
// ensure at least one descriptor property matches at least one property of this field
if (
!this.properties.some(({ property: fieldProperty }) =>
descriptor.properties.some(
(descriptorProperty) => fieldProperty.name === descriptorProperty.name && fieldProperty.classInfo.name === descriptorProperty.class,
),
)
) {
return false;
}
// ensure path from select to property in field and in descriptor matches
let stepsCount = 0;
let currAncestor = this.parent;
while (currAncestor) {
const pathFromCurrentToItsParent = RelationshipPath.reverse(currAncestor.pathToPrimaryClass);
for (const step of pathFromCurrentToItsParent) {
if (descriptor.pathFromSelectToPropertyClass.length < stepsCount + 1) {
return false;
}
if (!RelatedClassInfo.equals(step, descriptor.pathFromSelectToPropertyClass[descriptor.pathFromSelectToPropertyClass.length - stepsCount - 1])) {
return false;
}
++stepsCount;
}
currAncestor = currAncestor.parent;
}
return true;
}
}
/**
* Describes a content field that's based on one or more similar EC array properties.
* @public
*/
export class ArrayPropertiesField extends PropertiesField {
public itemsField: PropertiesField;
public constructor(
category: CategoryDescription,
name: string,
label: string,
description: TypeDescription,
itemsField: PropertiesField,
isReadonly: boolean,
priority: number,
properties: Property[],
editor?: EditorDescription,
renderer?: RendererDescription,
) {
super(category, name, label, description, isReadonly, priority, properties, editor, renderer);
this.itemsField = itemsField;
}
public override isArrayPropertiesField(): this is ArrayPropertiesField {
return true;
}
public override clone() {
const clone = new ArrayPropertiesField(
this.category,
this.name,
this.label,
this.type,
this.itemsField.clone(),
this.isReadonly,
this.priority,
this.properties,
this.editor,
this.renderer,
);
clone.rebuildParentship(this.parent);
return clone;
}
/** Serialize this object to JSON */
public override toJSON(): ArrayPropertiesFieldJSON {
return {
...super.toJSON(),
itemsField: this.itemsField.toJSON(),
};
}
/** Serialize this object to compressed JSON */
public override toCompressedJSON(classesMap: { [id: string]: CompressedClassInfoJSON }): ArrayPropertiesFieldJSON<string> {
return {
...super.toCompressedJSON(classesMap),
itemsField: this.itemsField.toCompressedJSON(classesMap),
};
}
/** Deserialize [[ArrayPropertiesField]] from JSON */
public static override fromJSON(json: ArrayPropertiesFieldJSON, categories: CategoryDescription[]): ArrayPropertiesField {
const field = Object.create(ArrayPropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
itemsField: PropertiesField.fromJSON(json.itemsField, categories),
});
}
/**
* Deserialize an [[ArrayPropertiesField]] from compressed JSON.
* @public
*/
public static override fromCompressedJSON(
json: ArrayPropertiesFieldJSON<Id64String>,
classesMap: { [id: string]: CompressedClassInfoJSON },
categories: CategoryDescription[],
): ArrayPropertiesField {
const field = Object.create(ArrayPropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
properties: json.properties.map((propertyJson) => fromCompressedPropertyJSON(propertyJson, classesMap)),
itemsField: PropertiesField.fromCompressedJSON(json.itemsField, classesMap, categories),
});
}
}
/**
* Describes a content field that's based on one or more similar EC struct properties.
* @public
*/
export class StructPropertiesField extends PropertiesField {
public memberFields: PropertiesField[];
public constructor(
category: CategoryDescription,
name: string,
label: string,
description: TypeDescription,
memberFields: PropertiesField[],
isReadonly: boolean,
priority: number,
properties: Property[],
editor?: EditorDescription,
renderer?: RendererDescription,
) {
super(category, name, label, description, isReadonly, priority, properties, editor, renderer);
this.memberFields = memberFields;
}
public override isStructPropertiesField(): this is StructPropertiesField {
return true;
}
public override clone() {
const clone = new StructPropertiesField(
this.category,
this.name,
this.label,
this.type,
this.memberFields.map((m) => m.clone()),
this.isReadonly,
this.priority,
this.properties,
this.editor,
this.renderer,
);
clone.rebuildParentship(this.parent);
return clone;
}
/** Serialize this object to JSON */
public override toJSON(): StructPropertiesFieldJSON {
return {
...super.toJSON(),
memberFields: this.memberFields.map((m) => m.toJSON()),
};
}
/** Serialize this object to compressed JSON */
public override toCompressedJSON(classesMap: { [id: string]: CompressedClassInfoJSON }): StructPropertiesFieldJSON<string> {
return {
...super.toCompressedJSON(classesMap),
memberFields: this.memberFields.map((m) => m.toCompressedJSON(classesMap)),
};
}
/** Deserialize [[StructPropertiesField]] from JSON */
public static override fromJSON(json: StructPropertiesFieldJSON, categories: CategoryDescription[]): StructPropertiesField {
const field = Object.create(StructPropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
memberFields: json.memberFields.map((m) => PropertiesField.fromJSON(m, categories)),
});
}
/**
* Deserialize a [[StructPropertiesField]] from compressed JSON.
* @public
*/
public static override fromCompressedJSON(
json: StructPropertiesFieldJSON<Id64String>,
classesMap: { [id: string]: CompressedClassInfoJSON },
categories: CategoryDescription[],
): StructPropertiesField {
const field = Object.create(StructPropertiesField.prototype);
return Object.assign(field, json, {
category: this.getCategoryFromFieldJson(json, categories),
properties: json.properties.map((propertyJson) => fromCompressedPropertyJSON(propertyJson, classesMap)),
memberFields: json.memberFields.map((m) => PropertiesField.fromCompressedJSON(m, classesMap, categories)),
});
}
}
/**
* Describes a content field that contains [Nested content]($docs/presentation/content/Terminology#nested-content).
*
* @public
*/
export class NestedContentField extends Field {
/** Information about an ECClass whose properties are nested inside this field */
public contentClassInfo: ClassInfo;
/** Relationship path to [Primary class]($docs/presentation/content/Terminology#primary-class) */
public pathToPrimaryClass: RelationshipPath;
/**
* Meaning of the relationship between the [primary class]($docs/presentation/content/Terminology#primary-class)
* and content class of this field.
*
* The value is set up through [[RelatedPropertiesSpecification.relationshipMeaning]] attribute when setting up
* presentation rules for creating the content.
*/
public relationshipMeaning: RelationshipMeaning;
/**
* When content descriptor is requested in a polymorphic fashion, fields get created if at least one of the concrete classes
* has it. In certain situations it's necessary to know which concrete classes caused that and this attribute is
* here to help.
*
* **Example:** There's a base class `A` and it has two derived classes `B` and `C` and class `B` has a relationship to class `D`.
* When content descriptor is requested for class `A` polymorphically, it's going to contain fields for all properties of class `B`,
* class `C` and a nested content field for the `B -> D` relationship. The nested content field's `actualPrimaryClassIds` attribute
* will contain ID of class `B`, identifying that only this specific class has the relationship.
*
* @beta
*/
public actualPrimaryClassIds: Id64String[];
/** Contained nested fields */
public nestedFields: Field[];
/** Flag specifying whether field should be expanded */
public autoExpand?: boolean;
/**
* Creates an instance of NestedContentField.
* @param category Category information
* @param name Unique name
* @param label Display label
* @param type Description of this field's values data type
* @param isReadonly Are values in this field read-only
* @param priority Priority of the field
* @param contentClassInfo Information about an ECClass whose properties are nested inside this field
* @param pathToPrimaryClass Relationship path to [Primary class]($docs/presentation/content/Terminology#primary-class)
* @param nestedFields Contained nested fields
* @param editor Property editor used to edit values of this field
* @param autoExpand Flag specifying whether field should be expanded
* @param relationshipMeaning RelationshipMeaning of the field
* @param renderer Property renderer used to render values of this field
*/
public constructor(
category: CategoryDescription,
name: string,
label: string,
description: TypeDescription,
isReadonly: boolean,
priority: number,
contentClassInfo: ClassInfo,
pathToPrimaryClass: RelationshipPath,
nestedFields: Field[],
editor?: EditorDescription,
autoExpand?: boolean,
renderer?: RendererDescription,
) {
super(category, name, label, description, isReadonly, priority, editor, renderer);
this.contentClassInfo = contentClassInfo;
this.pathToPrimaryClass = pathToPrimaryClass;
this.relationshipMeaning = RelationshipMeaning.RelatedInstance;
this.nestedFields = nestedFields;
this.autoExpand = autoExpand;
this.actualPrimaryClassIds = [];
}
public override clone() {
const clone = new NestedContentField(
this.category,
this.name,
this.label,
this.type,
this.isReadonly,
this.priority,
this.contentClassInfo,
this.pathToPrimaryClass,
this.nestedFields.map((n) => n.clone()),
this.editor,
this.autoExpand,
this.renderer,
);
clone.actualPrimaryClassIds = this.actualPrimaryClassIds;
clone.relationshipMeaning = this.relationshipMeaning;
clone.rebuildParentship(this.parent);
return clone;
}
/**
* Get field by its name
* @param name Name of the field to find
* @param recurse Recurse into nested fields
*/
public getFieldByName(name: string, recurse?: boolean): Field | undefined {
return getFieldByName(this.nestedFields, name, recurse);
}
/** Serialize this object to JSON */
public override toJSON(): NestedContentFieldJSON {
return {
...super.toJSON(),
contentClassInfo: this.contentClassInfo,
pathToPrimaryClass: this.pathToPrimaryClass,
relationshipMeaning: this.relationshipMeaning,
actualPrimaryClassIds: this.actualPrimaryClassIds,
nestedFields: this.nestedFields.map((field: Field) => field.toJSON()),
autoExpand: this.autoExpand,
};
}
/** Serialize this object to compressed JSON */
public override toCompressedJSON(classesMap: { [id: string]: CompressedClassInfoJSON }): NestedContentFieldJSON<string> {
const { id, ...leftOverInfo } = this.contentClassInfo;
classesMap[id] = leftOverInfo;
return {
...super.toCompressedJSON(classesMap),
contentClassInfo: id,
pathToPrimaryClass: this.pathToPrimaryClass.map((classInfo) => RelatedClassInfo.toCompressedJSON(classInfo, classesMap)),
nestedFields: this.nestedFields.map((field) => field.toCompressedJSON(classesMap)),
};
}
/**
* Deserialize [[NestedContentField]] from JSON
* @deprecated in 3.x. Use [[NestedContentField.fromCompressedJSON]]
*/
public static override fromJSON(json: NestedContentFieldJSON | undefined, categories: CategoryDescription[]): NestedContentField | undefined {
if (!json) {
return undefined;
}
const field = Object.create(NestedContentField.prototype);
return Object.assign(field, json, this.fromCommonJSON(json, categories), {
nestedFields: json.nestedFields
.map((nestedFieldJson: FieldJSON) => Field.fromJSON(nestedFieldJson, categories))
.filter((nestedField): nestedField is Field => !!nestedField),
});
}
/** Deserialize a [[NestedContentField]] from compressed JSON. */
public static override fromCompressedJSON(
json: NestedContentFieldJSON<Id64String>,
classesMap: { [id: string]: CompressedClassInfoJSON },
categories: CategoryDescription[],
) {
assert(classesMap.hasOwnProperty(json.contentClassInfo));
const field = Object.create(NestedContentField.prototype);
return Object.assign(field, json, this.fromCommonJSON(json, categories), {
category: this.getCategoryFromFieldJson(json, categories),
nestedFields: json.nestedFields
.map((nestedFieldJson: FieldJSON) => Field.fromCompressedJSON(nestedFieldJson, classesMap, categories))
.filter((nestedField): nestedField is Field => !!nestedField),
contentClassInfo: { id: json.contentClassInfo, ...classesMap[json.contentClassInfo] },
pathToPrimaryClass: json.pathToPrimaryClass.map((stepJson) => RelatedClassInfo.fromCompressedJSON(stepJson, classesMap)),
});
}
// eslint-disable-next-line deprecation/deprecation
private static fromCommonJSON(json: NestedContentFieldJSON<ClassInfoJSON | string>, categories: CategoryDescription[]): Partial<NestedContentField> {
return {
category: this.getCategoryFromFieldJson(json, categories),
relationshipMeaning: json.relationshipMeaning ?? RelationshipMeaning.RelatedInstance,
actualPrimaryClassIds: json.actualPrimaryClassIds ?? [],
autoExpand: json.autoExpand,
};
}
/** Resets parent of this field and all nested fields. */
public override resetParentship(): void {
super.resetParentship();
for (const nestedField of this.nestedFields) {
nestedField.resetParentship();
}
}
/**
* Sets provided [[NestedContentField]] as parent of this fields and recursively updates
* all nested fields parents.
*/
public override rebuildParentship(parentField?: NestedContentField): void {
super.rebuildParentship(parentField);
for (const nestedField of this.nestedFields) {
nestedField.rebuildParentship(this);
}
}
}
/** @internal */
export const getFieldByName = (fields: Field[], name: string | undefined, recurse?: boolean): Field | undefined => {
if (name) {
for (const field of fields) {
if (field.name === name) {
return field;
}
if (recurse && field.isNestedContentField()) {
const nested = getFieldByName(field.nestedFields, name, recurse);
if (nested) {
return nested;
}
}
}
}
return undefined;
};
/** @internal */
export const getFieldByDescriptor = (fields: Field[], fieldDescriptor: FieldDescriptor, recurse?: boolean): Field | undefined => {
for (const field of fields) {
if (field.matchesDescriptor(fieldDescriptor)) {
return field;
}
if (recurse && field.isNestedContentField()) {
const nested = getFieldByDescriptor(field.nestedFields, fieldDescriptor, recurse);
if (nested) {
return nested;
}
}
}
return undefined;
};
/**
* Types of different field descriptors.
* @public
*/
export enum FieldDescriptorType {
Name = "name",
Properties = "properties",
}
/**
* Base for a field descriptor
* @public
*/
export interface FieldDescriptorBase {
type: FieldDescriptorType;
}
/**
* A union of all possible field descriptor types
* @public
*/
export type FieldDescriptor = NamedFieldDescriptor | PropertiesFieldDescriptor;
/** @public */
// eslint-disable-next-line @typescript-eslint/no-redeclare
export namespace FieldDescriptor {
/** Is this a named field descriptor */
export function isNamed(d: FieldDescriptor): d is NamedFieldDescriptor {
return d.type === FieldDescriptorType.Name;
}
/** Is this a properties field descriptor */
export function isProperties(d: FieldDescriptor): d is PropertiesFieldDescriptor {
return d.type === FieldDescriptorType.Properties;
}
}
/**
* Field descriptor that identifies a content field by its unique name.
* @public
*/
export interface NamedFieldDescriptor extends FieldDescriptorBase {
type: FieldDescriptorType.Name;
fieldName: string;
}
/**
* Field descriptor that identifies a properties field using a list of
* properties that the field contains.
* @public
*/
export interface PropertiesFieldDescriptor extends FieldDescriptorBase {
type: FieldDescriptorType.Properties;
pathFromSelectToPropertyClass: StrippedRelationshipPath;
/**
* A list of properties that describe the field. At least one property in the list must
* match at least one property in the field for the descriptor to be considered matching.
*/
properties: Array<{
/** Full class name */
class: string;
/** Property name */
name: string;
}>;
}
function fromCompressedPropertyJSON(compressedPropertyJSON: PropertyJSON<string>, classesMap: { [id: string]: CompressedClassInfoJSON }): Property {
return {
property: fromCompressedPropertyInfoJSON(compressedPropertyJSON.property, classesMap),
};
}
function fromCompressedPropertyInfoJSON(compressedPropertyJSON: PropertyInfoJSON<string>, classesMap: { [id: string]: CompressedClassInfoJSON }): PropertyInfo {
assert(classesMap.hasOwnProperty(compressedPropertyJSON.classInfo));
const { navigationPropertyInfo, ...leftOverPropertyJSON } = compressedPropertyJSON;
return {
...leftOverPropertyJSON,
classInfo: { id: compressedPropertyJSON.classInfo, ...classesMap[compressedPropertyJSON.classInfo] },
...(navigationPropertyInfo ? { navigationPropertyInfo: NavigationPropertyInfo.fromCompressedJSON(navigationPropertyInfo, classesMap) } : undefined),
};
}