RevisionPath.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import CopyButton from '../CopyButton';
  4. export default class RevisionPath extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. pages: [],
  9. isListPage: false,
  10. isLinkToListPage: true,
  11. };
  12. }
  13. componentWillMount() {
  14. // whether list page or not
  15. const isListPage = this.props.pagePath.match(/\/$/);
  16. this.setState({ isListPage });
  17. // whether set link to '/'
  18. const behaviorType = this.props.crowi.getConfig()['behaviorType'];
  19. const isLinkToListPage = ('crowi-plus' !== behaviorType);
  20. this.setState({ isLinkToListPage });
  21. // generate pages obj
  22. let splitted = this.props.pagePath.split(/\//);
  23. splitted.shift(); // omit first element with shift()
  24. if (splitted[splitted.length-1] === '') {
  25. splitted.pop(); // omit last element with unshift()
  26. }
  27. let pages = [];
  28. let parentPath = '/';
  29. splitted.forEach((pageName) => {
  30. pages.push({
  31. pagePath: parentPath + pageName,
  32. pageName: pageName,
  33. });
  34. parentPath += pageName + '/';
  35. });
  36. this.setState({ pages });
  37. }
  38. showToolTip() {
  39. $('#btnCopy').tooltip('show');
  40. setTimeout(() => {
  41. $('#btnCopy').tooltip('hide');
  42. }, 1000);
  43. }
  44. generateLinkElementToListPage(pagePath, isLinkToListPage, isLastElement) {
  45. if (isLinkToListPage) {
  46. return <a href={pagePath+'/'} className={(isLastElement && !this.state.isListPage) ? 'last-path' : ''}>/</a>;
  47. }
  48. else if (!isLastElement) {
  49. return <span className="text-primary">/</span>;
  50. }
  51. else {
  52. return <span></span>
  53. }
  54. }
  55. render() {
  56. // define style
  57. const rootStyle = {
  58. marginRight: "0.2em",
  59. }
  60. const separatorStyle = {
  61. marginLeft: "0.2em",
  62. marginRight: "0.2em",
  63. }
  64. const pageLength = this.state.pages.length;
  65. const afterElements = [];
  66. this.state.pages.forEach((page, index) => {
  67. const isLastElement = (index == pageLength-1);
  68. // add elements for page
  69. afterElements.push(
  70. <span key={page.pagePath} className="path-segment">
  71. <a href={page.pagePath}>{page.pageName}</a>
  72. </span>);
  73. // add elements for '/'
  74. afterElements.push(
  75. <span key={page.pagePath+'/'} className="separator" style={separatorStyle}>
  76. {this.generateLinkElementToListPage(page.pagePath, this.state.isLinkToListPage, isLastElement)}
  77. </span>
  78. );
  79. });
  80. return (
  81. <span>
  82. <span className="separator" style={rootStyle}>
  83. <a href="/">/</a>
  84. </span>
  85. {afterElements}
  86. <CopyButton buttonId="btnCopyRevisionPath" text={this.props.pagePath}
  87. buttonClassName="btn btn-default" iconClassName="fa fa-clone text-muted" />
  88. </span>
  89. );
  90. }
  91. }
  92. RevisionPath.propTypes = {
  93. pagePath: PropTypes.string.isRequired,
  94. crowi: PropTypes.object.isRequired,
  95. };