actions.tsx 2.1 KB

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