|
| 1 | +declare var PCore; |
| 2 | + |
| 3 | +export const TABLE_CELL = "SdkRenderer"; |
| 4 | +export const DELETE_ICON = "DeleteIcon"; |
| 5 | + |
| 6 | +// BUG-615253: Workaround for autosize in table with lazy loading components |
| 7 | +/* istanbul ignore next */ |
| 8 | +function getFiledWidth(field, label) { |
| 9 | + let width; |
| 10 | + switch (field.type) { |
| 11 | + case "Time": |
| 12 | + width = 150; |
| 13 | + break; |
| 14 | + case "Date": |
| 15 | + width = 160; |
| 16 | + break; |
| 17 | + case "DateTime": |
| 18 | + width = 205; |
| 19 | + break; |
| 20 | + case "AutoComplete": |
| 21 | + case "TextArea": |
| 22 | + width = 190; |
| 23 | + break; |
| 24 | + case "Currency": |
| 25 | + case "TextInput": |
| 26 | + width = 182; |
| 27 | + break; |
| 28 | + case "Checkbox": |
| 29 | + // eslint-disable-next-line no-case-declarations |
| 30 | + const text = document.createElement("span"); |
| 31 | + document.body.appendChild(text); |
| 32 | + text.style.fontSize = "13px"; |
| 33 | + text.style.position = "absolute"; |
| 34 | + text.innerHTML = label; |
| 35 | + width = Math.ceil(text.clientWidth) + 30; |
| 36 | + document.body.removeChild(text); |
| 37 | + break; |
| 38 | + default: |
| 39 | + width = 180; |
| 40 | + } |
| 41 | + return width; |
| 42 | +} |
| 43 | + |
| 44 | +export const getContext = (thePConn) => { |
| 45 | + const contextName = thePConn.getContextName(); |
| 46 | + const pageReference = thePConn.getPageReference(); |
| 47 | + let { referenceList } = thePConn.getStateProps().config; |
| 48 | + const pageReferenceForRows = referenceList.startsWith(".") |
| 49 | + ? `${pageReference}.${referenceList.substring(1)}` |
| 50 | + : referenceList; |
| 51 | + |
| 52 | + // removing "caseInfo.content" prefix to avoid setting it as a target while preparing pageInstructions |
| 53 | + referenceList = pageReferenceForRows.replace( |
| 54 | + PCore.getConstants().CASE_INFO.CASE_INFO_CONTENT, |
| 55 | + "" |
| 56 | + ); |
| 57 | + |
| 58 | + return { |
| 59 | + contextName, |
| 60 | + referenceListStr: referenceList, |
| 61 | + pageReferenceForRows |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +export const populateRowKey = (rawData) => { |
| 66 | + return rawData.map((row, index) => { |
| 67 | + return { ...row, index }; |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +export const getApiContext = (processedData, pConnect, reorderCB) => { |
| 72 | + return { |
| 73 | + fetchData: () => { |
| 74 | + return new Promise((resolve) => { |
| 75 | + resolve({ |
| 76 | + data: processedData, |
| 77 | + filteredRecordCount: processedData.length, |
| 78 | + totalRecordCount: processedData.length |
| 79 | + }); |
| 80 | + }); |
| 81 | + }, |
| 82 | + fetchPersonalizations: () => { |
| 83 | + return Promise.resolve({}); |
| 84 | + }, |
| 85 | + applyRowReorder: (sourceKey, destinationKey) => { |
| 86 | + // indexes are keys for simple table so, it should work. |
| 87 | + reorderCB(); |
| 88 | + return Promise.resolve( |
| 89 | + pConnect |
| 90 | + .getListActions() |
| 91 | + .reorder(parseInt(sourceKey, 10), parseInt(destinationKey, 10)) |
| 92 | + ); |
| 93 | + } |
| 94 | + }; |
| 95 | +}; |
| 96 | + |
| 97 | +export const buildMetaForListView = ( |
| 98 | + fieldMetadata, |
| 99 | + fields, |
| 100 | + type, |
| 101 | + ruleClass, |
| 102 | + name, |
| 103 | + propertyLabel, |
| 104 | + parameters |
| 105 | +) => { |
| 106 | + return { |
| 107 | + name, |
| 108 | + config: { |
| 109 | + type, |
| 110 | + referenceList: fieldMetadata.datasource.name, |
| 111 | + parameters: parameters ?? fieldMetadata.datasource.parameters, |
| 112 | + personalization: false, |
| 113 | + grouping: true, |
| 114 | + globalSearch: true, |
| 115 | + reorderFields: true, |
| 116 | + toggleFieldVisibility: true, |
| 117 | + personalizationId: "" /* TODO */, |
| 118 | + template: "ListView", |
| 119 | + presets: [ |
| 120 | + { |
| 121 | + name: "presets", |
| 122 | + template: "Table", |
| 123 | + config: {}, |
| 124 | + children: [ |
| 125 | + { |
| 126 | + name: "Columns", |
| 127 | + type: "Region", |
| 128 | + children: fields |
| 129 | + } |
| 130 | + ], |
| 131 | + label: propertyLabel, |
| 132 | + id: "P_" /* TODO */ |
| 133 | + } |
| 134 | + ], |
| 135 | + ruleClass |
| 136 | + } |
| 137 | + }; |
| 138 | +}; |
| 139 | + |
| 140 | +export const buildFieldsForTable = (configFields, fields, bReadOnly) => { |
| 141 | + const fieldDefs = configFields.map((field, index) => { |
| 142 | + return { |
| 143 | + type: "text", |
| 144 | + label: fields[index].config.label || fields[index].config.caption, |
| 145 | + fillAvailableSpace: !!field.config.fillAvailableSpace, |
| 146 | + id: index, |
| 147 | + name: field.config.value.substr(4), |
| 148 | + cellRenderer: TABLE_CELL, |
| 149 | + sort: false, |
| 150 | + noContextMenu: true, |
| 151 | + showMenu: false, |
| 152 | + meta: { |
| 153 | + ...field |
| 154 | + }, |
| 155 | + // BUG-615253: Workaround for autosize in table with lazy loading components |
| 156 | + width: getFiledWidth(field, fields[index].config.label) |
| 157 | + }; |
| 158 | + }); |
| 159 | + |
| 160 | + // ONLY add DELETE_ICON to fields when the table is requested as EDITABLE |
| 161 | + if (!bReadOnly) { |
| 162 | + fieldDefs.push({ |
| 163 | + type: "text", |
| 164 | + id: fieldDefs.length, |
| 165 | + cellRenderer: DELETE_ICON, |
| 166 | + sort: false, |
| 167 | + noContextMenu: true, |
| 168 | + showMenu: false, |
| 169 | + // BUG-615253: Workaround for autosize in table with lazy loading components |
| 170 | + width: 46 |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + return fieldDefs; |
| 175 | +}; |
| 176 | + |
| 177 | +export const createMetaForTable = (fields, renderMode) => { |
| 178 | + return { |
| 179 | + height: { |
| 180 | + minHeight: "auto", |
| 181 | + fitHeightToElement: "fitHeightToElement", |
| 182 | + deltaAdjustment: "deltaAdjustment", |
| 183 | + autoSize: true |
| 184 | + }, |
| 185 | + fieldDefs: fields, |
| 186 | + itemKey: "index", |
| 187 | + grouping: false, |
| 188 | + reorderFields: false, |
| 189 | + reorderItems: renderMode === "Editable", |
| 190 | + dragHandle: renderMode === "Editable", |
| 191 | + globalSearch: false, |
| 192 | + personalization: false, |
| 193 | + toggleFieldVisibility: false, |
| 194 | + toolbar: false, |
| 195 | + footer: false, |
| 196 | + filterExpression: null, |
| 197 | + editing: false, |
| 198 | + timezone: PCore.getEnvironmentInfo().getTimeZone() |
| 199 | + }; |
| 200 | +}; |
| 201 | + |
| 202 | +/** |
| 203 | + * This method returns a callBack function for Add action. |
| 204 | + * @param {object} pConnect - PConnect object |
| 205 | + * @param {number} index - index of the page list to add |
| 206 | + */ |
| 207 | +export const getAddRowCallback = (pConnect, index) => { |
| 208 | + return () => pConnect.getListActions().insert({}, index); |
| 209 | +}; |
| 210 | + |
| 211 | +/** |
| 212 | + * This method creates a PConnect object with proper options for Add and Delete actions |
| 213 | + * @param {string} contextName - contextName |
| 214 | + * @param {string} referenceList - referenceList |
| 215 | + * @param {string} pageReference - pageReference |
| 216 | + */ |
| 217 | +export const createPConnect = (contextName, referenceList, pageReference) => { |
| 218 | + const options = { |
| 219 | + context: contextName, |
| 220 | + pageReference, |
| 221 | + referenceList |
| 222 | + }; |
| 223 | + |
| 224 | + // create PConnect object |
| 225 | + const config = { meta: {}, options }; |
| 226 | + const { getPConnect } = PCore.createPConnect(config); |
| 227 | + |
| 228 | + return getPConnect(); |
| 229 | +}; |
0 commit comments