-
Notifications
You must be signed in to change notification settings - Fork 223
/
derived.js
181 lines (150 loc) · 5.53 KB
/
derived.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* eslint-disable @typescript-eslint/no-unused-vars*/
export default {
getSelectedItem: (props, moment, _) => {
const selectedItemIndex =
props.selectedItemIndex === undefined ||
Number.isNaN(parseInt(props.selectedItemIndex))
? -1
: parseInt(props.selectedItemIndex);
const items = props.listData || [];
if (selectedItemIndex === -1) {
const emptyRow = { ...items[0] };
Object.keys(emptyRow).forEach((key) => {
emptyRow[key] = "";
});
return emptyRow;
}
const selectedItem = { ...items[selectedItemIndex] };
return selectedItem;
},
//
getItems: (props, moment, _) => {
let item = {};
Object.keys(props.template).map((widgetName) => {
item[widgetName] = { ...props.template[widgetName] };
});
let updatedItems = [];
for (let itemIndex = 0; itemIndex < props.listData.length; itemIndex++) {
let currentItem = JSON.parse(JSON.stringify(item));
const widgetKeys = Object.keys(currentItem);
for (let i = 0; i < widgetKeys.length; i++) {
const currentWidgetName = widgetKeys[i];
let currentWidget = currentItem[currentWidgetName];
const filteredWidget = {};
const dynamicPaths = _.compact(
currentWidget.dynamicBindingPathList?.map((path) => path.key),
);
dynamicPaths.forEach((path) => {
const evaluatedProperty = _.get(
props.template,
currentWidget.widgetName + "." + path,
);
if (Array.isArray(evaluatedProperty)) {
const evaluatedValue = evaluatedProperty[itemIndex];
_.set(currentWidget, path, evaluatedValue);
}
});
if (props.childrenDefaultPropertiesMap) {
Object.keys(props.childrenDefaultPropertiesMap).map((key) => {
const defaultKey = props.childrenDefaultPropertiesMap[key];
const widgetName = key.split(".").shift();
if (widgetName === currentWidget.widgetName) {
const defaultPropertyValue = _.get(
props.template,
currentWidget.widgetName + "." + defaultKey,
undefined,
);
if (Array.isArray(defaultPropertyValue)) {
const evaluatedValue = defaultPropertyValue[itemIndex];
_.set(currentWidget, key.split(".").pop(), evaluatedValue);
} else if (defaultPropertyValue) {
_.set(
currentWidget,
key.split(".").pop(),
defaultPropertyValue,
);
}
}
});
}
const metaProperties = _.get(
props.childMetaProperties,
currentWidget.widgetName,
{},
);
Object.keys(metaProperties).map((key) => {
const metaPropertyValue = _.get(
metaProperties,
key + "." + itemIndex,
undefined,
);
if (typeof metaPropertyValue !== "undefined") {
_.set(currentWidget, key, metaPropertyValue);
}
});
if (props.childrenEntityDefinitions) {
currentItem[currentWidgetName] = _.pick(
currentWidget,
props.childrenEntityDefinitions[currentWidget.type],
);
}
}
updatedItems[itemIndex] = currentItem;
}
return updatedItems;
},
// Patch #12438 - hard-coded DEFAULT_GRID_ROW_HEIGHT(parentRowSpace) for calculating template/component height. Ideally it should be dynamic based on props.
getPageSize: (props, moment, _) => {
const LIST_WIDGET_PAGINATION_HEIGHT = 36;
const DEFAULT_GRID_ROW_HEIGHT = 10;
const WIDGET_PADDING = DEFAULT_GRID_ROW_HEIGHT * 0.4;
const itemsCount = (props.listData || []).length;
let gridGap = 0;
try {
gridGap = parseInt(props.gridGap);
if (!_.isNumber(props.gridGap) || _.isNaN(props.gridGap)) {
gridGap = 0;
}
} catch {
gridGap = 0;
}
gridGap = gridGap >= -8 ? gridGap : 0;
const averageGridGap = itemsCount
? gridGap * ((itemsCount - 1) / itemsCount)
: 0;
const templateBottomRow = props.templateBottomRow;
const templateHeight = templateBottomRow * DEFAULT_GRID_ROW_HEIGHT;
const componentHeight =
(props.bottomRow - props.topRow) * DEFAULT_GRID_ROW_HEIGHT;
const spaceAvailableWithoutPaginationControls =
componentHeight - WIDGET_PADDING * 2;
const spaceAvailableWithPaginationControls =
spaceAvailableWithoutPaginationControls - LIST_WIDGET_PAGINATION_HEIGHT;
const spaceTakenByOneContainer = templateHeight + averageGridGap;
const spaceTakenByAllContainers = spaceTakenByOneContainer * itemsCount;
const paginationControlsEnabled =
spaceTakenByAllContainers > spaceAvailableWithoutPaginationControls ||
props.serverSidePaginationEnabled;
const totalAvailableSpace = paginationControlsEnabled
? spaceAvailableWithPaginationControls
: spaceAvailableWithoutPaginationControls;
const pageSize = totalAvailableSpace / spaceTakenByOneContainer;
return _.isNaN(pageSize) ? 0 : _.floor(pageSize);
},
//
// this is just a patch for #7520
getChildAutoComplete: (props, moment, _) => {
const data = [...props.listData];
const structure =
Array.isArray(data) && data.length > 0
? Object.assign(
{},
...Object.keys(data[0]).map((key) => ({
[key]: "",
})),
)
: {};
return { currentItem: structure, currentIndex: "" };
},
//
};