extensionsComponent.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  2. import { faEllipsisH } from '@fortawesome/free-solid-svg-icons';
  3. import * as React from "react";
  4. import { Nullable, IExplorerExtensibilityGroup } from "babylonjs";
  5. interface IExtensionsComponentProps {
  6. target: any,
  7. extensibilityGroups?: IExplorerExtensibilityGroup[]
  8. }
  9. export class ExtensionsComponent extends React.Component<IExtensionsComponentProps, { popupVisible: boolean }> {
  10. private _popup: Nullable<HTMLDivElement>;
  11. constructor(props: IExtensionsComponentProps) {
  12. super(props);
  13. this.state = { popupVisible: false };
  14. }
  15. showPopup() {
  16. this.setState({ popupVisible: true });
  17. }
  18. componentDidMount() {
  19. if (!this._popup) {
  20. return;
  21. }
  22. this._popup.focus();
  23. }
  24. componentDidUpdate() {
  25. if (!this._popup) {
  26. return;
  27. }
  28. this._popup.focus();
  29. }
  30. render() {
  31. if (!this.props.extensibilityGroups) {
  32. return null;
  33. }
  34. let options = [];
  35. for (var group of this.props.extensibilityGroups) {
  36. if (group.predicate(this.props.target)) {
  37. options.push(...group.entries);
  38. }
  39. }
  40. if (options.length === 0) {
  41. return null;
  42. }
  43. return (
  44. <div ref="extensions" className="extensions" onClick={() => this.showPopup()}>
  45. <div title="Additional options" className="icon">
  46. <FontAwesomeIcon icon={faEllipsisH} />
  47. </div>
  48. <div ref={(input) => { this._popup = input }} tabIndex={-1} className={this.state.popupVisible ? "popup show" : "popup"} onBlur={() => this.setState({ popupVisible: false })}>
  49. {
  50. options.map(extensibility => {
  51. return (
  52. <div key={extensibility.label} className="popupMenu" onClick={() => extensibility.action(this.props.target)}>
  53. {extensibility.label}
  54. </div>
  55. )
  56. })
  57. }
  58. </div>
  59. </div>
  60. )
  61. }
  62. }