1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-14 15:35:35 +00:00
seahub/frontend/src/components/cur-dir-path/dir-path.js

89 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-12-11 09:52:19 +00:00
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
2018-12-12 09:51:12 +00:00
import { Link } from '@reach/router';
import { siteRoot, gettext } from '../../utils/constants';
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,
};
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);
}
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>
)}
{currentPath === '/' ?
<span>{repoName}</span>:
<a className="path-link" data-path="/" onClick={this.onPathClick}>{repoName}</a>
}
{pathElem}
</div>
);
}
}
DirPath.propTypes = propTypes;
export default DirPath;