UNPKG

choerodon-ui

Version:

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

1,382 lines (1,142 loc) 39.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _tslib = require("tslib"); var _mobx = require("mobx"); var _raf = _interopRequireDefault(require("raf")); var _omit = _interopRequireDefault(require("lodash/omit")); var _isEqual = _interopRequireDefault(require("lodash/isEqual")); var _isObject = _interopRequireDefault(require("lodash/isObject")); var _merge = _interopRequireDefault(require("lodash/merge")); var _configure = require("../../../lib/configure"); var _warning = _interopRequireDefault(require("../../../lib/_util/warning")); var _DataSet = _interopRequireDefault(require("./DataSet")); var _Validator = _interopRequireDefault(require("../validator/Validator")); var _enum = require("./enum"); var _LookupCodeStore = _interopRequireDefault(require("../stores/LookupCodeStore")); var _LovCodeStore = _interopRequireDefault(require("../stores/LovCodeStore")); var _AttachmentStore = _interopRequireDefault(require("../stores/AttachmentStore")); var _localeContext = _interopRequireDefault(require("../locale-context")); var _utils = require("./utils"); var _isSame = _interopRequireDefault(require("../_util/isSame")); var _isSameLike = _interopRequireDefault(require("../_util/isSameLike")); var _utils2 = require("../axios/utils"); var _utils3 = require("../field/utils"); var _utils4 = require("../stores/utils"); var _utils5 = require("../number-field/utils"); var _AttachmentFile = _interopRequireDefault(require("./AttachmentFile")); function isEqualDynamicProps(oldProps, newProps) { if (newProps === oldProps) { return true; } if ((0, _isObject["default"])(newProps) && (0, _isObject["default"])(oldProps)) { var newKeys = Object.keys(newProps); var length = newKeys.length; if (length) { if (length !== Object.keys(oldProps).length) { return false; } return newKeys.every(function (key) { var value = newProps[key]; var oldValue = oldProps[key]; if (oldValue === value) { return true; } if (typeof value === 'function' && typeof oldValue === 'function') { return value.toString() === oldValue.toString(); } return (0, _isEqual["default"])(oldValue, value); }); } } return (0, _isEqual["default"])(newProps, oldProps); } function getPropsFromLovConfig(lovCode, propsName) { var props = {}; if (lovCode) { var config = _LovCodeStore["default"].getConfig(lovCode); if (config) { propsName.forEach(function (name) { var value = config[name]; if (value) { props[name] = value; } }); } } return props; } var LOOKUP_SIDE_EFFECT_KEYS = ['type', 'lookupUrl', 'lookupCode', 'lookupAxiosConfig', 'lovCode', 'lovQueryAxiosConfig', 'lovPara', 'cascadeMap', 'lovQueryUrl', 'optionsProps']; var LOV_SIDE_EFFECT_KEYS = ['lovCode', 'lovDefineAxiosConfig', 'lovDefineUrl', 'optionsProps']; var LOOKUP_TOKEN = '__lookup_token__'; var Field = /*#__PURE__*/ function () { function Field() { var _this = this; var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var dataSet = arguments.length > 1 ? arguments[1] : undefined; var record = arguments.length > 2 ? arguments[2] : undefined; (0, _classCallCheck2["default"])(this, Field); (0, _mobx.runInAction)(function () { _this.dataSet = dataSet; _this.record = record; _this.props = _mobx.observable.map(props); // 优化性能,没有动态属性时不用处理, 直接引用dsField; 有options时,也不处理 if (!_this.getProp('options')) { var dynamicProps = _this.getProp('dynamicProps'); if (!record || typeof dynamicProps === 'function') { (0, _raf["default"])(function () { _this.fetchLookup(); _this.fetchLovConfig(); }); } else { var computedProps = _this.getProp('computedProps'); if (computedProps || dynamicProps) { var keys = Object.keys((0, _objectSpread2["default"])({}, computedProps, {}, dynamicProps)); if (keys.length) { var fetches = []; if (keys.some(function (key) { return LOOKUP_SIDE_EFFECT_KEYS.includes(key); })) { fetches.push(_this.fetchLookup); } if (keys.some(function (key) { return LOV_SIDE_EFFECT_KEYS.includes(key); })) { fetches.push(_this.fetchLovConfig); } if (fetches.length) { (0, _raf["default"])(function () { return fetches.forEach(function (one) { return one.call(_this); }); }); } } } } } }); } (0, _createClass2["default"])(Field, [{ key: "getProps", /** * 获取所有属性 * @return 属性对象 */ value: function getProps() { var dsField = this.findDataSetField(); var lovCode = this.get('lovCode'); return (0, _merge["default"])({ lookupUrl: (0, _configure.getConfig)('lookupUrl') }, Field.defaultProps, getPropsFromLovConfig(lovCode, ['textField', 'valueField']), dsField && dsField.props.toPOJO(), this.props.toPOJO()); } /** * 根据属性名获取属性值 * @param propsName 属性名 * @return {any} */ }, { key: "get", value: function get(propsName) { var prop = this.getProp(propsName); if (prop !== undefined) { return prop; } return Field.defaultProps[propsName]; } }, { key: "getProp", value: function getProp(propsName) { var _this2 = this; if (!['computedProps', 'dynamicProps'].includes(propsName)) { var computedPropsMap = (0, _utils.getIf)(this, 'computedProps', function () { return new Map(); }); var computedProp = computedPropsMap.get(propsName); if (computedProp) { var computedValue = computedProp.get(); if (computedValue !== undefined) { return computedValue; } } else { var computedProps = this.get('computedProps'); if (computedProps) { var newComputedProp = (0, _mobx.computed)(function () { var computProp = computedProps[propsName]; if (typeof computProp === 'function') { var prop = _this2.executeDynamicProps(computProp, propsName); if (prop !== undefined) { _this2.checkDynamicProp(propsName, prop); return prop; } } }, { name: propsName, context: this }); computedPropsMap.set(propsName, newComputedProp); var _computedValue = newComputedProp.get(); if (_computedValue !== undefined) { return _computedValue; } } } var dynamicProps = this.get('dynamicProps'); if (dynamicProps) { if (typeof dynamicProps === 'function') { (0, _warning["default"])(false, " The dynamicProps hook will be deprecated. Please use dynamicProps map.\n For e.g,\n Bad case:\n dynamicProps({ record }) {\n return {\n bind: record.get('xx'),\n label: record.get('yy'),\n }\n }\n Good case:\n dynamicProps = {\n bind({ record }) {\n return record.get('xx')\n },\n label({ record }) {\n return record.get('yy'),\n }\n }"); var props = this.executeDynamicProps(dynamicProps, propsName); if (props && propsName in props) { var prop = props[propsName]; this.checkDynamicProp(propsName, prop); return prop; } } else { var dynamicProp = dynamicProps[propsName]; if (typeof dynamicProp === 'function') { var _prop = this.executeDynamicProps(dynamicProp, propsName); if (_prop !== undefined) { this.checkDynamicProp(propsName, _prop); return _prop; } } } this.checkDynamicProp(propsName, undefined); } } var value = this.props.get(propsName); if (value !== undefined) { return value; } var dsField = this.findDataSetField(); if (dsField) { var dsValue = dsField.getProp(propsName); if (dsValue !== undefined) { return dsValue; } } if (propsName === 'lookup') { var dataSet = this.dataSet; if (dataSet) { var lookupToken = this.get(LOOKUP_TOKEN); var lookupCaches = dataSet.lookupCaches; if (lookupToken && lookupCaches) { return lookupCaches.get(lookupToken); } } } if (propsName === 'textField' || propsName === 'valueField') { var lovCode = this.get('lovCode'); var lovProp = getPropsFromLovConfig(lovCode, [propsName])[propsName]; if (lovProp) { return lovProp; } } if (propsName === 'lookupUrl') { return (0, _configure.getConfig)(propsName); } if (['min', 'max'].includes(propsName)) { if (this.type === _enum.FieldType.number) { if (propsName === 'max') { return _utils5.MAX_SAFE_INTEGER; } return _utils5.MIN_SAFE_INTEGER; } } return undefined; } /** * 设置属性值 * @param propsName 属性名 * @param value 属性值 * @return {any} */ }, { key: "set", value: function set(propsName, value) { var oldValue = this.get(propsName); if (!isEqualDynamicProps((0, _mobx.toJS)(oldValue), value)) { var dirtyProps = (0, _utils.getIf)(this, 'dirtyProps', {}); if (!(propsName in dirtyProps)) { (0, _mobx.set)(dirtyProps, propsName, oldValue); } else if ((0, _isSame["default"])((0, _mobx.toJS)(dirtyProps[propsName]), value)) { (0, _mobx.remove)(dirtyProps, propsName); } this.props.set(propsName, value); var record = this.record, dataSet = this.dataSet, name = this.name; if (record && propsName === 'type') { record.set(name, (0, _utils.processValue)(record.get(name), this)); } if (dataSet) { dataSet.fireEvent(_enum.DataSetEvents.fieldChange, { dataSet: dataSet, record: record, name: name, field: this, propsName: propsName, value: value, oldValue: oldValue }); } this.handlePropChange(propsName, value, oldValue); } } /** * 根据lookup值获取lookup对象 * @param value lookup值 * @return {object} */ }, { key: "getLookupData", value: function getLookupData() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getValue(); var valueField = this.get('valueField'); var data = {}; if (this.lookup) { return this.lookup.find(function (obj) { return (0, _isSameLike["default"])((0, _mobx.get)(obj, valueField), value); }) || data; } return data; } }, { key: "getValue", value: function getValue() { var dataSet = this.dataSet, name = this.name; var record = this.record || dataSet && dataSet.current; if (record) { return record.get(name); } } /** * 可以根据lookup值获取含义 * @param value lookup值 * @param boolean showValueIfNotFound * @return {string} */ }, { key: "getLookupText", value: function getLookupText() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getValue(); var showValueIfNotFound = arguments.length > 1 ? arguments[1] : undefined; var textField = this.get('textField'); var valueField = this.get('valueField'); var lookup = this.lookup; if (lookup) { var found = lookup.find(function (obj) { return (0, _isSameLike["default"])((0, _mobx.get)(obj, valueField), value); }); if (found) { return (0, _mobx.get)(found, textField); } if (showValueIfNotFound) { return value; } return undefined; } } /** * 可以根据options值获取含义 * @param value opions值 * @param boolean showValueIfNotFound * @return {string} */ }, { key: "getOptionsText", value: function getOptionsText() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getValue(); var showValueIfNotFound = arguments.length > 1 ? arguments[1] : undefined; var textField = this.get('textField'); var valueField = this.get('valueField'); var options = this.options; if (options) { var found = options.find(function (record) { return (0, _isSameLike["default"])(record.get(valueField), value); }); if (found) { return found.get(textField); } if (showValueIfNotFound) { return value; } return undefined; } } /** * 根据lookup值获取lookup含义 * @param value lookup值 * @param boolean showValueIfNotFound * @return {string} */ }, { key: "getText", value: function getText() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.getValue(); var showValueIfNotFound = arguments.length > 1 ? arguments[1] : undefined; var lookup = this.lookup; if (lookup && !(0, _isObject["default"])(value)) { return this.getLookupText(value, showValueIfNotFound); } var options = this.get('options'); var textField = this.get('textField'); if (options) { var valueField = this.get('valueField'); var found = options.find(function (record) { return (0, _isSameLike["default"])(record.get(valueField), value); }); if (found) { return found.get(textField); } } if (textField && (0, _isObject["default"])(value)) { if ((0, _mobx.isObservableObject)(value)) { return (0, _mobx.get)(value, textField); } return value[textField]; } return value; } }, { key: "setOptions", value: function setOptions(options) { this.set('options', options); } }, { key: "getOptions", value: function getOptions() { return this.options; } /** * 重置设置的属性 */ }, { key: "reset", value: function reset() { var dirtyProps = this.dirtyProps; if (dirtyProps) { this.props.merge(dirtyProps); this.dirtyProps = undefined; } } }, { key: "commit", value: function commit() { var validator = this.validator; if (validator) { validator.reset(); } } /** * 是否必选 * @return true | false */ }, { key: "setLovPara", /** * 设置Lov的查询参数 * @param {String} name * @param {Object} value */ value: function setLovPara(name, value) { var p = (0, _mobx.toJS)(this.get('lovPara')) || {}; if (value === null) { delete p[name]; } else { p[name] = value; } this.set('lovPara', p); } }, { key: "getValidatorProps", value: function getValidatorProps() { var record = this.record; if (record) { var dataSet = this.dataSet, name = this.name, type = this.type, required = this.required, attachmentCount = this.attachmentCount; var baseType = (0, _utils.getBaseType)(type); var customValidator = this.get('validator'); var max = this.get('max'); var min = this.get('min'); var format = this.get('format') || (0, _utils3.getDateFormatByField)(this, this.type); var pattern = this.get('pattern'); var step = this.get('step'); var nonStrictStep = this.get('nonStrictStep') === undefined ? (0, _configure.getConfig)('numberFieldNonStrictStep') : this.get('nonStrictStep'); var minLength = baseType !== _enum.FieldType.string ? undefined : this.get('minLength'); var maxLength = baseType !== _enum.FieldType.string ? undefined : this.get('maxLength'); var label = this.get('label'); var range = this.get('range'); var multiple = this.get('multiple'); var unique = this.get('unique'); var defaultValidationMessages = this.get('defaultValidationMessages'); var validatorProps = { type: type, required: required, record: record, dataSet: dataSet, name: name, unique: unique, customValidator: customValidator, pattern: pattern, max: (0, _utils.getLimit)(max, record), min: (0, _utils.getLimit)(min, record), step: step, nonStrictStep: nonStrictStep, minLength: minLength, maxLength: maxLength, label: label, range: range, multiple: multiple, format: format, attachmentCount: attachmentCount, defaultValidationMessages: defaultValidationMessages }; if (!this.validatorPropKeys) { this.validatorPropKeys = Object.keys((0, _omit["default"])(validatorProps, ['label', 'defaultValidationMessages'])); } return validatorProps; } } /** * 校验字段值 * 只有通过record.getField()获取的field才能校验 * @return true | false */ }, { key: "checkValidity", value: function () { var _checkValidity = (0, _asyncToGenerator2["default"])( /*#__PURE__*/ _regenerator["default"].mark(function _callee() { var report, valid, record, validator, value, _args = arguments; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: report = _args.length > 0 && _args[0] !== undefined ? _args[0] : true; valid = true; record = this.record; if (!record) { _context.next = 11; break; } validator = this.validator; if (validator) { validator.reset(); } else { validator = new _Validator["default"](this); this.validator = validator; } value = record.get(this.name); _context.next = 9; return validator.checkValidity(value); case 9: valid = _context.sent; if (report && !record.validating) { record.reportValidity(valid); } case 11: return _context.abrupt("return", valid); case 12: case "end": return _context.stop(); } } }, _callee, this); })); function checkValidity() { return _checkValidity.apply(this, arguments); } return checkValidity; }() /** * 请求lookup值, 如有缓存值直接获得。 * @param noCache default: undefined * @return Promise<object[]> */ }, { key: "fetchLookup", value: function fetchLookup() { var _this3 = this; var noCache = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var dataSet = this.dataSet; if (dataSet) { var lookup = this.lookup; var lookupCaches = (0, _utils.getIf)(dataSet, 'lookupCaches', function () { return _mobx.observable.map(); }); var oldToken = this.get(LOOKUP_TOKEN); var batch = this.get('lookupBatchAxiosConfig') || (0, _configure.getConfig)('lookupBatchAxiosConfig'); var lookupCode = this.get('lookupCode'); var promise; if (batch && lookupCode && Object.keys((0, _utils4.getLovPara)(this, this.record)).length === 0) { var cachedLookup = lookupCaches.get(lookupCode); if (lookupCode !== oldToken) { this.set(LOOKUP_TOKEN, lookupCode); } if (cachedLookup) { if ((0, _mobx.isArrayLike)(cachedLookup)) { promise = Promise.resolve(cachedLookup); } else { this.pending = true; promise = cachedLookup; } } if (!promise) { this.pending = true; promise = _LookupCodeStore["default"].fetchLookupDataInBatch(lookupCode, batch).then((0, _mobx.action)(function (result) { if (result) { lookupCaches.set(lookupCode, result); } return result; })); lookupCaches.set(lookupCode, promise); } } else { var axiosConfig = _LookupCodeStore["default"].getAxiosConfig(this, noCache); if (axiosConfig.url) { var lookupToken = (0, _utils2.buildURLWithAxiosConfig)(axiosConfig); if (lookupToken !== oldToken) { this.set(LOOKUP_TOKEN, lookupToken); } if (!noCache) { var _cachedLookup = lookupCaches.get(lookupToken); if (_cachedLookup) { if ((0, _mobx.isArrayLike)(_cachedLookup)) { promise = Promise.resolve(_cachedLookup); } else { this.pending = true; promise = _cachedLookup; } } } if (!promise) { this.pending = true; promise = _LookupCodeStore["default"].fetchLookupData(axiosConfig).then((0, _mobx.action)(function (result) { if (result) { lookupCaches.set(lookupToken, result); } return result; })); lookupCaches.set(lookupToken, promise); } } } if (promise) { return promise.then((0, _mobx.action)(function (result) { _this3.pending = false; if (lookup && oldToken !== _this3.get(LOOKUP_TOKEN)) { var value = _this3.getValue(); var valueField = _this3.get('valueField'); if (value && valueField) { var _ref; var values = _this3.get('multiple') ? (_ref = []).concat.apply(_ref, (0, _toConsumableArray2["default"])(value)) : [].concat(value); _this3.set('lookupData', values.reduce(function (lookupData, v) { var found = lookup.find(function (item) { return (0, _isSameLike["default"])(item[valueField], v); }); if (found) { lookupData.push(found); } return lookupData; }, [])); } } return (0, _mobx.toJS)(result); }))["catch"](function (e) { _this3.pending = false; throw e; }); } } return Promise.resolve(undefined); } }, { key: "fetchLovConfig", value: function fetchLovConfig() { var lovCode = this.get('lovCode'); if (lovCode) { _LovCodeStore["default"].fetchConfig(lovCode, this); } } }, { key: "fetchAttachments", value: function fetchAttachments(props) { var _this4 = this; var bucketName = props.bucketName, bucketDirectory = props.bucketDirectory, attachmentUUID = props.attachmentUUID, storageCode = props.storageCode; var _getConfig = (0, _configure.getConfig)('attachment'), fetchList = _getConfig.fetchList; if (fetchList) { fetchList({ bucketName: bucketName, bucketDirectory: bucketDirectory, attachmentUUID: attachmentUUID, storageCode: storageCode }).then((0, _mobx.action)(function (results) { _this4.attachments = results.map(function (file) { return new _AttachmentFile["default"](file); }); })); } } }, { key: "fetchAttachmentCount", value: function fetchAttachmentCount(uuid) { var _this5 = this; var _getConfig2 = (0, _configure.getConfig)('attachment'), batchFetchCount = _getConfig2.batchFetchCount; if (batchFetchCount && !this.attachments) { _AttachmentStore["default"].fetchCountInBatch(uuid).then(function (count) { _this5.attachmentCount = count; }); } } }, { key: "isValid", value: function isValid() { return this.valid; } }, { key: "getValidationMessage", value: function getValidationMessage() { var validator = this.validator; return validator ? validator.validationMessage : undefined; } }, { key: "getValidityState", value: function getValidityState() { var validator = this.validator; return validator ? validator.validity : undefined; } }, { key: "getValidationErrorValues", value: function getValidationErrorValues() { var validator = this.validator; return validator ? validator.validationResults : []; } }, { key: "ready", value: function ready() { return Promise.resolve(true); } }, { key: "findDataSetField", value: function findDataSetField() { var dataSet = this.dataSet, name = this.name, record = this.record; if (record && dataSet && name) { return dataSet.getField(name); } } }, { key: "checkDynamicProp", value: function checkDynamicProp(propsName, newProp) { var _this6 = this; var lastDynamicProps = (0, _utils.getIf)(this, 'lastDynamicProps', {}); var oldProp = lastDynamicProps[propsName]; if (!isEqualDynamicProps(oldProp, newProp)) { (0, _raf["default"])((0, _mobx.action)(function () { var validator = _this6.validator, validatorPropKeys = _this6.validatorPropKeys; if (validator && (propsName === 'validator' || validatorPropKeys && validatorPropKeys.includes(propsName))) { validator.reset(); } _this6.handlePropChange(propsName, newProp, oldProp); })); } lastDynamicProps[propsName] = newProp; } }, { key: "handlePropChange", value: function handlePropChange(propsName, newProp, oldProp) { if (propsName === 'bind' && this.type !== _enum.FieldType.intl) { var record = this.record; if (record && !this.dirty) { if (newProp && oldProp) { record.init(newProp, record.get(oldProp)); } if (oldProp) { record.init(oldProp, undefined); } } return; } if (LOOKUP_SIDE_EFFECT_KEYS.includes(propsName)) { this.set('lookupData', undefined); this.fetchLookup(); } if (LOV_SIDE_EFFECT_KEYS.includes(propsName)) { this.fetchLovConfig(); } } }, { key: "executeDynamicProps", value: function executeDynamicProps(dynamicProps, propsName) { var dataSet = this.dataSet, name = this.name, record = this.record; var dynamicPropsComputingChains = (0, _utils.getIf)(this, 'dynamicPropsComputingChains', []); if (dynamicPropsComputingChains.includes(propsName)) { (0, _warning["default"])(false, "Cycle dynamicProps execution of field<".concat(name, ">. [").concat(dynamicPropsComputingChains.join(' -> '), " -> ").concat(propsName, "]")); } else if (dataSet) { dynamicPropsComputingChains.push(propsName); try { return dynamicProps({ dataSet: dataSet, record: record, name: name }); } catch (e) { if (process.env.NODE_ENV !== 'production') { console.warn(e); } } finally { dynamicPropsComputingChains.pop(); } } } }, { key: "attachments", get: function get() { var dataSet = this.dataSet, record = this.record; if (dataSet && record) { var attachmentCaches = dataSet.attachmentCaches; var uuid = record.get(this.name); if (uuid && attachmentCaches) { var cache = attachmentCaches.get(uuid); if (cache) { return (0, _mobx.get)(cache, 'attachments'); } } } return this.get('attachments'); }, set: function set(attachments) { var _this7 = this; (0, _mobx.runInAction)(function () { var record = _this7.record; if (record) { var uuid = record.get(_this7.name); var dataSet = _this7.dataSet; if (dataSet && uuid) { var attachmentCaches = (0, _utils.getIf)(dataSet, 'attachmentCaches', function () { return _mobx.observable.map(); }); if (attachmentCaches) { var cache = attachmentCaches.get(uuid); if (cache) { (0, _mobx.set)(cache, 'attachments', attachments); } else { attachmentCaches.set(uuid, { attachments: attachments }); } return; } } } _this7.set('attachments', attachments); }); } }, { key: "attachmentCount", get: function get() { var attachments = this.attachments; if (attachments) { return attachments.length; } var dataSet = this.dataSet, record = this.record; if (dataSet && record) { var attachmentCaches = dataSet.attachmentCaches; var uuid = record.get(this.name); if (uuid && attachmentCaches) { var cache = attachmentCaches.get(uuid); if (cache) { return (0, _mobx.get)(cache, 'count'); } } } return this.get('attachmentCount'); }, set: function set(count) { var _this8 = this; (0, _mobx.runInAction)(function () { var record = _this8.record; if (record) { var uuid = record.get(_this8.name); var dataSet = _this8.dataSet; if (dataSet && uuid) { var attachmentCaches = (0, _utils.getIf)(dataSet, 'attachmentCaches', function () { return _mobx.observable.map(); }); if (attachmentCaches) { var cache = attachmentCaches.get(uuid); if (cache) { (0, _mobx.set)(cache, 'count', count); } else { attachmentCaches.set(uuid, { count: count }); } return; } } } _this8.set('attachmentCount', count); }); } }, { key: "pristineProps", get: function get() { return (0, _objectSpread2["default"])({}, this.props.toPOJO(), {}, this.dirtyProps); }, set: function set(props) { var _this9 = this; (0, _mobx.runInAction)(function () { var dirtyProps = _this9.dirtyProps; if (dirtyProps) { var newProps = {}; var dirtyKeys = Object.keys(dirtyProps); if (dirtyKeys.length) { dirtyKeys.forEach(function (key) { var item = _this9.props.get(key); newProps[key] = item; if ((0, _isSame["default"])(item, props[key])) { delete dirtyProps[key]; } else { dirtyProps[key] = props[key]; } }); } _this9.props.replace((0, _objectSpread2["default"])({}, props, {}, newProps)); } else { _this9.props.replace(props); } }); } }, { key: "lookup", get: function get() { var dataSet = this.dataSet; if (dataSet) { var lookupToken = this.get(LOOKUP_TOKEN); var lookupCaches = dataSet.lookupCaches; if (lookupToken && lookupCaches) { var lookup = lookupCaches.get(lookupToken); if ((0, _mobx.isArrayLike)(lookup)) { var valueField = this.get('valueField'); var lookupData = this.get('lookupData'); if (lookupData) { var others = lookupData.filter(function (data) { return lookup.every(function (item) { return item[valueField] !== data[valueField]; }); }); if (others.length) { return others.concat(lookup.slice()); } } return lookup; } } } return undefined; } }, { key: "options", get: function get() { var options = this.get('options'); if (options) { return options; } // 确保 lookup 相关配置介入观察 _LookupCodeStore["default"].getAxiosConfig(this); var optionsProps = this.get('optionsProps'); var lookup = this.lookup, type = this.type; if (lookup) { var parentField = this.get('parentField'); var idField = this.get('idField') || this.get('valueField'); var selection = this.get('multiple') ? _enum.DataSetSelection.multiple : _enum.DataSetSelection.single; return new _DataSet["default"]((0, _objectSpread2["default"])({ data: lookup, paging: false, selection: selection, idField: idField, parentField: parentField }, optionsProps)); } var lovCode = this.get('lovCode'); if (lovCode) { if (type === _enum.FieldType.object || type === _enum.FieldType.auto) { return _LovCodeStore["default"].getLovDataSet(lovCode, this, optionsProps); } } return undefined; } }, { key: "dirty", get: function get() { var record = this.record, name = this.name; if (record) { var dirtyData = record.dirtyData; if (dirtyData) { var dirtyNames = (0, _toConsumableArray2["default"])(dirtyData.keys()); if (dirtyNames.length) { if (dirtyNames.includes((0, _utils.getChainFieldName)(record, name))) { return true; } if (this.type === _enum.FieldType.intl) { var tlsKey = (0, _configure.getConfig)('tlsKey'); if (record.get(tlsKey) && Object.keys(_localeContext["default"].supports).some(function (lang) { return dirtyNames.includes((0, _utils.getChainFieldName)(record, "".concat(tlsKey, ".").concat(name, ".").concat(lang))); })) { return true; } } } } } return false; } }, { key: "name", get: function get() { return this.props.get('name'); } }, { key: "order", get: function get() { return this.get('order'); }, set: function set(order) { this.set('order', order); } }, { key: "valid", get: function get() { var validator = this.validator; return validator ? validator.validity.valid : true; } }, { key: "validationMessage", get: function get() { return this.getValidationMessage(); } }, { key: "required", get: function get() { return this.get('required'); } /** * 设置是否必选 * @param required 是否必选 */ , set: function set(required) { this.set('required', required); } /** * 是否只读 * @return true | false */ }, { key: "readOnly", get: function get() { return this.get('readOnly'); } /** * 是否禁用 * @return true | false */ , /** * 设置是否只读 * @param readOnly 是否只读 */ set: function set(readOnly) { this.set('readOnly', readOnly); } /** * 设置是否禁用 * @param disabled 是否禁用 */ }, { key: "disabled", get: function get() { return this.get('disabled'); }, set: function set(disabled) { this.set('disabled', disabled); } /** * 获取字段类型 * @return 获取字段类型 */ }, { key: "type", get: function get() { return this.get('type'); } /** * 设置字段类型 * @param type 字段类型 */ , set: function set(type) { this.set('type', type); } }]); return Field; }(); exports["default"] = Field; Field.defaultProps = { type: _enum.FieldType.auto, required: false, readOnly: false, disabled: false, group: false, textField: _utils.defaultTextField, valueField: _utils.defaultValueField, trueValue: true, falseValue: false, trim: _enum.FieldTrim.both }; (0, _tslib.__decorate)([_mobx.observable], Field.prototype, "validator", void 0); (0, _tslib.__decorate)([_mobx.observable], Field.prototype, "pending", void 0); (0, _tslib.__decorate)([_mobx.observable], Field.prototype, "props", void 0); (0, _tslib.__decorate)([_mobx.observable], Field.prototype, "dirtyProps", void 0); (0, _tslib.__decorate)([_mobx.computed], Field.prototype, "options", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "set", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "reset", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "commit", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "setLovPara", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "checkValidity", null); (0, _tslib.__decorate)([_mobx.action], Field.prototype, "fetchLookup", null); //# sourceMappingURL=Field.js.map