Skip to content
Snippets Groups Projects
state-tree.tsx 2.58 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>]</>

David Sehnal's avatar
David Sehnal committed
        if (cell.status !== 'ok' || !cell.obj) {
David Sehnal's avatar
David Sehnal committed
            return <div style={{ borderLeft: '1px solid black', paddingLeft: '7px' }}>
David Sehnal's avatar
David Sehnal committed
                {remove} {cell.status} {cell.errorText}
David Sehnal's avatar
David Sehnal committed
            </div>;
        }
David Sehnal's avatar
David Sehnal committed
        const obj = cell.obj as PluginStateObject.Any;
        const props = obj.props;
        const type = obj.type;
David Sehnal's avatar
David Sehnal committed
        return <div style={{ borderLeft: '0px solid #999', paddingLeft: '0px' }}>
            {remove}[<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}
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>