drip-table
Version:
A tiny and powerful enterprise-class solution for building tables.
1,107 lines (1,028 loc) • 32.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validateDripTableRequiredProps = exports.validateDripTableProp = exports.validateDripTableColumnSchema = exports.getDripTableValidatePropsKeys = exports.DRIP_TABLE_GENERIC_CSS_SCHEMA = exports.DRIP_TABLE_CSS_SCHEMA = void 0;
var _ajv = _interopRequireDefault(require("ajv"));
var _ajvKeywords = _interopRequireDefault(require("ajv-keywords"));
var _cache = _interopRequireDefault(require("./cache"));
var _json = require("./json");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 _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 _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* 递归设置 Schema 用户自定属性并返回设置后的 Schema 对象
* @param schema Schema 对象
* @param options 用户自定属性设置项
* @returns 新的 Schema 对象
*/
var finalizeSchema = function finalizeSchema(schema, options) {
if (schema.type === 'object') {
var _options$additionalPr;
schema = _objectSpread(_objectSpread({}, schema), {}, {
additionalProperties: (_options$additionalPr = options === null || options === void 0 ? void 0 : options.additionalProperties) !== null && _options$additionalPr !== void 0 ? _options$additionalPr : false
});
}
if (schema.properties) {
schema = _objectSpread(_objectSpread({}, schema), {}, {
properties: Object.fromEntries(Object.entries(schema.properties).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
k = _ref2[0],
p = _ref2[1];
return [k, finalizeSchema(p, options)];
}))
});
}
if (schema.patternProperties) {
schema = _objectSpread(_objectSpread({}, schema), {}, {
patternProperties: Object.fromEntries(Object.entries(schema.patternProperties).map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
k = _ref4[0],
p = _ref4[1];
return [k, finalizeSchema(p, options)];
}))
});
}
if (schema.oneOf) {
schema = _objectSpread(_objectSpread({}, schema), {}, {
oneOf: schema.oneOf.map(function (s) {
return finalizeSchema(s, options);
})
});
}
if (schema.anyOf) {
schema = _objectSpread(_objectSpread({}, schema), {}, {
anyOf: schema.anyOf.map(function (s) {
return finalizeSchema(s, options);
})
});
}
return schema;
};
var DRIP_TABLE_CSS_SCHEMA = {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
};
exports.DRIP_TABLE_CSS_SCHEMA = DRIP_TABLE_CSS_SCHEMA;
var DRIP_TABLE_GENERIC_CSS_SCHEMA = {
anyOf: [{
type: 'string'
}, DRIP_TABLE_CSS_SCHEMA]
};
exports.DRIP_TABLE_GENERIC_CSS_SCHEMA = DRIP_TABLE_GENERIC_CSS_SCHEMA;
var DRIP_TABLE_GENERIC_RENDER_ELEMENT_SCHEMA = {
type: 'array',
items: {
type: 'object',
properties: {
type: {
type: 'string'
}
},
required: ['type'],
discriminator: {
propertyName: 'type'
},
oneOf: [{
type: 'object',
properties: {
type: {
const: 'spacer'
}
}
}, {
type: 'object',
properties: {
type: {
const: 'text'
},
text: {
type: 'string'
}
},
required: ['text']
}, {
type: 'object',
properties: {
type: {
const: 'html'
},
html: {
type: 'string'
}
},
required: ['html']
}, {
type: 'object',
properties: {
type: {
const: 'search'
},
wrapperClassName: {
type: 'string'
},
wrapperStyle: DRIP_TABLE_CSS_SCHEMA,
placeholder: {
type: 'string'
},
allowClear: {
type: 'boolean'
},
searchButtonText: {
type: 'string'
},
searchButtonSize: {
enum: ['large', 'middle', 'small']
},
searchKeys: {
type: 'array',
items: {
type: 'object',
properties: {
label: {
type: 'string'
},
value: {
typeof: ['number', 'string']
}
},
required: ['label', 'value']
}
},
searchKeyDefaultValue: {
typeof: ['number', 'string']
}
}
}, {
type: 'object',
properties: {
type: {
const: 'slot'
},
slot: {
type: 'string'
},
props: {
typeof: 'object'
}
},
required: ['slot']
}, {
type: 'object',
properties: {
type: {
const: 'insert-button'
},
insertButtonClassName: {
type: 'string'
},
insertButtonStyle: DRIP_TABLE_CSS_SCHEMA,
insertButtonText: {
type: 'string'
},
showIcon: {
type: 'boolean'
}
}
}, {
type: 'object',
properties: {
type: {
const: 'display-column-selector'
},
selectorButtonText: {
type: 'string'
},
selectorButtonType: {
enum: ['ghost', 'primary', 'dashed', 'link', 'text', 'default']
}
}
}, {
type: 'object',
properties: {
type: {
const: 'layout-selector'
},
selectorButtonText: {
type: 'string'
},
selectorButtonType: {
enum: ['ghost', 'primary', 'dashed', 'link', 'text', 'default']
}
}
}]
}
};
var DRIP_TABLE_GENERIC_RENDER_SCHEMA = {
type: 'object',
properties: {
style: DRIP_TABLE_CSS_SCHEMA,
elements: DRIP_TABLE_GENERIC_RENDER_ELEMENT_SCHEMA
}
};
var AJV_CACHE;
var createAjv = function createAjv() {
if (!AJV_CACHE) {
AJV_CACHE = (0, _ajvKeywords.default)(new _ajv.default({
strict: true,
discriminator: true,
allowUnionTypes: true
}));
}
return AJV_CACHE;
};
var getAjvErrorMessage = function getAjvErrorMessage(ajv) {
var _ref5 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref5$separator = _ref5.separator,
separator = _ref5$separator === void 0 ? ', ' : _ref5$separator,
_ref5$dataVar = _ref5.dataVar,
dataVar = _ref5$dataVar === void 0 ? 'data' : _ref5$dataVar;
if (!ajv.errors || ajv.errors.length === 0) {
return '';
}
return ajv.errors.map(function (e) {
var params = e.params && _typeof(e.params) === 'object' ? Object.entries(e.params).map(function (_ref6) {
var _ref7 = _slicedToArray(_ref6, 2),
k = _ref7[0],
v = _ref7[1];
return "".concat(k, ": ").concat(JSON.stringify(v));
}).join(', ') : String(e.params);
return "".concat(dataVar).concat(e.instancePath, " ").concat(e.message).concat(params ? ", ".concat(params) : '');
}).reduce(function (text, msg) {
return "".concat(text).concat(separator).concat(msg);
});
};
/**
* Ajv Schema 缓存对象
*/
var DRIP_TABLE_PROPS_AJV_SCHEMA_CACHE = new Map();
var getDripTablePropsAjvSchema = function getDripTablePropsAjvSchema(options) {
var _options$additionalPr2;
var additionalProperties = (_options$additionalPr2 = options === null || options === void 0 ? void 0 : options.additionalProperties) !== null && _options$additionalPr2 !== void 0 ? _options$additionalPr2 : false;
var key = "CK-".concat(additionalProperties ? 1 : 0);
if (!DRIP_TABLE_PROPS_AJV_SCHEMA_CACHE.has(key)) {
var dripTableSchema = {
type: 'object',
properties: {
id: {
typeof: ['number', 'string']
},
theme: {
type: 'object',
properties: {
primaryColor: {
type: 'string'
},
primaryActiveColor: {
type: 'string'
},
primaryShadowColor: {
type: 'string'
},
textColor: {
type: 'string'
},
borderColor: {
type: 'string'
},
backgroundColor: {
type: 'string'
},
columnHeaderHoverBackgroundColor: {
type: 'string'
},
columnHeaderSortedBackgroundColor: {
type: 'string'
},
columnSortedBackgroundColor: {
type: 'string'
}
}
},
className: {
type: 'string'
},
style: DRIP_TABLE_CSS_SCHEMA,
innerClassName: {
type: 'string'
},
innerStyle: DRIP_TABLE_CSS_SCHEMA,
bordered: {
type: 'boolean'
},
borderRadius: {
type: 'string'
},
showHeader: {
type: 'boolean'
},
header: {
anyOf: [{
type: 'boolean'
}, DRIP_TABLE_GENERIC_RENDER_SCHEMA]
},
footer: DRIP_TABLE_GENERIC_RENDER_SCHEMA,
pagination: {
anyOf: [{
type: 'boolean'
}, {
type: 'object',
properties: {
sticky: {
type: 'boolean'
},
border: {
type: 'boolean'
},
simple: {
type: 'boolean'
},
size: {
enum: ['small', 'default']
},
pageSize: {
type: 'number'
},
position: {
enum: ['topLeft', 'topCenter', 'topRight', 'bottomLeft', 'bottomCenter', 'bottomRight']
},
showTotal: {
type: 'string'
},
showLessItems: {
type: 'boolean'
},
showQuickJumper: {
type: 'boolean'
},
showSizeChanger: {
type: 'boolean'
},
pageSizeOptions: {
anyOf: [{
type: 'array',
items: {
type: 'number'
}
}, {
type: 'array',
items: {
type: 'string'
}
}]
},
hideOnSinglePage: {
type: 'boolean'
},
style: DRIP_TABLE_GENERIC_CSS_SCHEMA,
pageNumberStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA,
pageStepperStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA,
pageSelectorStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA
}
}]
},
size: {
enum: ['small', 'middle', 'large', 'default']
},
sticky: {
type: 'boolean'
},
scroll: {
type: 'object',
properties: {
x: {
typeof: ['number', 'boolean', 'string']
},
y: {
typeof: ['number', 'string']
},
scrollToFirstRowOnChange: {
type: 'boolean'
}
}
},
rowSelection: {
anyOf: [{
type: 'boolean'
}, {
type: 'object',
properties: {
align: {
enum: ['left', 'center', 'right']
},
verticalAlign: {
enum: ['top', 'middle', 'bottom', 'stretch']
}
}
}]
},
rowDraggable: {
type: 'boolean'
},
defaultTableLayout: {
enum: ['card', 'table']
},
layout: {
type: 'object',
properties: {
card: {
type: 'object',
properties: {
columns: {
type: 'array',
items: {}
},
rowSize: {
type: 'number'
}
}
},
calendar: {
type: 'object',
properties: {
columns: {
type: 'array',
items: {}
}
}
}
}
},
editable: {
type: 'boolean'
},
ellipsis: {
type: 'boolean'
},
tableLayout: {
enum: ['auto', 'fixed']
},
stripe: {
type: 'boolean'
},
virtual: {
type: 'boolean'
},
rowHeight: {
type: 'number'
},
scrollY: {
type: 'number'
},
columns: {
type: 'array',
items: {}
},
rowKey: {
type: 'string'
},
rowSlotKey: {
type: 'string'
},
rowHeader: DRIP_TABLE_GENERIC_RENDER_SCHEMA,
rowFooter: DRIP_TABLE_GENERIC_RENDER_SCHEMA,
span: {
oneOf: [{
type: 'string'
}, {
type: 'array',
items: {
type: 'array',
items: {
type: 'number'
},
minItems: 4,
maxItems: 4
}
}, {
type: 'object',
properties: {
rectangles: {
type: 'array',
items: {
type: 'array',
items: {
type: 'number'
},
minItems: 4,
maxItems: 4
}
},
generator: {
type: 'string'
}
}
}]
},
emptyText: {
type: 'string'
},
initialSorter: {
type: 'object',
properties: {
key: {
type: 'string'
},
direction: {
enum: ['ascend', 'descend']
}
}
},
subtable: {},
// (不校验子表,因为 ajv 不支持循环引用)
ext: {}
},
required: ['columns']
};
var dripTableSubtableSchema = _objectSpread(_objectSpread({}, dripTableSchema), {}, {
properties: _objectSpread(_objectSpread({}, dripTableSchema.properties || []), {}, {
dataSourceKey: {
typeof: ['number', 'string']
}
}),
required: [].concat(_toConsumableArray(dripTableSchema.required || []), ['dataSourceKey'])
});
var subtablePropsSchema = {
type: 'object',
properties: {
defaultExpandAllRows: {
type: 'boolean'
},
defaultExpandedRowKeys: {
type: 'array',
items: {
type: ['number', 'string']
}
}
},
required: []
};
var propsSchema = {
type: 'object',
properties: _objectSpread(_objectSpread({
schema: dripTableSchema,
dataSource: {
type: 'array',
items: {}
},
className: {
type: 'string'
},
spinClassName: {
type: 'string'
},
spinInnerClassName: {
type: 'string'
},
style: DRIP_TABLE_CSS_SCHEMA,
selectedRowKeys: {},
displayColumnKeys: {},
total: {
type: 'number'
},
currentPage: {
type: 'number'
},
loading: {
type: 'boolean'
}
}, subtablePropsSchema.properties), {}, {
components: {},
defaultComponentLib: {
type: 'string'
},
slots: {},
icons: {},
ajv: {
anyOf: [{
type: 'object',
properties: {
additionalProperties: {
type: 'boolean'
}
}
}, {
type: 'boolean'
}]
},
ext: {},
sticky: {
type: 'object',
properties: {
offsetHeader: {
type: 'number'
},
offsetScroll: {
type: 'number'
},
getContainer: {
instanceof: 'Function'
}
}
},
subtableProps: {
type: 'array',
items: {
type: 'object',
properties: {
subtableID: {
type: ['number', 'string']
},
recordKeys: {
type: 'array',
items: {}
},
default: {
type: 'boolean'
},
properties: subtablePropsSchema
},
required: ['properties']
}
},
title: {
instanceof: 'Function'
},
footer: {
instanceof: 'Function'
},
emptyText: {
instanceof: 'Function'
},
subtableTitle: {
instanceof: 'Function'
},
subtableFooter: {
instanceof: 'Function'
},
rowExpandable: {
instanceof: 'Function'
},
expandedRowRender: {
instanceof: 'Function'
},
rowHeaderVisible: {
instanceof: 'Function'
},
rowFooterVisible: {
instanceof: 'Function'
},
rowSelectable: {
instanceof: 'Function'
},
schemaFunctionPreprocessor: {
instanceof: 'Function'
},
componentDidMount: {
instanceof: 'Function'
},
componentDidUpdate: {
instanceof: 'Function'
},
componentWillUnmount: {
instanceof: 'Function'
},
onRowClick: {
instanceof: 'Function'
},
onRowDoubleClick: {
instanceof: 'Function'
},
onRowExpand: {
instanceof: 'Function'
},
onRowCollapse: {
instanceof: 'Function'
},
onSelectionChange: {
instanceof: 'Function'
},
onSearch: {
instanceof: 'Function'
},
onInsertButtonClick: {
instanceof: 'Function'
},
onPageChange: {
instanceof: 'Function'
},
onPaginationChange: {
instanceof: 'Function'
},
onSorterChange: {
instanceof: 'Function'
},
onFilterChange: {
instanceof: 'Function'
},
onChange: {
instanceof: 'Function'
},
onDataSourceChange: {
instanceof: 'Function'
},
onDisplayColumnKeysChange: {
instanceof: 'Function'
},
onEvent: {
instanceof: 'Function'
},
renderSelection: {},
renderPagination: {},
renderHeaderCellFilter: {},
createEvaluator: {},
__PARENT_INFO__: {}
}),
required: ['schema', 'dataSource'].concat(_toConsumableArray(subtablePropsSchema.required))
};
DRIP_TABLE_PROPS_AJV_SCHEMA_CACHE.set(key, {
props: finalizeSchema(propsSchema, options),
subtable: finalizeSchema(dripTableSubtableSchema, options)
});
}
var schemas = DRIP_TABLE_PROPS_AJV_SCHEMA_CACHE.get(key);
if (!schemas) {
throw new Error('Get drip-table props ajv schema failed.');
}
return schemas;
};
var validateDripTableRequiredProps = function validateDripTableRequiredProps(props, options) {
if (props && _typeof(props) === 'object') {
var schemas = getDripTablePropsAjvSchema(options);
var _iterator = _createForOfIteratorHelper(schemas.props.required),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var key = _step.value;
if (props[key] === void 0) {
return "props must have required property '".concat(key, "', missingProperty: \"").concat(key, "\"");
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return null;
};
/**
* 根据用户传入的表格属性获取需要校验的属性名称列表,输出固定长度的结果提供给 React Hooks 使用
* @param props DripTable 用户传入属性 props 对象
* @param options ajv 校验选项
* @returns 需要校验的属性名称列表
*/
exports.validateDripTableRequiredProps = validateDripTableRequiredProps;
var getDripTableValidatePropsKeys = function getDripTableValidatePropsKeys(props, options) {
var schemas = getDripTablePropsAjvSchema(options);
var propertiesKeys = Object.keys(schemas.props.properties);
var propertiesCount = propertiesKeys.length;
if (options !== null && options !== void 0 && options.additionalProperties) {
return propertiesKeys;
}
return _toConsumableArray(new Set([].concat(_toConsumableArray(Object.keys(props)), propertiesKeys))).filter(function (_, i) {
return i < propertiesCount;
});
};
exports.getDripTableValidatePropsKeys = getDripTableValidatePropsKeys;
var DRIP_TABLE_AJV_PROPS_CACHE = new _cache.default();
var DRIP_TABLE_AJV_PROPS_KEY_CACHE = new _cache.default();
/**
* 校验 DripTable Props 是否符合 Schema 要求
* @param key DripTable Props key
* @param value DripTable Props value
* @param options ajv 校验选项
* @returns 校验失败提示内容,成功返回 null
*/
var validateDripTableProp = function validateDripTableProp(key, value, options) {
// 检查缓存
if (DRIP_TABLE_AJV_PROPS_CACHE.has(key, value, options)) {
return DRIP_TABLE_AJV_PROPS_CACHE.get(key, value, options) || null;
}
var valueKey = (0, _json.encodeJSON)(value);
var optionsKey = (0, _json.encodeJSON)(options);
if (valueKey && optionsKey && DRIP_TABLE_AJV_PROPS_KEY_CACHE.has(key, valueKey, optionsKey)) {
return DRIP_TABLE_AJV_PROPS_KEY_CACHE.get(key, valueKey, optionsKey) || null;
} // 缓存未命中
var ajv = createAjv();
var schemas = getDripTablePropsAjvSchema(options); // 校验指定的 Props Schema
if (value === void 0) {
if (schemas.props.required.includes(key)) {
return "props must have required property '".concat(key, "', missingProperty: \"").concat(key, "\"");
}
return null;
}
var schema = schemas.props.properties[key];
if (!schema && !(options !== null && options !== void 0 && options.additionalProperties)) {
return "props must NOT have additional properties, additionalProperty: \"".concat(key, "\"");
}
var res = null;
if (schema && !ajv.validate(schema, value)) {
res = getAjvErrorMessage(ajv, {
dataVar: "props.".concat(key),
separator: '; '
});
} else if (key === 'schema') {
// 递归校验子表 Schema
var subtable = value.subtable;
var prefix = 'props.schema.subtable';
while (subtable) {
if (!ajv.validate(schemas.subtable, subtable)) {
res = getAjvErrorMessage(ajv, {
dataVar: prefix,
separator: '; '
});
break;
}
subtable = subtable.subtable;
prefix += '.subtable';
}
}
DRIP_TABLE_AJV_PROPS_CACHE.set(key, value, options, res);
DRIP_TABLE_AJV_PROPS_KEY_CACHE.set(key, valueKey, optionsKey, res);
return res;
};
exports.validateDripTableProp = validateDripTableProp;
var DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE = new _cache.default();
/**
* 校验 DripTableColumnSchema 是否符合 Schema 要求
* @param data DripTableColumnSchema 数据
* @param schema DripTableColumnSchema 的 Schema 对象
* @param options ajv 校验选项
* @returns 校验失败提示内容,成功返回 null
*/
var validateDripTableColumnSchema = function validateDripTableColumnSchema(data, schema, options) {
// 检查缓存
if (DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.has(data, schema, options)) {
return DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.get(data, schema, options) || null;
}
var dataKey = (0, _json.encodeJSON)(data);
var schemaKey = (0, _json.encodeJSON)(schema);
var optionsKey = (0, _json.encodeJSON)(options);
if (dataKey && schemaKey && optionsKey && DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.has(dataKey, schemaKey, optionsKey)) {
return DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.get(dataKey, schemaKey, optionsKey) || null;
} // 缓存未命中
var ajv = createAjv();
var dripTableColumnSchema = finalizeSchema({
type: 'object',
properties: {
component: {
type: 'string'
},
options: schema || {},
key: {
type: 'string'
},
title: {
anyOf: [{
type: 'string'
}, {
type: 'object',
properties: {
style: DRIP_TABLE_CSS_SCHEMA,
body: {
anyOf: [{
type: 'string'
}, {
type: 'object',
properties: {
style: DRIP_TABLE_CSS_SCHEMA,
content: {
type: 'string'
}
}
}]
},
header: DRIP_TABLE_GENERIC_RENDER_SCHEMA,
footer: DRIP_TABLE_GENERIC_RENDER_SCHEMA
},
required: ['body']
}]
},
style: DRIP_TABLE_GENERIC_CSS_SCHEMA,
hoverStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA,
rowHoverStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA,
columnHoverStyle: DRIP_TABLE_GENERIC_CSS_SCHEMA,
width: {
typeof: ['string', 'number']
},
align: {
enum: ['left', 'center', 'right']
},
verticalAlign: {
enum: ['top', 'middle', 'bottom', 'stretch']
},
dataIndex: {
anyOf: [{
type: 'array',
items: {
type: 'string'
}
}, {
type: 'string'
}]
},
dataTranslation: {
type: 'string'
},
defaultValue: {},
description: {
type: 'string'
},
clipboard: {
type: 'boolean'
},
fixed: {
anyOf: [{
enum: ['left', 'right']
}, {
type: 'boolean'
}]
},
hidden: {
anyOf: [{
type: 'string'
}, {
type: 'boolean'
}]
},
disable: {
anyOf: [{
type: 'string'
}, {
type: 'boolean'
}]
},
editable: {
anyOf: [{
type: 'string'
}, {
type: 'boolean'
}]
},
hidable: {
type: 'boolean'
},
sorter: {
type: 'string'
},
sortDirections: {
type: 'array',
items: {
enum: ['ascend', 'descend']
}
},
filters: {
type: 'array',
items: {
type: 'object',
properties: {
text: {},
value: {
typeof: ['string', 'number', 'boolean']
}
},
required: ['text', 'value']
}
},
filtersMaxSelect: {
type: 'number'
},
defaultFilteredValue: {
typeof: ['string', 'number', 'object']
}
},
required: ['component', schema ? 'options' : '', 'key', 'title', 'dataIndex'].map(function (_) {
return _;
})
}, options);
var res = ajv.validate(dripTableColumnSchema, data) ? null : getAjvErrorMessage(ajv, {
dataVar: 'column',
separator: '; '
});
DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.set(data, schema, options, res);
DRIP_TABLE_AJV_COLUMN_SCHEMA_RESULT_CACHE.set(dataKey, schemaKey, optionsKey, res);
return res;
};
exports.validateDripTableColumnSchema = validateDripTableColumnSchema;