plugin.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 * as React from 'react';
  9. import { PluginContext } from '../mol-plugin/context';
  10. import { formatTime } from '../mol-util';
  11. import { LogEntry } from '../mol-util/log-entry';
  12. import { PluginReactContext, PluginUIComponent } from './base';
  13. import { AnimationViewportControls, DefaultStructureTools, LociLabels, StateSnapshotViewportControls, TrajectoryViewportControls, SelectionViewportControls } from './controls';
  14. import { LeftPanelControls } from './left-panel';
  15. import { SequenceView } from './sequence';
  16. import { BackgroundTaskProgress, OverlayTaskProgress } from './task';
  17. import { Toasts } from './toast';
  18. import { Viewport, ViewportControls } from './viewport';
  19. import { PluginCommands } from '../mol-plugin/commands';
  20. export class Plugin extends React.Component<{ plugin: PluginContext }, {}> {
  21. region(kind: 'left' | 'right' | 'bottom' | 'main', element: JSX.Element) {
  22. return <div className={`msp-layout-region msp-layout-${kind}`}>
  23. <div className='msp-layout-static'>
  24. {element}
  25. </div>
  26. </div>;
  27. }
  28. render() {
  29. return <PluginReactContext.Provider value={this.props.plugin}>
  30. <Layout />
  31. </PluginReactContext.Provider>;
  32. }
  33. }
  34. export class PluginContextContainer extends React.Component<{ plugin: PluginContext }> {
  35. render() {
  36. return <PluginReactContext.Provider value={this.props.plugin}>
  37. <div className='msp-plugin'>
  38. {this.props.children}
  39. </div>
  40. </PluginReactContext.Provider>;
  41. }
  42. }
  43. type RegionKind = 'top' | 'left' | 'right' | 'bottom' | 'main'
  44. class Layout extends PluginUIComponent {
  45. componentDidMount() {
  46. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  47. }
  48. region(kind: RegionKind, Element?: React.ComponentClass) {
  49. return <div className={`msp-layout-region msp-layout-${kind}`}>
  50. <div className='msp-layout-static'>
  51. {Element ? <Element /> : null}
  52. </div>
  53. </div>;
  54. }
  55. get layoutVisibilityClassName() {
  56. const layout = this.plugin.layout.state;
  57. const controls = (this.plugin.spec.layout && this.plugin.spec.layout.controls) || {};
  58. const classList: string[] = [];
  59. if (controls.top === 'none' || !layout.showControls || layout.regionState.top === 'hidden') {
  60. classList.push('msp-layout-hide-top');
  61. }
  62. if (controls.left === 'none' || !layout.showControls || layout.regionState.left === 'hidden') {
  63. classList.push('msp-layout-hide-left');
  64. } else if (layout.regionState.left === 'collapsed') {
  65. classList.push('msp-layout-collapse-left');
  66. }
  67. if (controls.right === 'none' || !layout.showControls || layout.regionState.right === 'hidden') {
  68. classList.push('msp-layout-hide-right');
  69. }
  70. if (controls.bottom === 'none' || !layout.showControls || layout.regionState.bottom === 'hidden') {
  71. classList.push('msp-layout-hide-bottom');
  72. }
  73. return classList.join(' ');
  74. }
  75. get layoutClassName() {
  76. const layout = this.plugin.layout.state;
  77. const classList: string[] = ['msp-plugin-content'];
  78. if (layout.isExpanded) {
  79. classList.push('msp-layout-expanded');
  80. } else {
  81. classList.push('msp-layout-standard', `msp-layout-standard-${layout.controlsDisplay}`);
  82. }
  83. return classList.join(' ');
  84. }
  85. onDrop = (ev: React.DragEvent<HTMLDivElement>) => {
  86. ev.preventDefault();
  87. let file: File | undefined | null;
  88. if (ev.dataTransfer.items) {
  89. // Use DataTransferItemList interface to access the file(s)
  90. for (let i = 0; i < ev.dataTransfer.items.length; i++) {
  91. if (ev.dataTransfer.items[i].kind !== 'file') continue;
  92. file = ev.dataTransfer.items[i].getAsFile();
  93. break;
  94. }
  95. } else {
  96. file = ev.dataTransfer.files[0];
  97. }
  98. if (!file) return;
  99. const fn = file?.name.toLowerCase() || '';
  100. if (fn.endsWith('molx') || fn.endsWith('molj')) {
  101. PluginCommands.State.Snapshots.OpenFile(this.plugin, { file });
  102. }
  103. }
  104. onDragOver = (ev: React.DragEvent<HTMLDivElement>) => {
  105. ev.preventDefault();
  106. }
  107. render() {
  108. const layout = this.plugin.layout.state;
  109. const controls = this.plugin.spec.layout?.controls || {};
  110. const viewport = this.plugin.spec.components?.viewport?.view || DefaultViewport;
  111. return <div className='msp-plugin' onDrop={this.onDrop} onDragOver={this.onDragOver}>
  112. <div className={this.layoutClassName}>
  113. <div className={this.layoutVisibilityClassName}>
  114. {this.region('main', viewport)}
  115. {layout.showControls && controls.top !== 'none' && this.region('top', controls.top || SequenceView)}
  116. {layout.showControls && controls.left !== 'none' && this.region('left', controls.left || LeftPanelControls)}
  117. {layout.showControls && controls.right !== 'none' && this.region('right', controls.right || ControlsWrapper)}
  118. {layout.showControls && controls.bottom !== 'none' && this.region('bottom', controls.bottom || Log)}
  119. </div>
  120. {!this.plugin.spec.components?.hideTaskOverlay && <OverlayTaskProgress />}
  121. </div>
  122. </div>;
  123. }
  124. }
  125. export class ControlsWrapper extends PluginUIComponent {
  126. render() {
  127. const StructureTools = this.plugin.spec.components?.structureTools || DefaultStructureTools;
  128. return <div className='msp-scrollable-container'>
  129. {/* <CurrentObject /> */}
  130. <StructureTools />
  131. </div>;
  132. }
  133. }
  134. export class DefaultViewport extends PluginUIComponent {
  135. render() {
  136. const VPControls = this.plugin.spec.components?.viewport?.controls || ViewportControls;
  137. return <>
  138. <Viewport />
  139. <div className='msp-viewport-top-left-controls'>
  140. <AnimationViewportControls />
  141. <TrajectoryViewportControls />
  142. <StateSnapshotViewportControls />
  143. </div>
  144. <SelectionViewportControls />
  145. <VPControls />
  146. <BackgroundTaskProgress />
  147. <div className='msp-highlight-toast-wrapper'>
  148. <LociLabels />
  149. <Toasts />
  150. </div>
  151. </>;
  152. }
  153. }
  154. export class Log extends PluginUIComponent<{}, { entries: List<LogEntry> }> {
  155. private wrapper = React.createRef<HTMLDivElement>();
  156. componentDidMount() {
  157. this.subscribe(this.plugin.events.log, () => this.setState({ entries: this.plugin.log.entries }));
  158. }
  159. componentDidUpdate() {
  160. this.scrollToBottom();
  161. }
  162. state = { entries: this.plugin.log.entries };
  163. private scrollToBottom() {
  164. const log = this.wrapper.current;
  165. if (log) log.scrollTop = log.scrollHeight - log.clientHeight - 1;
  166. }
  167. render() {
  168. // TODO: ability to show full log
  169. // showing more entries dramatically slows animations.
  170. const maxEntries = 10;
  171. const xs = this.state.entries, l = xs.size;
  172. const entries: JSX.Element[] = [];
  173. for (let i = Math.max(0, l - maxEntries), o = 0; i < l; i++) {
  174. const e = xs.get(i);
  175. entries.push(<li key={o++}>
  176. <div className={'msp-log-entry-badge msp-log-entry-' + e!.type} />
  177. <div className='msp-log-timestamp'>{formatTime(e!.timestamp)}</div>
  178. <div className='msp-log-entry'>{e!.message}</div>
  179. </li>);
  180. }
  181. return <div ref={this.wrapper} className='msp-log' style={{ position: 'absolute', top: '0', right: '0', bottom: '0', left: '0', overflowY: 'auto' }}>
  182. <ul className='msp-list-unstyled'>{entries}</ul>
  183. </div>;
  184. }
  185. }