RevisionPath.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 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. fontSize: "0.6em",
  66. marginLeft: "0.5em",
  67. padding: "0 2px",
  68. border: 'none',
  69. };
  70. const pageLength = this.state.pages.length;
  71. const afterElements = [];
  72. this.state.pages.forEach((page, index) => {
  73. const isLastElement = (index == pageLength-1);
  74. // add elements for page
  75. afterElements.push(
  76. <span key={page.pagePath} className="path-segment">
  77. <a href={page.pagePath}>{page.pageName}</a>
  78. </span>);
  79. // add elements for '/'
  80. afterElements.push(
  81. <span key={page.pagePath+'/'} className="separator" style={separatorStyle}>
  82. {this.generateLinkElementToListPage(page.pagePath, this.state.isLinkToListPage, isLastElement)}
  83. </span>
  84. );
  85. });
  86. return (
  87. <span>
  88. <span className="separator" style={rootStyle}>
  89. <a href="/">/</a>
  90. </span>
  91. {afterElements}
  92. <CopyButton buttonId="btnCopyRevisionPath" text={this.props.pagePath}
  93. buttonClassName="btn btn-default btn-muted" iconClassName="fa fa-clone text-muted" />
  94. <a href="#edit-form" className="btn btn-default btn-muted" style={editButtonStyle}>
  95. <i className="fa fa-edit text-muted"></i>
  96. </a>
  97. </span>
  98. );
  99. }
  100. }
  101. RevisionPath.propTypes = {
  102. pagePath: PropTypes.string.isRequired,
  103. crowi: PropTypes.object.isRequired,
  104. };