loci-label.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2018-2021 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 { PluginContext } from '../../mol-plugin/context';
  8. import { Loci } from '../../mol-model/loci';
  9. import { Representation } from '../../mol-repr/representation';
  10. import { MarkerAction } from '../../mol-util/marker-action';
  11. import { arrayRemoveAtInPlace } from '../../mol-util/array';
  12. export type LociLabel = JSX.Element | string
  13. export type LociLabelProvider = {
  14. label: (loci: Loci, repr?: Representation<any>) => LociLabel | undefined
  15. group?: (entry: LociLabel) => string
  16. /** Labels from providers with higher priority are shown first */
  17. priority?: number
  18. }
  19. export class LociLabelManager {
  20. providers: LociLabelProvider[] = [];
  21. clearProviders() {
  22. this.providers = [];
  23. this.isDirty = true;
  24. this.showLabels();
  25. }
  26. addProvider(provider: LociLabelProvider) {
  27. this.providers.push(provider);
  28. this.providers.sort((a, b) => (b.priority || 0) - (a.priority || 0));
  29. this.isDirty = true;
  30. this.showLabels();
  31. }
  32. removeProvider(provider: LociLabelProvider) {
  33. this.providers = this.providers.filter(p => p !== provider);
  34. this.isDirty = true;
  35. this.showLabels();
  36. }
  37. private locis: Representation.Loci[] = []
  38. private mark(loci: Representation.Loci, action: MarkerAction) {
  39. const idx = this.locis.findIndex(l => Representation.Loci.areEqual(loci, l));
  40. if (idx === -1 && action === MarkerAction.Highlight) {
  41. this.locis.push(loci);
  42. this.isDirty = true;
  43. } else if (idx !== -1 && action === MarkerAction.RemoveHighlight) {
  44. arrayRemoveAtInPlace(this.locis, idx);
  45. this.isDirty = true;
  46. }
  47. }
  48. private isDirty = false
  49. private labels: LociLabel[] = []
  50. private groupedLabels = new Map<string, LociLabel[]>()
  51. private showLabels() {
  52. this.ctx.behaviors.labels.highlight.next({ labels: this.getLabels() });
  53. }
  54. private getLabels() {
  55. if (this.isDirty) {
  56. this.groupedLabels.clear();
  57. this.labels.length = 0;
  58. for (const provider of this.providers) {
  59. for (const loci of this.locis) {
  60. if (Loci.isEmpty(loci.loci)) continue;
  61. const label = provider.label(loci.loci, loci.repr);
  62. if (label) {
  63. const hash = provider.group ? provider.group(label) : label.toString();
  64. const group = this.groupedLabels.get(hash);
  65. if (group) group.push(label);
  66. else this.groupedLabels.set(hash, [label]);
  67. }
  68. }
  69. }
  70. this.labels.length = 0;
  71. this.groupedLabels.forEach((group, hash) => {
  72. const count = group.length;
  73. const entry = count > 1 && group[0] !== group[1]
  74. ? hash : group[0];
  75. this.labels.push(count === 1 ? entry : `${entry} <small>|| \u00D7 ${count}</small>`);
  76. });
  77. this.isDirty = false;
  78. }
  79. return this.labels;
  80. }
  81. constructor(public ctx: PluginContext) {
  82. ctx.managers.interactivity.lociHighlights.addProvider((loci, action, noRender) => {
  83. this.mark(loci, action);
  84. if (!noRender) this.showLabels();
  85. });
  86. }
  87. }