11import { Type } from '@angular/core' ;
2- import { QueryVariables } from '../../../classes/query-variable-manager' ;
3- import { UntypedModelService } from '../../../types/types' ;
2+ import { type ExtractTallOne , type ExtractVall , UntypedModelService } from '../../../types/types' ;
43
5- export type NaturalHierarchicConfiguration < T extends UntypedModelService = UntypedModelService > = {
4+ export type NodeConfig < T extends UntypedModelService = UntypedModelService > = {
65 /**
76 * An AbstractModelService to be used to fetch items
87 */
98 service : Type < T > ;
109
1110 /**
12- * A list of FilterConditionField name to filter items
13- *
14- * Those will be used directly to build filter to fetch items, so they must be
15- * valid API FilterConditionField names for the given service.
16- *
17- * Eg: given the QuestionService, possible names would be:
18- *
19- * - "chapter" to filter by the question's chapter
20- * - "parent" to filter by the question's parent question
21- */
22- parentsRelationNames ?: string [ ] ;
23-
24- /**
25- * A list of FilterConditionField name to declare hierarchy
26- *
27- * Those must be the `parentsRelationNames` name, that correspond to this service,
28- * of all children services.
29- *
30- * Eg: given the QuestionService, possible names would be:
31- *
32- * - "questions" coming from ChapterService
33- * - "questions" coming from QuestionService
11+ * Whether this node is at the root of the tree (there can be multiple roots in one tree)
3412 */
35- childrenRelationNames ?: string [ ] ;
13+ root ?: true | ExtractVall < T > [ 'filter' ] ;
3614
3715 /**
3816 * Additional filters applied in the query sent by getList function
3917 */
40- filter ?: QueryVariables [ 'filter' ] ;
18+ filter ?: ExtractVall < T > [ 'filter' ] ;
4119
4220 /**
4321 * Key of the returned literal container models by config / service
@@ -55,12 +33,60 @@ export type NaturalHierarchicConfiguration<T extends UntypedModelService = Untyp
5533 *
5634 * In fact, this means isDisabled. Also applies to unselect.
5735 */
58- isSelectableCallback ?: ( item : any ) => boolean ;
36+ isSelectableCallback ?: ( item : ExtractTallOne < T > ) => boolean ;
5937
6038 /**
6139 * Functions that receives a model and returns a string for display value
6240 *
6341 * If missing, fallback on global `NaturalHierarchicSelectorComponent.displayWith`
6442 */
65- displayWith ?: ( item : any ) => string ;
43+ displayWith ?: ( item : ExtractTallOne < T > ) => string ;
6644} ;
45+
46+ type RelationConfig < Nodes extends NodeConfig [ ] > = {
47+ /**
48+ * The parent node, eg: ChapterService
49+ */
50+ parent : Nodes [ number ] ;
51+
52+ /**
53+ * The child node, eg: QuestionService
54+ */
55+ child : Nodes [ number ] ;
56+
57+ /**
58+ * One of the keys of the `FilterGroupCondition` for the child service, to filter children by their parent(s)
59+ *
60+ * Those will be used directly to build filter to fetch children, so they must be
61+ * valid API `FilterGroupCondition` keys for the given child service.
62+ *
63+ * Eg: given the `QuestionService`, possible names would be:
64+ *
65+ * - "chapter" to filter the questions by their chapter
66+ * - "parent" to filter the questions by their parent question
67+ */
68+ field : string ;
69+ } ;
70+
71+ export type NaturalHierarchicConfiguration < Nodes extends NodeConfig [ ] > = {
72+ /**
73+ * All possible nodes in the tree
74+ */
75+ nodes : Nodes ;
76+
77+ /**
78+ * All possible relations between nodes
79+ */
80+ relations : RelationConfig < Nodes > [ ] ;
81+ } ;
82+
83+ export function nodeConfig < T extends UntypedModelService > ( node : NodeConfig < T > ) : NodeConfig < T > {
84+ return node ;
85+ }
86+
87+ export function hierarchicConfig < Nodes extends NodeConfig [ ] > (
88+ nodes : Nodes ,
89+ relations : RelationConfig < Nodes > [ ] ,
90+ ) : NaturalHierarchicConfiguration < Nodes > {
91+ return { nodes : nodes , relations : relations } ;
92+ }
0 commit comments