tree.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { PluginContext } from '../context';
  8. import { PluginStateObject } from 'mol-plugin/state/base';
  9. export class Tree extends React.Component<{ plugin: PluginContext }, { }> {
  10. componentWillMount() {
  11. this.props.plugin.events.stateUpdated.subscribe(() => this.forceUpdate());
  12. }
  13. render() {
  14. const n = this.props.plugin.state.data.tree.nodes.get(this.props.plugin.state.data.tree.rootRef)!;
  15. return <div>
  16. {n.children.map(c => <TreeNode plugin={this.props.plugin} nodeRef={c!} key={c} />)}
  17. </div>;
  18. }
  19. }
  20. export class TreeNode extends React.Component<{ plugin: PluginContext, nodeRef: string }, { }> {
  21. render() {
  22. const n = this.props.plugin.state.data.tree.nodes.get(this.props.nodeRef)!;
  23. const obj = this.props.plugin.state.data.objects.get(this.props.nodeRef)!;
  24. return <div style={{ borderLeft: '1px solid black', paddingLeft: '5px' }}>
  25. {(obj.obj!.props as PluginStateObject.Props).label}
  26. {n.children.size === 0
  27. ? void 0
  28. : <div style={{ marginLeft: '10px' }}>{n.children.map(c => <TreeNode plugin={this.props.plugin} nodeRef={c!} key={c} />)}</div>
  29. }
  30. </div>;
  31. }
  32. }