1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-14 13:13:22 +00:00
seahub/frontend/src/components/file-view/file-toolbar.js

297 lines
10 KiB
JavaScript
Raw Normal View History

2019-03-14 04:00:02 +00:00
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { ButtonGroup, ButtonDropdown, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
import IconButton from '../icon-button';
import { gettext, siteRoot } from '../../utils/constants';
import { Utils } from '../../utils/utils';
2019-03-14 04:00:02 +00:00
import ModalPortal from '../modal-portal';
import ShareDialog from '../dialog/share-dialog';
import { seafileAPI } from '../../utils/seafile-api';
import toaster from '../toast';
const propTypes = {
isLocked: PropTypes.bool.isRequired,
lockedByMe: PropTypes.bool.isRequired,
2019-04-22 03:31:39 +00:00
onSave: PropTypes.func,
2019-04-19 13:32:54 +00:00
isSaving: PropTypes.bool,
2019-04-22 03:31:39 +00:00
needSave: PropTypes.bool,
toggleLockFile: PropTypes.func.isRequired,
toggleCommentPanel: PropTypes.func.isRequired,
toggleDetailsPanel: PropTypes.func.isRequired
};
const {
canLockUnlockFile,
2019-03-14 04:00:02 +00:00
repoID, repoName, repoEncrypted, parentDir, filePerm, filePath,
fileType,
2019-03-14 04:00:02 +00:00
fileName,
canEditFile, err,
fileEnc, // for 'edit', not undefined only for some kinds of files (e.g. text file)
canDownloadFile, enableComment
} = window.app.pageOptions;
class FileToolbar extends React.Component {
2019-03-14 04:00:02 +00:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
2019-04-19 14:40:47 +00:00
dropdownOpen: false,
moreDropdownOpen: false,
isShareDialogOpen: false,
2019-03-14 04:00:02 +00:00
};
}
async componentDidMount() {
if (filePerm && filePerm.startsWith('custom-')) {
this.isCustomPermission = true;
const permissionID = filePerm.split('-')[1];
try {
const permissionRes = await seafileAPI.getCustomPermission(repoID, permissionID);
this.customPermission = permissionRes.data.permission;
// share dialog need a global custom_permission
window.custom_permission = this.customPermission;
this.setState({isLoading: false});
} catch(error) {
let errorMsg = Utils.getErrorMsg(error);
toaster.danger(errorMsg);
this.setState({isLoading: false});
}
} else {
this.setState({isLoading: false});
}
}
2019-03-14 04:00:02 +00:00
toggleShareDialog = () => {
this.setState({isShareDialogOpen: !this.state.isShareDialogOpen});
}
toggleMoreOpMenu = () => {
this.setState({
moreDropdownOpen: !this.state.moreDropdownOpen
});
}
2019-04-19 14:44:41 +00:00
toggle = () => {
this.setState({
dropdownOpen: !this.state.dropdownOpen
});
}
2019-04-19 14:40:47 +00:00
render() {
if (this.state.isLoading) {
return null;
}
const { isLocked, lockedByMe } = this.props;
const { moreDropdownOpen } = this.state;
let showLockUnlockBtn = false;
let lockUnlockText, lockUnlockIcon;
if (canLockUnlockFile) {
if (!isLocked) {
showLockUnlockBtn = true;
lockUnlockText = gettext('Lock');
lockUnlockIcon = 'fa fa-lock';
} else if (lockedByMe) {
showLockUnlockBtn = true;
lockUnlockText = gettext('Unlock');
lockUnlockIcon = 'fa fa-unlock';
}
}
2019-03-14 04:00:02 +00:00
let showShareBtn = false;
if (repoEncrypted) {
showShareBtn = true; // for internal link
} else if (filePerm == 'rw' || filePerm == 'r') {
2019-03-14 04:00:02 +00:00
showShareBtn = true;
}
let canComment = enableComment;
const { isCustomPermission, customPermission } = this;
if (isCustomPermission) {
const { download_external_link } = customPermission.permission;
showShareBtn = download_external_link;
canComment = false;
}
return (
2019-03-14 04:00:02 +00:00
<Fragment>
2019-04-19 14:40:47 +00:00
<ButtonGroup className="d-none d-md-block">
{fileType == 'PDF' && (
<IconButton
id="seafile-pdf-print"
icon="fa fa-print"
text={gettext('Print')}
/>
)}
<IconButton
2019-03-14 04:00:02 +00:00
id="open-parent-folder"
icon="fa fa-folder-open"
text={gettext('Open parent folder')}
tag="a"
2019-03-14 04:00:02 +00:00
href={`${siteRoot}library/${repoID}/${Utils.encodePath(repoName + parentDir)}`}
/>
2019-03-14 04:00:02 +00:00
{showLockUnlockBtn && (
<IconButton
id="lock-unlock-file"
icon={lockUnlockIcon}
text={lockUnlockText}
onClick={this.props.toggleLockFile}
/>
)}
{showShareBtn && (
<IconButton
id="share-file"
icon='fa fa-share-alt'
text={gettext('Share')}
onClick={this.toggleShareDialog}
/>
)}
{(canEditFile && fileType != 'SDoc' && !err) &&
( this.props.isSaving ?
<button type={'button'} aria-label={gettext('Saving...')} className={'btn btn-icon btn-secondary btn-active'}>
2019-04-19 14:32:19 +00:00
<i className={'fa fa-spin fa-spinner'}/></button> :
(
2019-04-22 03:31:39 +00:00
this.props.needSave ?
2019-04-19 14:32:19 +00:00
<IconButton
text={gettext('Save')}
id={'saveButton'}
icon={'fa fa-save'}
// button imported in this file does not have functionalities of
// isActive as button imported in markdowneditor has
//isActive={!isContentChanged}
2019-04-22 03:31:39 +00:00
onClick={this.props.onSave}
2019-04-19 14:32:19 +00:00
/> :
<button type={'button'} className={'btn btn-icon btn-secondary btn-active'} disabled>
<i className={'fa fa-save'}/></button>
)
)}
2019-03-14 04:00:02 +00:00
{canDownloadFile && (
<IconButton
id="download-file"
icon="fa fa-download"
text={gettext('Download')}
tag="a"
href="?dl=1"
/>
)}
<IconButton
id="file-details"
icon='fas fa-info'
text={gettext('Details')}
onClick={this.props.toggleDetailsPanel}
/>
{filePerm == 'rw' && (
2019-03-14 04:00:02 +00:00
<IconButton
id="open-via-client"
icon="sf3-font sf3-font-desktop"
text={gettext('Open via Client')}
tag="a"
href={`seafile://openfile?repo_id=${encodeURIComponent(repoID)}&path=${encodeURIComponent(filePath)}`}
2019-03-14 04:00:02 +00:00
/>
)}
<ButtonDropdown isOpen={moreDropdownOpen} toggle={this.toggleMoreOpMenu}>
<DropdownToggle aria-label={gettext('More Operations')}>
<span className="fas fa-ellipsis-v"></span>
</DropdownToggle>
<DropdownMenu right={true}>
{canComment && (
<DropdownItem onClick={this.props.toggleCommentPanel}>
{gettext('Comment')}
</DropdownItem>
)}
{filePerm == 'rw' && (
<a href={`${siteRoot}repo/file_revisions/${repoID}/?p=${encodeURIComponent(filePath)}&referer=${encodeURIComponent(location.href)}`} className="dropdown-item">
{gettext('History')}
</a>
)}
</DropdownMenu>
</ButtonDropdown>
2019-03-14 04:00:02 +00:00
</ButtonGroup>
2019-04-19 14:44:41 +00:00
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle} className="d-block d-md-none">
2019-04-22 04:12:43 +00:00
<ButtonGroup >
{(canEditFile && fileType != 'SDoc' && !err) &&
(this.props.isSaving ?
<button type={'button'} aria-label={gettext('Saving...')} className={'btn btn-icon btn-secondary btn-active'}>
<i className={'fa fa-spin fa-spinner'}/></button> :
(
2019-04-22 03:31:39 +00:00
this.props.needSave ?
<IconButton
text={gettext('Save')}
id={'saveButton'}
icon={'fa fa-save'}
// button imported in this file does not have functionalities of
// isActive as button imported in markdowneditor has
//isActive={!isContentChanged}
2019-04-22 03:31:39 +00:00
onClick={this.props.onSave}
/> :
<button type={'button'} className={'btn btn-icon btn-secondary btn-active'} disabled>
<i className={'fa fa-save'}/></button>
)
)}
</ButtonGroup>
2019-04-22 04:12:43 +00:00
<DropdownToggle className="sf2-icon-more mx-1" aria-label={gettext('More Operations')}></DropdownToggle>
2019-04-19 14:44:41 +00:00
<DropdownMenu right={true}>
<DropdownItem>
<a href={`${siteRoot}library/${repoID}/${Utils.encodePath(repoName + parentDir)}`} className="text-inherit">
{gettext('Open parent folder')}
</a>
</DropdownItem>
{showLockUnlockBtn && (
<DropdownItem onClick={this.props.toggleLockFile}>
{lockUnlockText}
</DropdownItem>
)}
{showShareBtn && (
<DropdownItem onClick={this.toggleShareDialog}>
{gettext('Share')}
</DropdownItem>
)}
{filePerm == 'rw' && (
<DropdownItem>
<a href={`${siteRoot}repo/file_revisions/${repoID}/?p=${encodeURIComponent(filePath)}&referer=${encodeURIComponent(location.href)}`} className="text-inherit">
{gettext('History')}
</a>
</DropdownItem>
)}
{canDownloadFile && (
<DropdownItem>
<a href="?dl=1" className="text-inherit">
{gettext('Download')}
</a>
</DropdownItem>
)}
{canComment && (
2019-04-19 14:44:41 +00:00
<DropdownItem onClick={this.props.toggleCommentPanel}>
{gettext('Comment')}
</DropdownItem>
)}
<DropdownItem onClick={this.props.toggleDetailsPanel}>{gettext('Details')}</DropdownItem>
2019-04-19 14:44:41 +00:00
</DropdownMenu>
</Dropdown>
2019-04-19 14:40:47 +00:00
{this.state.isShareDialogOpen && (
<ModalPortal>
<ShareDialog
itemType='file'
itemName={fileName}
itemPath={filePath}
userPerm={filePerm}
repoID={repoID}
repoEncrypted={repoEncrypted}
toggleDialog={this.toggleShareDialog}
/>
</ModalPortal>
)}
2019-03-14 04:00:02 +00:00
</Fragment>
);
}
}
FileToolbar.propTypes = propTypes;
export default FileToolbar;