RevisionPath.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Dropdown from 'react-bootstrap/es/Dropdown';
  4. import MenuItem from 'react-bootstrap/es/MenuItem';
  5. import Clipboard from 'react-clipboard.js';
  6. export default class RevisionPath extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. pages: [],
  11. isListPage: false,
  12. isLinkToListPage: true,
  13. };
  14. // retrieve xss library from window
  15. this.xss = window.xss;
  16. }
  17. componentWillMount() {
  18. // whether list page or not
  19. const isListPage = this.props.pagePath.match(/\/$/);
  20. this.setState({ isListPage });
  21. // whether set link to '/'
  22. const behaviorType = this.props.crowi.getConfig().behaviorType;
  23. const isLinkToListPage = (!behaviorType || behaviorType === 'crowi');
  24. this.setState({ isLinkToListPage });
  25. // generate pages obj
  26. const splitted = this.props.pagePath.split(/\//);
  27. splitted.shift(); // omit first element with shift()
  28. if (splitted[splitted.length - 1] === '') {
  29. splitted.pop(); // omit last element with unshift()
  30. }
  31. const pages = [];
  32. let parentPath = '/';
  33. splitted.forEach((pageName) => {
  34. pages.push({
  35. pagePath: parentPath + encodeURIComponent(pageName),
  36. pageName: this.xss.process(pageName),
  37. });
  38. parentPath += `${pageName}/`;
  39. });
  40. this.setState({ pages });
  41. }
  42. showToolTip() {
  43. const buttonId = '#copyPagePathDropdown';
  44. $(buttonId).tooltip('show');
  45. setTimeout(() => {
  46. $(buttonId).tooltip('hide');
  47. }, 1000);
  48. }
  49. generateLinkElementToListPage(pagePath, isLinkToListPage, isLastElement) {
  50. /* eslint-disable no-else-return */
  51. if (isLinkToListPage) {
  52. return <a href={`${pagePath}/`} className={(isLastElement && !this.state.isListPage) ? 'last-path' : ''}>/</a>;
  53. }
  54. else if (!isLastElement) {
  55. return <span>/</span>;
  56. }
  57. else {
  58. return <span></span>;
  59. }
  60. /* eslint-enable no-else-return */
  61. }
  62. render() {
  63. // define styles
  64. const rootStyle = {
  65. marginRight: '0.2em',
  66. };
  67. const separatorStyle = {
  68. marginLeft: '0.2em',
  69. marginRight: '0.2em',
  70. };
  71. const editButtonStyle = {
  72. marginLeft: '0.5em',
  73. padding: '0 2px',
  74. };
  75. const pageLength = this.state.pages.length;
  76. const afterElements = [];
  77. this.state.pages.forEach((page, index) => {
  78. const isLastElement = (index === pageLength - 1);
  79. // add elements for page
  80. afterElements.push(
  81. <span key={page.pagePath} className="path-segment">
  82. <a href={page.pagePath}>{page.pageName}</a>
  83. </span>,
  84. );
  85. // add elements for '/'
  86. afterElements.push(
  87. <span key={`${page.pagePath}/`} className="separator" style={separatorStyle}>
  88. {this.generateLinkElementToListPage(page.pagePath, this.state.isLinkToListPage, isLastElement)}
  89. </span>,
  90. );
  91. });
  92. return (
  93. <span className="d-flex align-items-center">
  94. <span className="separator" style={rootStyle}>
  95. <a href="/">/</a>
  96. </span>
  97. {afterElements}
  98. <Dropdown id="copyPagePathDropdown">
  99. <Dropdown.Toggle bsSize="sm">
  100. <i className="ti-clipboard"></i>
  101. </Dropdown.Toggle>
  102. <Dropdown.Menu>
  103. <MenuItem>hoge</MenuItem>
  104. <MenuItem>fuga</MenuItem>
  105. </Dropdown.Menu>
  106. </Dropdown>
  107. <a href="#edit" className="btn btn-default btn-edit" style={editButtonStyle}>
  108. <i className="icon-note" />
  109. </a>
  110. </span>
  111. );
  112. }
  113. }
  114. RevisionPath.propTypes = {
  115. pagePath: PropTypes.string.isRequired,
  116. crowi: PropTypes.object.isRequired,
  117. };