@@ -5,12 +5,47 @@ import { MatOptionModule } from '@angular/material/core';
55import { MatSelectModule } from '@angular/material/select' ;
66import { MatFormFieldModule } from '@angular/material/form-field' ;
77import { interval } from 'rxjs' ;
8+ import isEqual from 'fast-deep-equal' ;
89import { AngularPConnectData , AngularPConnectService } from '../../../_bridge/angular-pconnect' ;
10+ import { DatapageService } from '../../../_services/datapage.service' ;
911import { Utils } from '../../../_helpers/utils' ;
1012import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component' ;
1113import { handleEvent } from '../../../_helpers/event-util' ;
1214import { PConnFieldProps } from '../../../_types/PConnProps.interface' ;
1315
16+ function flattenParameters ( params = { } ) {
17+ const flatParams = { } ;
18+ Object . keys ( params ) . forEach ( key => {
19+ const { name, value : theVal } = params [ key ] ;
20+ flatParams [ name ] = theVal ;
21+ } ) ;
22+
23+ return flatParams ;
24+ }
25+
26+ function preProcessColumns ( columnList ) {
27+ return columnList . map ( col => {
28+ const tempColObj = { ...col } ;
29+ tempColObj . value = col . value && col . value . startsWith ( '.' ) ? col . value . substring ( 1 ) : col . value ;
30+ return tempColObj ;
31+ } ) ;
32+ }
33+
34+ function getDisplayFieldsMetaData ( columnList ) {
35+ const displayColumns = columnList . filter ( col => col . display === 'true' ) ;
36+ const metaDataObj : any = { key : '' , primary : '' , secondary : [ ] } ;
37+ const keyCol = columnList . filter ( col => col . key === 'true' ) ;
38+ metaDataObj . key = keyCol . length > 0 ? keyCol [ 0 ] . value : 'auto' ;
39+ for ( let index = 0 ; index < displayColumns . length ; index += 1 ) {
40+ if ( displayColumns [ index ] . primary === 'true' ) {
41+ metaDataObj . primary = displayColumns [ index ] . value ;
42+ } else {
43+ metaDataObj . secondary . push ( displayColumns [ index ] . value ) ;
44+ }
45+ }
46+ return metaDataObj ;
47+ }
48+
1449interface IOption {
1550 key : string ;
1651 value : string ;
@@ -22,6 +57,11 @@ interface DropdownProps extends PConnFieldProps {
2257 datasource ?: any [ ] ;
2358 onRecordChange ?: any ;
2459 fieldMetadata ?: any ;
60+ listType ?: string ;
61+ columns ?: any [ ] ;
62+ deferDatasource ?: boolean ;
63+ datasourceMetadata ?: any ;
64+ parameters ?: any ;
2565}
2666
2767@Component ( {
@@ -53,7 +93,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
5393 testId = '' ;
5494 helperText : string ;
5595 hideLabel : any ;
56-
96+ theDatasource : any [ ] | null ;
5797 fieldControl = new FormControl ( '' , null ) ;
5898 fieldMetadata : any [ ] ;
5999 localeContext = '' ;
@@ -67,7 +107,8 @@ export class DropdownComponent implements OnInit, OnDestroy {
67107 constructor (
68108 private angularPConnect : AngularPConnectService ,
69109 private cdRef : ChangeDetectorRef ,
70- private utils : Utils
110+ private utils : Utils ,
111+ private dataPageService : DatapageService
71112 ) { }
72113
73114 ngOnInit ( ) : void {
@@ -78,8 +119,9 @@ export class DropdownComponent implements OnInit, OnDestroy {
78119 // Then, continue on with other initialization
79120
80121 // call updateSelf when initializing
81- // this.updateSelf();
82122 this . checkAndUpdate ( ) ;
123+ // this should get called afer checkAndUpdate
124+ this . getDatapageData ( ) ;
83125
84126 if ( this . formGroup$ ) {
85127 // add control to formGroup
@@ -122,7 +164,6 @@ export class DropdownComponent implements OnInit, OnDestroy {
122164 updateSelf ( ) : void {
123165 // moved this from ngOnInit() and call this from there instead...
124166 this . configProps$ = this . pConn$ . resolveConfigProps ( this . pConn$ . getConfigProps ( ) ) as DropdownProps ;
125-
126167 if ( this . configProps$ . value != undefined ) {
127168 this . value$ = this . configProps$ . value ;
128169 }
@@ -132,6 +173,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
132173 this . label$ = this . configProps$ . label ;
133174 this . helperText = this . configProps$ . helperText ;
134175 this . hideLabel = this . configProps$ . hideLabel ;
176+ const datasource = this . configProps$ . datasource ;
135177 // timeout and detectChanges to avoid ExpressionChangedAfterItHasBeenCheckedError
136178 setTimeout ( ( ) => {
137179 if ( this . configProps$ . required != null ) {
@@ -140,6 +182,11 @@ export class DropdownComponent implements OnInit, OnDestroy {
140182 this . cdRef . detectChanges ( ) ;
141183 } ) ;
142184
185+ if ( ! isEqual ( datasource , this . theDatasource ) ) {
186+ // inbound datasource is different, so update theDatasource
187+ this . theDatasource = datasource || null ;
188+ }
189+
143190 if ( this . configProps$ . visibility != null ) {
144191 this . bVisible$ = this . utils . getBooleanValue ( this . configProps$ . visibility ) ;
145192 }
@@ -161,14 +208,18 @@ export class DropdownComponent implements OnInit, OnDestroy {
161208
162209 this . componentReference = this . pConn$ . getStateProps ( ) . value ;
163210
164- const optionsList = [ ...this . utils . getOptionList ( this . configProps$ , this . pConn$ . getDataObject ( ) ) ] ;
165- optionsList ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
166- this . options$ = optionsList ;
167211 if ( this . value$ === '' && ! this . bReadonly$ ) {
168212 this . value$ = 'Select' ;
169213 }
170214
215+ if ( this . theDatasource ) {
216+ const optionsList = [ ...this . utils . getOptionList ( this . configProps$ , this . pConn$ . getDataObject ( ) ) ] ;
217+ optionsList ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
218+ this . options$ = optionsList ;
219+ }
220+
171221 this . actionsApi = this . pConn$ . getActionsApi ( ) ;
222+
172223 this . propName = this . pConn$ . getStateProps ( ) . value ;
173224 const className = this . pConn$ . getCaseInfo ( ) . getClassName ( ) ;
174225 const refName = this . propName ?. slice ( this . propName . lastIndexOf ( '.' ) + 1 ) ;
@@ -199,6 +250,56 @@ export class DropdownComponent implements OnInit, OnDestroy {
199250 }
200251 }
201252
253+ getDatapageData ( ) {
254+ const configProps = this . pConn$ . getConfigProps ( ) as DropdownProps ;
255+ let { listType, parameters, datasource = [ ] , columns = [ ] } = configProps ;
256+ const { deferDatasource, datasourceMetadata } = configProps ;
257+ const context = this . pConn$ . getContextName ( ) ;
258+ if ( deferDatasource && datasourceMetadata ?. datasource ?. name ) {
259+ listType = 'datapage' ;
260+ datasource = datasourceMetadata . datasource . name ;
261+ const { parameters : dataSourceParameters , propertyForDisplayText, propertyForValue } = datasourceMetadata . datasource ;
262+ parameters = flattenParameters ( dataSourceParameters ) ;
263+ const displayProp = propertyForDisplayText ?. startsWith ( '@P' ) ? propertyForDisplayText . substring ( 3 ) : propertyForDisplayText ;
264+ const valueProp = propertyForValue ?. startsWith ( '@P' ) ? propertyForValue . substring ( 3 ) : propertyForValue ;
265+ columns = [
266+ {
267+ key : 'true' ,
268+ setProperty : 'Associated property' ,
269+ value : valueProp
270+ } ,
271+ {
272+ display : 'true' ,
273+ primary : 'true' ,
274+ useForSearch : true ,
275+ value : displayProp
276+ }
277+ ] ;
278+ }
279+
280+ columns = preProcessColumns ( columns ) || [ ] ;
281+ if ( ! this . displayMode$ && listType !== 'associated' && typeof datasource === 'string' ) {
282+ this . getData ( datasource , parameters , columns , context ) ;
283+ }
284+ }
285+
286+ getData ( datasource , parameters , columns , context ) {
287+ this . dataPageService . getDataPageData ( datasource , parameters , context ) . then ( ( results : any ) => {
288+ const optionsData : any [ ] = [ ] ;
289+ const displayColumn = getDisplayFieldsMetaData ( columns ) ;
290+ results ?. forEach ( element => {
291+ const val = element [ displayColumn . primary ] ?. toString ( ) ;
292+ const obj = {
293+ key : element [ displayColumn . key ] || element . pyGUID ,
294+ value : val
295+ } ;
296+ optionsData . push ( obj ) ;
297+ } ) ;
298+ optionsData ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
299+ this . options$ = optionsData ;
300+ } ) ;
301+ }
302+
202303 isSelected ( buttonValue : string ) : boolean {
203304 return this . value$ === buttonValue ;
204305 }
0 commit comments