@lvxiaowu/antd4
Version:
antd4-components
312 lines (249 loc) • 11.4 kB
JavaScript
var _excluded = ["pageNum", "pageSize"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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; }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import { runInAction, makeObservable, observable, action } from 'mobx';
import { getEditorValues, getEditorFieldName } from './Context';
import { overrideStore, TABLE_STORE, resetStore } from '../utils';
import { DataType, Utils } from '@lvxiaowu/utils';
import SearchStore from '../SearchBar/store';
var TableStore = /*#__PURE__*/_createClass(function TableStore(_overrides) {
var _this = this;
_classCallCheck(this, TableStore);
this.$storeName = TABLE_STORE;
this.$searchBarStore = new SearchStore({
onSearch: function onSearch(params) {
_this.search(_objectSpread({
pageNum: 1
}, params));
}
});
this.getSearchBarStore = function () {
return _this.$searchBarStore;
};
this.getFormInstance = function () {
return _this.$searchBarStore.form;
};
this.getFinalParams = function (overrides) {
var searchParams = _this.$searchBarStore.getParams();
var finalParams;
if (DataType.isPlainObj(_this.pagination)) {
var _this$pagination = _this.pagination,
current = _this$pagination.current,
pageSize = _this$pagination.pageSize;
finalParams = _objectSpread(_objectSpread({}, searchParams), {}, {
pageNum: current,
pageSize: pageSize
}, overrides);
} else {
finalParams = _objectSpread(_objectSpread({}, searchParams), overrides);
delete finalParams.pageNum;
delete finalParams.pageSize;
}
if (_this.optimizeParams) {
finalParams = _this.optimizeParams(finalParams);
}
return Utils.omitValues(finalParams);
};
this.optimizeParams = void 0;
this.getParams = function () {
return _this.$searchBarStore.getParams();
};
this.setParams = function (params) {
var shouldSetFormValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
_this.$searchBarStore.setParams(params, shouldSetFormValues);
};
this.filters = {};
this.sorter = {};
this.pagination = {
showQuickJumper: true,
showTotal: function showTotal(total) {
return "\u5171 ".concat(total, " \u6761");
},
showSizeChanger: true,
current: 1,
pageSize: 10,
total: 0
};
this.selectedRows = [];
this.list = [];
this.loading = false;
this.selectedRowKeys = [];
this.search = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params, config) {
var _ref2, pageNum, pageSize, other, finalParams, filters, sorter, defaultData, data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_ref2 = DataType.isNum(params) ? {
pageNum: params
} : params || {}, pageNum = _ref2.pageNum, pageSize = _ref2.pageSize, other = _objectWithoutProperties(_ref2, _excluded);
if (!DataType.isNum(pageNum)) {
pageNum = _this.getPagination('current');
}
if (!DataType.isNum(pageSize)) {
pageSize = _this.getPagination('pageSize');
}
finalParams = _this.getFinalParams(_objectSpread(_objectSpread({}, other), {}, {
pageNum: pageNum,
pageSize: pageSize
})); // 其他参数:过滤,排序,选择的行数据和行keys
filters = _this.filters, sorter = _this.sorter;
defaultData = {
list: [],
total: 0
};
_context.prev = 6;
_this.loading = true;
_context.next = 10;
return _this.fetchList(finalParams, {
filters: filters,
sorter: sorter
});
case 10:
data = _context.sent;
// 异步函数中批量更新机制失效需要加runInAction
runInAction(function () {
_this.afterSearch(data || defaultData, finalParams, config);
});
_context.next = 18;
break;
case 14:
_context.prev = 14;
_context.t0 = _context["catch"](6);
console.error(_context.t0);
runInAction(function () {
_this.afterSearch(defaultData, finalParams, config);
});
case 18:
case "end":
return _context.stop();
}
}
}, _callee, null, [[6, 14]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
this.afterSearch = function (data, params, config) {
var pageNum = params.pageNum,
pageSize = params.pageSize; // 是否清空已选择项
var _ref3 = config || {},
_ref3$clearSelected = _ref3.clearSelected,
clearSelected = _ref3$clearSelected === void 0 ? true : _ref3$clearSelected;
var list = data.list,
total = data.total; // 是否进行服务端分页
var serverPaging = DataType.isNum(total);
if (!serverPaging) {
total = list.length;
}
if (clearSelected) {
_this.clearSelected();
}
_this.setPagination({
total: total,
current: pageNum,
pageSize: pageSize,
serverPaging: serverPaging
});
_this.list = list || [];
_this.loading = false;
};
this.fetchList = function () {
/*
需要返回list和total字段,如果total字段不返回,
则表示不进行服务端分页,之后改变分页不会再发送请求了,直接前端自动处理分页
*/
return {
list: [],
total: 0
};
};
this.setSelected = function (keys, selectedRows) {
_this.selectedRowKeys = keys;
_this.selectedRows = selectedRows;
};
this.getSelected = function () {
return {
rows: _this.selectedRows,
keys: _this.selectedRowKeys
};
};
this.clearSelected = function () {
_this.selectedRowKeys = [];
_this.selectedRows = [];
};
this.paging = function (_ref4, filters, sorter) {
var current = _ref4.current,
pageSize = _ref4.pageSize;
_this.filters = filters;
_this.sorter = sorter;
if (_this.getPagination('serverPaging')) {
_this.search({
pageNum: current,
pageSize: pageSize
});
} else {
// 不进行服务端分页的情况,就直接设置分页器,使表格自动分页,无需请求服务
_this.setPagination({
current: current,
pageSize: pageSize
});
}
};
this.setPagination = function (info) {
if (DataType.isPlainObj(_this.pagination)) {
_this.pagination = _objectSpread(_objectSpread({}, _this.pagination), info);
}
};
this.getPagination = function (key) {
if (DataType.isPlainObj(_this.pagination)) {
return key ? _this.pagination[key] : _this.pagination;
}
return key ? undefined : _this.pagination;
};
this.editor = {};
this.setEditor = function (editor) {
_this.editor = editor;
};
this.saveEditor = function () {
var _this$editor;
(_this$editor = _this.editor) === null || _this$editor === void 0 ? void 0 : _this$editor.submit();
};
this.getEditorValues = function (rowKeyData, columnKey) {
return getEditorValues(_this.editor, rowKeyData, columnKey);
};
this.getEditorFieldName = function (rowKeyData, columnKey) {
return getEditorFieldName(rowKeyData, columnKey);
};
this.dragSortList = function (list) {
_this.list = list;
};
this.resetStore = function () {
resetStore(_this, TableStore);
};
overrideStore(this, _overrides);
makeObservable(this, {
list: observable,
loading: observable,
selectedRowKeys: observable,
pagination: observable,
search: action,
fetchList: action,
afterSearch: action,
setSelected: action,
clearSelected: action,
paging: action,
dragSortList: action
});
});
export { TableStore as default };