UNPKG

molstar

Version:

A comprehensive macromolecular library.

259 lines 11.9 kB
"use strict"; /** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Snapshots = exports.ClearHighlights = exports.Highlight = exports.setSubtreeVisibility = exports.ToggleVisibility = exports.ToggleExpanded = exports.RemoveObject = exports.ApplyAction = exports.Update = exports.SetCurrentObject = exports.SyncBehaviors = exports.registerDefault = void 0; var tslib_1 = require("tslib"); var structure_1 = require("../../../mol-model/structure"); var snapshots_1 = require("../../../mol-plugin-state/manager/snapshots"); var objects_1 = require("../../../mol-plugin-state/objects"); var mol_state_1 = require("../../../mol-state"); var date_1 = require("../../../mol-util/date"); var download_1 = require("../../../mol-util/download"); var url_1 = require("../../../mol-util/url"); var commands_1 = require("../../commands"); var config_1 = require("../../config"); function registerDefault(ctx) { SyncBehaviors(ctx); SetCurrentObject(ctx); Update(ctx); ApplyAction(ctx); RemoveObject(ctx); ToggleExpanded(ctx); ToggleVisibility(ctx); Highlight(ctx); ClearHighlights(ctx); Snapshots(ctx); } exports.registerDefault = registerDefault; function SyncBehaviors(ctx) { ctx.state.events.object.created.subscribe(function (o) { if (!objects_1.PluginStateObject.isBehavior(o.obj)) return; o.obj.data.register(o.ref); }); ctx.state.events.object.removed.subscribe(function (o) { if (!objects_1.PluginStateObject.isBehavior(o.obj)) return; o.obj.data.unregister(); }); ctx.state.events.object.updated.subscribe(function (o) { if (o.action === 'recreate') { if (o.oldObj && objects_1.PluginStateObject.isBehavior(o.oldObj)) o.oldObj.data.unregister(); if (o.obj && objects_1.PluginStateObject.isBehavior(o.obj)) o.obj.data.register(o.ref); } }); } exports.SyncBehaviors = SyncBehaviors; function SetCurrentObject(ctx) { commands_1.PluginCommands.State.SetCurrentObject.subscribe(ctx, function (_a) { var state = _a.state, ref = _a.ref; return state.setCurrent(ref); }); } exports.SetCurrentObject = SetCurrentObject; function Update(ctx) { commands_1.PluginCommands.State.Update.subscribe(ctx, function (_a) { var state = _a.state, tree = _a.tree, options = _a.options; return ctx.runTask(state.updateTree(tree, options)); }); } exports.Update = Update; function ApplyAction(ctx) { commands_1.PluginCommands.State.ApplyAction.subscribe(ctx, function (_a) { var state = _a.state, action = _a.action, ref = _a.ref; return ctx.runTask(state.applyAction(action.action, action.params, ref)); }); } exports.ApplyAction = ApplyAction; function RemoveObject(ctx) { function remove(state, ref) { var tree = state.build().delete(ref); return ctx.runTask(state.updateTree(tree)); } commands_1.PluginCommands.State.RemoveObject.subscribe(ctx, function (_a) { var state = _a.state, ref = _a.ref, removeParentGhosts = _a.removeParentGhosts; if (removeParentGhosts) { var tree = state.tree; var curr = tree.transforms.get(ref); if (curr.parent === ref) return remove(state, ref); while (true) { var children = tree.children.get(curr.parent); if (curr.parent === curr.ref || children.size > 1) return remove(state, curr.ref); var parent_1 = tree.transforms.get(curr.parent); // TODO: should this use "cell state" instead? if (!parent_1.state.isGhost) return remove(state, curr.ref); curr = parent_1; } } else { return remove(state, ref); } }); } exports.RemoveObject = RemoveObject; function ToggleExpanded(ctx) { commands_1.PluginCommands.State.ToggleExpanded.subscribe(ctx, function (_a) { var state = _a.state, ref = _a.ref; return state.updateCellState(ref, function (_a) { var isCollapsed = _a.isCollapsed; return ({ isCollapsed: !isCollapsed }); }); }); } exports.ToggleExpanded = ToggleExpanded; function ToggleVisibility(ctx) { commands_1.PluginCommands.State.ToggleVisibility.subscribe(ctx, function (_a) { var state = _a.state, ref = _a.ref; return setSubtreeVisibility(state, ref, !state.cells.get(ref).state.isHidden); }); } exports.ToggleVisibility = ToggleVisibility; function setSubtreeVisibility(state, root, value) { mol_state_1.StateTree.doPreOrder(state.tree, state.transforms.get(root), { state: state, value: value }, setVisibilityVisitor); } exports.setSubtreeVisibility = setSubtreeVisibility; function setVisibilityVisitor(t, tree, ctx) { ctx.state.updateCellState(t.ref, { isHidden: ctx.value }); } function Highlight(ctx) { commands_1.PluginCommands.Interactivity.Object.Highlight.subscribe(ctx, function (_a) { var state = _a.state, ref = _a.ref; if (!ctx.canvas3d || ctx.isBusy) return; ctx.managers.interactivity.lociHighlights.clearHighlights(); var refs = typeof ref === 'string' ? [ref] : ref; for (var _i = 0, refs_1 = refs; _i < refs_1.length; _i++) { var r = refs_1[_i]; var cell = state.cells.get(r); if (!cell) continue; if (objects_1.PluginStateObject.Molecule.Structure.is(cell.obj)) { ctx.managers.interactivity.lociHighlights.highlight({ loci: structure_1.Structure.Loci(cell.obj.data) }, false); } else if (cell && objects_1.PluginStateObject.isRepresentation3D(cell.obj)) { var repr = cell.obj.data.repr; ctx.managers.interactivity.lociHighlights.highlight({ loci: repr.getLoci(), repr: repr }, false); } else if (objects_1.PluginStateObject.Molecule.Structure.Selections.is(cell.obj)) { for (var _b = 0, _c = cell.obj.data; _b < _c.length; _b++) { var entry = _c[_b]; ctx.managers.interactivity.lociHighlights.highlight({ loci: entry.loci }, false); } } } // TODO: highlight volumes? // TODO: select structures of subtree? }); } exports.Highlight = Highlight; function ClearHighlights(ctx) { commands_1.PluginCommands.Interactivity.ClearHighlights.subscribe(ctx, function () { ctx.managers.interactivity.lociHighlights.clearHighlights(); }); } exports.ClearHighlights = ClearHighlights; function Snapshots(ctx) { var _this = this; ctx.config.set(config_1.PluginConfig.State.CurrentServer, ctx.config.get(config_1.PluginConfig.State.DefaultServer)); commands_1.PluginCommands.State.Snapshots.Clear.subscribe(ctx, function () { ctx.managers.snapshot.clear(); }); commands_1.PluginCommands.State.Snapshots.Remove.subscribe(ctx, function (_a) { var id = _a.id; ctx.managers.snapshot.remove(id); }); commands_1.PluginCommands.State.Snapshots.Add.subscribe(ctx, function (_a) { var name = _a.name, description = _a.description, params = _a.params; var entry = snapshots_1.PluginStateSnapshotManager.Entry(ctx.state.getSnapshot(params), { name: name, description: description }); ctx.managers.snapshot.add(entry); }); commands_1.PluginCommands.State.Snapshots.Replace.subscribe(ctx, function (_a) { var id = _a.id, params = _a.params; ctx.managers.snapshot.replace(id, ctx.state.getSnapshot(params)); }); commands_1.PluginCommands.State.Snapshots.Move.subscribe(ctx, function (_a) { var id = _a.id, dir = _a.dir; ctx.managers.snapshot.move(id, dir); }); commands_1.PluginCommands.State.Snapshots.Apply.subscribe(ctx, function (_a) { var id = _a.id; var snapshot = ctx.managers.snapshot.setCurrent(id); if (!snapshot) return; return ctx.state.setSnapshot(snapshot); }); commands_1.PluginCommands.State.Snapshots.Upload.subscribe(ctx, function (_a) { var name = _a.name, description = _a.description, playOnLoad = _a.playOnLoad, serverUrl = _a.serverUrl, params = _a.params; return fetch((0, url_1.urlCombine)(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.managers.snapshot.getStateSnapshot({ name: name, description: description, playOnLoad: playOnLoad })) }); }); commands_1.PluginCommands.State.Snapshots.Fetch.subscribe(ctx, function (_a) { var url = _a.url; return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var json; return (0, tslib_1.__generator)(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, ctx.runTask(ctx.fetch({ url: url, type: 'json' }))]; case 1: json = _b.sent(); return [4 /*yield*/, ctx.managers.snapshot.setStateSnapshot(json.data)]; case 2: _b.sent(); return [2 /*return*/]; } }); }); }); commands_1.PluginCommands.State.Snapshots.DownloadToFile.subscribe(ctx, function (_a) { var name = _a.name, type = _a.type, params = _a.params; return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var filename, data; return (0, tslib_1.__generator)(this, function (_b) { switch (_b.label) { case 0: filename = "mol-star_state_" + (name || (0, date_1.getFormattedTime)()) + "." + (type === 'json' ? 'molj' : 'molx'); return [4 /*yield*/, ctx.managers.snapshot.serialize({ type: type, params: params })]; case 1: data = _b.sent(); (0, download_1.download)(data, "" + filename); return [2 /*return*/]; } }); }); }); commands_1.PluginCommands.State.Snapshots.OpenFile.subscribe(ctx, function (_a) { var file = _a.file; return ctx.managers.snapshot.open(file); }); commands_1.PluginCommands.State.Snapshots.OpenUrl.subscribe(ctx, function (_a) { var url = _a.url, type = _a.type; return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var data; return (0, tslib_1.__generator)(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, ctx.runTask(ctx.fetch({ url: url, type: 'binary' }))]; case 1: data = _b.sent(); return [2 /*return*/, ctx.managers.snapshot.open(new File([data], "state." + type))]; } }); }); }); } exports.Snapshots = Snapshots; //# sourceMappingURL=state.js.map