Skip to content
Snippets Groups Projects
state-tree.tsx 3.47 KiB
Newer Older
David Sehnal's avatar
David Sehnal committed
/**
 * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author David Sehnal <david.sehnal@gmail.com>
 */

import * as React from 'react';
import { PluginStateObject } from 'mol-plugin/state/objects';
David Sehnal's avatar
David Sehnal committed
import { State } from 'mol-state'
David Sehnal's avatar
David Sehnal committed
import { PluginCommands } from 'mol-plugin/command';
import { PluginComponent } from './base';
import { merge } from 'rxjs';
export class StateTree extends PluginComponent<{ state: State }, { }> {
        this.subscribe(this.props.state.events.changed, () => this.forceUpdate());
David Sehnal's avatar
David Sehnal committed
    render() {
David Sehnal's avatar
David Sehnal committed
        // const n = this.props.plugin.state.data.tree.nodes.get(this.props.plugin.state.data.tree.rootRef)!;
David Sehnal's avatar
David Sehnal committed
        const n = this.props.state.tree.root.ref;
David Sehnal's avatar
David Sehnal committed
        return <div>
            <StateTreeNode state={this.props.state} nodeRef={n} key={n} />
David Sehnal's avatar
David Sehnal committed
            { /* n.children.map(c => <StateTreeNode plugin={this.props.plugin} nodeRef={c!} key={c} />) */}
export class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, { }> {
    componentDidMount() {
        this.subscribe(merge(this.context.events.state.data.object.cellState, this.context.events.state.behavior.object.cellState), o => {
            if (o.ref === this.props.nodeRef && o.state === this.props.state) this.forceUpdate();
        });
    }

David Sehnal's avatar
David Sehnal committed
    render() {
David Sehnal's avatar
David Sehnal committed
        const n = this.props.state.tree.transforms.get(this.props.nodeRef)!;
David Sehnal's avatar
David Sehnal committed
        const cell = this.props.state.cells.get(this.props.nodeRef)!;
David Sehnal's avatar
David Sehnal committed

        const remove = <>[<a href='#' onClick={e => {
            e.preventDefault();
            PluginCommands.State.RemoveObject.dispatch(this.context, { state: this.props.state, ref: this.props.nodeRef });
David Sehnal's avatar
David Sehnal committed
        }}>X</a>]</>

        let label: any;
David Sehnal's avatar
David Sehnal committed
        if (cell.status !== 'ok' || !cell.obj) {
David Sehnal's avatar
David Sehnal committed
            const name = (n.transformer.definition.display && n.transformer.definition.display.name) || n.transformer.definition.name;
            label = <><b>{cell.status}</b> <a href='#' onClick={e => {
                e.preventDefault();
                PluginCommands.State.SetCurrentObject.dispatch(this.context, { state: this.props.state, ref: this.props.nodeRef });
            }}>{name}</a>: <i>{cell.errorText}</i></>;
        } else {
            const obj = cell.obj as PluginStateObject.Any;
            label = <><a href='#' onClick={e => {
David Sehnal's avatar
David Sehnal committed
                e.preventDefault();
                PluginCommands.State.SetCurrentObject.dispatch(this.context, { state: this.props.state, ref: this.props.nodeRef });
            }}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
David Sehnal's avatar
David Sehnal committed
        const cellState = this.props.state.tree.cellStates.get(this.props.nodeRef);

        const expander = <>
            [<a href='#' onClick={e => {
                e.preventDefault();
                PluginCommands.State.ToggleExpanded.dispatch(this.context, { state: this.props.state, ref: this.props.nodeRef });
David Sehnal's avatar
David Sehnal committed
            }}>{cellState.isCollapsed ? '+' : '-'}</a>]
David Sehnal's avatar
David Sehnal committed
        const children = this.props.state.tree.children.get(this.props.nodeRef);
        return <div>
            {remove}{children.size === 0 ? void 0 : expander} {label}
David Sehnal's avatar
David Sehnal committed
            {cellState.isCollapsed || children.size === 0
David Sehnal's avatar
David Sehnal committed
                ? 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>