PagePath.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React from 'react';
  2. import CopyButton from '../CopyButton';
  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. 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. return (
  55. <span className="page-path">
  56. <span className="separator">
  57. <a href="/">/</a>
  58. </span>
  59. {afterElements}
  60. <CopyButton buttonClassName="btn btn-default" text={this.props.pagePath} iconClassName="fa fa-clone text-muted" />
  61. </span>
  62. );
  63. }
  64. }
  65. PagePath.propTypes = {
  66. pagePath: React.PropTypes.string.isRequired,
  67. };