UNPKG

molstar

Version:

A comprehensive macromolecular library.

164 lines (163 loc) 8.7 kB
/** * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __assign, __extends } from "tslib"; import { PluginBehavior } from '../../../behavior/behavior'; import { Structure, Model } from '../../../../mol-model/structure'; import { PluginStateObject } from '../../../../mol-plugin-state/objects'; import { StateSelection } from '../../../../mol-state'; export var StructureInfo = PluginBehavior.create({ name: 'structure-info-prop', category: 'custom-props', display: { name: 'Structure Info' }, ctor: /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } Object.defineProperty(class_1.prototype, "maxModelIndex", { get: function () { var _a, _b; var maxIndex = -1; var cells = this.ctx.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Model)); for (var _i = 0, cells_1 = cells; _i < cells_1.length; _i++) { var c = cells_1[_i]; var index = ((_a = c.obj) === null || _a === void 0 ? void 0 : _a.data) && Model.Index.get((_b = c.obj) === null || _b === void 0 ? void 0 : _b.data).value; if (index !== undefined && index > maxIndex) maxIndex = index; } return maxIndex; }, enumerable: false, configurable: true }); Object.defineProperty(class_1.prototype, "maxStructureIndex", { get: function () { var _a, _b; var maxIndex = -1; var cells = this.ctx.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure)); for (var _i = 0, cells_2 = cells; _i < cells_2.length; _i++) { var c = cells_2[_i]; var index = ((_a = c.obj) === null || _a === void 0 ? void 0 : _a.data) && Structure.Index.get((_b = c.obj) === null || _b === void 0 ? void 0 : _b.data).value; if (index !== undefined && index > maxIndex) maxIndex = index; } return maxIndex; }, enumerable: false, configurable: true }); Object.defineProperty(class_1.prototype, "asymIdOffset", { get: function () { var _a; var auth = 0; var label = 0; var cells = this.ctx.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Model)); for (var _i = 0, cells_3 = cells; _i < cells_3.length; _i++) { var c = cells_3[_i]; var m = (_a = c.obj) === null || _a === void 0 ? void 0 : _a.data; if (m) { var count = Model.AsymIdCount.get(m); var offset = Model.AsymIdOffset.get(m).value; if (count !== undefined && offset !== undefined) { auth = Math.max(auth, offset.auth + count.auth); label = Math.max(label, offset.label + count.label); } } } return { auth: auth, label: label }; }, enumerable: false, configurable: true }); class_1.prototype.setModelMaxIndex = function () { var _a; var value = this.maxModelIndex; var cells = this.ctx.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Model)); for (var _i = 0, cells_4 = cells; _i < cells_4.length; _i++) { var c = cells_4[_i]; var m = (_a = c.obj) === null || _a === void 0 ? void 0 : _a.data; if (m) { if (Model.MaxIndex.get(m).value !== value) { Model.MaxIndex.set(m, { value: value }, value); } } } }; class_1.prototype.setStructureMaxIndex = function () { var _a; var value = this.maxModelIndex; var cells = this.ctx.state.data.select(StateSelection.Generators.rootsOfType(PluginStateObject.Molecule.Structure)); for (var _i = 0, cells_5 = cells; _i < cells_5.length; _i++) { var c = cells_5[_i]; var s = (_a = c.obj) === null || _a === void 0 ? void 0 : _a.data; if (s) { if (Structure.MaxIndex.get(s).value !== value) { Structure.MaxIndex.set(s, { value: value }, value); } } } }; class_1.prototype.handleModel = function (model, oldModel) { if (Model.Index.get(model).value === undefined) { var oldIndex = oldModel && Model.Index.get(oldModel).value; var value = oldIndex !== null && oldIndex !== void 0 ? oldIndex : (this.maxModelIndex + 1); Model.Index.set(model, { value: value }, value); } if (Model.AsymIdOffset.get(model).value === undefined) { var oldOffset = oldModel && Model.AsymIdOffset.get(oldModel).value; var value = oldOffset !== null && oldOffset !== void 0 ? oldOffset : __assign({}, this.asymIdOffset); Model.AsymIdOffset.set(model, { value: value }, value); } }; class_1.prototype.handleStructure = function (structure, oldStructure) { if (structure.parent !== undefined) return; if (Structure.Index.get(structure).value !== undefined) return; var oldIndex = oldStructure && Structure.Index.get(oldStructure).value; var value = oldIndex !== null && oldIndex !== void 0 ? oldIndex : (this.maxStructureIndex + 1); Structure.Index.set(structure, { value: value }, value); }; class_1.prototype.handle = function (ref, obj, oldObj) { if (PluginStateObject.Molecule.Structure.is(obj)) { var transform = this.ctx.state.data.tree.transforms.get(ref); if (!transform.transformer.definition.isDecorator && obj.data.parent === undefined) { this.handleStructure(obj.data, oldObj === null || oldObj === void 0 ? void 0 : oldObj.data); } } else if (PluginStateObject.Molecule.Model.is(obj)) { var transform = this.ctx.state.data.tree.transforms.get(ref); if (!transform.transformer.definition.isDecorator) { this.handleModel(obj.data, oldObj === null || oldObj === void 0 ? void 0 : oldObj.data); } } }; class_1.prototype.register = function () { var _this = this; this.ctx.customModelProperties.register(Model.AsymIdOffset, true); this.ctx.customModelProperties.register(Model.Index, true); this.ctx.customModelProperties.register(Model.MaxIndex, true); this.ctx.customStructureProperties.register(Structure.Index, true); this.ctx.customStructureProperties.register(Structure.MaxIndex, true); this.subscribeObservable(this.ctx.state.data.events.object.created, function (o) { _this.handle(o.ref, o.obj); _this.setModelMaxIndex(); _this.setStructureMaxIndex(); }); this.subscribeObservable(this.ctx.state.data.events.object.updated, function (o) { _this.handle(o.ref, o.obj, o.oldObj); }); }; class_1.prototype.unregister = function () { this.ctx.customModelProperties.unregister(Model.AsymIdOffset.descriptor.name); this.ctx.customModelProperties.unregister(Model.Index.descriptor.name); this.ctx.customModelProperties.unregister(Model.MaxIndex.descriptor.name); this.ctx.customStructureProperties.unregister(Structure.Index.descriptor.name); this.ctx.customStructureProperties.unregister(Structure.MaxIndex.descriptor.name); }; return class_1; }(PluginBehavior.Handler)) });