UNPKG

saas-dynamic-component

Version:

saas-dynamic-component ==== saas平台动态引擎组件库,目前包括查询引擎、表格引擎,后续待增加...

412 lines (346 loc) 15.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _dec, _dec2, _class, _desc, _value, _class2, _descriptor, _descriptor2, _descriptor3; var _templateObject = _taggedTemplateLiteral(['\n <ant-Input defaultValue=', ' onChange=', ' />\n '], ['\n <ant-Input defaultValue=', ' onChange=', ' />\n ']), _templateObject2 = _taggedTemplateLiteral(['\n <ant-Select class="edit-select" defaultValue=', ' onChange=', '>\n <#each {option}>\n <ant-Option value={value} key={@index}>{name}</ant-Option>\n </#each>\n </ant-Select>\n '], ['\n <ant-Select class="edit-select" defaultValue=', ' onChange=', '>\n <#each {option}>\n <ant-Option value={value} key={@index}>{name}</ant-Option>\n </#each>\n </ant-Select>\n ']); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _mobx = require('mobx'); var _mobxReact = require('mobx-react'); var _nornj = require('nornj'); var _nornj2 = _interopRequireDefault(_nornj); var _nornjReact = require('nornj-react'); var _coreDecorators = require('core-decorators'); var _tableEngine = require('./tableEngine.scss'); var _tableEngine2 = _interopRequireDefault(_tableEngine); var _tableEngineT = require('./tableEngine.t.html'); var _tableEngineT2 = _interopRequireDefault(_tableEngineT); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } function _initDefineProp(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.'); } var TableEngine = (_dec = (0, _nornjReact.registerTmpl)('TableEngine'), _dec2 = (0, _mobxReact.inject)('store'), _dec(_class = _dec2(_class = (0, _mobxReact.observer)(_class = (_class2 = function (_Component) { _inherits(TableEngine, _Component); function TableEngine() { var _temp, _this, _ret; _classCallCheck(this, TableEngine); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _initDefineProp(_this, 'resultData', _descriptor, _this), _initDefineProp(_this, 'groupExpand', _descriptor2, _this), _initDefineProp(_this, 'allColumns', _descriptor3, _this), _temp), _possibleConstructorReturn(_this, _ret); } TableEngine.prototype.componentDidMount = function componentDidMount() { var _props = this.props, tableEngine = _props.store.tableEngine, tableAction = _props.tableAction, appCode = _props.appCode, menuUrl = _props.menuUrl; this.props.onRef(this); this._refreshParams(); this.reloadTableResult(); }; // 获取表格引擎的数据并加工 TableEngine.prototype.reloadTableResult = function reloadTableResult() { var _this2 = this; var _props2 = this.props, tableEngine = _props2.store.tableEngine, tableAction = _props2.tableAction, appCode = _props2.appCode, menuUrl = _props2.menuUrl; tableEngine.setResultLoading(true); tableEngine.getTableColumns({ appCode: appCode, menuUrl: menuUrl }).then(function (result) { if (result) { var sorter = result.find(function (item) { return item.defaultSortField == 1; }); if (sorter) { sorter = { field: sorter.dataIndex, order: sorter.defaultSortType }; tableEngine.setSorter(sorter); } tableEngine.setResultLoading(false); _this2.resultData = result; _this2.changeGroupMember(false); _this2.loadData(1); // 查找分组,并做求和处理 _this2.groupExpand = []; _this2.resultData.map(function (v) { if (v.isTableGroupPro == 1 && v.children) { var val = []; v.children.map(function (item) { if (item.isDefaultExpand == 1) { _this2.groupExpand[item.dataIndex] = val; } else { val.push(item.dataIndex); } }); } }); } }); }; // 改变组内成员的显示 TableEngine.prototype.changeGroupMember = function changeGroupMember(type) { var _props3 = this.props, _props3$store = _props3.store, tableEngine = _props3$store.tableEngine, tableListSetting = _props3$store.tableListSetting, tableAction = _props3.tableAction; var columns = [], dataBack = void 0; if (type) { columns = this.resetResult(this.resultData, null, 1); } else { columns = this.resetResult(this.resultData, null, 0); } // columns = dataBack.arrColumns; if (Object.keys(tableAction).length !== 0) { columns.push(tableAction); } for (var i = columns.length - 2; i > 0; i--) { if (columns[i].fixed == null && columns[i].children == null) { columns[i].width = null; break; } } tableEngine.setTableAction(columns); tableEngine.setAllColumns(this.allColumns); }; // 多页面使用重置参数 TableEngine.prototype._refreshParams = function _refreshParams() { var _props$store = this.props.store, tableEngine = _props$store.tableEngine, searchEngine = _props$store.searchEngine; tableEngine.setResultLoading(true); tableEngine.setSorter(null); searchEngine.setQueryParams([]); tableEngine.setCurrentPage(1); tableEngine.setPageSize(10); tableEngine.setTableData([]); }; TableEngine.prototype.loadData = function loadData(page) { var _props4 = this.props, _props4$store = _props4.store, tableEngine = _props4$store.tableEngine, searchEngine = _props4$store.searchEngine, form = _props4.form, appCode = _props4.appCode, menuUrl = _props4.menuUrl; tableEngine.setCurrentPage(page || tableEngine.currentPage); var params = { fieldCodes: tableEngine.fieldCodes, queryParams: searchEngine.queryParams, pageNum: page || tableEngine.currentPage, pageSize: tableEngine.pageSize, operationInfo: tableEngine.opsInfo.query }; if (tableEngine.sorter) { params.sort = tableEngine.sorter.field + ',' + tableEngine.sorter.order; } tableEngine.setTableLoading(true); tableEngine.translateParams(params, { appCode: appCode, menuUrl: menuUrl }).then(function (result) { if (result) { tableEngine.searchTableData(result, tableEngine.operations.query + '?appCode=' + appCode + '&menuUrl=' + menuUrl).then(function () { tableEngine.setTableLoading(false); }); } }); }; /** * * @param {*} columns * @param {*} parent * @param {*} isShow 0不显示组内设置isDefaultExpand == 0的成员,1相反。 */ TableEngine.prototype.resetResult = function resetResult(columns, parent, isShow) { var _this3 = this; var arrColumns = []; var allColumns = []; columns.map(function (item) { var params = { dataIndex: item.dataIndex, title: item.title, sorter: item.isSorted, width: item.width != null ? Number(item.width) : null, fixed: item.fixed }; // 表格类型为枚举 if (item.showType == 15) { params.render = function (text, record, index) { return item.showMap[text]; }; } // 表格类型为百分比 if (item.showType == 10) { params.render = function (text) { if (text > 0) { return (text * 100).toFixed(2) + '%'; } else if (text == 0) { return 0; } }; } if (item.editAble) { params.render = function (text, record, index) { if (record.editable) { if (item.showType == 1) { return (0, _nornj2.default)(_templateObject, text, function (e) { return _this3.changeEdit(e, item.dataIndex, text, record, index); })(); } else if (item.showType == 15) { var map = []; for (var key in item.showMap) { map.push({ value: key, name: item.showMap[key] }); } return (0, _nornj2.default)(_templateObject2, item.showMap[text], function (e) { return _this3.changeEdit(e, item.dataIndex, text, record, index); })({ option: map }); } } else { if (item.showType == 15) { return item.showMap[text]; } else { return text; } } }; } // 组内成员且isShow == 0时,isDefaultExpand生效,否则isDefaultExpand不生效 if (parent) { if (isShow == 0) { if (item.isDefaultExpand) { allColumns.push(params); item.userConfig && arrColumns.push(params); } } else { allColumns.push(params); item.userConfig && arrColumns.push(params); } } else { if (!item.isHidden) { allColumns.push(params); item.userConfig && arrColumns.push(params); } } // 判断是分组 if (item.isTableGroupPro == 1) { params.children = _this3.resetResult(item.children, item, isShow); } }); this.allColumns = allColumns; return arrColumns; }; TableEngine.prototype.changeEdit = function changeEdit(e, code, text, record, index) { var _this4 = this; var tableEngine = this.props.store.tableEngine; this.props.onChangeEdit(e, code, text, record, index, function (type) { if (type == 'sum' || type == 'avg') { record[code] = e.target.value; for (var key in _this4.groupExpand) { if (_this4.groupExpand[key].indexOf(code) > -1) { var sumTotal = 0; _this4.groupExpand[key].map(function (i) { var number = parseInt(record[i]); sumTotal += !isNaN(number) ? number : 0; }); record[key] = sumTotal; if (type == 'avg') { record[key] = sumTotal / _this4.groupExpand[key].length; } } } } tableEngine.setTableData([].concat(_toConsumableArray(tableEngine.tableData))); }, tableEngine.backTableData[index]); }; TableEngine.prototype.onChangeSorter = function onChangeSorter(pagination, filters, sorter) { var _props$store2 = this.props.store, tableEngine = _props$store2.tableEngine, searchEngine = _props$store2.searchEngine; this.props.onChangePage(pagination, filters, sorter); tableEngine.setCurrentPage(pagination.current); tableEngine.setPageSize(pagination.pageSize); if (Object.keys(sorter).length !== 0) { tableEngine.setSorter(sorter); } this.loadData(pagination.current); }; TableEngine.prototype.render = function render() { var _props5 = this.props, tableEngine = _props5.store.tableEngine, form = _props5.form; return _tableEngineT2.default.tableEngine(this.props, this, { styles: _tableEngine2.default, tableEngine: tableEngine, tableColumns: tableEngine.tableColumns, tableData: tableEngine.tableData, listTotal: tableEngine.listTotal, pageSize: tableEngine.pageSize, currentPage: tableEngine.currentPage }); }; return TableEngine; }(_react.Component), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, 'resultData', [_mobx.observable], { enumerable: true, initializer: function initializer() { return []; } }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, 'groupExpand', [_mobx.observable], { enumerable: true, initializer: function initializer() { return {}; } }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, 'allColumns', [_mobx.observable], { enumerable: true, initializer: function initializer() { return []; } }), _applyDecoratedDescriptor(_class2.prototype, 'changeEdit', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class2.prototype, 'changeEdit'), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, 'onChangeSorter', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class2.prototype, 'onChangeSorter'), _class2.prototype)), _class2)) || _class) || _class) || _class); exports.default = TableEngine; TableEngine.defaultProps = { onShowSizeChange: function onShowSizeChange() {}, onRef: function onRef() {}, onChangePage: function onChangePage() {}, tableAction: {}, showQuickJumper: false, showSizeChanger: false };