PagePath.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. 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. <ClipboardButton className="btn btn-default"
  61. button-id="btnCopy" button-data-toggle="tooltip" button-title="copied!" button-data-placement="bottom" button-data-trigger="manual"
  62. data-clipboard-text={this.props.pagePath} onSuccess={this.showToolTip}>
  63. <i className="fa fa-clone text-muted"></i>
  64. </ClipboardButton>
  65. </span>
  66. );
  67. }
  68. }
  69. PagePath.propTypes = {
  70. pagePath: React.PropTypes.string.isRequired,
  71. };