Skip to content
Snippets Groups Projects
Commit 9f803b22 authored by David Sehnal's avatar David Sehnal
Browse files

mol-plugin: state tree tweaks

parent 6769158e
No related branches found
No related tags found
No related merge requests found
...@@ -58,22 +58,26 @@ class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, { ...@@ -58,22 +58,26 @@ class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, {
}); });
} }
toggleExpanded = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
PluginCommands.State.ToggleExpanded.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}
render() { render() {
const cellState = this.props.state.tree.cellStates.get(this.props.nodeRef); const cellState = this.cellState;
const expander = <> const expander = <>
[<a href='#' onClick={e => { [<a href='#' onClick={this.toggleExpanded}>{cellState.isCollapsed ? '+' : '-'}</a>]
e.preventDefault();
PluginCommands.State.ToggleExpanded.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}}>{cellState.isCollapsed ? '+' : '-'}</a>]
</>; </>;
const children = this.props.state.tree.children.get(this.props.nodeRef); const children = this.props.state.tree.children.get(this.props.nodeRef);
return <div> return <div>
{children.size === 0 ? void 0 : expander} <StateTreeNodeLabel nodeRef={this.props.nodeRef} state={this.props.state} /> {children.size === 0 ? void 0 : expander} <StateTreeNodeLabel nodeRef={this.props.nodeRef} state={this.props.state} />
{cellState.isCollapsed || children.size === 0 {children.size === 0
? void 0 ? void 0
: <div style={{ marginLeft: '7px', paddingLeft: '3px', borderLeft: '1px solid #999' }}>{children.map(c => <StateTreeNode state={this.props.state} nodeRef={c!} key={c} />)}</div> : <div style={{ marginLeft: '7px', paddingLeft: '3px', borderLeft: '1px solid #999', display: cellState.isCollapsed ? 'none' : 'block' }}>
{children.map(c => <StateTreeNode state={this.props.state} nodeRef={c!} key={c} />)}
</div>
} }
</div>; </div>;
} }
...@@ -92,58 +96,53 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State ...@@ -92,58 +96,53 @@ class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State
let isCurrent = this.is(this.props.state.behaviors.currentObject.value); let isCurrent = this.is(this.props.state.behaviors.currentObject.value);
this.subscribe(this.plugin.state.behavior.currentObject, e => { this.subscribe(this.plugin.state.behavior.currentObject, e => {
let update = false;
if (this.is(e)) { if (this.is(e)) {
if (!isCurrent) { if (!isCurrent) {
isCurrent = true; isCurrent = true;
update = true; this.forceUpdate();
} }
} else if (isCurrent) { } else if (isCurrent) {
isCurrent = false; isCurrent = false;
update = true; // have to check the node wasn't remove
} if (e.state.tree.transforms.has(this.props.nodeRef)) this.forceUpdate();
if (update && e.state.tree.transforms.has(this.props.nodeRef)) {
this.forceUpdate();
} }
}); });
} }
setCurrent = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
PluginCommands.State.SetCurrentObject.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}
remove = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
PluginCommands.State.RemoveObject.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}
toggleVisible = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
PluginCommands.State.ToggleVisibility.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}
render() { render() {
const n = this.props.state.tree.transforms.get(this.props.nodeRef)!; const n = this.props.state.tree.transforms.get(this.props.nodeRef)!;
const cell = this.props.state.cells.get(this.props.nodeRef)!; const cell = this.props.state.cells.get(this.props.nodeRef)!;
const isCurrent = this.is(this.props.state.behaviors.currentObject.value); const isCurrent = this.is(this.props.state.behaviors.currentObject.value);
const remove = <>[<a href='#' onClick={e => { const remove = <>[<a href='#' onClick={this.remove}>X</a>]</>
e.preventDefault();
PluginCommands.State.RemoveObject.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}}>X</a>]</>
let label: any; let label: any;
if (cell.status !== 'ok' || !cell.obj) { if (cell.status !== 'ok' || !cell.obj) {
const name = (n.transformer.definition.display && n.transformer.definition.display.name) || n.transformer.definition.name; const name = (n.transformer.definition.display && n.transformer.definition.display.name) || n.transformer.definition.name;
label = <><b>{cell.status}</b> <a href='#' onClick={e => { label = <><b>{cell.status}</b> <a href='#' onClick={this.setCurrent}>{name}</a>: <i>{cell.errorText}</i></>;
e.preventDefault();
PluginCommands.State.SetCurrentObject.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}}>{name}</a>: <i>{cell.errorText}</i></>;
} else { } else {
const obj = cell.obj as PluginStateObject.Any; const obj = cell.obj as PluginStateObject.Any;
label = <><a href='#' onClick={e => { label = <><a href='#' onClick={this.setCurrent}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
e.preventDefault();
PluginCommands.State.SetCurrentObject.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
} }
const cellState = this.props.state.tree.cellStates.get(this.props.nodeRef); const cellState = this.props.state.tree.cellStates.get(this.props.nodeRef);
const visibility = <>[<a href='#' onClick={this.toggleVisible}>{cellState.isHidden ? 'H' : 'V'}</a>]</>;
if (!cellState) console.log('missing state', this.props.nodeRef, this.props.state.tree, this.props.state.tree.transforms.has(this.props.nodeRef));
const visibility = <>
[<a href='#' onClick={e => {
e.preventDefault();
PluginCommands.State.ToggleVisibility.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
}}>{cellState.isHidden ? 'H' : 'V'}</a>]
</>;
return <> return <>
{remove}{visibility} {isCurrent ? <b>{label}</b> : label} {remove}{visibility} {isCurrent ? <b>{label}</b> : label}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment