Skip to content
Snippets Groups Projects
state-tree.tsx 2.86 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 { PluginContext } from '../context';
import { PluginStateObject } from 'mol-plugin/state/base';
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';
David Sehnal's avatar
David Sehnal committed
export class StateTree extends React.Component<{ plugin: PluginContext, state: State }, { }> {
David Sehnal's avatar
David Sehnal committed
    componentDidMount() {
        // TODO: move to constructor?
David Sehnal's avatar
David Sehnal committed
        this.props.state.context.events.updated.subscribe(() => 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.rootRef;
David Sehnal's avatar
David Sehnal committed
        return <div>
David Sehnal's avatar
David Sehnal committed
            <StateTreeNode plugin={this.props.plugin} 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} />) */}
David Sehnal's avatar
David Sehnal committed
export class StateTreeNode extends React.Component<{ plugin: PluginContext, nodeRef: string, state: State }, { }> {
David Sehnal's avatar
David Sehnal committed
    render() {
David Sehnal's avatar
David Sehnal committed
        const n = this.props.state.tree.nodes.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.Data.RemoveObject.dispatch(this.props.plugin, { ref: this.props.nodeRef });
        }}>X</a>]</>

        let label: any;
David Sehnal's avatar
David Sehnal committed
        if (cell.status !== 'ok' || !cell.obj) {
            const name = (n.value.transformer.definition.display && n.value.transformer.definition.display.name) || n.value.transformer.definition.name;
            label = <><b>{cell.status}</b> <a href='#' onClick={e => {
                e.preventDefault();
                PluginCommands.Data.SetCurrentObject.dispatch(this.props.plugin, { ref: this.props.nodeRef });
            }}>{name}</a>: <i>{cell.errorText}</i></>;
        } else {
            const obj = cell.obj as PluginStateObject.Any;
            const props = obj.props;
            const type = obj.type;
            label = <>[<span title={type.description}>{ type.shortName }</span>] <a href='#' onClick={e => {
David Sehnal's avatar
David Sehnal committed
                e.preventDefault();
                PluginCommands.Data.SetCurrentObject.dispatch(this.props.plugin, { ref: this.props.nodeRef });
            }}>{props.label}</a> {props.description ? <small>{props.description}</small> : void 0}</>;
        }

        return <div>
            {remove}{label}
David Sehnal's avatar
David Sehnal committed
            {n.children.size === 0
                ? void 0
David Sehnal's avatar
David Sehnal committed
                : <div style={{ marginLeft: '7px', paddingLeft: '3px', borderLeft: '1px solid #999' }}>{n.children.map(c => <StateTreeNode plugin={this.props.plugin} state={this.props.state} nodeRef={c!} key={c} />)}</div>