import { TreeItemSpecializedComponent } from "./treeItemSpecializedComponent"; import { Observable, Nullable, IExplorerExtensibilityGroup } from "babylonjs"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons"; import { Tools } from "../../tools"; import * as ReactDOM from "react-dom"; import * as React from "react"; export interface ITreeItemSelectableComponentProps { entity: any, selectedEntity?: any, mustExpand?: boolean, offset: number, extensibilityGroups?: IExplorerExtensibilityGroup[], onSelectionChangedObservable?: Observable, filter: Nullable } export class TreeItemSelectableComponent extends React.Component { private _wasSelected = false; constructor(props: ITreeItemSelectableComponentProps) { super(props); this.state = { isSelected: this.props.entity === this.props.selectedEntity, isExpanded: this.props.mustExpand || Tools.LookForItem(this.props.entity, this.props.selectedEntity) }; } switchExpandedState(): void { this.setState({ isExpanded: !this.state.isExpanded }); } shouldComponentUpdate(nextProps: ITreeItemSelectableComponentProps, nextState: { isExpanded: boolean, isSelected: boolean }) { if (!nextState.isExpanded && this.state.isExpanded) { return true; } if (nextProps.selectedEntity) { if (nextProps.entity === nextProps.selectedEntity) { nextState.isSelected = true; return true; } else { nextState.isSelected = false; } if (Tools.LookForItem(nextProps.entity, nextProps.selectedEntity)) { nextState.isExpanded = true; return true; } } return true; } scrollIntoView() { const element = ReactDOM.findDOMNode(this) as Element; if (element) { element.scrollIntoView(); } } componentDidMount() { if (this.state.isSelected) { this.scrollIntoView(); } } componentDidUpdate() { if (this.state.isSelected && !this._wasSelected) { this.scrollIntoView(); } this._wasSelected = false; } onSelect() { if (!this.props.onSelectionChangedObservable) { return; } this._wasSelected = true; const entity = this.props.entity; this.props.onSelectionChangedObservable.notifyObservers(entity); } renderChildren() { const entity = this.props.entity; if (!entity.getChildren && !entity.children || !this.state.isExpanded) { return null; } const children = Tools.SortAndFilter(entity, entity.getChildren ? entity.getChildren() : entity.children); return ( children.map(item => { return ( ); }) ) } render() { const marginStyle = { paddingLeft: (10 * (this.props.offset + 0.5)) + "px" }; const entity = this.props.entity; const chevron = this.state.isExpanded ? : const children = Tools.SortAndFilter(entity, entity.getChildren ? entity.getChildren() : entity.children); const hasChildren = children.length > 0; if (!entity.reservedDataStore) { entity.reservedDataStore = {}; } entity.reservedDataStore.setExpandedState = (value: boolean) => { this.setState({ isExpanded: value }); } entity.reservedDataStore.isExpanded = this.state.isExpanded; if (this.props.filter) { const lowerCaseFilter = this.props.filter.toLowerCase(); if (!entity.name || entity.name.toLowerCase().indexOf(lowerCaseFilter) === -1) { if (!hasChildren) { return null; } if (entity.getDescendants) { if (entity.getDescendants(false, (n: any) => { console.log(n.name); return n.name && n.name.toLowerCase().indexOf(lowerCaseFilter) !== -1 }).length === 0) { return null; } } } } return (
{ hasChildren &&
this.switchExpandedState()}> {chevron}
} this.onSelect()} onSelectionChangedObservable={this.props.onSelectionChangedObservable} />
{ this.renderChildren() }
); } }