lineContainerComponent.tsx 3.9 KB

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