RevisionPath.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = (!behaviorType || 'crowi' === 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 + encodeURIComponent(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>/</span>;
  50. }
  51. else {
  52. return <span></span>
  53. }
  54. }
  55. render() {
  56. // define styles
  57. const rootStyle = {
  58. marginRight: "0.2em",
  59. }
  60. const separatorStyle = {
  61. marginLeft: "0.2em",
  62. marginRight: "0.2em",
  63. }
  64. const editButtonStyle = {
  65. marginLeft: "0.5em",
  66. padding: "0 2px",
  67. };
  68. const pageLength = this.state.pages.length;
  69. const afterElements = [];
  70. this.state.pages.forEach((page, index) => {
  71. const isLastElement = (index == pageLength-1);
  72. // add elements for page
  73. afterElements.push(
  74. <span key={page.pagePath} className="path-segment">
  75. <a href={page.pagePath}>{page.pageName}</a>
  76. </span>
  77. );
  78. // add elements for '/'
  79. afterElements.push(
  80. <span key={page.pagePath+'/'} className="separator" style={separatorStyle}>
  81. {this.generateLinkElementToListPage(page.pagePath, this.state.isLinkToListPage, isLastElement)}
  82. </span>
  83. );
  84. });
  85. return (
  86. <span className="d-flex align-items-center">
  87. <span className="separator" style={rootStyle}>
  88. <a href="/">/</a>
  89. </span>
  90. {afterElements}
  91. <CopyButton buttonId="btnCopyRevisionPath" text={this.props.pagePath}
  92. buttonClassName="btn btn-default btn-copy" iconClassName="ti-clipboard" />
  93. <a href="#edit-form" className="btn btn-default btn-edit" style={editButtonStyle}>
  94. <i className="icon-note"></i>
  95. </a>
  96. </span>
  97. );
  98. }
  99. }
  100. RevisionPath.propTypes = {
  101. pagePath: PropTypes.string.isRequired,
  102. crowi: PropTypes.object.isRequired,
  103. };