plugin.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Copyright (c) 2018-2020 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 { List } from 'immutable';
  8. import { formatTime } from '../mol-util';
  9. import { LogEntry } from '../mol-util/log-entry';
  10. import * as React from 'react';
  11. import { PluginContext } from '../mol-plugin/context';
  12. import { PluginReactContext, PluginUIComponent } from './base';
  13. import { LociLabels, TrajectoryViewportControls, StateSnapshotViewportControls, AnimationViewportControls, DefaultStructureTools } from './controls';
  14. import { StateObjectActionSelect } from './state/actions';
  15. import { BackgroundTaskProgress } from './task';
  16. import { Viewport, ViewportControls } from './viewport';
  17. import { StateTransform } from '../mol-state';
  18. import { UpdateTransformControl } from './state/update-transform';
  19. import { SequenceView } from './sequence';
  20. import { Toasts } from './toast';
  21. import { SectionHeader, ExpandGroup } from './controls/common';
  22. import { LeftPanelControls } from './left-panel';
  23. import { StateTreeSpine } from '../mol-state/tree/spine';
  24. export class Plugin extends React.Component<{ plugin: PluginContext }, {}> {
  25. region(kind: 'left' | 'right' | 'bottom' | 'main', element: JSX.Element) {
  26. return <div className={`msp-layout-region msp-layout-${kind}`}>
  27. <div className='msp-layout-static'>
  28. {element}
  29. </div>
  30. </div>
  31. }
  32. render() {
  33. return <PluginReactContext.Provider value={this.props.plugin}>
  34. <Layout />
  35. </PluginReactContext.Provider>;
  36. }
  37. }
  38. export class PluginContextContainer extends React.Component<{ plugin: PluginContext }> {
  39. render() {
  40. return <PluginReactContext.Provider value={this.props.plugin}>
  41. <div className='msp-plugin'>
  42. {this.props.children}
  43. </div>
  44. </PluginReactContext.Provider>;
  45. }
  46. }
  47. type RegionKind = 'top' | 'left' | 'right' | 'bottom' | 'main'
  48. class Layout extends PluginUIComponent {
  49. componentDidMount() {
  50. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  51. }
  52. region(kind: RegionKind, Element?: React.ComponentClass) {
  53. return <div className={`msp-layout-region msp-layout-${kind}`}>
  54. <div className='msp-layout-static'>
  55. {Element ? <Element /> : null}
  56. </div>
  57. </div>;
  58. }
  59. get layoutVisibilityClassName() {
  60. const layout = this.plugin.layout.state;
  61. const controls = (this.plugin.spec.layout && this.plugin.spec.layout.controls) || { };
  62. const classList: string[] = []
  63. if (controls.top === 'none' || !layout.showControls || layout.regionState.top === 'hidden') {
  64. classList.push('msp-layout-hide-top')
  65. }
  66. if (controls.left === 'none' || !layout.showControls || layout.regionState.left === 'hidden') {
  67. classList.push('msp-layout-hide-left')
  68. } else if (layout.regionState.left === 'collapsed') {
  69. classList.push('msp-layout-collapse-left')
  70. }
  71. if (controls.right === 'none' || !layout.showControls || layout.regionState.right === 'hidden') {
  72. classList.push('msp-layout-hide-right')
  73. }
  74. if (controls.bottom === 'none' || !layout.showControls || layout.regionState.bottom === 'hidden') {
  75. classList.push('msp-layout-hide-bottom')
  76. }
  77. return classList.join(' ')
  78. }
  79. get layoutClassName() {
  80. const layout = this.plugin.layout.state;
  81. const classList: string[] = ['msp-plugin-content']
  82. if (layout.isExpanded) {
  83. classList.push('msp-layout-expanded')
  84. } else {
  85. classList.push('msp-layout-standard', `msp-layout-standard-${layout.controlsDisplay}`)
  86. }
  87. return classList.join(' ')
  88. }
  89. render() {
  90. const layout = this.plugin.layout.state;
  91. const controls = this.plugin.spec.layout?.controls || { };
  92. const viewport = this.plugin.spec.components?.viewport || DefaultViewport;
  93. return <div className='msp-plugin'>
  94. <div className={this.layoutClassName}>
  95. <div className={this.layoutVisibilityClassName}>
  96. {this.region('main', viewport)}
  97. {layout.showControls && controls.top !== 'none' && this.region('top', controls.top || SequenceView)}
  98. {layout.showControls && controls.left !== 'none' && this.region('left', controls.left || LeftPanelControls)}
  99. {layout.showControls && controls.right !== 'none' && this.region('right', controls.right || ControlsWrapper)}
  100. {layout.showControls && controls.bottom !== 'none' && this.region('bottom', controls.bottom || Log)}
  101. </div>
  102. </div>
  103. </div>;
  104. }
  105. }
  106. export class ControlsWrapper extends PluginUIComponent {
  107. render() {
  108. const StructureTools = this.plugin.spec.components?.structureTools || DefaultStructureTools;
  109. return <div className='msp-scrollable-container'>
  110. <CurrentObject />
  111. <StructureTools />
  112. </div>;
  113. }
  114. }
  115. export class DefaultViewport extends PluginUIComponent {
  116. render() {
  117. const VPControls = this.plugin.spec.components?.viewportControls || ViewportControls;
  118. return <>
  119. <Viewport />
  120. <div className='msp-viewport-top-left-controls'>
  121. <AnimationViewportControls />
  122. <TrajectoryViewportControls />
  123. <StateSnapshotViewportControls />
  124. </div>
  125. <VPControls />
  126. <BackgroundTaskProgress />
  127. <div className='msp-highlight-toast-wrapper'>
  128. <LociLabels />
  129. <Toasts />
  130. </div>
  131. </>;
  132. }
  133. }
  134. export class Log extends PluginUIComponent<{}, { entries: List<LogEntry> }> {
  135. private wrapper = React.createRef<HTMLDivElement>();
  136. componentDidMount() {
  137. this.subscribe(this.plugin.events.log, () => this.setState({ entries: this.plugin.log.entries }));
  138. }
  139. componentDidUpdate() {
  140. this.scrollToBottom();
  141. }
  142. state = { entries: this.plugin.log.entries };
  143. private scrollToBottom() {
  144. const log = this.wrapper.current;
  145. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  146. }
  147. render() {
  148. // TODO: ability to show full log
  149. // showing more entries dramatically slows animations.
  150. const maxEntries = 10;
  151. const xs = this.state.entries, l = xs.size;
  152. const entries: JSX.Element[] = [];
  153. for (let i = Math.max(0, l - maxEntries), o = 0; i < l; i++) {
  154. const e = xs.get(i);
  155. entries.push(<li key={o++}>
  156. <div className={'msp-log-entry-badge msp-log-entry-' + e!.type} />
  157. <div className='msp-log-timestamp'>{formatTime(e!.timestamp)}</div>
  158. <div className='msp-log-entry'>{e!.message}</div>
  159. </li>);
  160. }
  161. return <div ref={this.wrapper} className='msp-log' style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'auto' }}>
  162. <ul className='msp-list-unstyled'>{entries}</ul>
  163. </div>;
  164. }
  165. }
  166. export class CurrentObject extends PluginUIComponent {
  167. get current() {
  168. return this.plugin.state.behavior.currentObject.value;
  169. }
  170. componentDidMount() {
  171. this.subscribe(this.plugin.state.behavior.currentObject, o => {
  172. this.forceUpdate();
  173. });
  174. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, o => {
  175. this.forceUpdate();
  176. });
  177. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  178. const current = this.current;
  179. if (current.ref !== ref || current.state !== state) return;
  180. this.forceUpdate();
  181. });
  182. }
  183. render() {
  184. const tabName = this.plugin.behaviors.layout.leftPanelTabName.value;
  185. if (tabName !== 'data' && tabName !== 'settings') return null;
  186. const current = this.current;
  187. const ref = current.ref;
  188. if (ref === StateTransform.RootRef) return null;
  189. const cell = current.state.cells.get(ref)!;
  190. const transform = cell.transform;
  191. let showActions = true;
  192. if (ref === StateTransform.RootRef) {
  193. const children = current.state.tree.children.get(ref);
  194. showActions = children.size !== 0;
  195. }
  196. if (!showActions) return null;
  197. const actions = cell.status === 'ok' && <StateObjectActionSelect state={current.state} nodeRef={ref} plugin={this.plugin} />
  198. if (cell.status === 'error') {
  199. return <>
  200. <SectionHeader icon='flow-cascade' title={`${cell.obj?.label || transform.transformer.definition.display.name}`} desc={transform.transformer.definition.display.name} />
  201. <UpdateTransformControl state={current.state} transform={transform} customHeader='none' />
  202. {actions}
  203. </>;
  204. }
  205. if (cell.status !== 'ok') return null;
  206. const decoratorChain = StateTreeSpine.getDecoratorChain(this.current.state, this.current.ref);
  207. const parent = decoratorChain[decoratorChain.length - 1];
  208. let decorators: JSX.Element[] | undefined = decoratorChain.length > 1 ? [] : void 0;
  209. for (let i = decoratorChain.length - 2; i >= 0; i--) {
  210. const d = decoratorChain[i];
  211. decorators!.push(<ExpandGroup key={`${d.transform.transformer.id}-${i}`} header={d.transform.transformer.definition.display.name}>
  212. <UpdateTransformControl state={current.state} transform={d.transform} customHeader='none' />
  213. </ExpandGroup>);
  214. }
  215. return <>
  216. <SectionHeader icon='flow-cascade' title={`${parent.obj?.label || parent.transform.transformer.definition.display.name}`} desc={parent.transform.transformer.definition.display.name} />
  217. <UpdateTransformControl state={current.state} transform={parent.transform} customHeader='none' />
  218. {decorators && <div className='msp-controls-section'>{decorators}</div>}
  219. {actions}
  220. </>;
  221. }
  222. }