2018-12-11 09:52:19 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-11-27 06:47:19 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-12 09:51:12 +00:00
|
|
|
import { Link } from '@reach/router';
|
2018-11-27 06:47:19 +00:00
|
|
|
import { siteRoot, gettext } from '../../utils/constants';
|
2018-12-14 04:19:24 +00:00
|
|
|
import InternalLinkDialog from '../dialog/internal-link-dialog';
|
2018-11-27 06:47:19 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
repoName: PropTypes.string.isRequired,
|
|
|
|
currentPath: PropTypes.string.isRequired,
|
|
|
|
onPathClick: PropTypes.func.isRequired,
|
2018-12-13 06:40:09 +00:00
|
|
|
onTabNavClick: PropTypes.func,
|
|
|
|
pathPrefix: PropTypes.array,
|
2018-12-14 04:19:24 +00:00
|
|
|
repoID: PropTypes.string.isRequired,
|
|
|
|
isViewFile: PropTypes.bool.isRequired,
|
2018-11-27 06:47:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirPath extends React.Component {
|
|
|
|
|
|
|
|
onPathClick = (e) => {
|
|
|
|
let path = e.target.dataset.path;
|
|
|
|
this.props.onPathClick(path);
|
|
|
|
}
|
|
|
|
|
2018-12-13 06:40:09 +00:00
|
|
|
onTabNavClick = (tabName, id) => {
|
|
|
|
this.props.onTabNavClick(tabName, id);
|
|
|
|
}
|
|
|
|
|
2018-11-27 06:47:19 +00:00
|
|
|
turnPathToLink = (path) => {
|
|
|
|
path = path[path.length - 1] === '/' ? path.slice(0, path.length - 1) : path;
|
|
|
|
let pathList = path.split('/');
|
|
|
|
let nodePath = '';
|
|
|
|
let pathElem = pathList.map((item, index) => {
|
|
|
|
if (item === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (index === (pathList.length - 1)) {
|
|
|
|
return (
|
|
|
|
<span key={index}><span className="path-split">/</span>{item}</span>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
nodePath += '/' + item;
|
|
|
|
return (
|
|
|
|
<span key={index} >
|
|
|
|
<span className="path-split">/</span>
|
|
|
|
<a className="path-link" data-path={nodePath} onClick={this.onPathClick}>{item}</a>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pathElem;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let { currentPath, repoName } = this.props;
|
|
|
|
let pathElem = this.turnPathToLink(currentPath);
|
|
|
|
return (
|
2018-12-13 14:11:08 +00:00
|
|
|
<div className="path-container">
|
2018-12-13 06:40:09 +00:00
|
|
|
{this.props.pathPrefix && this.props.pathPrefix.map((item, index) => {
|
|
|
|
return (
|
|
|
|
<Fragment key={index}>
|
|
|
|
<Link to={item.url} className="normal" onClick={() => this.onTabNavClick(item.name, item.id)}>{gettext(item.showName)}</Link>
|
2018-12-11 09:52:19 +00:00
|
|
|
<span className="path-split">/</span>
|
2018-12-13 06:40:09 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{this.props.pathPrefix && this.props.pathPrefix.length === 0 && (
|
|
|
|
<Fragment>
|
|
|
|
<Link to={siteRoot + 'my-libs/'} className="normal" onClick={() => this.onTabNavClick.bind(this, 'my-libs')}>{gettext('Libraries')}</Link>
|
|
|
|
<span className="path-split">/</span>
|
|
|
|
</Fragment>
|
|
|
|
)
|
2018-12-11 09:52:19 +00:00
|
|
|
}
|
2018-12-13 06:40:09 +00:00
|
|
|
{!this.props.pathPrefix && (
|
|
|
|
<Fragment>
|
|
|
|
<a href={siteRoot + 'my-libs/'} className="normal" onClick={() => this.onTabNavClick.bind(this, 'my-libs')}>{gettext('Libraries')}</a>
|
|
|
|
<span className="path-split">/</span>
|
|
|
|
</Fragment>
|
|
|
|
)}
|
2018-11-27 06:47:19 +00:00
|
|
|
{currentPath === '/' ?
|
|
|
|
<span>{repoName}</span>:
|
|
|
|
<a className="path-link" data-path="/" onClick={this.onPathClick}>{repoName}</a>
|
|
|
|
}
|
|
|
|
{pathElem}
|
2018-12-14 04:19:24 +00:00
|
|
|
{ this.props.isViewFile &&
|
2019-01-31 09:37:02 +00:00
|
|
|
<InternalLinkDialog
|
|
|
|
repoID={this.props.repoID}
|
|
|
|
path={this.props.currentPath}
|
2018-12-14 04:19:24 +00:00
|
|
|
/>
|
|
|
|
}
|
2018-11-27 06:47:19 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirPath.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default DirPath;
|