source.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { HierarchyRef, ModelRef, TrajectoryRef } from '../../mol-plugin-state/manager/structure/hierarchy-state';
  8. import { CollapsableControls, CollapsableState } from '../base';
  9. import { ActionMenu } from '../controls/action-menu';
  10. import { Icon } from '../controls/icons';
  11. interface StructureSourceControlState extends CollapsableState {
  12. isBusy: boolean,
  13. show?: 'hierarchy' | 'actions'
  14. }
  15. export class StructureSourceControls extends CollapsableControls<{}, StructureSourceControlState> {
  16. protected defaultState(): StructureSourceControlState {
  17. return {
  18. header: 'Source',
  19. isCollapsed: false,
  20. isBusy: false
  21. };
  22. }
  23. componentDidMount() {
  24. this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.current, () => this.forceUpdate());
  25. this.subscribe(this.plugin.behaviors.state.isBusy, v => {
  26. this.setState({ isBusy: v })
  27. });
  28. }
  29. private item = (ref: HierarchyRef) => {
  30. const selected = this.plugin.managers.structure.hierarchy.currentSeletionSet;
  31. return { label: ref.cell.obj?.label, selected: selected.has(ref.cell.transform.ref), value: [ref] } as ActionMenu.Item;
  32. }
  33. getTrajectoryItems = (t: TrajectoryRef): ActionMenu.Items => {
  34. if (t.models.length === 0) return this.item(t);
  35. return [t.cell.obj?.label!, ...t.models.map(this.getModelItems)];
  36. }
  37. private getModelItems = (m: ModelRef): ActionMenu.Items => {
  38. if (m.structures.length === 0) return this.item(m);
  39. if (m.structures.length === 1) {
  40. const selected = this.plugin.managers.structure.hierarchy.currentSeletionSet;
  41. const ref = m.structures[0];
  42. return { label: `${m.cell.obj?.label} | ${ref.cell.obj?.label}`, selected: selected.has(ref.cell.transform.ref), value: [m, ref] } as ActionMenu.Item;
  43. }
  44. return [m.cell.obj?.label!, ...m.structures.map(this.item)];
  45. }
  46. get hierarchyItems() {
  47. return this.plugin.managers.structure.hierarchy.state.current.trajectories.map(this.getTrajectoryItems);
  48. }
  49. get label() {
  50. const { structures, models, trajectories } = this.plugin.managers.structure.hierarchy.state.current;
  51. // TODO: better labels
  52. if (structures.length === 1) {
  53. const s = structures[0];
  54. if (s.model.trajectory.models.length === 1) return s.cell.obj?.data.label;
  55. return `${s.model.cell.obj?.label} | ${s.cell.obj?.data.label}`;
  56. }
  57. if (structures.length > 1) {
  58. return `${structures.length} structures`;
  59. }
  60. if (models.length > 0) {
  61. return `${models.length} model(s)`;
  62. }
  63. if (trajectories.length > 0) {
  64. return `${trajectories.length} trajectory(s)`;
  65. }
  66. return 'No structure loaded';
  67. }
  68. selectHierarchy: ActionMenu.OnSelectMany = (items) => {
  69. if (!items || items.length === 0) return 0;
  70. const refs: HierarchyRef[] = [];
  71. for (const i of items) {
  72. for (const r of (i.value as HierarchyRef[])) refs.push(r);
  73. }
  74. this.plugin.managers.structure.hierarchy.updateCurrent(refs, items[0].selected ? 'remove' : 'add')
  75. }
  76. toggleHierarchy = () => this.setState({ show: this.state.show !== 'hierarchy' ? 'hierarchy' : void 0 });
  77. toggleActions = () => this.setState({ show: this.state.show !== 'actions' ? 'actions' : void 0 });
  78. get actions() {
  79. const ret: ActionMenu.Items = [
  80. ActionMenu.Item('Show all models', () => this.plugin.managers.structure.hierarchy.createAllModels(this.plugin.managers.structure.hierarchy.state.current.trajectories[0]))
  81. ];
  82. return ret;
  83. }
  84. selectAction: ActionMenu.OnSelect = item => {
  85. if (!item) return;
  86. this.setState({ show: void 0 });
  87. (item?.value as any)();
  88. }
  89. renderControls() {
  90. return <>
  91. <div className='msp-flex-row' style={{ marginTop: '1px' }}>
  92. <button className='msp-btn msp-form-control msp-flex-item' onClick={this.toggleHierarchy} style={{ overflow: 'hidden', textOverflow: 'ellipsis' }}>
  93. {this.label}
  94. </button>
  95. <button className='msp-btn msp-form-control msp-flex-item' onClick={this.toggleActions} style={{ flex: '0 0 40px', }}>
  96. <Icon name='dot-3' />
  97. </button>
  98. </div>
  99. {this.state.show === 'hierarchy' && <ActionMenu items={this.hierarchyItems} onSelect={this.selectHierarchy} multiselect />}
  100. {this.state.show === 'actions' && <ActionMenu items={this.actions} onSelect={this.selectAction} />}
  101. </>;
  102. }
  103. }