1
1
import { Component , EventEmitter , Input , Output } from '@angular/core' ;
2
2
import { catchError , debounceTime , distinctUntilChanged , map , merge , Observable , of , OperatorFunction , Subject , switchMap , tap } from "rxjs" ;
3
3
import { fromPromise } from "rxjs/internal/observable/innerFrom" ;
4
- import { ModelId } from "./query" ;
5
4
import { CommonCollection } from "fusio-sdk/dist/CommonCollection" ;
6
5
7
6
/**
@@ -10,12 +9,12 @@ import {CommonCollection} from "fusio-sdk/dist/CommonCollection";
10
9
@Component ( {
11
10
template : '' ,
12
11
} )
13
- export abstract class ObjectSelector < T extends ModelId > {
12
+ export abstract class ObjectSelector < T , R > {
14
13
15
14
@Input ( ) name ! : string ;
16
15
@Input ( ) disabled : boolean = false ;
17
- @Input ( ) data ?: number = undefined ;
18
- @Output ( ) dataChange = new EventEmitter < number > ( ) ;
16
+ @Input ( ) data ?: R = undefined ;
17
+ @Output ( ) dataChange = new EventEmitter < R > ( ) ;
19
18
20
19
focus$ = new Subject < string > ( ) ;
21
20
@@ -25,8 +24,14 @@ export abstract class ObjectSelector<T extends ModelId> {
25
24
selected ?: T
26
25
27
26
objectFormatter = ( object : T ) : string => {
28
- if ( typeof object === 'object' && object . hasOwnProperty ( this . getNameKey ( ) ) ) {
29
- return ( object as any ) [ this . getNameKey ( ) ] ;
27
+ const name = this . getNameProperty ( object ) ;
28
+ if ( name ) {
29
+ return name ;
30
+ }
31
+
32
+ const id = this . getIdProperty ( object ) ;
33
+ if ( id ) {
34
+ return '' + id ;
30
35
}
31
36
32
37
return '-' ;
@@ -61,16 +66,21 @@ export abstract class ObjectSelector<T extends ModelId> {
61
66
}
62
67
63
68
changeValue ( ) {
64
- if ( this . disabled ) {
69
+ if ( this . disabled || ! this . selected ) {
65
70
return ;
66
71
}
67
72
68
- const id = this . selected ?. id ;
73
+ const id = this . getIdProperty ( this . selected ) ;
69
74
if ( ! id ) {
70
75
return ;
71
76
}
72
77
73
- this . dataChange . emit ( parseInt ( '' + id ) ) ;
78
+ this . dataChange . emit ( id ) ;
79
+ }
80
+
81
+ protected getIdKey ( ) : string
82
+ {
83
+ return 'id' ;
74
84
}
75
85
76
86
protected getNameKey ( ) : string
@@ -82,4 +92,22 @@ export abstract class ObjectSelector<T extends ModelId> {
82
92
83
93
protected abstract get ( id : string ) : Promise < T > ;
84
94
95
+ private getIdProperty ( object : T ) : R | undefined
96
+ {
97
+ if ( typeof object === 'object' && object && object . hasOwnProperty ( this . getIdKey ( ) ) ) {
98
+ return ( object as any ) [ this . getIdKey ( ) ] ;
99
+ }
100
+
101
+ return undefined ;
102
+ }
103
+
104
+ private getNameProperty ( object : T ) : string | undefined
105
+ {
106
+ if ( typeof object === 'object' && object && object . hasOwnProperty ( this . getNameKey ( ) ) ) {
107
+ return ( object as any ) [ this . getNameKey ( ) ] ;
108
+ }
109
+
110
+ return undefined ;
111
+ }
112
+
85
113
}
0 commit comments