loci-label-manager.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { PluginContext } from '../../mol-plugin/context';
  7. import { Loci } from '../../mol-model/loci';
  8. import { Representation } from '../../mol-repr/representation';
  9. export type LociLabelEntry = JSX.Element | string
  10. export type LociLabelProvider = (info: Loci, repr?: Representation<any>) => LociLabelEntry | undefined
  11. export class LociLabelManager {
  12. providers: LociLabelProvider[] = [];
  13. addProvider(provider: LociLabelProvider) {
  14. this.providers.push(provider);
  15. }
  16. removeProvider(provider: LociLabelProvider) {
  17. this.providers = this.providers.filter(p => p !== provider);
  18. // Event.Interactivity.Highlight.dispatch(this.ctx, []);
  19. }
  20. private empty: any[] = [];
  21. private getInfo({ loci, repr }: Representation.Loci) {
  22. if (!loci || loci.kind === 'empty-loci') return this.empty;
  23. const info: LociLabelEntry[] = [];
  24. for (let p of this.providers) {
  25. const e = p(loci, repr);
  26. if (e) info.push(e);
  27. }
  28. return info;
  29. }
  30. constructor(public ctx: PluginContext) {
  31. ctx.behaviors.interaction.highlight.subscribe(ev => ctx.behaviors.labels.highlight.next({ entries: this.getInfo(ev.current) }));
  32. }
  33. }