pdbe-molstar
Version:
Molstar implementation for PDBe
74 lines (73 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginCustomControls = exports.ExtensionCustomState = void 0;
exports.PluginCustomState = PluginCustomState;
;
;
/** Access `plugin.customState` only through this function to get proper typing.
* Supports getting and setting properties. */
function PluginCustomState(plugin) {
var _a;
return (_a = plugin.customState) !== null && _a !== void 0 ? _a : (plugin.customState = {});
}
/** Functions for accessing plugin-bound custom state for extensions. */
exports.ExtensionCustomState = {
/** Get plugin-bound custom state for a specific extension. If not present, initialize with empty object. */
get(plugin, extensionId) {
var _a, _b;
var _c;
const extensionStates = (_a = (_c = PluginCustomState(plugin)).extensions) !== null && _a !== void 0 ? _a : (_c.extensions = {});
const extensionState = (_b = extensionStates[extensionId]) !== null && _b !== void 0 ? _b : (extensionStates[extensionId] = {});
return extensionState;
},
/** Remove plugin-bound custom state for a specific extension (if present). */
clear(plugin, extensionId) {
var _a;
var _b;
const extensionStates = (_a = (_b = PluginCustomState(plugin)).extensions) !== null && _a !== void 0 ? _a : (_b.extensions = {});
delete extensionStates[extensionId];
},
/** Return function which gets plugin-bound custom state for a specific extension. */
getter(extensionId) {
return (plugin) => this.get(plugin, extensionId);
},
};
/** Functions for registering/unregistering custom UI controls */
exports.PluginCustomControls = {
/** Get custom controls in the specified UI `region`. */
get(plugin, region) {
var _a, _b;
var _c;
const customControls = (_a = (_c = PluginCustomState(plugin)).customControls) !== null && _a !== void 0 ? _a : (_c.customControls = {});
return (_b = customControls[region]) !== null && _b !== void 0 ? _b : (customControls[region] = initialPluginCustomControls(plugin, region));
},
/** Register a custom control in the specified UI `region`. */
add(plugin, region, name, control) {
const registry = exports.PluginCustomControls.get(plugin, region);
if (!registry.has(name)) {
registry.set(name, control);
}
return {
delete: () => exports.PluginCustomControls.delete(plugin, region, name),
};
},
/** Unregister a custom control in the specified UI `region`. */
delete(plugin, region, name) {
const registry = exports.PluginCustomControls.get(plugin, region);
registry.delete(name);
},
/** Register/unregister a custom control in the specified UI `region`. */
toggle(plugin, region, name, control, show) {
if (show) {
exports.PluginCustomControls.add(plugin, region, name, control);
}
else {
exports.PluginCustomControls.delete(plugin, region, name);
}
},
};
function initialPluginCustomControls(plugin, region) {
if (region === 'structure-tools')
return plugin.customStructureControls;
return new Map();
}