import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { DropdownToggle, Dropdown, DropdownMenu, DropdownItem } from 'reactstrap'; import { gettext, siteRoot } from '../../utils/constants'; import { Utils } from '../../utils/utils'; import ModalPotal from '../modal-portal'; import ShareDialog from '../dialog/share-dialog'; import EditFileTagDialog from '../dialog/edit-filetag-dialog'; const propTypes = { path: PropTypes.string.isRequired, repoID: PropTypes.string.isRequired, repoTags: PropTypes.array.isRequired, userPerm: PropTypes.string.isRequired, repoEncrypted: PropTypes.bool.isRequired, enableDirPrivateShare: PropTypes.bool.isRequired, isGroupOwnedRepo: PropTypes.bool.isRequired, filePermission: PropTypes.string, fileTags: PropTypes.array.isRequired, onFileTagChanged: PropTypes.func.isRequired, showShareBtn: PropTypes.bool.isRequired, dirent: PropTypes.object, children: PropTypes.object }; class ViewFileToolbar extends React.Component { constructor(props) { super(props); this.state = { isDropdownMenuOpen: false, isMoreMenuShow: false, isShareDialogShow: false, isEditTagDialogShow: false, }; } toggleDropdownMenu = () => { this.setState({isDropdownMenuOpen: !this.state.isDropdownMenuOpen}); }; onDropdownToggleKeyDown = (e) => { if (e.key == 'Enter' || e.key == 'Space') { this.toggleDropdownMenu(); } }; onDropDownMouseMove = (e) => { if (this.state.isSubMenuShown && e.target && e.target.className === 'dropdown-item') { this.setState({ isSubMenuShown: false }); } }; toggleSubMenu = (e) => { e.stopPropagation(); this.setState({ isSubMenuShown: !this.state.isSubMenuShown}, () => { this.toggleDropdownMenu(); }); }; toggleSubMenuShown = (item) => { this.setState({ isSubMenuShown: true, currentItem: item.text }); }; onMenuItemKeyDown = (item, e) => { if (e.key == 'Enter' || e.key == 'Space') { item.onClick(); } }; onEditClick = (e) => { e.preventDefault(); let { path, repoID } = this.props; let url = siteRoot + 'lib/' + repoID + '/file' + Utils.encodePath(path) + '?mode=edit'; window.open(url); }; toggleMore = () => { this.setState({isMoreMenuShow: !this.state.isMoreMenuShow}); }; onShareToggle = () => { this.setState({isShareDialogShow: !this.state.isShareDialogShow}); }; onEditFileTagToggle = () => { this.setState({isEditTagDialogShow: !this.state.isEditTagDialogShow}); }; onHistoryClick = () => { let historyUrl = siteRoot + 'repo/file_revisions/' + this.props.repoID + '/?p=' + Utils.encodePath(this.props.path); location.href = historyUrl; }; render() { const { filePermission, showShareBtn } = this.props; let opList = []; if (filePermission === 'rw' || filePermission === 'cloud-edit') { opList.push({ 'icon': 'rename', 'text': gettext('Edit'), 'onClick': this.onEditClick }); } if (filePermission === 'rw') { /* let newSubOpList = []; if (showShareBtn) { newSubOpList.push({ 'text': gettext('Share'), 'onClick': this.onShareToggle }); } newSubOpList.push( {'text': gettext('Tags'), 'onClick': this.onEditFileTagToggle}, {'text': gettext('History'), 'onClick': this.onHistoryClick} ); opList.push({ 'icon': 'more-vertical', 'text': gettext('More'), 'subOpList': newSubOpList }); */ if (showShareBtn) { opList.push({ 'icon': 'share', 'text': gettext('Share'), 'onClick': this.onShareToggle }); } opList.push( {'icon': 'tag', 'text': gettext('Tags'), 'onClick': this.onEditFileTagToggle}, {'icon': 'history', 'text': gettext('History'), 'onClick': this.onHistoryClick} ); } return ( {opList.length > 0 && {this.props.children} {opList.map((item, index)=> { if (item == 'Divider') { return ; } else if (item.subOpList) { return ( {e.stopPropagation();}} > {item.text} {item.subOpList.map((item, index)=> { if (item == 'Divider') { return ; } else { return ({item.text}); } })} ); } else { return ( {item.text} ); } })} } {this.state.isShareDialogShow && ( )} {this.state.isEditTagDialogShow && ( )} ); } } ViewFileToolbar.propTypes = propTypes; export default ViewFileToolbar;