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