PagePath.js 2.0 KB

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