@@ -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 = '' ;
@@ -65,7 +105,8 @@ export class DropdownComponent implements OnInit, OnDestroy {
65105 constructor (
66106 private angularPConnect : AngularPConnectService ,
67107 private cdRef : ChangeDetectorRef ,
68- private utils : Utils
108+ private utils : Utils ,
109+ private dataPageService : DatapageService
69110 ) { }
70111
71112 ngOnInit ( ) : void {
@@ -76,8 +117,9 @@ export class DropdownComponent implements OnInit, OnDestroy {
76117 // Then, continue on with other initialization
77118
78119 // call updateSelf when initializing
79- // this.updateSelf();
80120 this . checkAndUpdate ( ) ;
121+ // this should get called afer checkAndUpdate
122+ this . getDatapageData ( ) ;
81123
82124 if ( this . formGroup$ ) {
83125 // add control to formGroup
@@ -120,7 +162,6 @@ export class DropdownComponent implements OnInit, OnDestroy {
120162 updateSelf ( ) : void {
121163 // moved this from ngOnInit() and call this from there instead...
122164 this . configProps$ = this . pConn$ . resolveConfigProps ( this . pConn$ . getConfigProps ( ) ) as DropdownProps ;
123-
124165 if ( this . configProps$ . value != undefined ) {
125166 this . value$ = this . configProps$ . value ;
126167 }
@@ -130,6 +171,7 @@ export class DropdownComponent implements OnInit, OnDestroy {
130171 this . label$ = this . configProps$ . label ;
131172 this . helperText = this . configProps$ . helperText ;
132173 this . hideLabel = this . configProps$ . hideLabel ;
174+ const datasource = this . configProps$ . datasource ;
133175 // timeout and detectChanges to avoid ExpressionChangedAfterItHasBeenCheckedError
134176 setTimeout ( ( ) => {
135177 if ( this . configProps$ . required != null ) {
@@ -138,6 +180,11 @@ export class DropdownComponent implements OnInit, OnDestroy {
138180 this . cdRef . detectChanges ( ) ;
139181 } ) ;
140182
183+ if ( ! isEqual ( datasource , this . theDatasource ) ) {
184+ // inbound datasource is different, so update theDatasource
185+ this . theDatasource = datasource || null ;
186+ }
187+
141188 if ( this . configProps$ . visibility != null ) {
142189 this . bVisible$ = this . utils . getBooleanValue ( this . configProps$ . visibility ) ;
143190 }
@@ -159,14 +206,16 @@ export class DropdownComponent implements OnInit, OnDestroy {
159206
160207 this . componentReference = ( this . pConn$ . getStateProps ( ) as any ) . value ;
161208
162- // @ts -ignore - parameter “contextName” for getDataObject method should be optional
163- const optionsList = [ ...this . utils . getOptionList ( this . configProps$ , this . pConn$ . getDataObject ( ) ) ] ;
164- optionsList ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
165- this . options$ = optionsList ;
166209 if ( this . value$ === '' && ! this . bReadonly$ ) {
167210 this . value$ = 'Select' ;
168211 }
169212
213+ if ( this . theDatasource ) {
214+ const optionsList = [ ...this . utils . getOptionList ( this . configProps$ , this . pConn$ . getDataObject ( ) ) ] ;
215+ optionsList ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
216+ this . options$ = optionsList ;
217+ }
218+
170219 const propName = ( this . pConn$ . getStateProps ( ) as any ) . value ;
171220 const className = this . pConn$ . getCaseInfo ( ) . getClassName ( ) ;
172221 const refName = propName ?. slice ( propName . lastIndexOf ( '.' ) + 1 ) ;
@@ -198,6 +247,56 @@ export class DropdownComponent implements OnInit, OnDestroy {
198247 }
199248 }
200249
250+ getDatapageData ( ) {
251+ const configProps = this . pConn$ . getConfigProps ( ) as DropdownProps ;
252+ let { listType, parameters, datasource = [ ] , columns = [ ] } = configProps ;
253+ const { deferDatasource, datasourceMetadata } = configProps ;
254+ const context = this . pConn$ . getContextName ( ) ;
255+ if ( deferDatasource && datasourceMetadata ?. datasource ?. name ) {
256+ listType = 'datapage' ;
257+ datasource = datasourceMetadata . datasource . name ;
258+ const { parameters : dataSourceParameters , propertyForDisplayText, propertyForValue } = datasourceMetadata . datasource ;
259+ parameters = flattenParameters ( dataSourceParameters ) ;
260+ const displayProp = propertyForDisplayText ?. startsWith ( '@P' ) ? propertyForDisplayText . substring ( 3 ) : propertyForDisplayText ;
261+ const valueProp = propertyForValue ?. startsWith ( '@P' ) ? propertyForValue . substring ( 3 ) : propertyForValue ;
262+ columns = [
263+ {
264+ key : 'true' ,
265+ setProperty : 'Associated property' ,
266+ value : valueProp
267+ } ,
268+ {
269+ display : 'true' ,
270+ primary : 'true' ,
271+ useForSearch : true ,
272+ value : displayProp
273+ }
274+ ] ;
275+ }
276+
277+ columns = preProcessColumns ( columns ) || [ ] ;
278+ if ( ! this . displayMode$ && listType !== 'associated' && typeof datasource === 'string' ) {
279+ this . getData ( datasource , parameters , columns , context ) ;
280+ }
281+ }
282+
283+ getData ( datasource , parameters , columns , context ) {
284+ this . dataPageService . getDataPageData ( datasource , parameters , context ) . then ( ( results : any ) => {
285+ const optionsData : any [ ] = [ ] ;
286+ const displayColumn = getDisplayFieldsMetaData ( columns ) ;
287+ results ?. forEach ( element => {
288+ const val = element [ displayColumn . primary ] ?. toString ( ) ;
289+ const obj = {
290+ key : element [ displayColumn . key ] || element . pyGUID ,
291+ value : val
292+ } ;
293+ optionsData . push ( obj ) ;
294+ } ) ;
295+ optionsData ?. unshift ( { key : 'Select' , value : this . pConn$ . getLocalizedValue ( 'Select...' , '' , '' ) } ) ;
296+ this . options$ = optionsData ;
297+ } ) ;
298+ }
299+
201300 isSelected ( buttonValue : string ) : boolean {
202301 return this . value$ === buttonValue ;
203302 }
0 commit comments