UNPKG

shradmin

Version:

A frontend Framework for building admin application, using antd and mirrorjs.

204 lines (157 loc) 7.04 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _mirrorx = require('mirrorx'); var _compose = require('recompose/compose'); var _compose2 = _interopRequireDefault(_compose); var _querystring = require('querystring'); var _startsWith = require('lodash/startsWith'); var _startsWith2 = _interopRequireDefault(_startsWith); var _isString = require('lodash/isString'); var _isString2 = _interopRequireDefault(_isString); var _isEqual = require('lodash/isEqual'); var _isEqual2 = _interopRequireDefault(_isEqual); var _i18n = require('../../i18n'); var _common = require('../common'); var _Actions = require('./Actions'); var _Actions2 = _interopRequireDefault(_Actions); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } var List = function (_Component) { _inherits(List, _Component); function List() { var _temp, _this, _ret; _classCallCheck(this, List); 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), _this.getRequestParams = function () { var _this$props = _this.props, query = _this$props.query, params = _this$props.params; return _extends({}, params, query); }, _this.updateData = function (requestParams) { var _this$props2 = _this.props, model = _this$props2.model, filter = _this$props2.filter; requestParams._filter = _extends({}, requestParams._filter, filter); _mirrorx.actions[model].getList(requestParams); }, _this.setParams = function (newParams) { var _this$props3 = _this.props, model = _this$props3.model, query = _this$props3.query, params = _this$props3.params, location = _this$props3.location; var requestParams = _extends({}, params, query, newParams); _mirrorx.actions[model].setListParams(requestParams); _mirrorx.actions.routing.push(_extends({}, location, { search: '?' + (0, _querystring.stringify)(_extends({}, requestParams, { _filter: JSON.stringify(requestParams._filter) })) })); }, _this.refresh = function (event) { event.stopPropagation(); var requestParams = _this.getRequestParams(); _this.updateData(requestParams); }, _temp), _possibleConstructorReturn(_this, _ret); } List.prototype.componentDidMount = function componentDidMount() { var _props = this.props, model = _props.model, query = _props.query; _mirrorx.actions[model].setListParams(query); var requestParams = this.getRequestParams(); this.updateData(requestParams); }; List.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { var model = nextProps.model, query = nextProps.query, params = nextProps.params; if (model !== this.props.model || !(0, _isEqual2.default)(query, this.props.query)) { this.updateData(_extends({}, params, query)); } }; List.prototype.render = function render() { var _props2 = this.props, actions = _props2.actions, filters = _props2.filters, model = _props2.model, children = _props2.children, hasCreate = _props2.hasCreate, data = _props2.data, translate = _props2.translate, params = _props2.params, total = _props2.total; var requestParams = this.getRequestParams(); return _react2.default.createElement( _common.Page, { header: _react2.default.createElement( 'div', null, actions && _react2.default.cloneElement(actions, { model: model, refresh: this.refresh, hasCreate: hasCreate }), filters && _react2.default.cloneElement(filters, { model: model, translate: translate, setParams: this.setParams, values: requestParams._filter || {} }) ) }, children && _react2.default.cloneElement(children, { data: data, model: model, translate: translate, params: params, total: total, setParams: this.setParams }) ); }; return List; }(_react.Component); List.propTypes = process.env.NODE_ENV !== "production" ? { actions: _propTypes2.default.element, model: _propTypes2.default.string, data: _propTypes2.default.array, translate: _propTypes2.default.func, params: _propTypes2.default.object, total: _propTypes2.default.number, filter: _propTypes2.default.object } : {}; List.defaultProps = { actions: _react2.default.createElement(_Actions2.default, null), data: [], params: {}, total: 0 }; var getQuery = function getQuery(search) { var query = (0, _querystring.parse)((0, _startsWith2.default)(search, '?') ? search.substr(1) : search); if (query._filter && (0, _isString2.default)(query._filter)) { query._filter = JSON.parse(query._filter); } return query; }; var enhance = (0, _compose2.default)((0, _mirrorx.connect)(function (state, props) { var model = props.model; var _state$model = state[model], params = _state$model.params, list = _state$model.list, ids = _state$model.ids, total = _state$model.total; return { params: params, data: ids.map(function (id) { return list[id].record; }), total: total, query: getQuery(state.routing.location.search), location: state.routing.location }; }), _i18n.translate); exports.default = enhance(List); module.exports = exports['default'];