ze-react-component-library
Version:
ZeroETP React Component Library
118 lines (92 loc) • 3.25 kB
JavaScript
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return __assign.apply(this, arguments);
};
import { getNameProperty } from "zeroetp-api-sdk";
export var canUseCrossTable = function canUseCrossTable(logicform) {
var _a;
if (Array.isArray(logicform.groupby) && logicform.groupby.length === 2) {
if (((_a = logicform.preds) === null || _a === void 0 ? void 0 : _a.length) === 1) {
return true;
}
}
return false;
};
var getIDKey = function getIDKey(prop, item) {
if (!prop.schema) {
return item[prop.name];
}
var nameProp = getNameProperty(prop.schema);
return item[prop.name][nameProp.name];
};
export var crossResult = function crossResult(ret, horizontalColumns, parent) {
var _a;
var _b = JSON.parse(JSON.stringify(ret)),
result = _b.result,
columnProperties = _b.columnProperties,
logicform = _b.logicform;
var idProp0 = columnProperties[0];
var idProp1 = columnProperties[1];
var measurementName = columnProperties[2].name;
var newData = [];
var newColumnProperties = []; // 1. 搞newColumnProperties
newColumnProperties.push(__assign({}, idProp0)); // 第一列的数据变为columnProperties。
if (horizontalColumns) {
for (var _i = 0, horizontalColumns_1 = horizontalColumns; _i < horizontalColumns_1.length; _i++) {
var item = horizontalColumns_1[_i];
newColumnProperties.push(__assign(__assign({}, columnProperties[2]), {
name: item
}));
}
} else {
var secondColKeys = new Set();
for (var _c = 0, result_1 = result; _c < result_1.length; _c++) {
var item = result_1[_c];
secondColKeys.add(getIDKey(columnProperties[1], item));
}
for (var _d = 0, _e = Array.from(secondColKeys); _d < _e.length; _d++) {
var key = _e[_d];
newColumnProperties.push(__assign(__assign({}, columnProperties[2]), {
name: key
}));
}
}
result.forEach(function (item) {
var _a;
var id = typeof item[idProp0.name] === "string" ? item[idProp0.name] : item[idProp0.name]._id;
if (parent) {
id = "" + parent._id + id;
} // 看一下是不是有__开头合并项
if (item._id && item._id.startsWith("__")) {
id = "__" + item._id.split("_")[2];
}
var hitIndex = newData.findIndex(function (i) {
return i._id === id;
});
if (hitIndex < 0) {
newData.push((_a = {
_id: id
}, _a[idProp0.name] = item[idProp0.name], _a));
hitIndex = newData.length - 1;
}
var idKey = getIDKey(idProp1, item);
newData[hitIndex][idKey] = item[measurementName];
}); // 更新logicform
var newLogicform = logicform;
if (((_a = logicform === null || logicform === void 0 ? void 0 : logicform.groupby) === null || _a === void 0 ? void 0 : _a.length) > 1) {
newLogicform.groupby = [logicform.groupby[0]];
}
return __assign(__assign({}, ret), {
logicform: newLogicform,
columnProperties: newColumnProperties,
result: newData
});
};