molstar
Version:
A comprehensive macromolecular library.
142 lines • 6.14 kB
JavaScript
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { __extends } from "tslib";
import { setSubtreeVisibility } from '../../../mol-plugin/behavior/static/state';
import { PluginComponent } from '../../component';
import { buildVolumeHierarchy, VolumeHierarchy } from './hierarchy-state';
import { createVolumeRepresentationParams } from '../../helpers/volume-representation-params';
import { StateTransforms } from '../../transforms';
var VolumeHierarchyManager = /** @class */ (function (_super) {
__extends(VolumeHierarchyManager, _super);
function VolumeHierarchyManager(plugin) {
var _this = _super.call(this) || this;
_this.plugin = plugin;
_this.state = {
syncedTree: _this.dataState.tree,
notified: false,
hierarchy: VolumeHierarchy(),
selection: void 0
};
_this.behaviors = {
selection: _this.ev.behavior({
hierarchy: _this.current,
volume: _this.selection
})
};
_this.subscribe(plugin.state.data.events.changed, function (e) {
if (e.inTransaction || plugin.behaviors.state.isAnimating.value)
return;
_this.sync(true);
});
_this.subscribe(plugin.behaviors.state.isAnimating, function (isAnimating) {
if (!isAnimating && !plugin.behaviors.state.isUpdating.value)
_this.sync(true);
});
return _this;
}
Object.defineProperty(VolumeHierarchyManager.prototype, "dataState", {
get: function () {
return this.plugin.state.data;
},
enumerable: false,
configurable: true
});
Object.defineProperty(VolumeHierarchyManager.prototype, "current", {
get: function () {
this.sync(false);
return this.state.hierarchy;
},
enumerable: false,
configurable: true
});
Object.defineProperty(VolumeHierarchyManager.prototype, "selection", {
get: function () {
this.sync(false);
return this.state.selection;
},
enumerable: false,
configurable: true
});
VolumeHierarchyManager.prototype.sync = function (notify) {
if (!notify && this.dataState.inUpdate)
return;
if (this.state.syncedTree === this.dataState.tree) {
if (notify && !this.state.notified) {
this.state.notified = true;
this.behaviors.selection.next({ hierarchy: this.state.hierarchy, volume: this.state.selection });
}
return;
}
this.state.syncedTree = this.dataState.tree;
var update = buildVolumeHierarchy(this.plugin.state.data, this.current);
if (!update.changed) {
return;
}
var hierarchy = update.hierarchy;
this.state.hierarchy = hierarchy;
if (!this.state.selection) {
this.state.selection = hierarchy.volumes[0];
}
else {
this.state.selection = hierarchy.refs.has(this.state.selection.cell.transform.ref) ? hierarchy.refs.get(this.state.selection.cell.transform.ref) : hierarchy.volumes[0];
}
if (notify) {
this.state.notified = true;
this.behaviors.selection.next({ hierarchy: hierarchy, volume: this.state.selection });
}
else {
this.state.notified = false;
}
};
VolumeHierarchyManager.prototype.setCurrent = function (volume) {
this.state.selection = volume || this.state.hierarchy.volumes[0];
this.behaviors.selection.next({ hierarchy: this.state.hierarchy, volume: volume || this.state.hierarchy.volumes[0] });
};
// TODO: have common util
VolumeHierarchyManager.prototype.remove = function (refs, canUndo) {
if (refs.length === 0)
return;
var deletes = this.plugin.state.data.build();
for (var _i = 0, refs_1 = refs; _i < refs_1.length; _i++) {
var r = refs_1[_i];
deletes.delete(typeof r === 'string' ? r : r.cell.transform.ref);
}
return deletes.commit({ canUndo: canUndo ? 'Remove' : false });
};
// TODO: have common util
VolumeHierarchyManager.prototype.toggleVisibility = function (refs, action) {
if (refs.length === 0)
return;
var isHidden = action !== void 0
? (action === 'show' ? false : true)
: !refs[0].cell.state.isHidden;
for (var _i = 0, refs_2 = refs; _i < refs_2.length; _i++) {
var c = refs_2[_i];
setSubtreeVisibility(this.dataState, c.cell.transform.ref, isHidden);
}
};
VolumeHierarchyManager.prototype.addRepresentation = function (ref, type) {
var _a;
var update = this.dataState.build()
.to(ref.cell)
.apply(StateTransforms.Representation.VolumeRepresentation3D, createVolumeRepresentationParams(this.plugin, (_a = ref.cell.obj) === null || _a === void 0 ? void 0 : _a.data, {
type: type,
}));
return update.commit({ canUndo: 'Add Representation' });
};
return VolumeHierarchyManager;
}(PluginComponent));
export { VolumeHierarchyManager };
(function (VolumeHierarchyManager) {
function getRepresentationTypes(plugin, pivot) {
var _a, _b;
return ((_a = pivot === null || pivot === void 0 ? void 0 : pivot.cell.obj) === null || _a === void 0 ? void 0 : _a.data)
? plugin.representation.volume.registry.getApplicableTypes((_b = pivot.cell.obj) === null || _b === void 0 ? void 0 : _b.data)
: plugin.representation.volume.registry.types;
}
VolumeHierarchyManager.getRepresentationTypes = getRepresentationTypes;
})(VolumeHierarchyManager || (VolumeHierarchyManager = {}));
//# sourceMappingURL=hierarchy.js.map