lineContainerComponent.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 && !this.props.globalState.selectedLineContainerTitle) {
  49. return;
  50. }
  51. if (this.props.globalState && this.props.globalState.selectedLineContainerTitle === this.props.title) {
  52. setTimeout(() => {
  53. this.props.globalState!.selectedLineContainerTitle = "";
  54. });
  55. this.setState({ isExpanded: true, isHighlighted: true });
  56. window.setTimeout(() => {
  57. this.setState({ isHighlighted: false });
  58. }, 5000);
  59. } else {
  60. this.setState({isExpanded: false});
  61. }
  62. }
  63. renderHeader() {
  64. const className = this.state.isExpanded ? "collapse" : "collapse closed";
  65. return (
  66. <div className="header" onClick={() => this.switchExpandedState()}>
  67. <div className="title">
  68. {this.props.title}
  69. </div>
  70. <div className={className}>
  71. <FontAwesomeIcon icon={faChevronDown} />
  72. </div>
  73. </div>
  74. );
  75. }
  76. render() {
  77. if (!this.state.isExpanded) {
  78. return (
  79. <div className="paneContainer">
  80. <div className="paneContainer-content">
  81. {
  82. this.renderHeader()
  83. }
  84. </div>
  85. </div>
  86. );
  87. }
  88. return (
  89. <div className="paneContainer">
  90. <div className="paneContainer-content">
  91. {
  92. this.renderHeader()
  93. }
  94. <div className="paneList">
  95. {this.props.children}
  96. </div >
  97. </div>
  98. <div className={"paneContainer-highlight-border" + (!this.state.isHighlighted ? " transparent" : "")}>
  99. </div>
  100. </div>
  101. );
  102. }
  103. }