fx-form-widget
Version:
151 lines (147 loc) • 5.32 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.createBindMap = void 0;
exports.downloadFile = downloadFile;
exports.transformFromAnchor = exports.transformFrom = exports.looseJsonParse = exports.isNotNullValue = exports.getQuery = exports.downloadTemplateDOC = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _qs = _interopRequireDefault(require("qs"));
var looseJsonParse = exports.looseJsonParse = function looseJsonParse(obj) {
// return Function('"use strict";return (' + obj + ')')();
return JSON.parse(obj);
};
// 导入的json => 组件 schema
var transformFrom = exports.transformFrom = function transformFrom(partitions, readonly, hideInForm) {
// 渲染组件
var templateSchemaData = [];
if (!partitions) {
return;
}
if (partitions.length > 0) {
partitions.forEach(function (item) {
var partitionType = item.partitionType,
schemas = item.schemas;
var fieldName = schemas[0].viewSchema.fieldName;
if (hideInForm && hideInForm.includes(fieldName)) return;
var defaultReadonly = schemas[0].viewSchema.readonly;
if (readonly !== undefined) {
if (typeof readonly === 'boolean') {
defaultReadonly = readonly;
} else if (typeof readonly === 'object') {
defaultReadonly = readonly.includes(fieldName) ? true : defaultReadonly;
}
}
if (partitionType === 'baseItem' || partitionType === 'advanceItem' || partitionType === 'layoutItem') {
// 判断是基础组件(不包含表单容器) 分割线特殊
templateSchemaData.push((0, _extends2["default"])({}, schemas[0], {
viewSchema: (0, _extends2["default"])({}, schemas[0].viewSchema, {
readonly: defaultReadonly
})
}));
} else if (partitionType === 'form' || partitionType === 'group') {
var formData = (0, _extends2["default"])({}, schemas[0], {
viewSchema: (0, _extends2["default"])({}, schemas[0].viewSchema, {
readonly: defaultReadonly
})
});
formData.children = schemas[0].children.map(function (child) {
return (0, _extends2["default"])({}, child, {
viewSchema: (0, _extends2["default"])({}, child.viewSchema, {
readonly: defaultReadonly
})
});
});
templateSchemaData.push(formData);
}
});
}
return templateSchemaData;
};
var transformFromAnchor = exports.transformFromAnchor = function transformFromAnchor(templateSchemaData) {
var anchorInfoList = [];
templateSchemaData === null || templateSchemaData === void 0 ? void 0 : templateSchemaData.forEach(function (item) {
if (item.widget === 'separator' || item.widget === 'group') {
anchorInfoList.push({
title: item.viewSchema.title,
anchor: item.wid
});
}
});
return anchorInfoList;
};
/**
* 判断值是否真的为空,不存在
*/
var isNotNullValue = exports.isNotNullValue = function isNotNullValue(value) {
if (value === '' || value === undefined || value === null) {
return false;
} else {
return true;
}
};
// 下载为文件
function downloadFile(data, type, fileName) {
if (type === void 0) {
type = 'vnd.ms-excel';
}
if (fileName === void 0) {
fileName = '';
}
var blob = new Blob([data], {
type: "application/" + type
});
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}
var downloadTemplateDOC = exports.downloadTemplateDOC = function downloadTemplateDOC(docUrl, fileName, fileType) {
var type = fileType || 'vnd.ms-excel';
fetch(docUrl).then(function (response) {
return response.blob();
}).then(function (res) {
downloadFile(res, type, fileName);
});
};
var getQuery = exports.getQuery = function getQuery() {
return _qs["default"].parse(window.location.search.substring(1));
};
var createBindMap = exports.createBindMap = function createBindMap(schmeas) {
var newBindMap = {};
schmeas.map(function (item) {
if (!newBindMap[item.isBind]) {
newBindMap[item.isBind] = [{
bindUrl: item.bindUrl,
bindValueKey: [item.bindValueKey],
fieldName: [item.fieldName]
}];
} else {
var _loop = function _loop(_key) {
if (_key === item.isBind) {
newBindMap[_key].forEach(function (childItem, index) {
console.log(item, newBindMap[_key][index]);
if (item.bindUrl === newBindMap[_key][index].bindUrl) {
// url是一样的则进行接口合并
newBindMap[_key][index].bindValueKey.push(item.bindValueKey);
newBindMap[_key][index].fieldName.push(item.fieldName);
} else {
newBindMap[_key].push({
bindUrl: item.bindUrl,
bindValueKey: [item.bindValueKey],
fieldName: [item.fieldName]
});
}
});
}
};
for (var _key in newBindMap) {
_loop(_key);
}
}
});
return newBindMap;
};