Skip to content
Snippets Groups Projects
state-tree.tsx 5.71 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';
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} />
            {/* n.children.map(c => <StateTreeNode plugin={this.props.plugin} nodeRef={c!} key={c} />) */}
class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, { }> {
    is(e: State.ObjectEvent) {
        return e.ref === this.props.nodeRef && e.state === this.props.state;
    }

    get cellState() {
David Sehnal's avatar
David Sehnal committed
        return this.props.state.cellStates.get(this.props.nodeRef);
    componentDidMount() {
        let isCollapsed = this.cellState.isCollapsed;
        this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
            if (this.is(e) && isCollapsed !== e.cellState.isCollapsed) {
                isCollapsed = e.cellState.isCollapsed;
                this.forceUpdate();
            }
        });

        this.subscribe(this.plugin.events.state.cell.created, e => {
            if (this.props.state === e.state && this.props.nodeRef === e.cell.transform.parent) {
                this.forceUpdate();
            }
        });

        this.subscribe(this.plugin.events.state.cell.removed, e => {
            if (this.props.state === e.state && this.props.nodeRef === e.parent) {
                this.forceUpdate();
            }
        });
    }

    toggleExpanded = (e: React.MouseEvent<HTMLElement>) => {
        e.preventDefault();
        PluginCommands.State.ToggleExpanded.dispatch(this.plugin, { state: this.props.state, ref: this.props.nodeRef });
    }

        const cellState = this.cellState;

        const expander = <>
            [<a href='#' onClick={this.toggleExpanded}>{cellState.isCollapsed ? '+' : '-'}</a>]
        </>;

        const children = this.props.state.tree.children.get(this.props.nodeRef);
        return <div>
            {children.size === 0 ? void 0 : expander} <StateTreeNodeLabel nodeRef={this.props.nodeRef} state={this.props.state} />
            {children.size === 0
                : <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>;
    }
}

class StateTreeNodeLabel extends PluginComponent<{ nodeRef: string, state: State }> {
    is(e: State.ObjectEvent) {
        return e.ref === this.props.nodeRef && e.state === this.props.state;
    }

    componentDidMount() {
        this.subscribe(this.plugin.events.state.cell.stateUpdated, e => {
            if (this.is(e)) this.forceUpdate();
        });

        let isCurrent = this.is(this.props.state.behaviors.currentObject.value);

        this.subscribe(this.plugin.state.behavior.currentObject, e => {
            if (this.is(e)) {
                if (!isCurrent) {
                    isCurrent = true;
                    this.forceUpdate();
                }
            } else if (isCurrent) {
                isCurrent = false;
                // have to check the node wasn't remove
David Sehnal's avatar
David Sehnal committed
                if (e.state.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 });
    }

David Sehnal's avatar
David Sehnal committed
    render() {
David Sehnal's avatar
David Sehnal committed
        const n = this.props.state.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 isCurrent = this.is(this.props.state.behaviors.currentObject.value);

        const remove = <>[<a href='#' onClick={this.remove}>X</a>]</>
David Sehnal's avatar
David Sehnal committed

        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={this.setCurrent}>{name}</a>: <i>{cell.errorText}</i></>;
        } else {
            const obj = cell.obj as PluginStateObject.Any;
            label = <><a href='#' onClick={this.setCurrent}>{obj.label}</a> {obj.description ? <small>{obj.description}</small> : void 0}</>;
David Sehnal's avatar
David Sehnal committed
        const cellState = this.props.state.cellStates.get(this.props.nodeRef);
        const visibility = <>[<a href='#' onClick={this.toggleVisible}>{cellState.isHidden ? 'H' : 'V'}</a>]</>;
        return <>
            {remove}{visibility} {isCurrent ? <b>{label}</b> : label}
        </>;