UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

332 lines (279 loc) 9.96 kB
import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import { __decorate } from "tslib"; import isNil from 'lodash/isNil'; import { action, observable, runInAction } from 'mobx'; import { getConfig } from '../../../es/configure'; import warning from '../../../es/_util/warning'; import DataSet from '../data-set/DataSet'; import axios from '../axios'; import { FieldType } from '../data-set/enum'; import { LovFieldType } from '../lov/enum'; import { processAxiosConfig } from './utils'; function getFieldType(conditionFieldType) { switch (conditionFieldType) { case LovFieldType.INT: return FieldType.number; case LovFieldType.TEXT: return FieldType.string; case LovFieldType.DATE: return FieldType.date; case LovFieldType.DATETIME: return FieldType.dateTime; case LovFieldType.POPUP: return FieldType.object; default: return conditionFieldType || FieldType.string; } } function generateConditionField(fields, _ref) { var conditionField = _ref.conditionField, conditionFieldType = _ref.conditionFieldType, conditionFieldName = _ref.conditionFieldName, gridFieldName = _ref.gridFieldName, display = _ref.display, conditionFieldLovCode = _ref.conditionFieldLovCode, conditionFieldSelectCode = _ref.conditionFieldSelectCode, conditionFieldSelectUrl = _ref.conditionFieldSelectUrl, conditionFieldSelectTf = _ref.conditionFieldSelectTf, conditionFieldSelectVf = _ref.conditionFieldSelectVf, conditionFieldRequired = _ref.conditionFieldRequired, fieldProps = _ref.fieldProps; if (conditionField === 'Y') { var name = conditionFieldName || gridFieldName; var field = _objectSpread({ name: name, type: getFieldType(conditionFieldType), label: display, lovCode: conditionFieldLovCode || undefined, lookupCode: conditionFieldSelectCode || undefined, lookupUrl: conditionFieldSelectUrl || undefined, textField: conditionFieldSelectTf || undefined, valueField: conditionFieldSelectVf || undefined, required: conditionFieldRequired || undefined }, fieldProps); fields.push(field); if (conditionFieldType === LovFieldType.POPUP) { var aliasName = "__lov__".concat(name); field.name = aliasName; fields.push({ name: name, bind: "".concat(aliasName, ".").concat(conditionFieldSelectVf) }); } } } function generateGridField(fields, _ref2) { var gridField = _ref2.gridField, gridFieldName = _ref2.gridFieldName, display = _ref2.display, fieldProps = _ref2.fieldProps; if (gridField === 'Y') { fields.push(_objectSpread({ name: gridFieldName, label: display }, fieldProps)); } } export var LovCodeStore = /*#__PURE__*/ function () { function LovCodeStore() { _classCallCheck(this, LovCodeStore); this.pendings = {}; this.init(); } _createClass(LovCodeStore, [{ key: "init", value: function init() { this.lovCodes = observable.map(); } }, { key: "getDefineAxiosConfig", value: function getDefineAxiosConfig(code, field) { var lovDefineAxiosConfig = field && field.get('lovDefineAxiosConfig') || getConfig('lovDefineAxiosConfig'); var config = processAxiosConfig(lovDefineAxiosConfig, code, field); return _objectSpread({}, config, { url: config.url || this.getConfigUrl(code, field), method: config.method || 'post' }); } }, { key: "getConfig", value: function getConfig(code) { return this.lovCodes.get(code); } }, { key: "fetchConfig", value: function () { var _fetchConfig = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(code, field) { var _this = this; var config, axiosConfig, pending; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: config = this.getConfig(code); // SSR do not fetch the lookup if (!(!config && typeof window !== 'undefined')) { _context.next = 14; break; } axiosConfig = this.getDefineAxiosConfig(code, field); if (!axiosConfig) { _context.next = 14; break; } _context.prev = 4; pending = this.pendings[code] || this.axios(axiosConfig); this.pendings[code] = pending; _context.next = 9; return pending; case 9: config = _context.sent; runInAction(function () { if (config) { _this.lovCodes.set(code, config); } }); case 11: _context.prev = 11; delete this.pendings[code]; return _context.finish(11); case 14: return _context.abrupt("return", config); case 15: case "end": return _context.stop(); } } }, _callee, this, [[4,, 11, 14]]); })); function fetchConfig(_x, _x2) { return _fetchConfig.apply(this, arguments); } return fetchConfig; }() // lovCode 作为key 缓存了 ds }, { key: "getLovDataSet", value: function getLovDataSet(code, field, dataSetProps) { var config = this.getConfig(code); if (config) { var lovPageSize = config.lovPageSize, lovItems = config.lovItems, parentIdField = config.parentIdField, idField = config.idField, treeFlag = config.treeFlag, configDataSetProps = config.dataSetProps; var valueField = field ? field.get('valueField') : config.valueField; var dsProps = { transport: { read: this.getQueryAxiosConfig(code, field, config) }, primaryKey: valueField, cacheSelection: true }; if (!isNil(lovPageSize) && !isNaN(Number(lovPageSize))) { dsProps.pageSize = Number(lovPageSize); } else { dsProps.paging = false; } if (treeFlag === 'Y' && parentIdField && idField) { dsProps.parentField = parentIdField; dsProps.idField = idField; } if (lovItems && lovItems.length) { var _lovItems$sort$reduce = lovItems.sort(function (_ref3, _ref4) { var conditionFieldSequence = _ref3.conditionFieldSequence; var conditionFieldSequence2 = _ref4.conditionFieldSequence; return conditionFieldSequence - conditionFieldSequence2; }).reduce(function (obj, configItem) { generateConditionField(obj.querys, configItem); generateGridField(obj.fields, configItem); return obj; }, { querys: [], fields: [] }), querys = _lovItems$sort$reduce.querys, fields = _lovItems$sort$reduce.fields; if (querys.length) { dsProps.queryFields = querys; } if (fields.length) { dsProps.fields = fields; } } return new DataSet(_objectSpread({}, dsProps, {}, configDataSetProps, {}, dataSetProps)); } warning(false, "LOV: code<".concat(code, "> is not exists")); return undefined; } }, { key: "getConfigUrl", value: function getConfigUrl(code, field) { var lovDefineUrl = field && field.get('lovDefineUrl') || getConfig('lovDefineUrl'); if (typeof lovDefineUrl === 'function') { return lovDefineUrl(code); } return lovDefineUrl; } }, { key: "getQueryAxiosConfig", value: function getQueryAxiosConfig(code, field, config) { var _this2 = this; return function (props) { var lovQueryAxiosConfig = field && field.get('lovQueryAxiosConfig') || getConfig('lovQueryAxiosConfig'); var lovQueryUrl = _this2.getQueryUrl(code, field, props); var axiosConfig = processAxiosConfig(lovQueryAxiosConfig, code, config, props, lovQueryUrl); return _objectSpread({}, axiosConfig, { url: axiosConfig.url || lovQueryUrl, method: axiosConfig.method || 'post' }); }; } }, { key: "getQueryUrl", value: function getQueryUrl(code, field, props) { var config = this.getConfig(code); if (config) { var customUrl = config.customUrl; if (customUrl) { return customUrl; } } var lovQueryUrl = field && field.get('lovQueryUrl') || getConfig('lovQueryUrl'); if (typeof lovQueryUrl === 'function') { return lovQueryUrl(code, config, props); } return lovQueryUrl; } }, { key: "clearCache", value: function clearCache(codes) { var _this3 = this; if (codes) { codes.forEach(function (code) { _this3.lovCodes["delete"](code); }); } else { this.lovCodes.clear(); } } }, { key: "axios", get: function get() { return getConfig('axios') || axios; } }]); return LovCodeStore; }(); __decorate([observable], LovCodeStore.prototype, "lovCodes", void 0); __decorate([action], LovCodeStore.prototype, "init", null); __decorate([action], LovCodeStore.prototype, "clearCache", null); export default new LovCodeStore(); //# sourceMappingURL=LovCodeStore.js.map