apply-action.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/command';
  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. plugin: PluginContext,
  16. nodeRef: StateTransform.Ref,
  17. state: State,
  18. action: StateAction,
  19. hideHeader?: boolean,
  20. initiallyCollapsed?: boolean
  21. }
  22. export interface ComponentState {
  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.dispatch(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 = { 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. if (props.nodeRef === state.ref) return null;
  51. const version = props.state.transforms.get(props.nodeRef).version;
  52. if (version === state.version) return null;
  53. const source = props.state.cells.get(props.nodeRef)!.obj!;
  54. const params = props.action.definition.params
  55. ? PD.getDefaultValues(props.action.definition.params(source, props.plugin))
  56. : { };
  57. const newState: Partial<ApplyActionControl.ComponentState> = {
  58. ref: props.nodeRef,
  59. version,
  60. params,
  61. isInitial: true,
  62. error: void 0
  63. };
  64. return newState;
  65. }
  66. }