molstar
Version:
A comprehensive macromolecular library.
133 lines • 5.44 kB
JavaScript
/**
* Copyright (c) 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.VolumeHierarchy = exports.buildVolumeHierarchy = void 0;
var objects_1 = require("../../objects");
var mol_state_1 = require("../../../mol-state");
function buildVolumeHierarchy(state, previous) {
var build = BuildState(state, previous || VolumeHierarchy());
doPreOrder(state.tree, build);
if (previous)
previous.refs.forEach(isRemoved, build);
return { hierarchy: build.hierarchy, added: build.added, changed: build.changed };
}
exports.buildVolumeHierarchy = buildVolumeHierarchy;
function VolumeHierarchy() {
return { volumes: [], lazyVolumes: [], refs: new Map() };
}
exports.VolumeHierarchy = VolumeHierarchy;
function VolumeRef(cell) {
return { kind: 'volume', cell: cell, version: cell.transform.version, representations: [] };
}
function LazyVolumeRef(cell) {
return { kind: 'lazy-volume', cell: cell, version: cell.transform.version };
}
function VolumeRepresentationRef(cell, volume) {
return { kind: 'volume-representation', cell: cell, version: cell.transform.version, volume: volume };
}
function BuildState(state, oldHierarchy) {
return { state: state, oldHierarchy: oldHierarchy, hierarchy: VolumeHierarchy(), changed: false, added: new Set() };
}
function createOrUpdateRefList(state, cell, list, ctor) {
var args = [];
for (var _i = 4; _i < arguments.length; _i++) {
args[_i - 4] = arguments[_i];
}
var ref = ctor.apply(void 0, args);
list.push(ref);
state.hierarchy.refs.set(cell.transform.ref, ref);
var old = state.oldHierarchy.refs.get(cell.transform.ref);
if (old) {
if (old.version !== cell.transform.version)
state.changed = true;
}
else {
state.added.add(ref.cell.transform.ref);
state.changed = true;
}
return ref;
}
function isTypeRoot(t, target) {
return function (cell, state) { return !target(state) && t.is(cell.obj); };
}
function noop() { }
var Mapping = [
[isTypeRoot(objects_1.PluginStateObject.Volume.Data, function (t) { return t.currentVolume; }), function (state, cell) {
state.currentVolume = createOrUpdateRefList(state, cell, state.hierarchy.volumes, VolumeRef, cell);
}, function (state) { return state.currentVolume = void 0; }],
[function (cell) { return objects_1.PluginStateObject.Volume.Lazy.is(cell.obj); }, function (state, cell) {
createOrUpdateRefList(state, cell, state.hierarchy.lazyVolumes, LazyVolumeRef, cell);
}, noop],
[function (cell, state) {
return !cell.state.isGhost && !!state.currentVolume && objects_1.PluginStateObject.Volume.Representation3D.is(cell.obj);
}, function (state, cell) {
if (state.currentVolume) {
createOrUpdateRefList(state, cell, state.currentVolume.representations, VolumeRepresentationRef, cell, state.currentVolume);
}
return false;
}, noop]
];
function isValidCell(cell) {
if (!cell || !(cell === null || cell === void 0 ? void 0 : cell.parent) || !cell.parent.cells.has(cell.transform.ref))
return false;
var obj = cell.obj;
if (!obj || obj === mol_state_1.StateObject.Null || (cell.status !== 'ok' && cell.status !== 'error'))
return false;
return true;
}
function isRemoved(ref) {
var cell = ref.cell;
if (isValidCell(cell))
return;
this.changed = true;
}
function _preOrderFunc(c) { _doPreOrder(this, this.tree.transforms.get(c)); }
function _doPreOrder(ctx, root) {
var state = ctx.state;
var cell = state.state.cells.get(root.ref);
if (!isValidCell(cell))
return;
var onLeave = void 0;
var end = false;
for (var _i = 0, Mapping_1 = Mapping; _i < Mapping_1.length; _i++) {
var _a = Mapping_1[_i], test_1 = _a[0], f = _a[1], l = _a[2];
if (test_1(cell, state)) {
var cont = f(state, cell);
if (cont === false) {
end = true;
break;
}
onLeave = l;
break;
}
}
// TODO: might be needed in the future
// const { currentComponent, currentModel, currentStructure, currentTrajectory } = ctx.state;
// const inTrackedSubtree = currentComponent || currentModel || currentStructure || currentTrajectory;
// if (inTrackedSubtree && cell.transform.transformer.definition.isDecorator) {
// const ref = cell.transform.ref;
// const old = ctx.state.oldHierarchy.decorators.get(ref);
// if (old && old.version !== cell.transform.version) {
// ctx.state.changed = true;
// }
// ctx.state.hierarchy.decorators.set(cell.transform.ref, cell.transform);
// }
if (end)
return;
var children = ctx.tree.children.get(root.ref);
if (children && children.size) {
children.forEach(_preOrderFunc, ctx);
}
if (onLeave)
onLeave(state);
}
function doPreOrder(tree, state) {
var ctx = { tree: tree, state: state };
_doPreOrder(ctx, tree.root);
return ctx.state;
}
//# sourceMappingURL=hierarchy-state.js.map
;