RevisionPath.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React from 'react';
  2. import CopyButton from '../CopyButton';
  3. export default class RevisionPath extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. pages: [],
  8. isListPage: false,
  9. };
  10. }
  11. componentWillMount() {
  12. // whether list page or not
  13. const isListPage = this.props.pagePath.match(/\/$/);
  14. this.setState({ isListPage });
  15. // generate pages obj
  16. let splitted = this.props.pagePath.split(/\//);
  17. splitted.shift(); // omit first element with shift()
  18. if (splitted[splitted.length-1] === '') {
  19. splitted.pop(); // omit last element with unshift()
  20. }
  21. let pages = [];
  22. let parentPath = '/';
  23. splitted.forEach((pageName) => {
  24. pages.push({
  25. pagePath: parentPath + pageName,
  26. pageName: pageName,
  27. });
  28. parentPath += pageName + '/';
  29. });
  30. this.setState({ pages });
  31. }
  32. showToolTip() {
  33. $('#btnCopy').tooltip('show');
  34. setTimeout(() => {
  35. $('#btnCopy').tooltip('hide');
  36. }, 1000);
  37. }
  38. render() {
  39. const pageLength = this.state.pages.length;
  40. const afterElements = [];
  41. this.state.pages.forEach((page, index) => {
  42. const isLastElement = (index == pageLength-1);
  43. // add elements
  44. afterElements.push(
  45. <span key={page.pagePath} className="path-segment">
  46. <a href={page.pagePath}>{page.pageName}</a>
  47. </span>);
  48. afterElements.push(
  49. <span key={page.pagePath+'/'} className="separator">
  50. <a href={page.pagePath+'/'} className={(isLastElement && !this.state.isListPage) ? 'last-path' : ''}>/</a>
  51. </span>
  52. );
  53. });
  54. const buttonStyle = {
  55. fontSize: '1.5rem'
  56. }
  57. return (
  58. <span className="page-path">
  59. <span className="separator">
  60. <a href="/">/</a>
  61. </span>
  62. {afterElements}
  63. <CopyButton buttonId="btnCopyRevisionPath" text={this.props.pagePath}
  64. buttonClassName="btn btn-default" buttonStyle={buttonStyle} iconClassName="fa fa-clone text-muted" />
  65. </span>
  66. );
  67. }
  68. }
  69. RevisionPath.propTypes = {
  70. pagePath: React.PropTypes.string.isRequired,
  71. };