Skip to content
Snippets Groups Projects
context.ts 6.48 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';
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 lociLabels: LociLabelManager;

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

    readonly customModelProperties = new CustomPropertyRegistry();
David Sehnal's avatar
David Sehnal committed

David Sehnal's avatar
David Sehnal committed
    initViewer(canvas: HTMLCanvasElement, container: HTMLDivElement) {
        try {
            (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 = {
        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'): Task<string | Uint8Array> {
        return ajaxGet({ url, type });
        // 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();
David Sehnal's avatar
David Sehnal committed
        this.disposed = true;
    }

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
    }

    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.update(tree));
David Sehnal's avatar
David Sehnal committed
    }

    initDataActions() {
        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
    applyTransform(state: State, a: Transform.Ref, transformer: Transformer, params: any) {
        const tree = state.tree.build().to(a).apply(transformer, params);
David Sehnal's avatar
David Sehnal committed
        return PluginCommands.State.Update.dispatch(this, { state, tree });
David Sehnal's avatar
David Sehnal committed
    }
David Sehnal's avatar
David Sehnal committed
    updateTransform(state: State, a: Transform.Ref, params: any) {
        const tree = state.build().to(a).update(params);
David Sehnal's avatar
David Sehnal committed
        return PluginCommands.State.Update.dispatch(this, { state, tree });
    constructor(public spec: PluginSpec) {
David Sehnal's avatar
David Sehnal committed
        this.initBuiltInBehavior();
David Sehnal's avatar
David Sehnal committed

        this.initBehaviors();
        this.initDataActions();

        this.lociLabels = new LociLabelManager(this);
David Sehnal's avatar
David Sehnal committed
    // settings = ;
}