RevisionPath.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // define style
  40. const rootStyle = {
  41. marginRight: "0.2em",
  42. }
  43. const separatorStyle = {
  44. marginLeft: "0.2em",
  45. marginRight: "0.2em",
  46. }
  47. const pageLength = this.state.pages.length;
  48. const afterElements = [];
  49. this.state.pages.forEach((page, index) => {
  50. const isLastElement = (index == pageLength-1);
  51. // add elements
  52. afterElements.push(
  53. <span key={page.pagePath} className="path-segment">
  54. <a href={page.pagePath}>{page.pageName}</a>
  55. </span>);
  56. afterElements.push(
  57. <span key={page.pagePath+'/'} className="separator" style={separatorStyle}>
  58. <a href={page.pagePath+'/'} className={(isLastElement && !this.state.isListPage) ? 'last-path' : ''}>/</a>
  59. </span>
  60. );
  61. });
  62. return (
  63. <span>
  64. <span className="separator" style={rootStyle}>
  65. <a href="/">/</a>
  66. </span>
  67. {afterElements}
  68. <CopyButton buttonId="btnCopyRevisionPath" text={this.props.pagePath}
  69. buttonClassName="btn btn-default" iconClassName="fa fa-clone text-muted" />
  70. </span>
  71. );
  72. }
  73. }
  74. RevisionPath.propTypes = {
  75. pagePath: React.PropTypes.string.isRequired,
  76. };