2019-03-15 02:10:24 +00:00
import React , { Fragment } from 'react' ;
import PropTypes from 'prop-types' ;
2019-04-22 02:29:41 +00:00
import { DropdownToggle , Dropdown , DropdownMenu , DropdownItem , Tooltip } from 'reactstrap' ;
2019-03-15 02:10:24 +00:00
import { Utils } from '../../utils/utils' ;
2019-04-25 07:09:28 +00:00
import { gettext , siteRoot , canGenerateShareLink } from '../../utils/constants' ;
2019-03-15 02:10:24 +00:00
import { seafileAPI } from '../../utils/seafile-api' ;
import ModalPotal from '../modal-portal' ;
import ShareDialog from '../dialog/share-dialog' ;
import EditFileTagDialog from '../dialog/edit-filetag-dialog' ;
2019-03-29 02:36:34 +00:00
import RelatedFileDialogs from '../dialog/related-file-dialogs' ;
2019-03-15 02:10:24 +00:00
const propTypes = {
path : PropTypes . string . isRequired ,
repoID : PropTypes . string . isRequired ,
userPerm : PropTypes . string . isRequired ,
repoEncrypted : PropTypes . bool . isRequired ,
enableDirPrivateShare : PropTypes . bool . isRequired ,
isGroupOwnedRepo : PropTypes . bool . isRequired ,
2019-04-19 07:50:27 +00:00
filePermission : PropTypes . string ,
2019-03-15 02:10:24 +00:00
isDraft : PropTypes . bool . isRequired ,
hasDraft : PropTypes . bool . isRequired ,
fileTags : PropTypes . array . isRequired ,
relatedFiles : PropTypes . array . isRequired ,
onFileTagChanged : PropTypes . func . isRequired ,
onRelatedFileChange : PropTypes . func . isRequired ,
} ;
class ViewFileToolbar extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
isDraftMessageShow : false ,
2019-04-22 02:29:41 +00:00
isMoreMenuShow : false ,
2019-03-15 02:10:24 +00:00
isShareDialogShow : false ,
isEditTagDialogShow : false ,
2019-03-28 09:34:01 +00:00
isRelatedFileDialogShow : false ,
2019-03-29 02:36:34 +00:00
showRelatedFileDialog : false ,
2019-03-15 02:10:24 +00:00
} ;
}
onEditClick = ( e ) => {
e . preventDefault ( ) ;
let { path , repoID } = this . props ;
let url = siteRoot + 'lib/' + repoID + '/file' + Utils . encodePath ( path ) + '?mode=edit' ;
window . open ( url ) ;
}
onNewDraft = ( e ) => {
e . preventDefault ( ) ;
let { path , repoID } = this . props ;
seafileAPI . createDraft ( repoID , path ) . then ( res => {
window . location . href = siteRoot + 'lib/' + res . data . origin _repo _id + '/file' + res . data . draft _file _path + '?mode=edit' ;
} ) ;
}
onDraftHover = ( ) => {
this . setState ( { isDraftMessageShow : ! this . state . isDraftMessageShow } ) ;
}
2019-04-22 02:29:41 +00:00
toggleMore = ( ) => {
this . setState ( { isMoreMenuShow : ! this . state . isMoreMenuShow } ) ;
2019-03-15 02:10:24 +00:00
}
onShareToggle = ( ) => {
this . setState ( { isShareDialogShow : ! this . state . isShareDialogShow } ) ;
}
onEditFileTagToggle = ( ) => {
this . setState ( { isEditTagDialogShow : ! this . state . isEditTagDialogShow } ) ;
}
onListRelatedFileToggle = ( ) => {
2019-03-28 09:34:01 +00:00
this . setState ( {
isRelatedFileDialogShow : true ,
2019-03-29 02:36:34 +00:00
showRelatedFileDialog : true ,
2019-03-28 09:34:01 +00:00
} ) ;
}
toggleCancel = ( ) => {
this . setState ( {
isRelatedFileDialogShow : false ,
2019-03-29 02:36:34 +00:00
showRelatedFileDialog : false ,
2019-04-08 05:51:34 +00:00
} ) ;
2019-03-28 09:34:01 +00:00
}
2019-03-15 02:10:24 +00:00
render ( ) {
2019-04-19 07:50:27 +00:00
let { filePermission } = this . props ;
2019-03-15 02:10:24 +00:00
let name = Utils . getFileName ( this . props . path ) ;
let dirent = { name : name } ;
return (
< Fragment >
< div className = "dir-operation" >
2019-04-19 07:50:27 +00:00
{ ( ( filePermission === 'rw' || filePermission === 'cloud-edit' ) && ! this . props . hasDraft ) && (
2019-03-15 02:10:24 +00:00
< Fragment >
< button className = "btn btn-secondary operation-item" title = { gettext ( 'Edit File' ) } onClick = { this . onEditClick } > { gettext ( 'Edit' ) } < / b u t t o n >
< / F r a g m e n t >
) }
2019-04-19 07:50:27 +00:00
{ ( filePermission === 'rw' && ! this . props . isDraft && ! this . props . hasDraft ) && (
2019-03-15 02:10:24 +00:00
< Fragment >
< button id = "new-draft" className = "btn btn-secondary operation-item" onClick = { this . onNewDraft } > { gettext ( 'New Draft' ) } < / b u t t o n >
< Tooltip target = "new-draft" placement = "bottom" isOpen = { this . state . isDraftMessageShow } toggle = { this . onDraftHover } > { gettext ( 'Create a draft from this file, instead of editing it directly.' ) } < / T o o l t i p >
< / F r a g m e n t >
) }
2019-04-19 07:50:27 +00:00
{ filePermission === 'rw' && (
2019-04-22 02:29:41 +00:00
< Dropdown isOpen = { this . state . isMoreMenuShow } toggle = { this . toggleMore } >
< DropdownToggle className = 'btn btn-secondary operation-item' >
{ gettext ( 'More' ) }
< / D r o p d o w n T o g g l e >
< DropdownMenu >
2019-04-25 07:09:28 +00:00
{ canGenerateShareLink &&
< DropdownItem onClick = { this . onShareToggle } > { gettext ( 'Share' ) } < / D r o p d o w n I t e m >
}
2019-04-22 02:29:41 +00:00
< DropdownItem onClick = { this . onEditFileTagToggle } > { gettext ( 'Tags' ) } < / D r o p d o w n I t e m >
< DropdownItem onClick = { this . onListRelatedFileToggle } > { gettext ( 'Related Files' ) } < / D r o p d o w n I t e m >
< / D r o p d o w n M e n u >
< / D r o p d o w n >
2019-03-15 02:10:24 +00:00
) }
< / d i v >
{ this . state . isShareDialogShow && (
< ModalPotal >
< ShareDialog
itemType = { 'file' }
itemName = { Utils . getFileName ( this . props . path ) }
itemPath = { this . props . path }
repoID = { this . props . repoID }
repoEncrypted = { this . props . repoEncrypted }
enableDirPrivateShare = { this . props . enableDirPrivateShare }
userPerm = { this . props . userPerm }
isGroupOwnedRepo = { this . props . isGroupOwnedRepo }
toggleDialog = { this . onShareToggle }
/ >
< / M o d a l P o t a l >
) }
{ this . state . isEditTagDialogShow && (
< ModalPotal >
< EditFileTagDialog
filePath = { this . props . path }
repoID = { this . props . repoID }
fileTagList = { this . props . fileTags }
toggleCancel = { this . onEditFileTagToggle }
onFileTagChanged = { this . props . onFileTagChanged }
/ >
< / M o d a l P o t a l >
) }
2019-03-29 02:36:34 +00:00
{ this . state . showRelatedFileDialog &&
2019-03-15 02:10:24 +00:00
< ModalPotal >
2019-03-29 02:36:34 +00:00
< RelatedFileDialogs
repoID = { this . props . repoID }
filePath = { this . props . path }
relatedFiles = { this . props . relatedFiles }
toggleCancel = { this . toggleCancel }
onRelatedFileChange = { this . props . onRelatedFileChange }
dirent = { dirent }
viewMode = "list_related_file"
/ >
2019-03-15 02:10:24 +00:00
< / M o d a l P o t a l >
2019-03-29 02:36:34 +00:00
}
2019-03-15 02:10:24 +00:00
< / F r a g m e n t >
) ;
}
}
ViewFileToolbar . propTypes = propTypes ;
2019-04-22 02:29:41 +00:00
export default ViewFileToolbar ;