tree.tsx 14 KB

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