UNPKG

molstar

Version:

A comprehensive macromolecular library.

153 lines 7.49 kB
/** * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { __awaiter, __generator } from "tslib"; import { arrayFind } from '../../../mol-data/util'; import { StateObjectRef } from '../../../mol-state'; import { Task } from '../../../mol-task'; import { isProductionMode } from '../../../mol-util/debug'; import { objectForEach } from '../../../mol-util/object'; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { createStructureRepresentationParams } from '../../helpers/structure-representation-params'; import { StructureRepresentation3D } from '../../transforms/representation'; import { PresetStructureRepresentations } from './representation-preset'; import { arrayRemoveInPlace } from '../../../mol-util/array'; import { PluginConfig } from '../../../mol-plugin/config'; var StructureRepresentationBuilder = /** @class */ (function () { function StructureRepresentationBuilder(plugin) { var _this = this; this.plugin = plugin; this._providers = []; this.providerMap = new Map(); this.defaultProvider = PresetStructureRepresentations.auto; objectForEach(PresetStructureRepresentations, function (r) { return _this.registerPreset(r); }); } Object.defineProperty(StructureRepresentationBuilder.prototype, "dataState", { get: function () { return this.plugin.state.data; }, enumerable: false, configurable: true }); StructureRepresentationBuilder.prototype.resolveProvider = function (ref) { var _a; return typeof ref === 'string' ? (_a = PresetStructureRepresentations[ref]) !== null && _a !== void 0 ? _a : arrayFind(this._providers, function (p) { return p.id === ref; }) : ref; }; StructureRepresentationBuilder.prototype.hasPreset = function (s) { for (var _i = 0, _a = this._providers; _i < _a.length; _i++) { var p = _a[_i]; if (!p.isApplicable || p.isApplicable(s, this.plugin)) return true; } return false; }; Object.defineProperty(StructureRepresentationBuilder.prototype, "providers", { get: function () { return this._providers; }, enumerable: false, configurable: true }); StructureRepresentationBuilder.prototype.getPresets = function (s) { if (!s) return this.providers; var ret = []; for (var _i = 0, _a = this._providers; _i < _a.length; _i++) { var p = _a[_i]; if (p.isApplicable && !p.isApplicable(s, this.plugin)) continue; ret.push(p); } return ret; }; StructureRepresentationBuilder.prototype.getPresetSelect = function (s) { var options = []; for (var _i = 0, _a = this._providers; _i < _a.length; _i++) { var p = _a[_i]; if (s && p.isApplicable && !p.isApplicable(s, this.plugin)) continue; options.push([p.id, p.display.name, p.display.group]); } return PD.Select('auto', options); }; StructureRepresentationBuilder.prototype.getPresetsWithOptions = function (s) { var options = []; var map = Object.create(null); for (var _i = 0, _a = this._providers; _i < _a.length; _i++) { var p = _a[_i]; if (p.isApplicable && !p.isApplicable(s, this.plugin)) continue; options.push([p.id, p.display.name]); map[p.id] = p.params ? PD.Group(p.params(s, this.plugin)) : PD.EmptyGroup(); } if (options.length === 0) return PD.MappedStatic('', { '': PD.EmptyGroup() }); return PD.MappedStatic(options[0][0], map, { options: options }); }; StructureRepresentationBuilder.prototype.registerPreset = function (provider) { if (this.providerMap.has(provider.id)) { throw new Error("Representation provider with id '" + provider.id + "' already registered."); } this._providers.push(provider); this.providerMap.set(provider.id, provider); }; StructureRepresentationBuilder.prototype.unregisterPreset = function (provider) { this.providerMap.delete(provider.id); arrayRemoveInPlace(this._providers, provider); }; StructureRepresentationBuilder.prototype.applyPreset = function (parent, providerRef, params) { var _this = this; var _a; var provider = this.resolveProvider(providerRef); if (!provider) return; var state = this.plugin.state.data; var cell = StateObjectRef.resolveAndCheck(state, parent); if (!cell) { if (!isProductionMode) console.warn("Applying structure repr. provider to bad cell."); return; } var pd = ((_a = provider.params) === null || _a === void 0 ? void 0 : _a.call(provider, cell.obj, this.plugin)) || {}; var prms = params || (provider.params ? PD.getDefaultValues(pd) : {}); var defaults = this.plugin.config.get(PluginConfig.Structure.DefaultRepresentationPresetParams); prms = PD.merge(pd, defaults, prms); var task = Task.create("" + provider.display.name, function () { return provider.apply(cell, prms, _this.plugin); }); return this.plugin.runTask(task); }; StructureRepresentationBuilder.prototype.addRepresentation = function (structure, props, options) { return __awaiter(this, void 0, void 0, function () { var repr, selector; return __generator(this, function (_a) { switch (_a.label) { case 0: repr = this.dataState.build(); selector = this.buildRepresentation(repr, structure, props, options); if (!selector) return [2 /*return*/]; return [4 /*yield*/, repr.commit()]; case 1: _a.sent(); return [2 /*return*/, selector]; } }); }); }; StructureRepresentationBuilder.prototype.buildRepresentation = function (builder, structure, props, options) { var _a, _b; if (!structure) return; var data = (_b = (_a = StateObjectRef.resolveAndCheck(this.dataState, structure)) === null || _a === void 0 ? void 0 : _a.obj) === null || _b === void 0 ? void 0 : _b.data; if (!data) return; var params = createStructureRepresentationParams(this.plugin, data, props); return (options === null || options === void 0 ? void 0 : options.tag) ? builder.to(structure).applyOrUpdateTagged(options.tag, StructureRepresentation3D, params, { state: options === null || options === void 0 ? void 0 : options.initialState }).selector : builder.to(structure).apply(StructureRepresentation3D, params, { state: options === null || options === void 0 ? void 0 : options.initialState }).selector; }; return StructureRepresentationBuilder; }()); export { StructureRepresentationBuilder }; //# sourceMappingURL=representation.js.map