1
- import { Component , OnInit , Input , forwardRef , OnDestroy , OnChanges } from '@angular/core' ;
1
+ import { Component , OnInit , Input , forwardRef , OnDestroy , OnChanges , AfterViewInit } from '@angular/core' ;
2
2
import { CommonModule } from '@angular/common' ;
3
3
import { FormGroup } from '@angular/forms' ;
4
4
import { MatButtonModule } from '@angular/material/button' ;
5
5
import { AngularPConnectData , AngularPConnectService } from '../../../_bridge/angular-pconnect' ;
6
6
import { buildView , getReferenceList } from '../../../_helpers/field-group-utils' ;
7
7
import { Utils } from '../../../_helpers/utils' ;
8
8
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component' ;
9
+ import { evaluateAllowRowAction } from './utils' ;
9
10
10
11
interface FieldGroupTemplateProps {
11
12
// If any, enter additional props that only exist on this component
12
13
label ?: string ;
13
- showLabel ?: boolean ;
14
+ hideLabel ?: boolean ;
15
+ allowActions ?: any ;
16
+ allowRowDelete ?: any ;
14
17
referenceList ?: any [ ] ;
15
18
contextClass : string ;
16
19
renderMode ?: string ;
@@ -19,6 +22,7 @@ interface FieldGroupTemplateProps {
19
22
displayMode ?: string ;
20
23
fieldHeader ?: string ;
21
24
allowTableEdit : boolean ;
25
+ targetClassLabel ?: string ;
22
26
}
23
27
24
28
@Component ( {
@@ -28,26 +32,27 @@ interface FieldGroupTemplateProps {
28
32
standalone : true ,
29
33
imports : [ CommonModule , MatButtonModule , forwardRef ( ( ) => ComponentMapperComponent ) ]
30
34
} )
31
- export class FieldGroupTemplateComponent implements OnInit , OnDestroy , OnChanges {
35
+ export class FieldGroupTemplateComponent implements OnInit , OnDestroy , OnChanges , AfterViewInit {
32
36
@Input ( ) configProps$ : FieldGroupTemplateProps ;
33
37
@Input ( ) pConn$ : typeof PConnect ;
34
38
@Input ( ) formGroup$ : FormGroup ;
35
39
36
40
angularPConnectData : AngularPConnectData = { } ;
37
- inheritedProps$ : object ;
41
+
38
42
showLabel$ ?: boolean = true ;
39
43
label$ ?: string ;
40
44
readonlyMode : boolean ;
41
45
contextClass : any ;
42
- referenceList : any ;
43
- pageReference : any ;
44
46
heading : any ;
45
47
children : any ;
46
48
menuIconOverride$ : any ;
47
- prevRefLength : number ;
48
- allowAddEdit : boolean ;
49
+ referenceListLength : number ;
49
50
fieldHeader : any ;
50
51
52
+ allowAdd = true ;
53
+ allowEdit = true ;
54
+ allowDelete = true ;
55
+
51
56
constructor (
52
57
private angularPConnect : AngularPConnectService ,
53
58
private utils : Utils
@@ -58,9 +63,21 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
58
63
this . angularPConnectData = this . angularPConnect . registerAndSubscribeComponent ( this , this . onStateChange ) ;
59
64
this . updateSelf ( ) ;
60
65
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 ) ;
64
81
}
65
82
}
66
83
@@ -85,56 +102,57 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
85
102
const props = changes . configProps$ ;
86
103
if ( props . currentValue !== props . previousValue ) {
87
104
this . configProps$ = props . currentValue ;
105
+
88
106
if ( changes ?. pConn$ ?. currentValue ) {
89
107
this . pConn$ = changes ?. pConn$ ?. currentValue ;
90
108
}
109
+
91
110
this . updateSelf ( ) ;
92
111
}
93
112
}
94
113
}
95
114
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
+
96
121
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 ( ) ;
103
123
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 ;
105
131
106
- const renderMode = this . configProps$ . renderMode ;
107
- const displayMode = this . configProps$ . displayMode ;
108
132
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
+
113
138
const resolvedList = getReferenceList ( this . pConn$ ) ;
114
- this . pageReference = `${ this . pConn$ . getPageReference ( ) } ${ resolvedList } ` ;
115
139
this . pConn$ . setReferenceList ( resolvedList ) ;
140
+
116
141
if ( this . readonlyMode ) {
117
142
this . pConn$ . setInheritedProp ( 'displayMode' , 'DISPLAY_ONLY' ) ;
118
143
}
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 {
130
148
id : index ,
131
149
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
+ } ;
134
153
} ) ;
135
- this . children = children ;
136
154
}
137
- this . prevRefLength = this . referenceList . length ;
155
+ this . referenceListLength = referenceList ? .length || 0 ;
138
156
}
139
157
140
158
getStaticHeader = ( heading , index ) => {
@@ -149,10 +167,15 @@ export class FieldGroupTemplateComponent implements OnInit, OnDestroy, OnChanges
149
167
} ;
150
168
151
169
addFieldGroupItem ( ) {
152
- this . pConn$ . getListActions ( ) . insert ( { classID : this . contextClass } , this . referenceList . length ) ;
170
+ this . pConn$ . getListActions ( ) . insert ( { classID : this . contextClass } , this . referenceListLength ) ;
153
171
}
154
172
155
173
deleteFieldGroupItem ( index ) {
156
174
this . pConn$ . getListActions ( ) . deleteEntry ( index ) ;
157
175
}
176
+
177
+ getAddBtnLabel ( ) {
178
+ const { targetClassLabel } = this . configProps$ ;
179
+ return targetClassLabel ? `+ Add ${ targetClassLabel } ` : '+ Add' ;
180
+ }
158
181
}
0 commit comments