UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

617 lines (558 loc) 17.5 kB
'use strict'; var _searchBar = require('antd-mobile/lib/search-bar'); var _searchBar2 = _interopRequireDefault(_searchBar); var _actionSheet = require('antd-mobile/lib/action-sheet'); var _actionSheet2 = _interopRequireDefault(_actionSheet); var _toast = require('antd-mobile/lib/toast'); var _toast2 = _interopRequireDefault(_toast); var _modal = require('antd-mobile/lib/modal'); var _modal2 = _interopRequireDefault(_modal); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); 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/search-bar/style/css'); require('antd-mobile/lib/action-sheet/style/css'); require('antd-mobile/lib/toast/style/css'); require('antd-mobile/lib/modal/style/css'); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _YYLibraryNavBar = require('./YYLibraryNavBar'); var _YYLibraryNavBar2 = _interopRequireDefault(_YYLibraryNavBar); var _YYLibraryList = require('./YYLibraryList'); var _YYLibraryList2 = _interopRequireDefault(_YYLibraryList); var _YYFile = require('./YYFile'); var _YYFile2 = _interopRequireDefault(_YYFile); var _createfile = require('./createfile.js'); var _createfile2 = _interopRequireDefault(_createfile); var _RestUrl = require('../../common/RestUrl'); var _UploadFileUtils = require('../../utils/UploadFileUtils'); var _UploadFileUtils2 = _interopRequireDefault(_UploadFileUtils); require('./YYLibrary.less'); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); 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 alert = _modal2.default.alert; var isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent); var wrapProps = void 0; if (isIPhone) { wrapProps = { onTouchStart: function onTouchStart(e) { return e.preventDefault(); } }; } var YYLibrary = function (_React$Component) { _inherits(YYLibrary, _React$Component); function YYLibrary(props) { _classCallCheck(this, YYLibrary); var _this = _possibleConstructorReturn(this, (YYLibrary.__proto__ || Object.getPrototypeOf(YYLibrary)).call(this, props)); _this.onSelect = function (opt) { var buttonAuth = _this.props.buttonAuth; var key = opt.key; if (key === '1') { if (buttonAuth.upload) { if (window.YYPlugin) { _this.uploadFile(); } } else { _toast2.default.info('无上传权限', 1); } } if (key === '2') { if (buttonAuth.addBtn) { _this.onAddFile(); } else { _toast2.default.info('无新建权限', 1); } } if (key === '3') { _this.multiSelect(); } _this.setState({ visible: false, selected: opt.props.value }); }; _this.state = { visible: false, selected: '', multi: false, selectAll: false, add: false, selectRowKeys: [], selectRows: [], currentItem: null, copyFile: null, showFile: false }; return _this; } _createClass(YYLibrary, [{ key: 'modifyName', value: function modifyName(file) { var _this2 = this; this.setState({ add: false }, function () { _this2.props.updateFolder && _this2.props.updateFolder(file); }); } }, { key: 'onFileSave', value: function onFileSave(file) { var _this3 = this; this.setState({ add: false }, function () { _this3.props.addFolder && _this3.props.addFolder(file); }); } }, { key: 'uploadFile', value: function uploadFile() { var _props = this.props, source = _props.source, billType = _props.billType; var self = this; var sourceId = parseInt(Math.random() * 1000); var params = { billType: source ? source.billType : billType, sourceType: source ? source.sourceType : "MD001", sourceId: source ? source.sourceId : sourceId, userId: window.YYUtils.AuthToken.getUserId(), userName: window.YYUtils.AuthToken.getUserName(), camera: true, picture: true, file: true }; if (window.YYPlugin) { window.YYPlugin.call("CommonPlugin", "postFile", params, function success(result) { try { if (result) { var addObj = { billType: billType, type: 0, parentId: self.context.location && self.context.location.query.fileid, fileName: result.fileName, filePath: result.filePath, fileSize: result.fileSize, attachMgr: result.gid, attachObjs: result.sourceType }; self.props.addFile && self.props.addFile(Object.assign({}, addObj, { id: sourceId })); } } catch (e) { throw '上传文件失败'; } }); } else { _toast2.default.info('请在手机上进行调试或检查yyplus的引入!'); } } }, { key: 'onFilesChange', value: function onFilesChange(files) { if (!window.YYPlugin) { this.uploadPCFile(files[0]); } else { this.uploadFile(); } } }, { key: 'uploadPCFile', value: function uploadPCFile(files) { var self = this; var _props2 = this.props, source = _props2.source, billType = _props2.billType; var sourceId = parseInt(Math.random() * 1000); var params = { billType: source ? source.billType : billType, sourceType: source ? source.sourceType : "MD001", sourceId: source ? source.sourceId : sourceId, userId: window.YYUtils.AuthToken.getUserId(), userName: window.YYUtils.AuthToken.getUserName(), camera: true, picture: true, file: files, billId: source ? source.billId : "MD001" }; _UploadFileUtils2.default.multiFilesUpLoad(params, params.billType, { sourceId: params.sourceId, sourceType: params.sourceType }, function (_ref) { var _ref2 = _slicedToArray(_ref, 1), result = _ref2[0]; try { var backData = result.backData; var addObj = { billType: billType, type: 0, parentId: self.context.location && self.context.location.query.fileid, fileName: backData.fileName, filePath: backData.filePath, fileSize: backData.fileSize, attachMgr: backData.gid, attachObjs: backData.sourceType }; if (!addObj.parentId) { addObj.parentId = 0; } self.props.addFile && self.props.addFile(Object.assign({}, addObj, { id: sourceId })); } catch (e) { throw '上传文件失败'; } }); } }, { key: 'handleVisibleChange', value: function handleVisibleChange(visible) { this.setState({ visible: visible }); } }, { key: 'shareFile', value: function shareFile(file) { var source = this.props.source; if (file.type === 0) { if (file && window.YYPlugin) { var url = _RestUrl.ADDR + '/icop-file/onlinePreview?url=' + _RestUrl.ADDR + '/' + file.filePath; var params = { type: "3", text: file.fileName, url: url, title: file.fileName }; window.YYPlugin.call("CommonPlugin", 'share', params); } } else { _toast2.default.info('文件夹无法分享', 1); } } }, { key: 'onChange', value: function onChange(item) { var selectRowKeys = this.state.selectRowKeys; var selectRows = this.state.selectRows; var index = selectRowKeys.indexOf(item.id); if (index > -1) { selectRowKeys.splice(index, 1); selectRows.splice(index, 1); } else { selectRowKeys.push(item.id); selectRows.push(item); } this.setState({ selectRowKeys: selectRowKeys, selectRows: selectRows }); } }, { key: 'operation', value: function operation(item) { this.showActionSheet(item); } }, { key: 'multiSelect', value: function multiSelect() { this.setState({ currentItem: null, copyFile: null, multi: true }); } }, { key: 'onCancel', value: function onCancel() { this.setState({ multi: false }); } }, { key: 'onAddFile', value: function onAddFile() { this.setState({ currentItem: null, copyFile: null, add: true }); } }, { key: 'onCoyeFile', value: function onCoyeFile(item) { this.setState({ copyFile: item }); } }, { key: 'paste', value: function paste() { var target = this.context.location && this.context.location.query.fileid; var sourceId = this.state.copyFile && this.state.copyFile.id; this.props.copyFolder([sourceId], target); this.setState({ copyFile: null }); } }, { key: 'cancel', value: function cancel() { this.setState({ copyFile: null }); } }, { key: 'downLoad', value: function downLoad(item) { var source = this.props.source; if (item.type === 0) { var url = _RestUrl.ADDR + '/' + item.filePath + '?filename=' + item.fileName; var elemIF = document.createElement("iframe"); elemIF.src = url; elemIF.style.display = "none"; } else { _toast2.default.info('文件夹无法下载', 1); } } }, { key: 'handleClick', value: function handleClick(buttonIndex, item) { var _this4 = this; var buttonAuth = this.props.buttonAuth; switch (buttonIndex) { case 0: if (buttonAuth.deleteBtn) { alert('删除', '确定要删除此条文件记录?', [{ text: '取消', onPress: function onPress() {} }, { text: '确定', onPress: function onPress() { var deleteItems = []; deleteItems.push({ id: item.id, ts: item.ts }); _this4.props.deleteFile && _this4.props.deleteFile(deleteItems); } }]); } else { _toast2.default.info('无删除权限', 1); } break; case 1: if (buttonAuth.edit) { this.setState({ currentItem: item, add: true }); } else { _toast2.default.info('无重命名权限', 1); } break; case 2: if (buttonAuth.share) { this.shareFile(item); } else { _toast2.default.info('无分享权限', 1); } break; case 3: if (buttonAuth.copy) { this.onCoyeFile(item); } else { _toast2.default.info('无复制权限', 1); } } } }, { key: 'showActionSheet', value: function showActionSheet(item) { var _this5 = this; var BUTTONS = ['删除', '重命名', '分享', '复制', '取消']; _actionSheet2.default.showActionSheetWithOptions({ options: BUTTONS, cancelButtonIndex: BUTTONS.length - 1, destructiveButtonIndex: BUTTONS.length - 5, maskClosable: true, 'data-seed': 'logId', wrapProps: wrapProps }, function (buttonIndex) { _this5.handleClick(buttonIndex, item); }); } }, { key: 'onSelectAll', value: function onSelectAll() { var fileData = this.props.fileData; var selectRows = []; var selectRowKeys = []; fileData.map(function (item, index) { selectRowKeys.push(item.id), selectRows.push(item); }); this.setState({ selectAll: true, selectRowKeys: selectRowKeys, selectRows: selectRows }); } }, { key: 'onCancelSelectAll', value: function onCancelSelectAll() { this.setState({ selectAll: false, selectRowKeys: [], selectRows: [] }); } }, { key: 'cancelCreate', value: function cancelCreate() { this.setState({ add: false }); } }, { key: 'onRefresh', value: function onRefresh() { this.props.refresh && this.props.refresh(); } }, { key: 'searchText', value: function searchText(value) { this.props.searchFile && this.props.searchFile(value); } }, { key: 'cancelSearch', value: function cancelSearch() { if (this.refs.mySearch) { this.refs.mySearch.doClear(); this.props.cancelSearch && this.props.cancelSearch(); } } }, { key: 'closeFile', value: function closeFile() { this.setState({ showFile: false }); } }, { key: 'openFile', value: function openFile(params) { this.setState({ showFile: true, selectRow: params }); } }, { key: 'render', value: function render() { var _state = this.state, visible = _state.visible, multi = _state.multi, selectAll = _state.selectAll, add = _state.add, selectRows = _state.selectRows, selectRowKeys = _state.selectRowKeys, currentItem = _state.currentItem, copyFile = _state.copyFile, showFile = _state.showFile, selectRow = _state.selectRow; var _props3 = this.props, fileData = _props3.fileData, billType = _props3.billType, searchText = _props3.searchText, title = _props3.title, buttonAuth = _props3.buttonAuth; var wrapClz = (0, _classnames2.default)('yy-library', visible && 'change'); return _react2.default.createElement( 'div', { style: { height: "100%" }, className: wrapClz }, !showFile || _react2.default.createElement(_YYFile2.default, { selectRow: selectRow, closeFile: this.closeFile.bind(this), send: this.props.send, getCommentList: this.props.getCommentList, reached: this.props.reached, liked: this.props.liked, total: this.props.total, last: this.props.last, appendData: this.props.appendData, commentList: this.props.commentList }), !add && _react2.default.createElement(_YYLibraryNavBar2.default, { multi: multi, selectAll: selectAll, visible: visible, buttonAuth: buttonAuth, onVisibleChange: this.handleVisibleChange.bind(this), onSelect: this.onSelect.bind(this), onSelectAll: this.onSelectAll.bind(this), onCancelSelectAll: this.onCancelSelectAll.bind(this), onCancel: this.onCancel.bind(this), onChange: this.onFilesChange.bind(this) }), !add && _react2.default.createElement(_searchBar2.default, { ref: 'mySearch', placeholder: searchText, onSubmit: this.searchText.bind(this), onCancel: this.cancelSearch.bind(this), showCancelButton: true }), !add && _react2.default.createElement(_YYLibraryList2.default, { multi: multi, selectAll: selectAll, selectRowKeys: selectRowKeys, selectRows: selectRows, data: fileData, onChange: this.onChange.bind(this), operation: this.operation.bind(this), onRefresh: this.onRefresh.bind(this), openFile: this.openFile.bind(this) }), add && _react2.default.createElement(_createfile2.default, { modifyName: this.modifyName.bind(this), cancelCreate: this.cancelCreate.bind(this), currentItem: currentItem, onFileSave: this.onFileSave.bind(this), billType: billType, title: title, buttonAuth: buttonAuth }), copyFile && _react2.default.createElement( 'div', { className: 'buttons' }, _react2.default.createElement( 'div', { className: 'btn-paste', onClick: this.paste.bind(this) }, '\u7C98\u8D34' ), _react2.default.createElement( 'div', { className: 'btn-paste', onClick: this.cancel.bind(this) }, '\u53D6\u6D88' ) ) ); } }]); return YYLibrary; }(_react2.default.Component); YYLibrary.defaultProps = { searchText: '请输入文件关键字', fileData: [], billType: '', buttonAuth: { addBtn: true, edit: true, deleteBtn: true, upload: true, copy: true, share: true } }; YYLibrary.contextTypes = { router: _propTypes2.default.object.isRequired, location: _propTypes2.default.object.isRequired }; module.exports = YYLibrary;