base.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as React from 'react';
  8. import { Observable, Subscription } from 'rxjs';
  9. import { PluginContext } from '../mol-plugin/context';
  10. export const PluginReactContext = React.createContext(void 0 as any as PluginContext);
  11. export abstract class PluginUIComponent<P = {}, S = {}, SS = {}> extends React.Component<P, S, SS> {
  12. static contextType = PluginReactContext;
  13. readonly plugin: PluginContext;
  14. private subs: Subscription[] | undefined = void 0;
  15. protected subscribe<T>(obs: Observable<T>, action: (v: T) => void) {
  16. if (typeof this.subs === 'undefined') this.subs = []
  17. this.subs.push(obs.subscribe(action));
  18. }
  19. componentWillUnmount() {
  20. if (!this.subs) return;
  21. for (const s of this.subs) s.unsubscribe();
  22. this.subs = [];
  23. }
  24. protected init?(): void;
  25. constructor(props: P, context?: any) {
  26. super(props, context);
  27. this.plugin = context;
  28. if (this.init) this.init();
  29. }
  30. }
  31. export abstract class PurePluginUIComponent<P = {}, S = {}, SS = {}> extends React.PureComponent<P, S, SS> {
  32. static contextType = PluginReactContext;
  33. readonly plugin: PluginContext;
  34. private subs: Subscription[] | undefined = void 0;
  35. protected subscribe<T>(obs: Observable<T>, action: (v: T) => void) {
  36. if (typeof this.subs === 'undefined') this.subs = []
  37. this.subs.push(obs.subscribe(action));
  38. }
  39. componentWillUnmount() {
  40. if (!this.subs) return;
  41. for (const s of this.subs) s.unsubscribe();
  42. }
  43. protected init?(): void;
  44. constructor(props: P, context?: any) {
  45. super(props, context);
  46. this.plugin = context;
  47. if (this.init) this.init();
  48. }
  49. }
  50. export type _Props<C extends React.Component> = C extends React.Component<infer P> ? P : never
  51. export type _State<C extends React.Component> = C extends React.Component<any, infer S> ? S : never
  52. //
  53. export type CollapsableProps = { initiallyCollapsed?: boolean, header?: string }
  54. export type CollapsableState = { isCollapsed: boolean, header: string }
  55. export abstract class CollapsableControls<P = {}, S = {}, SS = {}> extends PluginUIComponent<P & CollapsableProps, S & CollapsableState, SS> {
  56. toggleCollapsed = () => {
  57. this.setState({ isCollapsed: !this.state.isCollapsed } as (S & CollapsableState))
  58. }
  59. protected abstract defaultState(): (S & CollapsableState)
  60. protected abstract renderControls(): JSX.Element | null
  61. render() {
  62. const wrapClass = this.state.isCollapsed
  63. ? 'msp-transform-wrapper msp-transform-wrapper-collapsed'
  64. : 'msp-transform-wrapper';
  65. return <div className={wrapClass}>
  66. <div className='msp-transform-header'>
  67. <button className='msp-btn msp-btn-block msp-btn-collapse' onClick={this.toggleCollapsed}>
  68. <span className={`msp-icon msp-icon-${this.state.isCollapsed ? 'expand' : 'collapse'}`} />
  69. {this.state.header}
  70. </button>
  71. </div>
  72. {!this.state.isCollapsed && this.renderControls()}
  73. </div>
  74. }
  75. constructor(props: P & CollapsableProps, context?: any) {
  76. super(props, context)
  77. const state = this.defaultState()
  78. if (props.initiallyCollapsed !== undefined) state.isCollapsed = props.initiallyCollapsed
  79. if (props.header !== undefined) state.header = props.header
  80. this.state = state
  81. }
  82. }