tree.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. */
  6. import ArrowDropDown from '@material-ui/icons/ArrowDropDown';
  7. import ArrowRight from '@material-ui/icons/ArrowRight';
  8. import Close from '@material-ui/icons/Close';
  9. import DeleteOutlined from '@material-ui/icons/DeleteOutlined';
  10. import HomeOutlined from '@material-ui/icons/HomeOutlined';
  11. import VisibilityOffOutlined from '@material-ui/icons/VisibilityOffOutlined';
  12. import VisibilityOutlined from '@material-ui/icons/VisibilityOutlined';
  13. import * as React from 'react';
  14. import { debounceTime, filter } from 'rxjs/operators';
  15. import { PluginStateObject } from '../../mol-plugin-state/objects';
  16. import { PluginCommands } from '../../mol-plugin/commands';
  17. import { State, StateAction, StateObject, StateObjectCell, StateTransform } from '../../mol-state';
  18. import { StateTreeSpine } from '../../mol-state/tree/spine';
  19. import { PluginUIComponent, _Props, _State } from '../base';
  20. import { ActionMenu } from '../controls/action-menu';
  21. import { Button, ControlGroup, IconButton } from '../controls/common';
  22. import { Icon } from '../controls/icons';
  23. import { ApplyActionControl } from './apply-action';
  24. import { UpdateTransformControl } from './update-transform';
  25. export class StateTree extends PluginUIComponent<{ state: State }, { showActions: boolean }> {
  26. state = { showActions: true };
  27. componentDidMount() {
  28. this.subscribe(this.plugin.state.events.cell.created, e => {
  29. if (e.cell.transform.parent === StateTransform.RootRef) this.forceUpdate();
  30. });
  31. this.subscribe(this.plugin.state.events.cell.removed, e => {
  32. if (e.parent === StateTransform.RootRef) this.forceUpdate();
  33. });
  34. }
  35. static getDerivedStateFromProps(props: { state: State }, state: { showActions: boolean }) {
  36. const n = props.state.tree.root.ref;
  37. const children = props.state.tree.children.get(n);
  38. const showActions = children.size === 0;
  39. if (state.showActions === showActions) return null;
  40. return { showActions };
  41. }
  42. render() {
  43. const ref = this.props.state.tree.root.ref;
  44. if (this.state.showActions) {
  45. return <div style={{ margin: '10px', cursor: 'default' }}>
  46. <p>Nothing to see here yet.</p>
  47. <p>Structures and Volumes can be loaded from the <Icon svg={HomeOutlined} /> tab.</p>
  48. </div>;
  49. }
  50. return <StateTreeNode cell={this.props.state.cells.get(ref)!} depth={0} />;
  51. }
  52. }
  53. class StateTreeNode extends PluginUIComponent<{ cell: StateObjectCell, depth: number }, { isCollapsed: boolean, isNull: boolean, showLabel: boolean }> {
  54. is(e: State.ObjectEvent) {
  55. return e.ref === this.ref && e.state === this.props.cell.parent;
  56. }
  57. get ref() {
  58. return this.props.cell.transform.ref;
  59. }
  60. componentDidMount() {
  61. this.subscribe(this.plugin.state.events.cell.stateUpdated, e => {
  62. if (this.props.cell === e.cell && this.is(e) && e.state.cells.has(this.ref)) {
  63. if (this.state.isCollapsed !== !!e.cell.state.isCollapsed
  64. || this.state.isNull !== StateTreeNode.isNull(e.cell)
  65. || this.state.showLabel !== StateTreeNode.showLabel(e.cell)) {
  66. this.forceUpdate();
  67. }
  68. }
  69. });
  70. this.subscribe(this.plugin.state.events.cell.created, e => {
  71. if (this.props.cell.parent === e.state && this.ref === e.cell.transform.parent) {
  72. this.forceUpdate();
  73. }
  74. });
  75. this.subscribe(this.plugin.state.events.cell.removed, e => {
  76. if (this.props.cell.parent === e.state && this.ref === e.parent) {
  77. this.forceUpdate();
  78. }
  79. });
  80. }
  81. state = {
  82. isCollapsed: !!this.props.cell.state.isCollapsed,
  83. isNull: StateTreeNode.isNull(this.props.cell),
  84. showLabel: StateTreeNode.showLabel(this.props.cell)
  85. }
  86. static getDerivedStateFromProps(props: _Props<StateTreeNode>, state: _State<StateTreeNode>): _State<StateTreeNode> | null {
  87. const isNull = StateTreeNode.isNull(props.cell);
  88. const showLabel = StateTreeNode.showLabel(props.cell);
  89. if (!!props.cell.state.isCollapsed === state.isCollapsed && state.isNull === isNull && state.showLabel === showLabel) return null;
  90. return { isCollapsed: !!props.cell.state.isCollapsed, isNull, showLabel };
  91. }
  92. private static hasDecorator(cell: StateObjectCell) {
  93. const children = cell.parent!.tree.children.get(cell.transform.ref);
  94. if (children.size !== 1) return false;
  95. return !!cell.parent?.tree.transforms.get(children.first()).transformer.definition.isDecorator;
  96. }
  97. private static isNull(cell?: StateObjectCell) {
  98. return !cell || !cell.parent || cell.obj === StateObject.Null || !cell.parent.tree.transforms.has(cell.transform.ref);
  99. }
  100. private static showLabel(cell: StateObjectCell) {
  101. return (cell.transform.ref !== StateTransform.RootRef) && (cell.status !== 'ok' || (!cell.state.isGhost && !StateTreeNode.hasDecorator(cell)));
  102. }
  103. render() {
  104. if (this.state.isNull) {
  105. return null;
  106. }
  107. const cell = this.props.cell!;
  108. const children = cell.parent!.tree.children.get(this.ref);
  109. if (!this.state.showLabel) {
  110. if (children.size === 0) return null;
  111. return <>
  112. {children.map(c => <StateTreeNode cell={cell.parent!.cells.get(c!)!} key={c} depth={this.props.depth} />)}
  113. </>;
  114. }
  115. const newDepth = this.props.depth + 1;
  116. return <>
  117. <StateTreeNodeLabel cell={cell} depth={this.props.depth} />
  118. {children.size === 0
  119. ? void 0
  120. : <div style={{ display: this.state.isCollapsed ? 'none' : 'block' }}>
  121. {children.map(c => <StateTreeNode cell={cell.parent!.cells.get(c!)!} key={c} depth={newDepth} />)}
  122. </div>
  123. }
  124. </>;
  125. }
  126. }
  127. interface StateTreeNodeLabelState {
  128. isCurrent: boolean,
  129. isCollapsed: boolean,
  130. action?: 'options' | 'apply',
  131. currentAction?: StateAction
  132. }
  133. class StateTreeNodeLabel extends PluginUIComponent<{ cell: StateObjectCell, depth: number }, StateTreeNodeLabelState> {
  134. is(e: State.ObjectEvent) {
  135. return e.ref === this.ref && e.state === this.props.cell.parent;
  136. }
  137. get ref() {
  138. return this.props.cell.transform.ref;
  139. }
  140. componentDidMount() {
  141. this.subscribe(this.plugin.state.events.cell.stateUpdated.pipe(filter(e => this.is(e)), debounceTime(33)), e => {
  142. this.forceUpdate();
  143. });
  144. this.subscribe(this.props.cell.parent!.behaviors.currentObject, e => {
  145. if (!this.is(e)) {
  146. if (this.state.isCurrent && e.state.transforms.has(this.ref)) {
  147. this._setCurrent(this.props.cell.parent!.current === this.ref, this.state.isCollapsed);
  148. }
  149. return;
  150. }
  151. if (e.state.transforms.has(this.ref)) {
  152. this._setCurrent(this.props.cell.parent!.current === this.ref, !!this.props.cell.state.isCollapsed);
  153. }
  154. });
  155. }
  156. private _setCurrent(isCurrent: boolean, isCollapsed: boolean) {
  157. if (isCurrent) {
  158. this.setState({ isCurrent, action: 'options', currentAction: void 0, isCollapsed });
  159. } else {
  160. this.setState({ isCurrent, action: void 0, currentAction: void 0, isCollapsed });
  161. }
  162. }
  163. state: StateTreeNodeLabelState = {
  164. isCurrent: this.props.cell.parent!.current === this.ref,
  165. isCollapsed: !!this.props.cell.state.isCollapsed,
  166. action: void 0,
  167. currentAction: void 0 as StateAction | undefined
  168. }
  169. static getDerivedStateFromProps(props: _Props<StateTreeNodeLabel>, state: _State<StateTreeNodeLabel>): _State<StateTreeNodeLabel> | null {
  170. const isCurrent = props.cell.parent!.current === props.cell.transform.ref;
  171. const isCollapsed = !!props.cell.state.isCollapsed;
  172. if (state.isCollapsed === isCollapsed && state.isCurrent === isCurrent) return null;
  173. return { isCurrent, isCollapsed, action: void 0, currentAction: void 0 };
  174. }
  175. setCurrent = (e?: React.MouseEvent<HTMLElement>) => {
  176. e?.preventDefault();
  177. e?.currentTarget.blur();
  178. PluginCommands.State.SetCurrentObject(this.plugin, { state: this.props.cell.parent!, ref: this.ref });
  179. }
  180. setCurrentRoot = (e?: React.MouseEvent<HTMLElement>) => {
  181. e?.preventDefault();
  182. e?.currentTarget.blur();
  183. PluginCommands.State.SetCurrentObject(this.plugin, { state: this.props.cell.parent!, ref: StateTransform.RootRef });
  184. }
  185. remove = (e?: React.MouseEvent<HTMLElement>) => {
  186. e?.preventDefault();
  187. PluginCommands.State.RemoveObject(this.plugin, { state: this.props.cell.parent!, ref: this.ref, removeParentGhosts: true });
  188. }
  189. toggleVisible = (e: React.MouseEvent<HTMLElement>) => {
  190. e.preventDefault();
  191. PluginCommands.State.ToggleVisibility(this.plugin, { state: this.props.cell.parent!, ref: this.ref });
  192. e.currentTarget.blur();
  193. }
  194. toggleExpanded = (e: React.MouseEvent<HTMLElement>) => {
  195. e.preventDefault();
  196. PluginCommands.State.ToggleExpanded(this.plugin, { state: this.props.cell.parent!, ref: this.ref });
  197. e.currentTarget.blur();
  198. }
  199. highlight = (e: React.MouseEvent<HTMLElement>) => {
  200. e.preventDefault();
  201. PluginCommands.Interactivity.Object.Highlight(this.plugin, { state: this.props.cell.parent!, ref: this.ref });
  202. e.currentTarget.blur();
  203. }
  204. clearHighlight = (e: React.MouseEvent<HTMLElement>) => {
  205. e.preventDefault();
  206. PluginCommands.Interactivity.ClearHighlights(this.plugin);
  207. e.currentTarget.blur();
  208. }
  209. hideApply = () => {
  210. this.setCurrentRoot();
  211. }
  212. get actions() {
  213. const cell = this.props.cell;
  214. const actions = [...cell.parent!.actions.fromCell(cell, this.plugin)];
  215. if (actions.length === 0) return;
  216. actions.sort((a, b) => a.definition.display.name < b.definition.display.name ? -1 : a.definition.display.name === b.definition.display.name ? 0 : 1);
  217. return [
  218. ActionMenu.Header('Apply Action'),
  219. ...actions.map(a => ActionMenu.Item(a.definition.display.name, () => this.setState({ action: 'apply', currentAction: a })))
  220. ];
  221. }
  222. selectAction: ActionMenu.OnSelect = item => {
  223. if (!item) return;
  224. (item?.value as any)();
  225. }
  226. updates(margin: string) {
  227. const cell = this.props.cell;
  228. const decoratorChain = StateTreeSpine.getDecoratorChain(cell.parent!, cell.transform.ref);
  229. const decorators = [];
  230. for (let i = decoratorChain.length - 1; i >= 0; i--) {
  231. const d = decoratorChain[i];
  232. decorators!.push(<UpdateTransformControl key={`${d.transform.transformer.id}-${i}`} state={cell.parent!} transform={d.transform} noMargin wrapInExpander expanderHeaderLeftMargin={margin} />);
  233. }
  234. return <div className='msp-tree-updates-wrapper'>{decorators}</div>;
  235. }
  236. render() {
  237. const cell = this.props.cell;
  238. const n = cell.transform;
  239. if (!cell) return null;
  240. const isCurrent = this.is(cell.parent!.behaviors.currentObject.value);
  241. const disabled = cell.status !== 'error' && cell.status !== 'ok';
  242. let label: React.ReactNode;
  243. if (cell.status === 'error' || !cell.obj) {
  244. const name = cell.status === 'error' ? cell.errorText : n.transformer.definition.display.name;
  245. label = <Button className='msp-btn-tree-label msp-no-hover-outline' noOverflow title={name} onClick={this.state.isCurrent ? this.setCurrentRoot : this.setCurrent} disabled={disabled}>
  246. {cell.status === 'error' && <b>[{cell.status}]</b>} <span>{name}</span>
  247. </Button>;
  248. } else {
  249. const obj = cell.obj as PluginStateObject.Any;
  250. const title = `${obj.label} ${obj.description ? obj.description : ''}`;
  251. label = <Button className={`msp-btn-tree-label msp-type-class-${obj.type.typeClass}`} noOverflow disabled={disabled} title={title} onClick={this.state.isCurrent ? this.setCurrentRoot : this.setCurrent}>
  252. <span>{obj.label}</span> {obj.description ? <small>{obj.description}</small> : void 0}
  253. </Button>;
  254. }
  255. const children = cell.parent!.tree.children.get(this.ref);
  256. const cellState = cell.state;
  257. const expand = <IconButton svg={cellState.isCollapsed ? ArrowRight : ArrowDropDown} flex='20px' disabled={disabled} onClick={this.toggleExpanded} transparent className='msp-no-hover-outline' style={{ visibility: children.size > 0 ? 'visible' : 'hidden' }} />;
  258. const remove = !cell.state.isLocked ? <IconButton svg={DeleteOutlined} onClick={this.remove} disabled={disabled} small toggleState={false} /> : void 0;
  259. const visibility = <IconButton svg={cellState.isHidden ? VisibilityOffOutlined : VisibilityOutlined} toggleState={false} disabled={disabled} small onClick={this.toggleVisible} />;
  260. const marginStyle: React.CSSProperties = {
  261. marginLeft: `${this.props.depth * 8}px`
  262. };
  263. const row = <div className={`msp-flex-row msp-tree-row${isCurrent ? ' msp-tree-row-current' : ''}`} onMouseEnter={this.highlight} onMouseLeave={this.clearHighlight} style={marginStyle}>
  264. {expand}
  265. {label}
  266. {remove}
  267. {visibility}
  268. </div>;
  269. if (!isCurrent) return row;
  270. if (this.state.action === 'apply' && this.state.currentAction) {
  271. return <div style={{ marginBottom: '1px' }}>
  272. {row}
  273. <ControlGroup header={`Apply ${this.state.currentAction.definition.display.name}`} initialExpanded={true} hideExpander={true} hideOffset={false} onHeaderClick={this.hideApply} topRightIcon={Close} headerLeftMargin={`${this.props.depth * 8 + 21}px`}>
  274. <ApplyActionControl onApply={this.hideApply} state={this.props.cell.parent!} action={this.state.currentAction} nodeRef={this.props.cell.transform.ref} hideHeader noMargin />
  275. </ControlGroup>
  276. </div>;
  277. }
  278. if (this.state.action === 'options') {
  279. const actions = this.actions;
  280. const updates = this.updates(`${this.props.depth * 8 + 21}px`);
  281. return <div style={{ marginBottom: '1px' }}>
  282. {row}
  283. {updates}
  284. {actions && <div style={{ marginLeft: `${this.props.depth * 8 + 21}px`, marginTop: '-1px' }}>
  285. <ActionMenu items={actions} onSelect={this.selectAction} />
  286. </div>}
  287. </div>;
  288. }
  289. return row;
  290. }
  291. }