UNPKG

@wufengteam/wform

Version:

@wufengteam/wform

186 lines 9.7 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /* eslint-disable no-case-declarations */ /* eslint-disable no-console */ import Decimal from 'decimal.js'; import { isPlainObject } from './tool'; var subFormStatistics = function subFormStatistics(values, refs) { if (isPlainObject(values)) { var subFormIdList = Object.keys(values).filter(function (key) { return key.startsWith('SubForm_'); }); subFormIdList.forEach(function (id) { var _a, _b; var dataStatisticsRules = (_b = (_a = refs[id]) === null || _a === void 0 ? void 0 : _a.getDataStatisticsRules) === null || _b === void 0 ? void 0 : _b.call(_a); if (Array.isArray(dataStatisticsRules) && dataStatisticsRules.length > 0) { var dataStatisticsRulesMap = {}; dataStatisticsRules.forEach(function (item) { if (item === null || item === void 0 ? void 0 : item.currentFormFieldInfo) { dataStatisticsRulesMap["".concat(item.currentFormFieldInfo.id)] = item; } }); // console.log('dataStatisticsRulesMap', dataStatisticsRulesMap); Object.values(dataStatisticsRulesMap).forEach(function (item) { var _a, _b; var statisticalFieldInfo = item.statisticalFieldInfo, currentFormFieldInfo = item.currentFormFieldInfo, calculationMethod = item.calculationMethod; if (currentFormFieldInfo && statisticalFieldInfo) { var statisticalFieldId = statisticalFieldInfo.id, compName = statisticalFieldInfo.compName; var currentFormFieldId = currentFormFieldInfo.id; var subFormValue = values[id]; if (Array.isArray(subFormValue) && calculationMethod) { // console.log('subFormValue', subFormValue); var currentFormFieldValues = []; if (['sum', 'average', 'maximum', 'minimum'].includes(calculationMethod)) { currentFormFieldValues = subFormValue.map(function (val) { return val[statisticalFieldId] ? val[statisticalFieldId] : 0; }); } else { currentFormFieldValues = subFormValue.map(function (val) { var _a; // console.log(statisticalFieldId, val[statisticalFieldId], statisticalFieldInfo); var result = 'false'; if (['ImgUpload', 'StdUpload', 'VideoUpload'].includes(compName)) { if (Array.isArray(val[statisticalFieldId]) && val[statisticalFieldId].length > 0) { result = val[statisticalFieldId]; } return result; } if (compName === 'Address') { if (Array.isArray((_a = val[statisticalFieldId]) === null || _a === void 0 ? void 0 : _a.value) && val[statisticalFieldId].value.length > 0) { result = val[statisticalFieldId]; } return result; } if (val[statisticalFieldId]) { result = val[statisticalFieldId]; } return result; }); } // console.log('currentFormFieldValues', currentFormFieldValues); var formVal = ''; switch (calculationMethod) { case 'sum': var sumResult = new Decimal(0); currentFormFieldValues.forEach(function (num) { sumResult = sumResult.plus(num); }); // console.log('sumResult:', sumResult.toNumber()); formVal = sumResult.toNumber(); break; case 'average': var sumVal = new Decimal(0); currentFormFieldValues.forEach(function (num) { sumVal = sumVal.plus(num); }); var averageResult = sumVal.dividedBy(currentFormFieldValues.length).toNumber(); // console.log('averageResult:', averageResult); (_b = (_a = refs === null || refs === void 0 ? void 0 : refs[currentFormFieldId]) === null || _a === void 0 ? void 0 : _a.setValue) === null || _b === void 0 ? void 0 : _b.call(_a, averageResult); break; case 'maximum': var maxResult = new Decimal(Math.max.apply(Math, _toConsumableArray(currentFormFieldValues))); // console.log('maxResult:', maxResult); formVal = maxResult; break; case 'minimum': var minResult = new Decimal(Math.min.apply(Math, _toConsumableArray(currentFormFieldValues))); // console.log('minResult:', minResult); formVal = minResult; break; case 'filledCount': if (Array.isArray(currentFormFieldValues)) { formVal = currentFormFieldValues.length; } else { formVal = 0; } break; case 'alreadyFilledCount': if (Array.isArray(currentFormFieldValues)) { var result = currentFormFieldValues.filter(function (i) { return i !== 'false'; }); if (Array.isArray(result)) { formVal = result.length; } else { formVal = 0; } } break; case 'notFilledCount': if (Array.isArray(currentFormFieldValues)) { var _result = currentFormFieldValues.filter(function (i) { return i === 'false'; }); if (Array.isArray(_result)) { formVal = _result.length; } else { formVal = 0; } } break; default: console.log(calculationMethod); } setTimeout(function () { var _a, _b; (_b = (_a = refs === null || refs === void 0 ? void 0 : refs[currentFormFieldId]) === null || _a === void 0 ? void 0 : _a.setValue) === null || _b === void 0 ? void 0 : _b.call(_a, formVal); }, 100); } } }); } }); } else if (values === 'init') { var _subFormIdList = Object.keys(refs).filter(function (key) { return key.startsWith('SubForm_'); }); _subFormIdList.forEach(function (id) { var _a, _b; var dataStatisticsRules = (_b = (_a = refs[id]) === null || _a === void 0 ? void 0 : _a.getDataStatisticsRules) === null || _b === void 0 ? void 0 : _b.call(_a); if (Array.isArray(dataStatisticsRules) && dataStatisticsRules.length > 0) { var dataStatisticsRulesMap = {}; dataStatisticsRules.forEach(function (item) { if (item === null || item === void 0 ? void 0 : item.currentFormFieldInfo) { dataStatisticsRulesMap["".concat(item.currentFormFieldInfo.id)] = item; } }); Object.values(dataStatisticsRulesMap).forEach(function (item) { var statisticalFieldInfo = item.statisticalFieldInfo, currentFormFieldInfo = item.currentFormFieldInfo, calculationMethod = item.calculationMethod; if (currentFormFieldInfo && statisticalFieldInfo) { var currentFormFieldId = currentFormFieldInfo.id; if (calculationMethod) { var formVal = ''; switch (calculationMethod) { case 'filledCount': formVal = 0; break; case 'alreadyFilledCount': formVal = 0; break; case 'notFilledCount': formVal = 0; break; default: console.log(calculationMethod); } setTimeout(function () { var _a, _b; (_b = (_a = refs === null || refs === void 0 ? void 0 : refs[currentFormFieldId]) === null || _a === void 0 ? void 0 : _a.setValue) === null || _b === void 0 ? void 0 : _b.call(_a, formVal); }, 100); } } }); } }); } }; export default subFormStatistics;