lineContainerComponent.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as React from "react";
  2. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  3. import { faChevronDown } from '@fortawesome/free-solid-svg-icons';
  4. import { GlobalState } from '../../components/globalState';
  5. interface ILineContainerComponentProps {
  6. globalState: GlobalState;
  7. title: string;
  8. children: any[] | any;
  9. closed?: boolean;
  10. }
  11. export class LineContainerComponent extends React.Component<ILineContainerComponentProps, { isExpanded: boolean, isHighlighted: boolean }> {
  12. private static _InMemoryStorage: { [key: string]: boolean };
  13. constructor(props: ILineContainerComponentProps) {
  14. super(props);
  15. let initialState: boolean;
  16. try {
  17. if (LineContainerComponent._InMemoryStorage && LineContainerComponent._InMemoryStorage[this.props.title] !== undefined) {
  18. initialState = LineContainerComponent._InMemoryStorage[this.props.title];
  19. } else if (typeof (Storage) !== "undefined" && localStorage.getItem(this.props.title) !== null) {
  20. initialState = localStorage.getItem(this.props.title) === "true";
  21. } else {
  22. initialState = !this.props.closed;
  23. }
  24. }
  25. catch (e) {
  26. LineContainerComponent._InMemoryStorage = {};
  27. LineContainerComponent._InMemoryStorage[this.props.title] = !this.props.closed
  28. initialState = !this.props.closed;
  29. }
  30. this.state = { isExpanded: initialState, isHighlighted: false };
  31. }
  32. switchExpandedState(): void {
  33. const newState = !this.state.isExpanded;
  34. try {
  35. if (LineContainerComponent._InMemoryStorage) {
  36. LineContainerComponent._InMemoryStorage[this.props.title] = newState;
  37. } else if (typeof (Storage) !== "undefined") {
  38. localStorage.setItem(this.props.title, newState ? "true" : "false");
  39. }
  40. }
  41. catch (e) {
  42. LineContainerComponent._InMemoryStorage = {};
  43. LineContainerComponent._InMemoryStorage[this.props.title] = newState;
  44. }
  45. this.setState({ isExpanded: newState });
  46. }
  47. componentDidMount() {
  48. if (!this.props.globalState.selectedLineContainerTitle) {
  49. return;
  50. }
  51. if (this.props.globalState.selectedLineContainerTitle === this.props.title) {
  52. this.props.globalState.selectedLineContainerTitle = "";
  53. this.setState({ isExpanded: true, isHighlighted: true });
  54. window.setTimeout(() => {
  55. this.setState({ isHighlighted: false });
  56. }, 5000);
  57. } else {
  58. this.setState({isExpanded: false});
  59. }
  60. }
  61. renderHeader() {
  62. const className = this.state.isExpanded ? "collapse" : "collapse closed";
  63. return (
  64. <div className="header" onClick={() => this.switchExpandedState()}>
  65. <div className="title">
  66. {this.props.title}
  67. </div>
  68. <div className={className}>
  69. <FontAwesomeIcon icon={faChevronDown} />
  70. </div>
  71. </div>
  72. );
  73. }
  74. render() {
  75. if (!this.state.isExpanded) {
  76. return (
  77. <div className="paneContainer">
  78. <div className="paneContainer-content">
  79. {
  80. this.renderHeader()
  81. }
  82. </div>
  83. </div>
  84. );
  85. }
  86. return (
  87. <div className="paneContainer">
  88. <div className="paneContainer-content">
  89. {
  90. this.renderHeader()
  91. }
  92. <div className="paneList">
  93. {this.props.children}
  94. </div >
  95. </div>
  96. <div className={"paneContainer-highlight-border" + (!this.state.isHighlighted ? " transparent" : "")}>
  97. </div>
  98. </div>
  99. );
  100. }
  101. }