Skip to content
Snippets Groups Projects
context.ts 7.99 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 { Canvas3D } from 'mol-canvas3d/canvas3d';
import { EmptyLoci, Loci } from 'mol-model/loci';
import { Representation } from 'mol-repr/representation';
import { StructureRepresentationRegistry } from 'mol-repr/structure/registry';
import { State, Transform, Transformer } from 'mol-state';
import { Task } from 'mol-task';
import { ColorTheme } from 'mol-theme/color';
import { SizeTheme } from 'mol-theme/size';
import { ThemeRegistryContext } from 'mol-theme/theme';
import { LogEntry } from 'mol-util/log-entry';
import { RxEventHelper } from 'mol-util/rx-event-helper';
import { merge } from 'rxjs';
import { BuiltInPluginBehaviors } from './behavior';
import { PluginCommand, PluginCommands } from './command';
import { PluginSpec } from './spec';
import { PluginState } from './state';
import { TaskManager } from './util/task-manager';
David Sehnal's avatar
David Sehnal committed
import { Color } from 'mol-util/color';
import { LociLabelEntry, LociLabelManager } from './util/loci-label-manager';
import { ajaxGet } from 'mol-util/data-source';
import { CustomPropertyRegistry } from './util/custom-prop-registry';
import { VolumeRepresentationRegistry } from 'mol-repr/volume/registry';
import { PLUGIN_VERSION, PLUGIN_VERSION_DATE } from './version';
David Sehnal's avatar
David Sehnal committed
import { PluginLayout } from './layout';
David Sehnal's avatar
David Sehnal committed
import { List } from 'immutable';
import { StateTransformParameters } from './ui/state/common';
David Sehnal's avatar
David Sehnal committed

export class PluginContext {
David Sehnal's avatar
David Sehnal committed
    private disposed = false;
    private ev = RxEventHelper.create();
    private tasks = new TaskManager();
David Sehnal's avatar
David Sehnal committed
    readonly state = new PluginState(this);
David Sehnal's avatar
David Sehnal committed
    readonly commands = new PluginCommand.Manager();
    readonly events = {
David Sehnal's avatar
David Sehnal committed
        state: {
            cell: {
                stateUpdated: merge(this.state.dataState.events.cell.stateUpdated, this.state.behaviorState.events.cell.stateUpdated),
                created: merge(this.state.dataState.events.cell.created, this.state.behaviorState.events.cell.created),
                removed: merge(this.state.dataState.events.cell.removed, this.state.behaviorState.events.cell.removed),
            },
            object: {
                created: merge(this.state.dataState.events.object.created, this.state.behaviorState.events.object.created),
                removed: merge(this.state.dataState.events.object.removed, this.state.behaviorState.events.object.removed),
                updated: merge(this.state.dataState.events.object.updated, this.state.behaviorState.events.object.updated)
            },
David Sehnal's avatar
David Sehnal committed
            cameraSnapshots: this.state.cameraSnapshots.events,
            snapshots: this.state.snapshots.events,
        log: this.ev<LogEntry>(),
        task: this.tasks.events,
        labels: {
            highlight: this.ev<{ entries: ReadonlyArray<LociLabelEntry> }>()
        },
        canvad3d: {
            settingsUpdated: this.ev()
David Sehnal's avatar
David Sehnal committed
    };

David Sehnal's avatar
David Sehnal committed
    readonly behaviors = {
        canvas: {
            highlightLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
            selectLoci: this.ev.behavior<{ loci: Loci, repr?: Representation.Any }>({ loci: EmptyLoci }),
David Sehnal's avatar
David Sehnal committed
        },
David Sehnal's avatar
David Sehnal committed
        command: this.commands.behaviour
    };

    readonly canvas3d: Canvas3D;
David Sehnal's avatar
David Sehnal committed
    readonly layout: PluginLayout = new PluginLayout(this);
David Sehnal's avatar
David Sehnal committed

    readonly lociLabels: LociLabelManager;

    readonly structureRepresentation = {
        registry: new StructureRepresentationRegistry(),
        themeCtx: { colorThemeRegistry: ColorTheme.createRegistry(), sizeThemeRegistry: SizeTheme.createRegistry() } as ThemeRegistryContext
    readonly volumeRepresentation = {
        registry: new VolumeRepresentationRegistry(),
        themeCtx: { colorThemeRegistry: ColorTheme.createRegistry(), sizeThemeRegistry: SizeTheme.createRegistry() } as ThemeRegistryContext
    }

    readonly customModelProperties = new CustomPropertyRegistry();
    readonly customParamEditors = new Map<string, StateTransformParameters.Class>();
David Sehnal's avatar
David Sehnal committed

David Sehnal's avatar
David Sehnal committed
    initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
        try {
David Sehnal's avatar
David Sehnal committed
            this.layout.setRoot(container);
            if (this.spec.initialLayout) this.layout.setProps(this.spec.initialLayout);
            (this.canvas3d as Canvas3D) = Canvas3D.create(canvas, container);
            PluginCommands.Canvas3D.SetSettings.dispatch(this, { settings: { backgroundColor: Color(0xFCFBF9) } });
David Sehnal's avatar
David Sehnal committed
            return true;
        } catch (e) {
            this.log.error('' + e);
David Sehnal's avatar
David Sehnal committed
            console.error(e);
            return false;
        }
    }

    readonly log = {
David Sehnal's avatar
David Sehnal committed
        entries: List<LogEntry>(),
        entry: (e: LogEntry) => this.events.log.next(e),
        error: (msg: string) => this.events.log.next(LogEntry.error(msg)),
        message: (msg: string) => this.events.log.next(LogEntry.message(msg)),
        info: (msg: string) => this.events.log.next(LogEntry.info(msg)),
        warn: (msg: string) => this.events.log.next(LogEntry.warning(msg)),
    };
David Sehnal's avatar
David Sehnal committed
    /**
     * This should be used in all transform related request so that it could be "spoofed" to allow
     * "static" access to resources.
     */
    fetch(url: string, type: 'string' | 'binary' = 'string', body?: string): Task<string | Uint8Array> {
        return ajaxGet({ url, type, body });
        // const req = await fetch(url, { referrerPolicy: 'origin-when-cross-origin' });
        // return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
    runTask<T>(task: Task<T>) {
        return this.tasks.run(task);
David Sehnal's avatar
David Sehnal committed
    }

David Sehnal's avatar
David Sehnal committed
    dispose() {
        if (this.disposed) return;
David Sehnal's avatar
David Sehnal committed
        this.commands.dispose();
David Sehnal's avatar
David Sehnal committed
        this.canvas3d.dispose();
        this.ev.dispose();
        this.state.dispose();
        this.tasks.dispose();
        this.layout.dispose();
David Sehnal's avatar
David Sehnal committed
        this.disposed = true;
    }

    applyTransform(state: State, a: Transform.Ref, transformer: Transformer, params: any) {
        const tree = state.tree.build().to(a).apply(transformer, params);
        return PluginCommands.State.Update.dispatch(this, { state, tree });
    }

    updateTransform(state: State, a: Transform.Ref, params: any) {
        const tree = state.build().to(a).update(params);
        return PluginCommands.State.Update.dispatch(this, { state, tree });
    }

David Sehnal's avatar
David Sehnal committed
    private initBuiltInBehavior() {
        BuiltInPluginBehaviors.State.registerDefault(this);
        BuiltInPluginBehaviors.Representation.registerDefault(this);
        BuiltInPluginBehaviors.Camera.registerDefault(this);
        BuiltInPluginBehaviors.Misc.registerDefault(this);
David Sehnal's avatar
David Sehnal committed

        merge(this.state.dataState.events.log, this.state.behaviorState.events.log).subscribe(e => this.events.log.next(e));
David Sehnal's avatar
David Sehnal committed
    }

    private async initBehaviors() {
        const tree = this.state.behaviorState.tree.build();

        for (const b of this.spec.behaviors) {
            tree.toRoot().apply(b.transformer, b.defaultParams, { ref: b.transformer.id });
David Sehnal's avatar
David Sehnal committed

        await this.runTask(this.state.behaviorState.updateTree(tree, true));
David Sehnal's avatar
David Sehnal committed
    }

        for (const a of this.spec.actions) {
            this.state.dataState.actions.add(a.action);
        }
David Sehnal's avatar
David Sehnal committed

David Sehnal's avatar
David Sehnal committed
    private initAnimations() {
        if (!this.spec.animations) return;
        for (const anim of this.spec.animations) {
            this.state.animation.register(anim);
        }
    }

    private initCustomParamEditors() {
        if (!this.spec.customParamEditors) return;
        for (const [t, e] of this.spec.customParamEditors) {
            this.customParamEditors.set(t.id, e);
        }
    constructor(public spec: PluginSpec) {
David Sehnal's avatar
David Sehnal committed
        this.events.log.subscribe(e => this.log.entries = this.log.entries.push(e));

David Sehnal's avatar
David Sehnal committed
        this.initBuiltInBehavior();
David Sehnal's avatar
David Sehnal committed

        this.initBehaviors();
        this.initDataActions();
David Sehnal's avatar
David Sehnal committed
        this.initAnimations();
        this.initCustomParamEditors();

        this.lociLabels = new LociLabelManager(this);
        this.log.message(`Mol* Plugin ${PLUGIN_VERSION} [${PLUGIN_VERSION_DATE.toLocaleString()}]`);
David Sehnal's avatar
David Sehnal committed
}