actions.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2018 - 2019 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 { State } from '../../mol-state';
  8. import { PluginUIComponent } from '../base';
  9. import { Icon } from '../controls/icons';
  10. import { ApplyActionControl } from './apply-action';
  11. import { Code } from '@material-ui/icons';
  12. export class StateObjectActions extends PluginUIComponent<{ state: State, nodeRef: string, hideHeader?: boolean, initiallyCollapsed?: boolean, alwaysExpandFirst?: boolean }> {
  13. get current() {
  14. return this.props.state.behaviors.currentObject.value;
  15. }
  16. componentDidMount() {
  17. // TODO: handle tree change: some state actions might become invalid
  18. // this.subscribe(this.props.state.events.changed, o => {
  19. // this.setState(createStateObjectActionSelectState(this.props));
  20. // });
  21. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  22. const current = this.current;
  23. if (current.ref !== ref || current.state !== state) return;
  24. this.forceUpdate();
  25. });
  26. this.subscribe(this.plugin.state.data.actions.events.added, () => this.forceUpdate());
  27. this.subscribe(this.plugin.state.data.actions.events.removed, () => this.forceUpdate());
  28. }
  29. render() {
  30. const { state, nodeRef: ref } = this.props;
  31. const cell = state.cells.get(ref)!;
  32. const actions = state.actions.fromCell(cell, this.plugin);
  33. if (actions.length === 0) return null;
  34. const def = cell.transform.transformer.definition;
  35. const display = cell.obj ? cell.obj.label : (def.display && def.display.name) || def.name;
  36. return <div className='msp-state-actions'>
  37. {!this.props.hideHeader && <div className='msp-section-header'><Icon svg={Code} /> {`Actions (${display})`}</div> }
  38. {actions.map((act, i) => <ApplyActionControl
  39. key={`${act.id}`} state={state} action={act} nodeRef={ref}
  40. initiallyCollapsed={i === 0 ? !this.props.alwaysExpandFirst && this.props.initiallyCollapsed : this.props.initiallyCollapsed} />)}
  41. </div>;
  42. }
  43. }