UNPKG

molstar

Version:

A comprehensive macromolecular library.

128 lines 5.14 kB
/** * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { PluginStateObject as SO } from '../../objects'; import { StateObject } from '../../../mol-state'; export 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 }; } export function VolumeHierarchy() { return { volumes: [], lazyVolumes: [], refs: new Map() }; } 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(SO.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 SO.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 && SO.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 === 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