headerComponent.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  2. import { faWindowRestore, faTimes, faArrowLeft } from "@fortawesome/free-solid-svg-icons";
  3. import { Observable, Observer, Nullable } from "babylonjs";
  4. import * as React from "react";
  5. export interface IHeaderComponentProps {
  6. title: string,
  7. handleBack?: boolean,
  8. noExpand?: boolean,
  9. noClose?: boolean,
  10. noCommands?: boolean,
  11. onPopup: () => void,
  12. onClose: () => void,
  13. onSelectionChangedObservable?: Observable<any>
  14. }
  15. export class HeaderComponent extends React.Component<IHeaderComponentProps, { isBackVisible: boolean }> {
  16. private _backStack = new Array<any>();
  17. private _onSelectionChangeObserver: Nullable<Observer<any>>;
  18. constructor(props: IHeaderComponentProps) {
  19. super(props);
  20. this.state = { isBackVisible: false };
  21. }
  22. componentWillMount() {
  23. if (!this.props.onSelectionChangedObservable) {
  24. return;
  25. }
  26. this._onSelectionChangeObserver = this.props.onSelectionChangedObservable.add((entity) => {
  27. if (this._backStack.length === 0 || entity !== this._backStack[this._backStack.length - 1]) {
  28. this._backStack.push(entity);
  29. this.setState({ isBackVisible: this._backStack.length > 1 });
  30. }
  31. });
  32. }
  33. componentWillUnmount() {
  34. if (this._onSelectionChangeObserver) {
  35. this.props.onSelectionChangedObservable!.remove(this._onSelectionChangeObserver);
  36. }
  37. }
  38. goBack() {
  39. this._backStack.pop(); // remove current
  40. var entity = this._backStack[this._backStack.length - 1];
  41. if (this.props.onSelectionChangedObservable) {
  42. this.props.onSelectionChangedObservable.notifyObservers(entity);
  43. }
  44. this.setState({ isBackVisible: this._backStack.length > 1 });
  45. }
  46. renderLogo() {
  47. if (this.props.noCommands) {
  48. return null;
  49. }
  50. if (this.props.handleBack) {
  51. if (!this.state.isBackVisible) {
  52. return null;
  53. }
  54. return (
  55. <div id="back" onClick={() => this.goBack()} >
  56. <FontAwesomeIcon icon={faArrowLeft} />
  57. </div>
  58. )
  59. }
  60. return (
  61. <img id="logo" src="https://www.babylonjs.com/Assets/logo-babylonjs-social-twitter.png" />
  62. )
  63. }
  64. render() {
  65. return (
  66. <div id="header">
  67. {this.renderLogo()}
  68. <div id="title">
  69. {this.props.title}
  70. </div>
  71. <div id="commands">
  72. {
  73. !this.props.noCommands && !this.props.noExpand &&
  74. <div className="expand" onClick={() => this.props.onPopup()}>
  75. <FontAwesomeIcon icon={faWindowRestore} />
  76. </div>
  77. }
  78. {
  79. !this.props.noCommands && !this.props.noClose &&
  80. <div className="close" onClick={() => this.props.onClose()}>
  81. <FontAwesomeIcon icon={faTimes} />
  82. </div>
  83. }
  84. </div>
  85. </div>
  86. )
  87. }
  88. }