molstar
Version:
A comprehensive macromolecular library.
110 lines (109 loc) • 5.19 kB
JavaScript
/**
* 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>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomElementProperty = void 0;
var tslib_1 = require("tslib");
var structure_1 = require("../../mol-model/structure/structure");
var int_1 = require("../../mol-data/int");
var custom_model_property_1 = require("./custom-model-property");
var custom_property_1 = require("../../mol-model/custom-property");
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 custom_model_property_1.CustomModelProperty.createProvider({
label: builder.label,
descriptor: (0, custom_property_1.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 tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__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 (structure_1.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 '".concat(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; },
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[int_1.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 = {}));
exports.CustomElementProperty = CustomElementProperty;
;