config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { Structure, Model } from '../mol-model/structure';
  8. import { PluginContext } from './context';
  9. export class PluginConfigItem<T = any> {
  10. toString() { return this.key; }
  11. valueOf() { return this.key; }
  12. constructor(public key: string, public defaultValue?: T) { }
  13. }
  14. function item<T>(key: string, defaultValue?: T) { return new PluginConfigItem(key, defaultValue); }
  15. export const PluginConfig = {
  16. item,
  17. General: {
  18. IsBusyTimeoutMs: item('plugin-config.is-busy-timeout', 750)
  19. },
  20. State: {
  21. DefaultServer: item('plugin-state.server', 'https://webchem.ncbr.muni.cz/molstar-state'),
  22. CurrentServer: item('plugin-state.server', 'https://webchem.ncbr.muni.cz/molstar-state')
  23. },
  24. VolumeStreaming: {
  25. DefaultServer: item('volume-streaming.server', 'https://ds.litemol.org'),
  26. CanStream: item('volume-streaming.can-stream', (s: Structure, plugin: PluginContext) => {
  27. return s.models.length === 1 && Model.probablyHasDensityMap(s.models[0]);
  28. }),
  29. EmdbHeaderServer: item('volume-streaming.emdb-header-server', 'https://ftp.wwpdb.org/pub/emdb/structures'),
  30. },
  31. Viewport: {
  32. ShowExpand: item('viewer.show-expand-button', true),
  33. ShowSelectionMode: item('viewer.show-selection-model-button', true),
  34. ShowAnimation: item('viewer.show-animation-button', true),
  35. }
  36. };
  37. export class PluginConfigManager {
  38. private _config = new Map<PluginConfigItem<any>, unknown>();
  39. get<T>(key: PluginConfigItem<T>) {
  40. if (!this._config.has(key)) return key.defaultValue;
  41. return this._config.get(key) as T;
  42. }
  43. set<T>(key: PluginConfigItem<T>, value: T) {
  44. this._config.set(key, value);
  45. }
  46. delete<T>(key: PluginConfigItem<T>) {
  47. this._config.delete(key);
  48. }
  49. constructor(initial?: Map<PluginConfigItem, unknown>) {
  50. if (!initial) return;
  51. initial.forEach((v, k) => this._config.set(k, v));
  52. }
  53. }