tree.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, StateObject, StateTransform, StateObjectCell } from 'mol-state'
  9. import { PluginCommands } from 'mol-plugin/command';
  10. import { PluginUIComponent, _Props, _State } from '../base';
  11. import { StateObjectActions } from './actions';
  12. export class StateTree extends PluginUIComponent<{ state: State }, { showActions: boolean }> {
  13. state = { showActions: true };
  14. componentDidMount() {
  15. this.subscribe(this.plugin.events.state.cell.created, e => {
  16. if (e.cell.transform.parent === StateTransform.RootRef) this.forceUpdate();
  17. });
  18. this.subscribe(this.plugin.events.state.cell.removed, e => {
  19. if (e.parent === StateTransform.RootRef) this.forceUpdate();
  20. });
  21. }
  22. static getDerivedStateFromProps(props: { state: State }, state: { showActions: boolean }) {
  23. const n = props.state.tree.root.ref;
  24. const children = props.state.tree.children.get(n);
  25. const showActions = children.size === 0;
  26. if (state.showActions === showActions) return null;
  27. return { showActions };
  28. }
  29. render() {
  30. const ref = this.props.state.tree.root.ref;
  31. if (this.state.showActions) {
  32. return <StateObjectActions state={this.props.state} nodeRef={ref} hideHeader={true} />
  33. }
  34. return <StateTreeNode cell={this.props.state.cells.get(ref)!} depth={0} />;
  35. }
  36. }
  37. class StateTreeNode extends PluginUIComponent<{ cell: StateObjectCell, depth: number }, { isCollapsed: boolean }> {
  38. is(e: State.ObjectEvent) {
  39. return e.ref === this.ref && e.state === this.props.cell.parent;
  40. }
  41. get ref() {
  42. return this.props.cell.transform.ref;
  43. }
  44. componentDidMount() {
  45. this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
  46. if (this.props.cell === e.cell && this.is(e) && e.state.cells.has(this.ref)) {
  47. this.forceUpdate();
  48. // if (!!this.props.cell.transform.state.isCollapsed !== this.state.isCollapsed) {
  49. // this.setState({ isCollapsed: !!e.cell.transform.state.isCollapsed });
  50. // }
  51. }
  52. });
  53. this.subscribe(this.plugin.events.state.cell.created, e => {
  54. if (this.props.cell.parent === e.state && this.ref === e.cell.transform.parent) {
  55. this.forceUpdate();
  56. }
  57. });
  58. this.subscribe(this.plugin.events.state.cell.removed, e => {
  59. if (this.props.cell.parent === e.state && this.ref === e.parent) {
  60. this.forceUpdate();
  61. }
  62. });
  63. }
  64. state = {
  65. isCollapsed: !!this.props.cell.transform.state.isCollapsed
  66. }
  67. static getDerivedStateFromProps(props: _Props<StateTreeNode>, state: _State<StateTreeNode>): _State<StateTreeNode> | null {
  68. if (!!props.cell.transform.state.isCollapsed === state.isCollapsed) return null;
  69. return { isCollapsed: !!props.cell.transform.state.isCollapsed };
  70. }
  71. render() {
  72. const cell = this.props.cell;
  73. if (!cell || cell.obj === StateObject.Null || !cell.parent.tree.transforms.has(cell.transform.ref)) {
  74. return null;
  75. }
  76. const cellState = cell.transform.state;
  77. const showLabel = cell.status !== 'ok' || !cell.transform.state.isGhost;
  78. const children = cell.parent.tree.children.get(this.ref);
  79. const newDepth = showLabel ? this.props.depth + 1 : this.props.depth;
  80. if (!showLabel) {
  81. if (children.size === 0) return null;
  82. return <div style={{ display: cellState.isCollapsed ? 'none' : 'block' }}>
  83. {children.map(c => <StateTreeNode cell={cell.parent.cells.get(c!)!} key={c} depth={newDepth} />)}
  84. </div>;
  85. }
  86. return <>
  87. <StateTreeNodeLabel cell={cell} depth={this.props.depth} />
  88. {children.size === 0
  89. ? void 0
  90. : <div style={{ display: cellState.isCollapsed ? 'none' : 'block' }}>
  91. {children.map(c => <StateTreeNode cell={cell.parent.cells.get(c!)!} key={c} depth={newDepth} />)}
  92. </div>
  93. }
  94. </>;
  95. }
  96. }
  97. class StateTreeNodeLabel extends PluginUIComponent<
  98. { cell: StateObjectCell, depth: number },
  99. { isCurrent: boolean, isCollapsed: boolean }> {
  100. is(e: State.ObjectEvent) {
  101. return e.ref === this.ref && e.state === this.props.cell.parent;
  102. }
  103. get ref() {
  104. return this.props.cell.transform.ref;
  105. }
  106. componentDidMount() {
  107. this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
  108. if (this.is(e)) this.forceUpdate();
  109. });
  110. this.subscribe(this.plugin.state.behavior.currentObject, e => {
  111. if (!this.is(e)) {
  112. if (this.state.isCurrent && e.state.transforms.has(this.ref)) {
  113. this.setState({ isCurrent: this.props.cell.parent.current === this.ref });
  114. }
  115. return;
  116. }
  117. if (e.state.transforms.has(this.ref)) {
  118. this.setState({
  119. isCurrent: this.props.cell.parent.current === this.ref,
  120. isCollapsed: !!this.props.cell.transform.state.isCollapsed
  121. });
  122. }
  123. });
  124. }
  125. state = {
  126. isCurrent: this.props.cell.parent.current === this.ref,
  127. isCollapsed: !!this.props.cell.transform.state.isCollapsed
  128. }
  129. static getDerivedStateFromProps(props: _Props<StateTreeNodeLabel>, state: _State<StateTreeNodeLabel>): _State<StateTreeNodeLabel> | null {
  130. const isCurrent = props.cell.parent.current === props.cell.transform.ref;
  131. const isCollapsed = !!props.cell.transform.state.isCollapsed;
  132. if (state.isCollapsed === isCollapsed && state.isCurrent === isCurrent) return null;
  133. return { isCurrent, isCollapsed };
  134. }
  135. setCurrent = (e: React.MouseEvent<HTMLElement>) => {
  136. e.preventDefault();
  137. e.currentTarget.blur();
  138. PluginCommands.State.SetCurrentObject.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref });
  139. }
  140. remove = (e: React.MouseEvent<HTMLElement>) => {
  141. e.preventDefault();
  142. PluginCommands.State.RemoveObject.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref, removeParentGhosts: true });
  143. }
  144. toggleVisible = (e: React.MouseEvent<HTMLElement>) => {
  145. e.preventDefault();
  146. PluginCommands.State.ToggleVisibility.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref });
  147. e.currentTarget.blur();
  148. }
  149. toggleExpanded = (e: React.MouseEvent<HTMLElement>) => {
  150. e.preventDefault();
  151. PluginCommands.State.ToggleExpanded.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref });
  152. e.currentTarget.blur();
  153. }
  154. highlight = (e: React.MouseEvent<HTMLElement>) => {
  155. e.preventDefault();
  156. PluginCommands.State.Highlight.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref });
  157. e.currentTarget.blur();
  158. }
  159. clearHighlight = (e: React.MouseEvent<HTMLElement>) => {
  160. e.preventDefault();
  161. PluginCommands.State.ClearHighlight.dispatch(this.plugin, { state: this.props.cell.parent, ref: this.ref });
  162. e.currentTarget.blur();
  163. }
  164. // private toggleUpdaterObs = new Subject();
  165. // toggleUpdater = (e: React.MouseEvent<HTMLAnchorElement>) => {
  166. // e.preventDefault();
  167. // e.currentTarget.blur();
  168. // this.toggleUpdaterObs.next();
  169. // }
  170. render() {
  171. const cell = this.props.cell;
  172. const n = cell.transform;
  173. if (!cell) return null;
  174. const isCurrent = this.state.isCurrent; // this.is(cell.parent.behaviors.currentObject.value);
  175. let label: any;
  176. if (cell.status === 'pending' || cell.status === 'processing') {
  177. const name = n.transformer.definition.display.name;
  178. label = <><b>[{cell.status}]</b> <span title={name}>{name}</span></>;
  179. } else if (cell.status !== 'ok' || !cell.obj) {
  180. const name = n.transformer.definition.display.name;
  181. const title = `${cell.errorText}`;
  182. label = <><a title={title} href='#' onClick={this.setCurrent}><b>[{cell.status}]</b> {name}: <i>{cell.errorText}</i> </a></>;
  183. } else {
  184. const obj = cell.obj as PluginStateObject.Any;
  185. const title = `${obj.label} ${obj.description ? obj.description : ''}`
  186. if (this.state.isCurrent) {
  187. label = <><a title={title} href='#'><b>{obj.label}</b> {obj.description ? <small>{obj.description}</small> : void 0}</a></>;
  188. } else {
  189. label = <><a title={title} href='#' onClick={this.setCurrent}>{obj.label} {obj.description ? <small>{obj.description}</small> : void 0}</a></>;
  190. }
  191. }
  192. const children = cell.parent.tree.children.get(this.ref);
  193. const cellState = cell.transform.state;
  194. const visibility = <button onClick={this.toggleVisible} className={`msp-btn msp-btn-link msp-tree-visibility${cellState.isHidden ? ' msp-tree-visibility-hidden' : ''}`}>
  195. <span className='msp-icon msp-icon-visual-visibility' />
  196. </button>;
  197. const style: React.HTMLAttributes<HTMLDivElement>['style'] = {
  198. marginLeft: /* this.state.isCurrent ? void 0 :*/ `${this.props.depth * 10}px`,
  199. // paddingLeft: !this.state.isCurrent ? void 0 : `${this.props.depth * 10}px`,
  200. borderLeft: /* isCurrent || */ this.props.depth === 0 ? 'none' : void 0
  201. }
  202. const row = <div className={`msp-tree-row${isCurrent ? ' msp-tree-row-current' : ''}`} onMouseEnter={this.highlight} onMouseLeave={this.clearHighlight} style={style}>
  203. {label}
  204. {children.size > 0 && <button onClick={this.toggleExpanded} className='msp-btn msp-btn-link msp-tree-toggle-exp-button'>
  205. <span className={`msp-icon msp-icon-${cellState.isCollapsed ? 'expand' : 'collapse'}`} />
  206. </button>}
  207. {!cell.transform.state.isLocked && <button onClick={this.remove} className='msp-btn msp-btn-link msp-tree-remove-button'>
  208. <span className='msp-icon msp-icon-remove' />
  209. </button>}{visibility}
  210. </div>;
  211. // if (this.state.isCurrent) {
  212. // return <>
  213. // {row}
  214. // <StateTreeNodeTransform {...this.props} toggleCollapsed={this.toggleUpdaterObs} />
  215. // </>
  216. // }
  217. return row;
  218. }
  219. }
  220. // class StateTreeNodeTransform extends PluginUIComponent<{ nodeRef: string, state: State, depth: number, toggleCollapsed?: Observable<any> }> {
  221. // componentDidMount() {
  222. // // this.subscribe(this.plugin.events.state.object.updated, ({ ref, state }) => {
  223. // // if (this.props.nodeRef !== ref || this.props.state !== state) return;
  224. // // this.forceUpdate();
  225. // // });
  226. // }
  227. // render() {
  228. // const ref = this.props.nodeRef;
  229. // const cell = this.props.state.cells.get(ref)!;
  230. // const parent: StateObjectCell | undefined = (cell.sourceRef && this.props.state.cells.get(cell.sourceRef)!) || void 0;
  231. // if (!parent || parent.status !== 'ok') return null;
  232. // const transform = cell.transform;
  233. // return <UpdateTransformContol state={this.props.state} transform={transform} initiallyCollapsed={true} toggleCollapsed={this.props.toggleCollapsed} />;
  234. // }
  235. // }