1+ import { Component , OnInit , Input } from '@angular/core' ;
2+ import { AngularPConnectService } from "../../../_bridge/angular-pconnect" ;
3+ import { FormGroup } from '@angular/forms' ;
4+ const SELECTION_MODE = { SINGLE : 'single' , MULTI : 'multi' } ;
5+ @Component ( {
6+ selector : 'app-data-reference' ,
7+ templateUrl : './data-reference.component.html' ,
8+ styleUrls : [ './data-reference.component.scss' ]
9+ } )
10+ export class DataReferenceComponent implements OnInit {
11+
12+ constructor ( private angularPConnect : AngularPConnectService ) { }
13+
14+ @Input ( ) pConn$ : any ;
15+ @Input ( ) formGroup$ : FormGroup ;
16+ arFields$ : Array < any > = [ ] ;
17+
18+ label ;
19+ showLabel ;
20+ displayMode = "" ;
21+ allowAndPersistChangesInReviewMode = false ;
22+ referenceType = "" ;
23+ selectionMode = "" ;
24+ displayAs = "" ;
25+ ruleClass = "" ;
26+ parameters ;
27+ hideLabel = false ;
28+
29+ // Used with AngularPConnect
30+ angularPConnectData : any = { } ;
31+ PCore$ : any ;
32+ childrenToRender : Array < any > = [ ] ;
33+ dropDownDataSource : String = "" ;
34+ isDisplayModeEnabled : Boolean = false ;
35+ propsToUse : any = { } ;
36+ rawViewMetadata : any = { } ;
37+ viewName : String = "" ;
38+ firstChildMeta : any = { } ;
39+ refList : any ;
40+ canBeChangedInReviewMode : Boolean = false ;
41+ propName : String = ""
42+ firstChildPConnect ;
43+ children ;
44+ ngOnInit ( ) : void {
45+ if ( ! this . PCore$ ) {
46+ this . PCore$ = window . PCore ;
47+ }
48+
49+ // First thing in initialization is registering and subscribing to the AngularPConnect service
50+ this . angularPConnectData = this . angularPConnect . registerAndSubscribeComponent ( this , this . onStateChange ) ;
51+ this . children = this . pConn$ . getChildren ( ) ;
52+ this . updateSelf ( ) ;
53+ }
54+
55+ // Callback passed when subscribing to store change
56+ onStateChange ( ) {
57+ // Should always check the bridge to see if the component should
58+ // update itself (re-render)
59+ const bUpdateSelf = this . angularPConnect . shouldComponentUpdate ( this ) ;
60+
61+ // ONLY call updateSelf when the component should update
62+ if ( bUpdateSelf ) {
63+ this . updateSelf ( ) ;
64+ }
65+ }
66+
67+ updateSelf ( ) {
68+ // Update properties based on configProps
69+ const theConfigProps = this . pConn$ . getConfigProps ( ) ;
70+ this . label = theConfigProps . label ;
71+ this . showLabel = theConfigProps . showLabel ;
72+ this . displayMode = theConfigProps . displayMode ;
73+ this . allowAndPersistChangesInReviewMode = theConfigProps . allowAndPersistChangesInReviewMode ;
74+ this . referenceType = theConfigProps . referenceType ;
75+ this . selectionMode = theConfigProps . selectionMode ;
76+ this . displayAs = theConfigProps . displayAs ;
77+ this . ruleClass = theConfigProps . ruleClass ;
78+ this . parameters = theConfigProps . parameters ;
79+ this . hideLabel = theConfigProps . hideLabel ;
80+
81+ this . propsToUse = { label : this . label , showLabel : this . showLabel , ...this . pConn$ . getInheritedProps ( ) } ;
82+ if ( this . propsToUse . showLabel === false ) {
83+ this . propsToUse . label = '' ;
84+ }
85+ this . rawViewMetadata = this . pConn$ . getRawMetadata ( ) ;
86+ this . viewName = this . rawViewMetadata . name ;
87+ this . firstChildMeta = this . rawViewMetadata . children [ 0 ] ;
88+ this . refList = this . rawViewMetadata . config . referenceList ;
89+ this . canBeChangedInReviewMode = this . allowAndPersistChangesInReviewMode && ( this . displayAs === 'autocomplete' || this . displayAs === 'dropdown' ) ;
90+ // this.childrenToRender = this.children;
91+ this . isDisplayModeEnabled = [ "LABELS_LEFT" , "STACKED_LARGE_VAL" ] . includes ( this . displayMode ) ;
92+
93+ if ( this . firstChildMeta ?. type !== 'Region' ) {
94+ this . firstChildPConnect = this . pConn$ . getChildren ( ) [ 0 ] . getPConnect ;
95+
96+ /* remove refresh When condition from those old view so that it will not be used for runtime */
97+ if ( this . firstChildMeta . config ?. readOnly ) {
98+ delete this . firstChildMeta . config . readOnly ;
99+ }
100+ if ( this . firstChildMeta ?. type === 'Dropdown' ) {
101+ this . firstChildMeta . config . datasource . source = this . rawViewMetadata . config ?. parameters
102+ ? this . dropDownDataSource
103+ : '@DATASOURCE ' . concat ( this . refList ) . concat ( '.pxResults' ) ;
104+ } else if ( this . firstChildMeta ?. type === 'AutoComplete' ) {
105+ this . firstChildMeta . config . datasource = this . refList ;
106+
107+ /* Insert the parameters to the component only if present */
108+ if ( this . rawViewMetadata . config ?. parameters ) {
109+ this . firstChildMeta . config . parameters = this . parameters ;
110+ }
111+ }
112+ // set displayMode conditionally
113+ if ( ! this . canBeChangedInReviewMode ) {
114+ this . firstChildMeta . config . displayMode = this . displayMode ;
115+ }
116+ if ( this . firstChildMeta . type === 'SimpleTableSelect' && this . selectionMode === SELECTION_MODE . MULTI ) {
117+ this . propName = this . PCore$ . getAnnotationUtils ( ) . getPropertyName ( this . firstChildMeta . config . selectionList ) ;
118+ } else {
119+ this . propName = this . PCore$ . getAnnotationUtils ( ) . getPropertyName ( this . firstChildMeta . config . value ) ;
120+ }
121+
122+ const theRecreatedFirstChild = this . recreatedFirstChild ( ) ;
123+ const viewsRegion = this . rawViewMetadata . children [ 1 ] ;
124+ if ( viewsRegion ?. name === 'Views' && viewsRegion . children . length ) {
125+ this . childrenToRender = [ theRecreatedFirstChild , ...this . children . slice ( 1 ) ] ;
126+ } else {
127+ this . childrenToRender = [ theRecreatedFirstChild ] ;
128+ }
129+ }
130+ }
131+
132+ handleSelection ( event ) {
133+ const caseKey = this . pConn$ . getCaseInfo ( ) . getKey ( ) ;
134+ const refreshOptions = { autoDetectRefresh : true } ;
135+ if ( this . canBeChangedInReviewMode && this . pConn$ . getValue ( '__currentPageTabViewName' ) ) {
136+ this . pConn$
137+ . getActionsApi ( )
138+ . refreshCaseView ( caseKey , this . pConn$ . getValue ( '__currentPageTabViewName' ) , null , refreshOptions ) ;
139+ this . PCore$ . getDeferLoadManager ( ) . refreshActiveComponents ( this . pConn$ . getContextName ( ) ) ;
140+ } else {
141+ const pgRef = this . pConn$ . getPageReference ( ) . replace ( 'caseInfo.content' , '' ) ;
142+ this . pConn$ . getActionsApi ( ) . refreshCaseView ( caseKey , this . viewName , pgRef , refreshOptions ) ;
143+ }
144+
145+ // AutoComplete sets value on event.id whereas Dropdown sets it on event.target.value
146+ const propValue = event ?. id || event ?. target . value ;
147+ if ( propValue && this . canBeChangedInReviewMode && this . isDisplayModeEnabled ) {
148+ this . PCore$ . getDataApiUtils ( )
149+ . getCaseEditLock ( caseKey )
150+ . then ( caseResponse => {
151+ const pageTokens = this . pConn$ . getPageReference ( ) . replace ( 'caseInfo.content' , '' ) . split ( '.' ) ;
152+ let curr = { } ;
153+ const commitData = curr ;
154+
155+ pageTokens . forEach ( el => {
156+ if ( el !== '' ) {
157+ curr [ el ] = { } ;
158+ curr = curr [ el ] ;
159+ }
160+ } ) ;
161+
162+ // expecting format like {Customer: {pyID:"C-100"}}
163+ const propArr = this . propName . split ( '.' ) ;
164+ propArr . forEach ( ( element , idx ) => {
165+ if ( idx + 1 === propArr . length ) {
166+ curr [ element ] = propValue ;
167+ } else {
168+ curr [ element ] = { } ;
169+ curr = curr [ element ] ;
170+ }
171+ } ) ;
172+
173+ this . PCore$ . getDataApiUtils ( )
174+ . updateCaseEditFieldsData (
175+ caseKey ,
176+ { [ caseKey ] : commitData } ,
177+ caseResponse . headers . etag ,
178+ this . pConn$ . getContextName ( )
179+ )
180+ . then ( response => {
181+ this . PCore$ . getContainerUtils ( ) . updateChildContainersEtag (
182+ this . pConn$ . getContextName ( ) ,
183+ response . headers . etag
184+ ) ;
185+ } ) ;
186+ } ) ;
187+ }
188+ } ;
189+
190+ // Re-create first child with overridden props
191+ // Memoized child in order to stop unmount and remount of the child component when data reference
192+ // rerenders without any actual change
193+ recreatedFirstChild ( ) {
194+ const { type, config } = this . firstChildMeta ;
195+ if ( this . firstChildMeta ?. type !== 'Region' ) {
196+ this . pConn$ . clearErrorMessages ( {
197+ property : this . propName
198+ } ) ;
199+ if ( ! this . canBeChangedInReviewMode && this . isDisplayModeEnabled && this . selectionMode === SELECTION_MODE . SINGLE ) {
200+ console . log ( 'In SingleRef' ) ;
201+ return null ;
202+ }
203+
204+ if ( this . isDisplayModeEnabled && this . selectionMode === SELECTION_MODE . MULTI ) {
205+ console . log ( 'In multiRef' ) ;
206+ return null ;
207+ }
208+
209+ // In the case of a datasource with parameters you cannot load the dropdown before the parameters
210+ if ( type === 'Dropdown' && this . rawViewMetadata . config ?. parameters && this . dropDownDataSource === null ) {
211+ return null ;
212+ }
213+
214+ return this . firstChildPConnect ( ) . createComponent ( {
215+ type,
216+ config : {
217+ ...config ,
218+ required : this . propsToUse . required ,
219+ visibility : this . propsToUse . visibility ,
220+ disabled : this . propsToUse . disabled ,
221+ label : this . propsToUse . label ,
222+ viewName : this . pConn$ . getCurrentView ( ) ,
223+ parameters : this . rawViewMetadata . config . parameters ,
224+ readOnly : false ,
225+ localeReference : this . rawViewMetadata . config . localeReference ,
226+ ...( this . selectionMode === SELECTION_MODE . SINGLE ? { referenceType : this . referenceType } : '' ) ,
227+ dataRelationshipContext : this . rawViewMetadata . config . contextClass && this . rawViewMetadata . config . name ? this . rawViewMetadata . config . name : null ,
228+ hideLabel : this . hideLabel ,
229+ onRecordChange : this . handleSelection
230+ }
231+ } ) ;
232+ }
233+ }
234+
235+ }
0 commit comments