@@ -74,34 +74,48 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD
74
74
parameterList : any
75
75
) {
76
76
const parsedItemDataList = await this . parseDataList ( itemDataList ) ;
77
- const itemRows = new ItemProcessor ( parsedItemDataList , parameterList ) . process ( rows ) ;
78
- const parsedItemRows = await this . hackProcessRows ( itemRows ) ;
79
- const replacedActionRows = this . setActionListMeta ( parsedItemRows , parsedItemDataList ) ;
77
+ const { itemRows, itemData } = new ItemProcessor ( parsedItemDataList , parameterList ) . process (
78
+ rows
79
+ ) ;
80
+ const itemRowsWithMeta = this . setItemMeta ( itemRows , itemData ) ;
81
+
82
+ const parsedItemRows = await this . hackProcessRows ( itemRowsWithMeta ) ;
80
83
// TODO - deep diff and only update changed
81
- this . itemRows = replacedActionRows ;
84
+ this . itemRows = parsedItemRows ;
82
85
this . cdr . markForCheck ( ) ;
83
86
}
84
87
85
88
/**
86
- * Update any action list set_item args to contain name of current data list and item id
87
- * and set_items action to include all currently displayed rows
89
+ * Update item dynamic evaluation context and action lists to include relevant
90
+ * item data
91
+ * @param templateRows List of template rows generated from itemData by item processor
92
+ * @param itemData List of original item data used to create item rows (post operations such as filter/sort)
88
93
* */
89
- private setActionListMeta (
90
- rows : FlowTypes . TemplateRow [ ] ,
91
- dataList : {
92
- [ index : string ] : any ;
93
- }
94
- ) {
95
- return rows . map ( ( r ) => {
94
+ private setItemMeta ( templateRows : FlowTypes . TemplateRow [ ] , itemData : FlowTypes . Data_listRow [ ] ) {
95
+ const lastItemIndex = itemData . length - 1 ;
96
+ const itemDataIDs = itemData . map ( ( item ) => item . id ) ;
97
+ // Reassign metadata fields previously assigned by item as rendered row count may have changed
98
+ return templateRows . map ( ( r ) => {
99
+ // Map the row item context to the original list of items rendered to know position in item list.
100
+ const itemIndex = itemDataIDs . indexOf ( r . _evalContext . itemContext . _id ) ;
101
+ // Update metadata fields as _first, _last and index may have changed based on dynamic updates
102
+ r . _evalContext . itemContext = {
103
+ ...r . _evalContext . itemContext ,
104
+ _index : itemIndex ,
105
+ _first : itemIndex === 0 ,
106
+ _last : itemIndex === lastItemIndex ,
107
+ } ;
108
+ // Update any action list set_item args to contain name of current data list and item id
109
+ // and set_items action to include all currently displayed rows
96
110
if ( r . action_list ) {
97
111
r . action_list = r . action_list . map ( ( a ) => {
98
112
if ( a . action_id === "set_item" ) {
99
- a . args = [ this . dataListName , r . _evalContext . itemContext . id ] ;
113
+ a . args = [ this . dataListName , r . _evalContext . itemContext . _id ] ;
100
114
}
101
115
if ( a . action_id === "set_items" ) {
102
116
// TODO - add a check for @item refs and replace parameter list with correct values
103
117
// for each individual item (default will be just to pick the first)
104
- a . args = [ this . dataListName , Object . values ( dataList ) . map ( ( v ) => v . id ) ] ;
118
+ a . args = [ this . dataListName , itemDataIDs ] ;
105
119
}
106
120
return a ;
107
121
} ) ;
@@ -114,7 +128,7 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD
114
128
* Ordinarily rows would be processed as part of the regular template processing,
115
129
* however this must be bypassed to allow multiple reprocessing on item updates
116
130
*/
117
- private async hackProcessRows ( rows ) {
131
+ private async hackProcessRows ( rows : FlowTypes . TemplateRow [ ] ) {
118
132
const processor = new TemplateRowService ( this . injector , {
119
133
name : "" ,
120
134
template : {
0 commit comments