common.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 { State, StateTransform, StateTransformer, StateAction, StateObject } from '../../mol-state';
  7. import * as React from 'react';
  8. import { PurePluginUIComponent } from '../base';
  9. import { ParameterControls, ParamOnChange } from '../controls/parameters';
  10. import { PluginContext } from '../../mol-plugin/context';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  12. import { Subject } from 'rxjs';
  13. import { Icon } from '../controls/icons';
  14. import { ExpandGroup } from '../controls/common';
  15. export { StateTransformParameters, TransformControlBase };
  16. class StateTransformParameters extends PurePluginUIComponent<StateTransformParameters.Props> {
  17. validate(params: any) {
  18. // TODO
  19. return void 0;
  20. }
  21. areInitial(params: any) {
  22. return PD.areEqual(this.props.info.params, params, this.props.info.initialValues);
  23. }
  24. onChange: ParamOnChange = ({ name, value }) => {
  25. const params = { ...this.props.params, [name]: value };
  26. this.props.events.onChange(params, this.areInitial(params), this.validate(params));
  27. };
  28. render() {
  29. return <ParameterControls params={this.props.info.params} values={this.props.params} onChange={this.onChange} onEnter={this.props.events.onEnter} isDisabled={this.props.isDisabled} />;
  30. }
  31. }
  32. namespace StateTransformParameters {
  33. export interface Props {
  34. info: {
  35. params: PD.Params,
  36. initialValues: any,
  37. isEmpty: boolean
  38. },
  39. events: {
  40. onChange: (params: any, areInitial: boolean, errors?: string[]) => void,
  41. onEnter: () => void,
  42. }
  43. params: any,
  44. isDisabled?: boolean,
  45. a?: StateObject,
  46. b?: StateObject
  47. }
  48. export type Class = React.ComponentClass<Props>
  49. function areParamsEmpty(params: PD.Params) {
  50. const keys = Object.keys(params);
  51. for (const k of keys) {
  52. if (!params[k].isHidden) return false;
  53. }
  54. return true;
  55. }
  56. export function infoFromAction(plugin: PluginContext, state: State, action: StateAction, nodeRef: StateTransform.Ref): Props['info'] {
  57. const source = state.cells.get(nodeRef)!.obj!;
  58. const params = action.definition.params ? action.definition.params(source, plugin) : { };
  59. const initialValues = PD.getDefaultValues(params);
  60. return {
  61. initialValues,
  62. params,
  63. isEmpty: areParamsEmpty(params)
  64. };
  65. }
  66. export function infoFromTransform(plugin: PluginContext, state: State, transform: StateTransform): Props['info'] {
  67. const cell = state.cells.get(transform.ref)!;
  68. // const source: StateObjectCell | undefined = (cell.sourceRef && state.cells.get(cell.sourceRef)!) || void 0;
  69. // const create = transform.transformer.definition.params;
  70. // const params = create ? create((source && source.obj) as any, plugin) : { };
  71. const params = (cell.params && cell.params.definition) || { };
  72. const initialValues = (cell.params && cell.params.values) || { };
  73. return {
  74. initialValues,
  75. params,
  76. isEmpty: areParamsEmpty(params)
  77. }
  78. }
  79. }
  80. namespace TransformControlBase {
  81. export interface ComponentState {
  82. params: any,
  83. error?: string,
  84. busy: boolean,
  85. isInitial: boolean,
  86. simpleOnly?: boolean,
  87. isCollapsed?: boolean
  88. }
  89. }
  90. abstract class TransformControlBase<P, S extends TransformControlBase.ComponentState> extends PurePluginUIComponent<P & { noMargin?: boolean, applyLabel?: string, onApply?: () => void, wrapInExpander?: boolean }, S> {
  91. abstract applyAction(): Promise<void>;
  92. abstract getInfo(): StateTransformParameters.Props['info'];
  93. abstract getHeader(): StateTransformer.Definition['display'] | 'none';
  94. abstract canApply(): boolean;
  95. abstract getTransformerId(): string;
  96. abstract canAutoApply(newParams: any): boolean;
  97. abstract applyText(): string;
  98. abstract isUpdate(): boolean;
  99. abstract getSourceAndTarget(): { a?: StateObject, b?: StateObject };
  100. abstract state: S;
  101. private busy: Subject<boolean>;
  102. private onEnter = () => {
  103. if (this.state.error) return;
  104. this.apply();
  105. }
  106. private autoApplyHandle: number | undefined = void 0;
  107. private clearAutoApply() {
  108. if (this.autoApplyHandle !== void 0) {
  109. clearTimeout(this.autoApplyHandle);
  110. this.autoApplyHandle = void 0;
  111. }
  112. }
  113. events: StateTransformParameters.Props['events'] = {
  114. onEnter: this.onEnter,
  115. onChange: (params, isInitial, errors) => {
  116. this.clearAutoApply();
  117. this.setState({ params, isInitial, error: errors && errors[0] }, () => {
  118. if (!isInitial && !this.state.error && this.canAutoApply(params)) {
  119. this.clearAutoApply();
  120. this.autoApplyHandle = setTimeout(this.apply, 50) as any as number;
  121. }
  122. });
  123. }
  124. }
  125. apply = async () => {
  126. this.clearAutoApply();
  127. this.setState({ busy: true });
  128. try {
  129. await this.applyAction();
  130. } catch {
  131. // eat errors because they should be handled elsewhere
  132. } finally {
  133. this.props.onApply?.();
  134. this.busy.next(false);
  135. }
  136. }
  137. init() {
  138. this.busy = new Subject();
  139. this.subscribe(this.busy, busy => this.setState({ busy }));
  140. }
  141. refresh = () => {
  142. this.setState({ params: this.getInfo().initialValues, isInitial: true, error: void 0 });
  143. }
  144. setDefault = () => {
  145. const info = this.getInfo();
  146. const params = PD.getDefaultValues(info.params);
  147. this.setState({ params, isInitial: PD.areEqual(info.params, params, info.initialValues), error: void 0 });
  148. }
  149. toggleExpanded = () => {
  150. this.setState({ isCollapsed: !this.state.isCollapsed });
  151. }
  152. render() {
  153. const info = this.getInfo();
  154. const isEmpty = info.isEmpty && this.isUpdate();
  155. const display = this.getHeader();
  156. const tId = this.getTransformerId();
  157. const ParamEditor: StateTransformParameters.Class = this.plugin.customParamEditors.has(tId)
  158. ? this.plugin.customParamEditors.get(tId)!
  159. : StateTransformParameters;
  160. const wrapClass = this.state.isCollapsed
  161. ? 'msp-transform-wrapper msp-transform-wrapper-collapsed'
  162. : 'msp-transform-wrapper';
  163. const { a, b } = this.getSourceAndTarget();
  164. const showBack = this.isUpdate() && !(this.state.busy || this.state.isInitial);
  165. const ctrl = <div className={wrapClass} style={{ marginBottom: this.props.noMargin ? 0 : void 0 }}>
  166. {display !== 'none' && !this.props.wrapInExpander && <div className='msp-transform-header'>
  167. <button className={`msp-btn msp-btn-block${isEmpty ? '' : ' msp-btn-collapse'}`} onClick={this.toggleExpanded} title={display.description}>
  168. {!isEmpty && <Icon name={this.state.isCollapsed ? 'expand' : 'collapse'} />}
  169. {display.name}
  170. </button>
  171. </div>}
  172. {!isEmpty && !this.state.isCollapsed && <>
  173. <ParamEditor info={info} a={a} b={b} events={this.events} params={this.state.params} isDisabled={this.state.busy} />
  174. <div className='msp-transform-apply-wrap'>
  175. <button className='msp-btn msp-btn-block msp-transform-default-params' onClick={this.setDefault} disabled={this.state.busy} title='Set default params'><Icon name='cw' /></button>
  176. {showBack && <button className='msp-btn msp-btn-block msp-transform-refresh msp-form-control' title='Refresh params' onClick={this.refresh} disabled={this.state.busy || this.state.isInitial}>
  177. <Icon name='back' /> Back
  178. </button>}
  179. <div className={`msp-transform-apply${!showBack ? ' msp-transform-apply-wider' : ''}`}>
  180. <button className={`msp-btn msp-btn-block msp-btn-commit msp-btn-commit-${this.canApply() ? 'on' : 'off'}`} onClick={this.apply} disabled={!this.canApply()}>
  181. {this.canApply() && <Icon name='ok' />}
  182. {this.props.applyLabel || this.applyText()}
  183. </button>
  184. </div>
  185. </div>
  186. </>}
  187. </div>;
  188. if (isEmpty || !this.props.wrapInExpander) return ctrl;
  189. return <ExpandGroup header={this.isUpdate() ? `Update ${display === 'none' ? '' : display.name}` : `Apply ${display === 'none' ? '' : display.name}` }>
  190. {ctrl}
  191. </ExpandGroup>;
  192. }
  193. }