apply-action.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 { PluginCommands } from '../../mol-plugin/commands';
  7. import { PluginContext } from '../../mol-plugin/context';
  8. import { State, StateTransform, StateAction } from '../../mol-state';
  9. import { memoizeLatest } from '../../mol-util/memoize';
  10. import { StateTransformParameters, TransformControlBase } from './common';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. export { ApplyActionControl };
  13. namespace ApplyActionControl {
  14. export interface Props {
  15. nodeRef: StateTransform.Ref,
  16. state: State,
  17. action: StateAction,
  18. hideHeader?: boolean,
  19. initiallyCollapsed?: boolean
  20. }
  21. export interface ComponentState {
  22. plugin: PluginContext,
  23. ref: StateTransform.Ref,
  24. version: string,
  25. params: any,
  26. error?: string,
  27. busy: boolean,
  28. isInitial: boolean
  29. }
  30. }
  31. class ApplyActionControl extends TransformControlBase<ApplyActionControl.Props, ApplyActionControl.ComponentState> {
  32. applyAction() {
  33. return PluginCommands.State.ApplyAction(this.plugin, {
  34. state: this.props.state,
  35. action: this.props.action.create(this.state.params),
  36. ref: this.props.nodeRef
  37. });
  38. }
  39. getInfo() { return this._getInfo(this.props.nodeRef, this.props.state.transforms.get(this.props.nodeRef).version); }
  40. getTransformerId() { return this.props.state.transforms.get(this.props.nodeRef).transformer.id; }
  41. getHeader() { return this.props.hideHeader ? 'none' : this.props.action.definition.display; }
  42. canApply() { return !this.state.error && !this.state.busy; }
  43. canAutoApply() { return false; }
  44. applyText() { return 'Apply'; }
  45. isUpdate() { return false; }
  46. getSourceAndTarget() { return { a: this.props.state.cells.get(this.props.nodeRef)!.obj }; }
  47. private _getInfo = memoizeLatest((t: StateTransform.Ref, v: string) => StateTransformParameters.infoFromAction(this.plugin, this.props.state, this.props.action, this.props.nodeRef));
  48. state = { plugin: this.plugin, ref: this.props.nodeRef, version: this.props.state.transforms.get(this.props.nodeRef).version, error: void 0, isInitial: true, params: this.getInfo().initialValues, busy: false, isCollapsed: this.props.initiallyCollapsed };
  49. static getDerivedStateFromProps(props: ApplyActionControl.Props, state: ApplyActionControl.ComponentState) {
  50. const version = props.state.transforms.get(props.nodeRef).version;
  51. if (props.nodeRef === state.ref && version === state.version) {
  52. return null;
  53. }
  54. const source = props.state.cells.get(props.nodeRef)!.obj!;
  55. const params = props.action.definition.params
  56. ? PD.getDefaultValues(props.action.definition.params(source, state.plugin))
  57. : { };
  58. const newState: Partial<ApplyActionControl.ComponentState> = {
  59. plugin: state.plugin,
  60. ref: props.nodeRef,
  61. version,
  62. params,
  63. isInitial: true,
  64. error: void 0
  65. };
  66. return newState;
  67. }
  68. }