Skip to content
Snippets Groups Projects
Select Git revision
  • 44bda1b326c70ef952756865d7a57128cad42023
  • master default protected
  • devel
  • hruska-feature-clients-api
  • malostik-#5066-deduplicate-idea-ids
  • warden-postgresql-port
  • hruska-feature-#6799-filter-keys
  • hruska-feature-5066-duplicateIdeaID
  • warden-client-3.0-beta3
  • warden-server-3.0-beta3
  • warden-client-2.2-final
  • warden-server-2.2-final
  • warden-client-3.0-beta2
  • warden-server-3.0-beta2
  • warden-client-2.2
  • warden-server-2.2-patch3
  • warden-client-3.0-beta1
  • warden-server-3.0-beta1
  • warden-server-2.2-patch1
  • warden-client-3.0-beta0
  • warden-server-3.0-beta0
  • warden-server-2.2
  • warden-server-2.1-patch1
  • warden-client-2.1
  • warden-server-2.1
  • warden-server-2.1-beta6
  • warden-server-2.1-beta5
  • warden-server-2.1-beta4
28 results

form.html

Blame
  • Forked from 713 / Warden / Warden - archive
    Source project has a limited visibility.
    state.ts 3.51 KiB
    /**
     * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import { PluginCommands } from '../../command';
    import { PluginContext } from '../../context';
    import { StateTree, Transform, State } from 'mol-state';
    import { PluginStateSnapshotManager } from 'mol-plugin/state/snapshots';
    
    export function registerDefault(ctx: PluginContext) {
        SetCurrentObject(ctx);
        Update(ctx);
        ApplyAction(ctx);
        RemoveObject(ctx);
        ToggleExpanded(ctx);
        ToggleVisibility(ctx);
        Snapshots(ctx);
    }
    
    export function SetCurrentObject(ctx: PluginContext) {
        PluginCommands.State.SetCurrentObject.subscribe(ctx, ({ state, ref }) => state.setCurrent(ref));
    }
    
    export function Update(ctx: PluginContext) {
        PluginCommands.State.Update.subscribe(ctx, ({ state, tree }) => ctx.runTask(state.update(tree)));
    }
    
    export function ApplyAction(ctx: PluginContext) {
        PluginCommands.State.ApplyAction.subscribe(ctx, ({ state, action, ref }) => ctx.runTask(state.apply(action.action, action.params, ref)));
    }
    
    export function RemoveObject(ctx: PluginContext) {
        PluginCommands.State.RemoveObject.subscribe(ctx, ({ state, ref }) => {
            const tree = state.tree.build().delete(ref).getTree();
            return ctx.runTask(state.update(tree));
        });
    }
    
    export function ToggleExpanded(ctx: PluginContext) {
        PluginCommands.State.ToggleExpanded.subscribe(ctx, ({ state, ref }) => state.updateCellState(ref, ({ isCollapsed }) => ({ isCollapsed: !isCollapsed })));
    }
    
    export function ToggleVisibility(ctx: PluginContext) {
        PluginCommands.State.ToggleVisibility.subscribe(ctx, ({ state, ref }) => setVisibility(state, ref, !state.tree.cellStates.get(ref).isHidden));
    }
    
    function setVisibility(state: State, root: Transform.Ref, value: boolean) {
        StateTree.doPreOrder(state.tree, state.tree.transforms.get(root), { state, value }, setVisibilityVisitor);
    }
    
    function setVisibilityVisitor(t: Transform, tree: StateTree, ctx: { state: State, value: boolean }) {
        ctx.state.updateCellState(t.ref, { isHidden: ctx.value });
    }
    
    export function Snapshots(ctx: PluginContext) {
        PluginCommands.State.Snapshots.Clear.subscribe(ctx, () => {
            ctx.state.snapshots.clear();
        });
    
        PluginCommands.State.Snapshots.Remove.subscribe(ctx, ({ id }) => {
            ctx.state.snapshots.remove(id);
        });
    
        PluginCommands.State.Snapshots.Add.subscribe(ctx, ({ name, description }) => {
            const entry = PluginStateSnapshotManager.Entry(name || new Date().toLocaleTimeString(), ctx.state.getSnapshot(), description);
            ctx.state.snapshots.add(entry);
        });
    
        PluginCommands.State.Snapshots.Apply.subscribe(ctx, ({ id }) => {
            const e = ctx.state.snapshots.getEntry(id);
            return ctx.state.setSnapshot(e.snapshot);
        });
    
        PluginCommands.State.Snapshots.Upload.subscribe(ctx, ({ name, description, serverUrl }) => {
            return fetch(`${serverUrl}/set?name=${encodeURIComponent(name || '')}&description=${encodeURIComponent(description || '')}`, {
                method: 'POST',
                mode: 'cors',
                referrer: 'no-referrer',
                headers: { 'Content-Type': 'application/json; charset=utf-8' },
                body: JSON.stringify(ctx.state.getSnapshot())
            }) as any as Promise<void>;
        });
    
        PluginCommands.State.Snapshots.Fetch.subscribe(ctx, async ({ url }) => {
            const req = await fetch(url, { referrer: 'no-referrer' });
            const json = await req.json();
            return ctx.state.setSnapshot(json.data);
        });
    }