UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

857 lines (760 loc) 29.6 kB
'use strict'; var _pullToRefresh = require('antd-mobile/lib/pull-to-refresh'); var _pullToRefresh2 = _interopRequireDefault(_pullToRefresh); var _button = require('antd-mobile/lib/button'); var _button2 = _interopRequireDefault(_button); var _modal = require('antd-mobile/lib/modal'); var _modal2 = _interopRequireDefault(_modal); var _toast = require('antd-mobile/lib/toast'); var _toast2 = _interopRequireDefault(_toast); var _listView = require('antd-mobile/lib/list-view'); var _listView2 = _interopRequireDefault(_listView); var _list = require('antd-mobile/lib/list'); var _list2 = _interopRequireDefault(_list); var _YYRepeater$defaultPr; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 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 _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); require('antd-mobile/lib/pull-to-refresh/style/css'); require('antd-mobile/lib/button/style/css'); require('antd-mobile/lib/modal/style/css'); require('antd-mobile/lib/toast/style/css'); require('antd-mobile/lib/list-view/style/css'); require('antd-mobile/lib/list/style/css'); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _YYModal = require('../modal/YYModal'); var _YYModal2 = _interopRequireDefault(_YYModal); var _YYNavBar = require('../navBar/YYNavBar'); var _YYNavBar2 = _interopRequireDefault(_YYNavBar); var _YYFormDev = require('../form/YYFormDev'); var _YYFormDev2 = _interopRequireDefault(_YYFormDev); var _YYIcon = require('../icon/YYIcon'); var _YYIcon2 = _interopRequireDefault(_YYIcon); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); require('./YYRepeater.less'); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 Item = _list2.default.Item; var Brief = Item.Brief; var YYRepeater = function (_React$Component) { _inherits(YYRepeater, _React$Component); function YYRepeater(props) { _classCallCheck(this, YYRepeater); var _this2 = _possibleConstructorReturn(this, (YYRepeater.__proto__ || Object.getPrototypeOf(YYRepeater)).call(this, props)); var dataSource = new _listView2.default.DataSource({ rowHasChanged: function rowHasChanged(row1, row2) { return row1 !== row2; } }); _this2.state = { dataSource: dataSource, isLoading: true, refreshing: true, data: [], initdata: [], footer: '', formModal: false, isAdd: false, EditRow: {}, count: 0, delData: [] }; return _this2; } _createClass(YYRepeater, [{ key: 'componentDidMount', value: function componentDidMount() { var _this3 = this; var RunInDesign = this.props.RunInDesign; if (RunInDesign) { this.setData([{}]); } setTimeout(function () { _this3.setState({ isLoading: false, refreshing: false }); }, 300); } }, { key: 'uuid', value: function uuid() { return new Date().getDate() + "_" + parseInt(Math.random() * 100000000); } }, { key: 'clickItem', value: function clickItem(row) { if (this.props.onClick) { this.props.onClick(row); } } }, { key: 'createRow', value: function createRow(rowData, sectionID, rowID, highlightRow) { var _this4 = this; var _props = this.props, customDraw = _props.customDraw, children = _props.children, RunInDesign = _props.RunInDesign; var cloneData = _lodash2.default.cloneDeep(rowData); if (customDraw) { if (RunInDesign) { return _react2.default.createElement( Item, { arrow: 'horizontal', multipleLine: true }, _react2.default.createElement( Brief, null, '\u60A8\u9700\u8981\u6253\u5F00\u81EA\u5B9A\u4E49\u6E32\u67D3\u884C\u4E8B\u4EF6,\u5E76\u5728\u6B64\u4E8B\u4EF6\u4E2D\u8FD4\u56DE\u4E00\u4E2A\u53EF\u6E32\u67D3\u7684\u7EC4\u4EF6\u5BF9\u8C61' ) ); } else if (this.props.renderRow) { return _react2.default.createElement( 'div', { onClick: this.clickItem.bind(this, rowData) }, this.props.renderRow(cloneData, sectionID, rowID, highlightRow) ); } } else { var itemControlData = this.getItemControlData(this.props.children); Object.keys(itemControlData).map(function (key) { itemControlData[key]["uikey"] = itemControlData[key]["uikey"] + "_" + sectionID + "_" + rowID; itemControlData[key]["nid"] = itemControlData[key]["nid"] + "_" + sectionID + "_" + rowID; itemControlData[key]["ref"] = itemControlData[key]["uikey"]; itemControlData[key]["sys_front_id"] = rowData["sys_front_id"]; itemControlData[key]["listDataChange"] = _this4.updateData; itemControlData[key]["_parent_obj"] = _this4; if (itemControlData[key].uitype == "YYFormDev") { itemControlData[key].autoSave = true; } }); if (this.props.renderItem) { this.props.renderItem(cloneData, itemControlData); } var itemControls = this.getRenderControl(children, itemControlData, cloneData); return _react2.default.createElement( 'div', { className: 'yy-reperater-item', style: { "position": "relative" }, onClick: this.clickItem.bind(this, rowData) }, itemControls ); } } }, { key: 'saveData', value: function saveData() { var _this5 = this; var _state = this.state, sysID = _state.EditRow.sys_front_id, isAdd = _state.isAdd; if (sysID) { if (this.refs.ModalForm) { this.refs.ModalForm.validateFields(function (errors, values) { var saveValue = _extends({}, values); if (errors) { _toast2.default.info("请输入必填项!"); } else { if (isAdd) { _this5.setAppendData([saveValue]); } else { _this5.updateData(sysID, saveValue, true); } _this5.setState({ formModal: false }); } }); } } } }, { key: 'updateData', value: function updateData(sysID, valueOptions, crud) { if (sysID) { var data = this.state.data; if (data && data.some(function (item) { return item.sys_front_id == sysID; })) { data.map(function (item) { if (item.sys_front_id == sysID) { var rowState = {}; if (item.rowState != "add") { rowState = { rowState: "edit" }; } Object.assign(item, valueOptions, rowState); } }); if (crud) { this.setData(data, true); } else { this.setState({ data: data }); } } } } }, { key: 'deleteDataAlert', value: function deleteDataAlert(row) { var _this6 = this; _modal2.default.alert("删除", "确定要删除这条记录吗?", [{ text: '取消' }, { text: '确定', onPress: function onPress() { _this6.delData(row); } }]); } }, { key: 'getDataBlob', value: function getDataBlob(data) { var dataBlob = {}; if (data && data.length) { data.map(function (item) { dataBlob[item.sys_front_id] = item; }); } return dataBlob; } }, { key: 'delData', value: function delData(data) { var _state2 = this.state, alldata = _state2.data, delData = _state2.delData; if (data && Array == data.constructor) { data.map(function (item) { if (item.sys_front_id) { alldata.map(function (dataItem, index) { if (dataItem.sys_front_id == item.sys_front_id) { if (dataItem.rowState != "add") { delData.push(Object.assign(dataItem, { rowState: "del" })); } alldata.splice(index, 1); } }); } else { console.log("删除的记录缺少列表的唯一标识!"); } }); } else if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) == "object") { alldata.map(function (dataItem, index) { if (dataItem.sys_front_id == data.sys_front_id) { if (dataItem.rowState != "add") { delData.push(Object.assign(dataItem, { rowState: "del" })); } alldata.splice(index, 1); } }); } var ds = this.state.dataSource.cloneWithRows(this.getDataBlob(alldata), alldata.map(function (dataItem) { return dataItem.sys_front_id; })); this.setState({ dataSource: ds, data: alldata, delData: delData }); } }, { key: 'setData', value: function setData(parseData, notClear) { var _this7 = this; var data = _lodash2.default.cloneDeep(parseData); if (data && data.length) { data.map(function (item) { item.sys_front_id = _this7.uuid(); }); } if (!notClear) { this.setState({ delData: [] }); } var ds = this.state.dataSource.cloneWithRows(this.getDataBlob(data), data.map(function (dataItem) { return dataItem.sys_front_id; })); setTimeout(function () { _this7.setState({ dataSource: ds, data: data, isLoading: false, refreshing: false }); }, 300); } }, { key: 'setIsLoading', value: function setIsLoading(flag) { this.setState({ isLoading: flag }); } }, { key: 'removeItem', value: function removeItem(row, event) { var RunInDesign = this.props.RunInDesign; if (!RunInDesign) { event.stopPropagation(); this.deleteDataAlert(row); } } }, { key: 'editItem', value: function editItem(row) { this.showFormModal(row); } }, { key: 'cancelSave', value: function cancelSave() { this.setState({ formModal: false }); } }, { key: 'showFormModal', value: function showFormModal(row, isAdd) { var childTable = this.props.childTable; if (childTable) { this.setState({ formModal: true, EditRow: row, isAdd: isAdd }); } } }, { key: 'getData', value: function getData() { var _state3 = this.state, data = _state3.data, delData = _state3.delData; var current = data.map(function (item, index) { return Object.assign({}, item, { sys_front_id: null }); }); return current.concat(delData); } }, { key: 'setAppendData', value: function setAppendData(parseData, addBefore) { var _this8 = this; var data = _lodash2.default.cloneDeep(parseData);var beforeData = this.props.beforeData; if (data && data.length) { data.map(function (item) { if (!item.sys_front_id) { item.sys_front_id = _this8.uuid(); } item.rowState = "add"; if (beforeData) { item = Object.assign(item, beforeData); } }); } var allData = void 0; if (!addBefore) { allData = this.state.data.concat(data); } else { allData = data.concat(this.state.data); } var ds = this.state.dataSource.cloneWithRows(this.getDataBlob(allData), allData.map(function (dataItem) { return dataItem.sys_front_id; })); this.setState({ dataSource: ds, data: allData, refreshing: false, isLoading: false }); } }, { key: 'onRefresh', value: function onRefresh(event) { var _this9 = this; this.setState({ refreshing: true, isLoading: true }); if (this.props.onRefresh) { this.props.onRefresh(); } else { setTimeout(function () { _this9.setState({ dataSource: _this9.state.dataSource.cloneWithRows(_this9.state.data), refreshing: false, isLoading: false }); }, 300); } } }, { key: 'onEndReached', value: function onEndReached(event) { var _this10 = this; if (!this.props.onEndReached) { return false; } setTimeout(function () { _this10.setState({ isLoading: true }); _this10.props.onEndReached(); }, 300); } }, { key: 'getRenderControl', value: function getRenderControl(children, info, rowData) { var _this11 = this; var controls = []; var childTable = this.props.childTable; Array.prototype.slice.call(children); if (children && children.length) { children.map(function (item, index) { var _item$props = item.props, uikey = _item$props.uikey, child = _item$props.children; var childView = void 0; if (childTable && item.props.uitype == "YYFormDev") { controls = controls.concat(_this11.getReadOnlyControl(item, rowData)); } else { if (child && child.length) { childView = _this11.getRenderControl(child, info, rowData); } controls.push(_react2.default.cloneElement(item, info[uikey], childView)); } }); } return controls; } }, { key: 'getChild', value: function getChild(value, data) { var _this12 = this; var values = []; data.map(function (item, index) { value.map(function (rowItem, indx) { if (item.value == rowItem) { values.push(item.label); } }); if (item.children && item.children.length) { var child = _this12.getChild(value, item.children); if (child && child.length) { values = values.concat(child); } } }); return values; } }, { key: 'getReadOnlyControl', value: function getReadOnlyControl(formDev, rowData) { var ItemList = []; if (formDev && formDev.props.children) { if (this.props.renderChildItem && this.props.renderChildItem(rowData, this)) { return this.props.renderChildItem(rowData, this); } return _react2.default.createElement( Item, { arrow: 'horizontal', thumb: _react2.default.createElement(_YYIcon2.default, { onClick: this.removeItem.bind(this, rowData), type: 'Delete', color: '#ff6262', size: 'xxs' }), multipleLine: true, onClick: this.editItem.bind(this, rowData), extra: '\u5B50\u8868\u503C' }, '\u5B50\u8868\u9879' ); } return ItemList; } }, { key: 'getItemControlData', value: function getItemControlData(children) { var _this13 = this; var data = {}; children && children.map(function (item) { if (item.props.uikey) { data[item.props.uikey] = Object.assign({}, item.props); if (item.props.children && item.props.children.length) { var itemChildren = item.props.children; Array.prototype.slice.call(itemChildren); var childControl = _this13.getItemControlData(itemChildren); Object.assign(data, childControl); } } }); return data; } }, { key: 'getFormControl', value: function getFormControl() { var _this14 = this; var _this = this; var _state4 = this.state, row = _state4.EditRow, formModal = _state4.formModal; if (!formModal) { return null; } if (row) { var itemControlData = this.getItemControlData(this.props.children); itemControlData = _lodash2.default.cloneDeep(itemControlData); Object.keys(itemControlData).map(function (key) { itemControlData[key]["sys_front_id"] = row["sys_front_id"]; itemControlData[key]["_parent_obj"] = _this14; if (itemControlData[key].uitype == "YYFormDev") { itemControlData[key].disabled = false; itemControlData[key].autoSave = false; itemControlData[key].ref = "ModalForm"; itemControlData[key].onViewDidMount = function (obj) { _lodash2.default.forEach(obj.props.children, function (formItem) { if ("YYDatePicker" === formItem.props.uitype || formItem.props.uityp == "YYCalendar") { if (formItem.props.uitype == "YYCalendar") { var calendarValue = {}; if (row[formItem.props.startField]) { calendarValue[formItem.props.tartField] = new Date(row[formItem.props.startField]); } if (row[formItem.props.endField]) { calendarValue[formItem.props.endField] = new Date(row[formItem.props.endField]); } row[formItem.props.field] = calendarValue; } else { if (row[formItem.props.field] && !(row[formItem.props.field] instanceof Date)) { row[formItem.props.field] = row[formItem.props.field] && row[formItem.props.field].replace(/\-/g, '/'); row[formItem.props.field] = new Date(row[formItem.props.field]); } } } else if ("YYPicker" === formItem.props.uitype) { var values = row[formItem.field]; if (typeof values == "string") { row[formItem.props.field] = values.split(','); } } else if ("YYRefer" === formItem.props.uitype) { var values = row[formItem.props.field]; if (_lodash2.default.isPlainObject(values)) { var cvalue = _lodash2.default.cloneDeep(values); if ("id" !== formItem.id) { delete cvalue['id']; row[formItem.props.field] = cvalue; } } else if (_lodash2.default.isArray(values)) { var cvalue = _lodash2.default.cloneDeep(values); if ("id" !== formItem.props.id) { cvalue.map(function (value) { delete value['id']; }); row[formItem.props.field] = cvalue; } } } }); obj.props.form.setFieldsValue(row); }; } }); var itemControls = this.getModalControl(this.props.children, itemControlData, row); return itemControls; } } }, { key: 'getModalControl', value: function getModalControl(children, info, row) { var _this15 = this; var controls = []; var childTable = this.props.childTable; Array.prototype.slice.call(children); if (children && children.length) { children.map(function (item, index) { var _item$props2 = item.props, uikey = _item$props2.uikey, child = _item$props2.children; var childView = void 0; if (child && child.length) { childView = _this15.dealChildControl(child, info, row); } if (info[uikey]) { if (info[uikey].uitype == "YYFormDev") { controls.push(_react2.default.createElement(_YYFormDev2.default, info[uikey], childView)); } else { controls.push(_react2.default.cloneElement(item, info[uikey], childView)); } } }); } return controls; } }, { key: 'dealChildControl', value: function dealChildControl(children, info, rowData) { var _this16 = this; var controls = []; var childTable = this.props.childTable; Array.prototype.slice.call(children); if (children && children.length) { children.map(function (item, index) { var _item$props3 = item.props, uikey = _item$props3.uikey, child = _item$props3.children; var childView = void 0; if (child && child.length) { childView = _this16.dealChildControl(child, info, rowData); } if (item.props.uitype == "YYImagePicker") { var sourceType = rowData[uikey] ? rowData[uikey] : _this16.uuid(); var source = _extends({}, info[uikey].source, { sourceType: sourceType }); var params = _extends({}, info[uikey], { sourceType: sourceType, source: source, inChild: true }); controls.push(_react2.default.cloneElement(item, params)); } else { controls.push(_react2.default.cloneElement(item, info[uikey], childView)); } }); } return controls; } }, { key: 'getChildModal', value: function getChildModal() { var navBarName = this.props.navBarName; var formModal = this.state.formModal; if (formModal) { return _react2.default.createElement( _YYModal2.default, { fill: true, popup: true, visible: formModal, animationType: 'slide-up' }, _react2.default.createElement( _YYNavBar2.default, { rightContent: '\u4FDD\u5B58', leftContent: '\u53D6\u6D88', onLeftClick: this.cancelSave.bind(this), onRightClick: this.saveData.bind(this) }, navBarName ), this.getFormControl() ); } return null; } }, { key: 'getAccordionPanelHeight', value: function getAccordionPanelHeight(children, data, RunInDesign) { var _props2 = this.props, useCrud = _props2.useCrud, addNavBar = _props2.addNavBar, showFooter = _props2.showFooter; var uitypes = ['YYInput', 'YYPicker', 'YYRefer', 'YYSwitch', 'YYDatePicker']; var height = addNavBar ? 45 : 0; var formHeight = 0; var dataLength = data && data.length ? data.length : 0; children.map(function (item, index) { if (item.props.uitype == 'YYFormDev') { if (item.props.visible) { if (item.props.children) { item.props.children.map(function (child, index) { var _child$props = child.props, uitype = _child$props.uitype, visible = _child$props.visible; var addHeight = 0; if (uitypes.some(function (item) { return item == uitype; }) && visible) formHeight += 50; if (uitype == 'YYItemText' && visible) formHeight += 40; if (uitype == 'YYImagePicker' && visible) formHeight += 200; if (uitype == 'YYTextareaItem' && visible) formHeight += 140; }); } } } }); if (!RunInDesign) formHeight = formHeight * dataLength; if (showFooter) height = height + formHeight + 50;else height = height + formHeight; return height; } }, { key: 'getHeight', value: function getHeight() { var _props3 = this.props, cutheight = _props3.cutheight, children = _props3.children, RunInDesign = _props3.RunInDesign, parentType = _props3.parentType; var data = this.state.data; var height = "calc(100% - " + cutheight + ")"; if (parentType === 'YYAccordionPanel') { height = this.getAccordionPanelHeight(children, data, RunInDesign); } return height; } }, { key: 'getClassName', value: function getClassName() { var _props4 = this.props, visible = _props4.visible, className = _props4.className; var repeaterClz = (0, _classnames2.default)('yy-repeater', !visible && 'hidden', className); return repeaterClz; } }, { key: 'renderFooter', value: function renderFooter() { var _props5 = this.props, footerBefore = _props5.footerBefore, footerAfter = _props5.footerAfter, showFooter = _props5.showFooter, addNavBar = _props5.addNavBar, addChildText = _props5.addChildText, childTable = _props5.childTable; var isLoading = this.state.isLoading; if (addNavBar && childTable) { var buttonDom = _react2.default.createElement( _button2.default, { size: 'small', type: 'ghost', onClick: this.showFormModal.bind(this, { sys_front_id: this.uuid() }, true) }, addChildText ); if (showFooter) { return _react2.default.createElement( 'div', null, buttonDom, _react2.default.createElement( 'div', { style: { padding: 10, textAlign: 'center' } }, isLoading ? footerBefore : footerAfter ) ); } else { return _react2.default.createElement( 'div', null, buttonDom ); } } else if (showFooter) { return _react2.default.createElement( 'div', { style: { padding: 10, textAlign: 'center' } }, isLoading ? footerBefore : footerAfter ); } return null; } }, { key: 'render', value: function render() { var _this17 = this; var _props6 = this.props, onEndReachedThreshold = _props6.onEndReachedThreshold, onRefresh = _props6.onRefresh; var _state5 = this.state, dataSource = _state5.dataSource, refreshing = _state5.refreshing; return _react2.default.createElement( _listView2.default, { ref: 'YYRepeater', key: "YYRepeater", renderRow: function renderRow(rowData, sectionID, rowID, highlightRow) { return _this17.createRow(rowData, sectionID, rowID, highlightRow); }, dataSource: dataSource, scrollRenderAheadDistance: 500, style: { height: this.getHeight() }, onEndReachedThreshold: onEndReachedThreshold, renderFooter: this.renderFooter.bind(this), className: this.getClassName(), onEndReached: this.onEndReached.bind(this), pullToRefresh: onRefresh ? _react2.default.createElement(_pullToRefresh2.default, { distanceToRefresh: 25, refreshing: refreshing, onRefresh: this.onRefresh.bind(this) }) : null }, this.getChildModal() ); } }]); return YYRepeater; }(_react2.default.Component); YYRepeater.defaultProps = (_YYRepeater$defaultPr = { data: [], appendData: [], onEndReachedhold: 10, initialListSize: 4, visible: true, addNavBar: false, navBarName: "子表", showFooter: true, footerBefore: "正在加载", footerAfter: "加载完成", cutheight: "0px", addChildText: "添加子表明细" }, _defineProperty(_YYRepeater$defaultPr, 'navBarName', "子表明细"), _defineProperty(_YYRepeater$defaultPr, 'childTable', false), _YYRepeater$defaultPr); module.exports = YYRepeater;