molstar
Version:
A comprehensive macromolecular library.
174 lines • 9.83 kB
JavaScript
"use strict";
/**
* Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginState = void 0;
var tslib_1 = require("tslib");
var mol_state_1 = require("../mol-state");
var objects_1 = require("../mol-plugin-state/objects");
var behavior_1 = require("./behavior");
var canvas3d_1 = require("../mol-canvas3d/canvas3d");
var commands_1 = require("./commands");
var param_definition_1 = require("../mol-util/param-definition");
var mol_util_1 = require("../mol-util");
var immer_1 = require("immer");
var rxjs_1 = require("rxjs");
var component_1 = require("../mol-plugin-state/component");
var config_1 = require("./config");
var PluginState = /** @class */ (function (_super) {
(0, tslib_1.__extends)(PluginState, _super);
function PluginState(plugin) {
var _this = _super.call(this) || this;
_this.plugin = plugin;
_this.data = mol_state_1.State.create(new objects_1.PluginStateObject.Root({}), { runTask: _this.plugin.runTask, globalContext: _this.plugin, historyCapacity: _this.plugin.config.get(config_1.PluginConfig.State.HistoryCapacity) });
_this.behaviors = mol_state_1.State.create(new behavior_1.PluginBehavior.Root({}), { runTask: _this.plugin.runTask, globalContext: _this.plugin, rootState: { isLocked: true } });
_this.events = {
cell: {
stateUpdated: (0, rxjs_1.merge)(_this.data.events.cell.stateUpdated, _this.behaviors.events.cell.stateUpdated),
created: (0, rxjs_1.merge)(_this.data.events.cell.created, _this.behaviors.events.cell.created),
removed: (0, rxjs_1.merge)(_this.data.events.cell.removed, _this.behaviors.events.cell.removed),
},
object: {
created: (0, rxjs_1.merge)(_this.data.events.object.created, _this.behaviors.events.object.created),
removed: (0, rxjs_1.merge)(_this.data.events.object.removed, _this.behaviors.events.object.removed),
updated: (0, rxjs_1.merge)(_this.data.events.object.updated, _this.behaviors.events.object.updated)
}
};
_this.snapshotParams = _this.ev.behavior(PluginState.DefaultSnapshotParams);
_this.setSnapshotParams = function (params) {
_this.snapshotParams.next((0, tslib_1.__assign)((0, tslib_1.__assign)({}, PluginState.DefaultSnapshotParams), params));
};
return _this;
}
Object.defineProperty(PluginState.prototype, "animation", {
get: function () { return this.plugin.managers.animation; },
enumerable: false,
configurable: true
});
PluginState.prototype.getSnapshot = function (params) {
var _a, _b;
var p = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, this.snapshotParams.value), params);
return {
id: mol_util_1.UUID.create22(),
data: p.data ? this.data.getSnapshot() : void 0,
behaviour: p.behavior ? this.behaviors.getSnapshot() : void 0,
animation: p.animation ? this.animation.getSnapshot() : void 0,
startAnimation: p.startAnimation ? !!p.startAnimation : void 0,
camera: p.camera ? {
current: this.plugin.canvas3d.camera.getSnapshot(),
transitionStyle: p.cameraTransition.name,
transitionDurationInMs: ((_a = p === null || p === void 0 ? void 0 : p.cameraTransition) === null || _a === void 0 ? void 0 : _a.name) === 'animate' ? p.cameraTransition.params.durationInMs : void 0
} : void 0,
canvas3d: p.canvas3d ? { props: (_b = this.plugin.canvas3d) === null || _b === void 0 ? void 0 : _b.props } : void 0,
interactivity: p.interactivity ? { props: this.plugin.managers.interactivity.props } : void 0,
structureFocus: this.plugin.managers.structure.focus.getSnapshot(),
durationInMs: p === null || p === void 0 ? void 0 : p.durationInMs
};
};
PluginState.prototype.setSnapshot = function (snapshot) {
var _a;
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var settings;
return (0, tslib_1.__generator)(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.animation.stop()];
case 1:
_b.sent();
if (!snapshot.behaviour) return [3 /*break*/, 3];
return [4 /*yield*/, this.plugin.runTask(this.behaviors.setSnapshot(snapshot.behaviour))];
case 2:
_b.sent();
_b.label = 3;
case 3:
if (!snapshot.data) return [3 /*break*/, 5];
return [4 /*yield*/, this.plugin.runTask(this.data.setSnapshot(snapshot.data))];
case 4:
_b.sent();
_b.label = 5;
case 5:
if (!((_a = snapshot.canvas3d) === null || _a === void 0 ? void 0 : _a.props)) return [3 /*break*/, 7];
settings = param_definition_1.ParamDefinition.normalizeParams(canvas3d_1.Canvas3DParams, snapshot.canvas3d.props, 'children');
return [4 /*yield*/, commands_1.PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: settings })];
case 6:
_b.sent();
_b.label = 7;
case 7:
if (snapshot.interactivity) {
if (snapshot.interactivity.props)
this.plugin.managers.interactivity.setProps(snapshot.interactivity.props);
}
if (snapshot.structureFocus) {
this.plugin.managers.structure.focus.setSnapshot(snapshot.structureFocus);
}
if (snapshot.animation) {
this.animation.setSnapshot(snapshot.animation);
}
if (snapshot.camera) {
commands_1.PluginCommands.Camera.Reset(this.plugin, {
snapshot: snapshot.camera.current,
durationMs: snapshot.camera.transitionStyle === 'animate'
? snapshot.camera.transitionDurationInMs
: void 0
});
}
if (snapshot.startAnimation) {
this.animation.start();
}
return [2 /*return*/];
}
});
});
};
PluginState.prototype.updateTransform = function (state, a, params, canUndo) {
var tree = state.build().to(a).update(params);
return commands_1.PluginCommands.State.Update(this.plugin, { state: state, tree: tree, options: { canUndo: canUndo } });
};
PluginState.prototype.updateBehavior = function (behavior, params) {
var tree = this.behaviors.build();
if (!this.behaviors.tree.transforms.has(behavior.id)) {
var defaultParams = behavior.createDefaultParams(void 0, this.plugin);
tree.to(behavior_1.PluginBehavior.getCategoryId(behavior)).apply(behavior, (0, immer_1.produce)(defaultParams, params), { ref: behavior.id });
}
else {
tree.to(behavior.id).update(params);
}
return this.plugin.runTask(this.behaviors.updateTree(tree));
};
PluginState.prototype.dispose = function () {
this.behaviors.cells.forEach(function (cell) {
if (behavior_1.PluginBehavior.Behavior.is(cell.obj)) {
cell.obj.data.unregister();
}
});
_super.prototype.dispose.call(this);
this.data.dispose();
this.behaviors.dispose();
this.animation.dispose();
};
return PluginState;
}(component_1.PluginComponent));
exports.PluginState = PluginState;
(function (PluginState) {
PluginState.SnapshotParams = {
durationInMs: param_definition_1.ParamDefinition.Numeric(1500, { min: 100, max: 15000, step: 100 }, { label: 'Duration in ms' }),
data: param_definition_1.ParamDefinition.Boolean(true),
behavior: param_definition_1.ParamDefinition.Boolean(false),
animation: param_definition_1.ParamDefinition.Boolean(true),
startAnimation: param_definition_1.ParamDefinition.Boolean(false),
canvas3d: param_definition_1.ParamDefinition.Boolean(true),
interactivity: param_definition_1.ParamDefinition.Boolean(true),
camera: param_definition_1.ParamDefinition.Boolean(true),
cameraTransition: param_definition_1.ParamDefinition.MappedStatic('animate', {
animate: param_definition_1.ParamDefinition.Group({
durationInMs: param_definition_1.ParamDefinition.Numeric(250, { min: 100, max: 5000, step: 500 }, { label: 'Duration in ms' }),
}),
instant: param_definition_1.ParamDefinition.Group({})
}, { options: [['animate', 'Animate'], ['instant', 'Instant']] })
};
PluginState.DefaultSnapshotParams = param_definition_1.ParamDefinition.getDefaultValues(PluginState.SnapshotParams);
})(PluginState || (PluginState = {}));
exports.PluginState = PluginState;
//# sourceMappingURL=state.js.map