UNPKG

molstar

Version:

A comprehensive macromolecular library.

107 lines 5.1 kB
/** * Copyright (c) 2019-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> */ import { __awaiter, __generator } from "tslib"; import { StructureElement } from '../../mol-model/structure/structure'; import { OrderedSet } from '../../mol-data/int'; import { CustomModelProperty } from './custom-model-property'; import { CustomPropertyDescriptor } from '../../mol-model/custom-property'; export { CustomElementProperty }; var CustomElementProperty; (function (CustomElementProperty) { function create(builder) { var _a; var modelProperty = createModelProperty(builder); return { propertyProvider: modelProperty, colorThemeProvider: ((_a = builder.coloring) === null || _a === void 0 ? void 0 : _a.getColor) && createColorThemeProvider(modelProperty, builder.coloring.getColor, builder.coloring.defaultColor), labelProvider: builder.getLabel && createLabelProvider(modelProperty, builder.getLabel) }; } CustomElementProperty.create = create; function createModelProperty(builder) { var _this = this; return CustomModelProperty.createProvider({ label: builder.label, descriptor: CustomPropertyDescriptor({ name: builder.name, }), type: builder.type || 'dynamic', defaultParams: {}, getParams: function (data) { return ({}); }, isApplicable: function (data) { return !builder.isApplicable || !!builder.isApplicable(data); }, obtain: function (ctx, data) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, builder.getData(data, ctx)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); } }); } function createColorThemeProvider(modelProperty, getColor, defaultColor) { function Coloring(ctx, props) { var color; var property = ctx.structure && modelProperty.get(ctx.structure.models[0]); var contextHash = property === null || property === void 0 ? void 0 : property.version; if ((property === null || property === void 0 ? void 0 : property.value) && ctx.structure) { var data_1 = property.value; color = function (location) { if (StructureElement.Location.is(location)) { var e = data_1.get(location.element); if (typeof e !== 'undefined') return getColor(e); } return defaultColor; }; } else { color = function () { return defaultColor; }; } return { factory: Coloring, granularity: 'group', color: color, props: props, contextHash: contextHash, description: "Assign element colors based on '" + modelProperty.label + "' data." }; } return { name: modelProperty.descriptor.name, label: modelProperty.label, category: 'Custom', factory: Coloring, getParams: function () { return ({}); }, defaultValues: {}, isApplicable: function (ctx) { return !!ctx.structure && !!modelProperty.get(ctx.structure.models[0]).value; }, ensureCustomProperties: { attach: function (ctx, data) { return data.structure ? modelProperty.attach(ctx, data.structure.models[0], void 0, true) : Promise.resolve(); }, detach: function (data) { return data.structure && data.structure.models[0].customProperties.reference(modelProperty.descriptor, false); } } }; } function createLabelProvider(modelProperty, getLabel) { return { label: function (loci) { if (loci.kind === 'element-loci') { var e = loci.elements[0]; if (!e || !e.unit.model.customProperties.hasReference(modelProperty.descriptor)) return; var data = modelProperty.get(e.unit.model).value; var element = e.unit.elements[OrderedSet.start(e.indices)]; var value = data === null || data === void 0 ? void 0 : data.get(element); if (value === undefined) return; return getLabel(value); } return; } }; } })(CustomElementProperty || (CustomElementProperty = {})); //# sourceMappingURL=custom-element-property.js.map