1- import { Component , OnInit , Input , forwardRef , OnDestroy , OnChanges } from '@angular/core' ;
1+ import { Component , OnInit , Input , forwardRef , OnDestroy , OnChanges , AfterViewInit } from '@angular/core' ;
22import { CommonModule } from '@angular/common' ;
33import { FormGroup } from '@angular/forms' ;
44import { MatButtonModule } from '@angular/material/button' ;
55import { AngularPConnectData , AngularPConnectService } from '../../../_bridge/angular-pconnect' ;
66import { buildView , getReferenceList } from '../../../_helpers/field-group-utils' ;
77import { Utils } from '../../../_helpers/utils' ;
88import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component' ;
9+ import { evaluateAllowRowAction } from './utils' ;
910
1011interface FieldGroupTemplateProps {
1112 // If any, enter additional props that only exist on this component
1213 label ?: string ;
13- showLabel ?: boolean ;
14+ hideLabel ?: boolean ;
15+ allowActions ?: any ;
16+ allowRowDelete ?: any ;
1417 referenceList ?: any [ ] ;
1518 contextClass : string ;
1619 renderMode ?: string ;
@@ -19,6 +22,7 @@ interface FieldGroupTemplateProps {
1922 displayMode ?: string ;
2023 fieldHeader ?: string ;
2124 allowTableEdit : boolean ;
25+ targetClassLabel ?: string ;
2226}
2327
2428@Component ( {
@@ -28,26 +32,27 @@ interface FieldGroupTemplateProps {
2832 standalone : true ,
2933 imports : [ CommonModule , MatButtonModule , forwardRef ( ( ) => ComponentMapperComponent ) ]
3034} )
31- export class FieldGroupTemplateComponent implements OnInit , OnDestroy , OnChanges {
35+ export class FieldGroupTemplateComponent implements OnInit , OnDestroy , OnChanges , AfterViewInit {
3236 @Input ( ) configProps$ : FieldGroupTemplateProps ;
3337 @Input ( ) pConn$ : typeof PConnect ;
3438 @Input ( ) formGroup$ : FormGroup ;
3539
3640 angularPConnectData : AngularPConnectData = { } ;
37- inheritedProps$ : object ;
41+
3842 showLabel$ ?: boolean = true ;
3943 label$ ?: string ;
4044 readonlyMode : boolean ;
4145 contextClass : any ;
42- referenceList : any ;
43- pageReference : any ;
4446 heading : any ;
4547 children : any ;
4648 menuIconOverride$ : any ;
47- prevRefLength : number ;
48- allowAddEdit : boolean ;
49+ referenceListLength : number ;
4950 fieldHeader : any ;
5051
52+ allowAdd = true ;
53+ allowEdit = true ;
54+ allowDelete = true ;
55+
5156 constructor (
5257 private angularPConnect : AngularPConnectService ,
5358 private utils : Utils
@@ -58,9 +63,21 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
5863 this . angularPConnectData = this . angularPConnect . registerAndSubscribeComponent ( this , this . onStateChange ) ;
5964 this . updateSelf ( ) ;
6065
61- const menuIconOverride$ = 'trash' ;
62- if ( menuIconOverride$ ) {
63- this . menuIconOverride$ = this . utils . getImageSrc ( menuIconOverride$ , this . utils . getSDKStaticContentUrl ( ) ) ;
66+ this . menuIconOverride$ = this . utils . getImageSrc ( 'trash' , this . utils . getSDKStaticContentUrl ( ) ) ;
67+
68+ const { allowActions, allowTableEdit, referenceList } = this . configProps$ ;
69+
70+ if ( allowActions && Object . keys ( allowActions ) . length > 0 ) {
71+ this . allowAdd = allowActions . allowAdd ?? allowTableEdit ?? true ;
72+ this . allowEdit = allowActions . allowEdit ?? true ;
73+ this . allowDelete = allowActions . allowDelete ?? allowTableEdit ?? true ;
74+ } else {
75+ this . allowAdd = allowTableEdit ?? true ;
76+ this . allowDelete = allowTableEdit ?? true ;
77+ }
78+
79+ if ( referenceList ?. length === 0 && ( this . allowAdd || this . allowEdit ) ) {
80+ this . pConn$ . getListActions ( ) . insert ( { classID : this . contextClass } , referenceList . length ) ;
6481 }
6582 }
6683
@@ -85,56 +102,57 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
85102 const props = changes . configProps$ ;
86103 if ( props . currentValue !== props . previousValue ) {
87104 this . configProps$ = props . currentValue ;
105+
88106 if ( changes ?. pConn$ ?. currentValue ) {
89107 this . pConn$ = changes ?. pConn$ ?. currentValue ;
90108 }
109+
91110 this . updateSelf ( ) ;
92111 }
93112 }
94113 }
95114
115+ ngAfterViewInit ( ) {
116+ const resolvedList = getReferenceList ( this . pConn$ ) ;
117+ // @ts -ignore - Expected 3 arguments, but got 1
118+ this . pConn$ . getListActions ( ) . initDefaultPageInstructions ( resolvedList ) ;
119+ }
120+
96121 updateSelf ( ) {
97- this . inheritedProps$ = this . pConn$ . getInheritedProps ( ) ;
98- this . label$ = this . configProps$ . label ;
99- this . showLabel$ = this . configProps$ . showLabel ;
100- // label & showLabel within inheritedProps takes precedence over configProps
101- this . label$ = ( this . inheritedProps$ as any ) . label || this . label$ ;
102- this . showLabel$ = ( this . inheritedProps$ as any ) . showLabel || this . showLabel$ ;
122+ const inheritedProps : any = this . pConn$ . getInheritedProps ( ) ;
103123
104- this . allowAddEdit = this . configProps$ . allowTableEdit ;
124+ const { label, hideLabel, allowRowDelete, referenceList, fieldHeader, renderMode, displayMode, heading, contextClass, lookForChildInConfig } =
125+ this . configProps$ ;
126+
127+ // label within inheritedProps takes precedence over configProps
128+ this . label$ = inheritedProps . label || label ;
129+
130+ this . showLabel$ = referenceList ?. length === 0 || ! hideLabel ;
105131
106- const renderMode = this . configProps$ . renderMode ;
107- const displayMode = this . configProps$ . displayMode ;
108132 this . readonlyMode = renderMode === 'ReadOnly' || displayMode === 'DISPLAY_ONLY' ;
109- this . contextClass = this . configProps$ . contextClass ;
110- const lookForChildInConfig = this . configProps$ . lookForChildInConfig ;
111- this . heading = this . configProps$ . heading ?? 'Row' ;
112- this . fieldHeader = this . configProps$ . fieldHeader ;
133+
134+ this . contextClass = contextClass ;
135+ this . heading = heading ?? 'Row' ;
136+ this . fieldHeader = fieldHeader ;
137+
113138 const resolvedList = getReferenceList ( this . pConn$ ) ;
114- this . pageReference = `${ this . pConn$ . getPageReference ( ) } ${ resolvedList } ` ;
115139 this . pConn$ . setReferenceList ( resolvedList ) ;
140+
116141 if ( this . readonlyMode ) {
117142 this . pConn$ . setInheritedProp ( 'displayMode' , 'DISPLAY_ONLY' ) ;
118143 }
119- this . referenceList = this . configProps$ . referenceList ;
120- if ( this . prevRefLength != this . referenceList ?. length ) {
121- // eslint-disable-next-line sonarjs/no-collapsible-if
122- if ( ! this . readonlyMode ) {
123- if ( this . referenceList ?. length === 0 && this . allowAddEdit !== false ) {
124- this . addFieldGroupItem ( ) ;
125- }
126- }
127- const children : any = [ ] ;
128- this . referenceList ?. forEach ( ( item , index ) => {
129- children . push ( {
144+
145+ if ( this . referenceListLength != referenceList ?. length ) {
146+ this . children = referenceList ?. map ( ( item , index ) => {
147+ return {
130148 id : index ,
131149 name : this . fieldHeader === 'propertyRef' ? this . getDynamicHeader ( item , index ) : this . getStaticHeader ( this . heading , index ) ,
132- children : buildView ( this . pConn$ , index , lookForChildInConfig )
133- } ) ;
150+ children : buildView ( this . pConn$ , index , lookForChildInConfig ) ,
151+ allowRowDelete : evaluateAllowRowAction ( allowRowDelete , item )
152+ } ;
134153 } ) ;
135- this . children = children ;
136154 }
137- this . prevRefLength = this . referenceList . length ;
155+ this . referenceListLength = referenceList ? .length || 0 ;
138156 }
139157
140158 getStaticHeader = ( heading , index ) => {
@@ -149,10 +167,15 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
149167 } ;
150168
151169 addFieldGroupItem ( ) {
152- this . pConn$ . getListActions ( ) . insert ( { classID : this . contextClass } , this . referenceList . length ) ;
170+ this . pConn$ . getListActions ( ) . insert ( { classID : this . contextClass } , this . referenceListLength ) ;
153171 }
154172
155173 deleteFieldGroupItem ( index ) {
156174 this . pConn$ . getListActions ( ) . deleteEntry ( index ) ;
157175 }
176+
177+ getAddBtnLabel ( ) {
178+ const { targetClassLabel } = this . configProps$ ;
179+ return targetClassLabel ? `+ Add ${ targetClassLabel } ` : '+ Add' ;
180+ }
158181}
0 commit comments