plugin.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 { 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 '../context';
  12. import { PluginReactContext, PluginUIComponent } from './base';
  13. import { LociLabels, TrajectoryViewportControls, StateSnapshotViewportControls, AnimationViewportControls, StructureToolsWrapper } 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 { ImageControls } from './image';
  22. import { SectionHeader } from './controls/common';
  23. import { LeftPanelControls } from './left-panel';
  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) {
  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) {
  72. classList.push('msp-layout-hide-right')
  73. }
  74. if (controls.bottom === 'none' || !layout.showControls) {
  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 && this.plugin.spec.layout.controls) || { };
  92. const viewport = (this.plugin.spec.layout && this.plugin.spec.layout.viewport) || ViewportWrapper;
  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. return <div className='msp-scrollable-container'>
  109. <CurrentObject />
  110. {/* <AnimationControlsWrapper /> */}
  111. {/* <CameraSnapshots /> */}
  112. <StructureToolsWrapper />
  113. <ImageControls />
  114. </div>;
  115. }
  116. }
  117. export class ViewportWrapper extends PluginUIComponent {
  118. render() {
  119. return <>
  120. <Viewport />
  121. <div className='msp-viewport-top-left-controls'>
  122. <AnimationViewportControls />
  123. <TrajectoryViewportControls />
  124. <StateSnapshotViewportControls />
  125. </div>
  126. <ViewportControls />
  127. <BackgroundTaskProgress />
  128. <div className='msp-highlight-toast-wrapper'>
  129. <LociLabels />
  130. <Toasts />
  131. </div>
  132. </>;
  133. }
  134. }
  135. export class Log extends PluginUIComponent<{}, { entries: List<LogEntry> }> {
  136. private wrapper = React.createRef<HTMLDivElement>();
  137. componentDidMount() {
  138. this.subscribe(this.plugin.events.log, () => this.setState({ entries: this.plugin.log.entries }));
  139. }
  140. componentDidUpdate() {
  141. this.scrollToBottom();
  142. }
  143. state = { entries: this.plugin.log.entries };
  144. private scrollToBottom() {
  145. const log = this.wrapper.current;
  146. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  147. }
  148. render() {
  149. // TODO: ability to show full log
  150. // showing more entries dramatically slows animations.
  151. const maxEntries = 10;
  152. const xs = this.state.entries, l = xs.size;
  153. const entries: JSX.Element[] = [];
  154. for (let i = Math.max(0, l - maxEntries), o = 0; i < l; i++) {
  155. const e = xs.get(i);
  156. entries.push(<li key={o++}>
  157. <div className={'msp-log-entry-badge msp-log-entry-' + e!.type} />
  158. <div className='msp-log-timestamp'>{formatTime(e!.timestamp)}</div>
  159. <div className='msp-log-entry'>{e!.message}</div>
  160. </li>);
  161. }
  162. return <div ref={this.wrapper} className='msp-log' style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'auto' }}>
  163. <ul className='msp-list-unstyled'>{entries}</ul>
  164. </div>;
  165. }
  166. }
  167. export class CurrentObject extends PluginUIComponent {
  168. get current() {
  169. return this.plugin.state.behavior.currentObject.value;
  170. }
  171. componentDidMount() {
  172. this.subscribe(this.plugin.state.behavior.currentObject, o => {
  173. this.forceUpdate();
  174. });
  175. this.subscribe(this.plugin.behaviors.layout.leftPanelTabName, o => {
  176. this.forceUpdate();
  177. });
  178. this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  179. const current = this.current;
  180. if (current.ref !== ref || current.state !== state) return;
  181. this.forceUpdate();
  182. });
  183. }
  184. render() {
  185. const tabName = this.plugin.behaviors.layout.leftPanelTabName.value;
  186. if (tabName !== 'data' && tabName !== 'settings') return null;
  187. const current = this.current;
  188. const ref = current.ref;
  189. if (ref === StateTransform.RootRef) return null;
  190. const cell = current.state.cells.get(ref)!;
  191. const transform = cell.transform;
  192. let showActions = true;
  193. if (ref === StateTransform.RootRef) {
  194. const children = current.state.tree.children.get(ref);
  195. showActions = children.size !== 0;
  196. }
  197. if (!showActions) return null;
  198. return <>
  199. {(cell.status === 'ok' || cell.status === 'error') && <>
  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. </> }
  203. {cell.status === 'ok' &&
  204. <StateObjectActionSelect state={current.state} nodeRef={ref} plugin={this.plugin} />
  205. }
  206. {/* <StateObjectActions state={current.state} nodeRef={ref} initiallyCollapsed />} */}
  207. </>;
  208. }
  209. }