PagePath.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React from 'react';
  2. import ClipboardButton from 'react-clipboard.js';
  3. export default class PagePath 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. render() {
  33. const pageLength = this.state.pages.length;
  34. const afterElements = [];
  35. this.state.pages.forEach((page, index) => {
  36. const isLastElement = (index == pageLength-1);
  37. // add elements
  38. afterElements.push(
  39. <span key={page.pagePath} className="path-segment">
  40. <a href={page.pagePath}>{page.pageName}</a>
  41. </span>);
  42. afterElements.push(
  43. <span key={page.pagePath+'/'} className="separator">
  44. <a href={page.pagePath+'/'} className={(isLastElement && !this.state.isListPage) ? 'last-path' : ''}>/</a>
  45. </span>
  46. );
  47. });
  48. return (
  49. <span className="page-path">
  50. <span className="separator">
  51. <a href="/">/</a>
  52. </span>
  53. {afterElements}
  54. <ClipboardButton className="btn btn-default" data-clipboard-text={this.props.pagePath}>
  55. <i className="fa fa-clone"></i>
  56. </ClipboardButton>
  57. </span>
  58. );
  59. }
  60. }
  61. PagePath.propTypes = {
  62. pagePath: React.PropTypes.string.isRequired,
  63. };