state-tree.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/objects';
  9. import { State } from 'mol-state'
  10. import { PluginCommands } from 'mol-plugin/command';
  11. export class StateTree extends React.Component<{ plugin: PluginContext, state: State }, { }> {
  12. componentDidMount() {
  13. // TODO: move to constructor?
  14. this.props.state.events.changed.subscribe(() => this.forceUpdate());
  15. }
  16. render() {
  17. // const n = this.props.plugin.state.data.tree.nodes.get(this.props.plugin.state.data.tree.rootRef)!;
  18. const n = this.props.state.tree.root.ref;
  19. return <div>
  20. <StateTreeNode plugin={this.props.plugin} state={this.props.state} nodeRef={n} key={n} />
  21. { /* n.children.map(c => <StateTreeNode plugin={this.props.plugin} nodeRef={c!} key={c} />) */}
  22. </div>;
  23. }
  24. }
  25. export class StateTreeNode extends React.Component<{ plugin: PluginContext, nodeRef: string, state: State }, { }> {
  26. render() {
  27. const n = this.props.state.tree.nodes.get(this.props.nodeRef)!;
  28. const cell = this.props.state.cells.get(this.props.nodeRef)!;
  29. const remove = <>[<a href='#' onClick={e => {
  30. e.preventDefault();
  31. PluginCommands.State.RemoveObject.dispatch(this.props.plugin, { state: this.props.state, ref: this.props.nodeRef });
  32. }}>X</a>]</>
  33. let label: any;
  34. if (cell.status !== 'ok' || !cell.obj) {
  35. const name = (n.transformer.definition.display && n.transformer.definition.display.name) || n.transformer.definition.name;
  36. label = <><b>{cell.status}</b> <a href='#' onClick={e => {
  37. e.preventDefault();
  38. PluginCommands.State.SetCurrentObject.dispatch(this.props.plugin, { state: this.props.state, ref: this.props.nodeRef });
  39. }}>{name}</a>: <i>{cell.errorText}</i></>;
  40. } else {
  41. const obj = cell.obj as PluginStateObject.Any;
  42. label = <><a href='#' onClick={e => {
  43. e.preventDefault();
  44. PluginCommands.State.SetCurrentObject.dispatch(this.props.plugin, { state: this.props.state, ref: this.props.nodeRef });
  45. }}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
  46. }
  47. const children = this.props.state.tree.children.get(this.props.nodeRef);
  48. return <div>
  49. {remove} {label}
  50. {children.size === 0
  51. ? void 0
  52. : <div style={{ marginLeft: '7px', paddingLeft: '3px', borderLeft: '1px solid #999' }}>{children.map(c => <StateTreeNode plugin={this.props.plugin} state={this.props.state} nodeRef={c!} key={c} />)}</div>
  53. }
  54. </div>;
  55. }
  56. }