/
RcsbFvStateManager.ts
134 lines (122 loc) · 4.54 KB
/
RcsbFvStateManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
DomainViewInterface,
EventType,
RcsbFvContextManager,
RcsbFvContextManagerType,
SetSelectionInterface
} from "../RcsbFvContextManager/RcsbFvContextManager";
import {RcsbScaleInterface} from "../../RcsbBoard/RcsbD3/RcsbD3ScaleFactory";
import {RcsbSelection} from "../../RcsbBoard/RcsbSelection";
import {Subscription} from "rxjs";
interface RcsbFvStateInterface {
readonly boardId: string;
readonly contextManager: RcsbFvContextManager;
readonly xScale: RcsbScaleInterface;
readonly selection: RcsbSelection;
}
/**
* Board Selection and Scale Manager
* */
export class RcsbFvStateManager {
private readonly boardId: string;
private readonly contextManager: RcsbFvContextManager;
private readonly xScale: RcsbScaleInterface;
private readonly selection: RcsbSelection;
private readonly subscription: Subscription;
constructor(config:RcsbFvStateInterface) {
this.boardId = config.boardId;
this.contextManager = config.contextManager;
this.xScale = config.xScale;
this.selection = config.selection;
this.subscription = this.subscribe();
}
public unsubscribe(): void {
this.subscription.unsubscribe();
}
/**Subscribe className to rxjs events (adding tracks, change scale, update board config)
* @return rxjs Subscription object
* */
private subscribe(): Subscription{
return this.contextManager.subscribe((obj:RcsbFvContextManagerType)=>{
if(obj.eventType===EventType.DOMAIN_VIEW){
this.setDomain(obj.eventData);
}else if(obj.eventType===EventType.SET_SELECTION){
this.setSelection(obj.eventData);
}else if(obj.eventType===EventType.ADD_SELECTION){
this.addSelection(obj.eventData);
}
});
}
/**Update selection object
* @param newSelection new selection object
* */
private setSelection(newSelection: SetSelectionInterface): void {
if(newSelection?.elements != null){
const list: Array<{begin:number; end?:number; isEmpty?:boolean;}> = newSelection.elements instanceof Array ? newSelection.elements : [newSelection.elements];
this.selection.setSelected(list.map((x) => {
return {
domId: this.boardId,
rcsbFvTrackDataElement: {
begin: x.begin,
end: x.end,
isEmpty: x.isEmpty,
nonSpecific: true
}
};
}),
newSelection.mode
);
}else{
this.selection.clearSelection(newSelection?.mode);
}
this.select(newSelection?.mode ?? 'select');
}
/**Add elements to selection object
* @param newSelection new selection elements to be added
* */
private addSelection(newSelection: SetSelectionInterface): void {
if(newSelection?.elements != null){
const list: Array<{begin:number; end?:number; isEmpty?:boolean;}> = newSelection.elements instanceof Array ? newSelection.elements : [newSelection.elements];
this.selection.addSelected(list.map((x) => {
return {
domId: this.boardId,
rcsbFvTrackDataElement: {
begin: x.begin,
end: x.end,
isEmpty: x.isEmpty,
nonSpecific: true
}
};
}),
newSelection.mode
);
this.select(newSelection?.mode ?? 'select');
}
}
/**Force current selection in all tracks.*/
private select(mode:'select'|'hover'): void{
this.contextManager.next({
eventType: EventType.SELECTION,
eventData: {
trackId: this.boardId,
mode:mode
}
});
}
/**Update d3 xScale domain
* @param domainData new xScale domain
* */
private setDomain(domainData: DomainViewInterface): void {
this.xScale.domain(domainData.domain);
this.setScale();
}
/**Force all board track annotation cells to set xScale. Called when a new track has been added*/
private setScale(): void{
if(this.xScale!=null) {
this.contextManager.next({
eventType: EventType.SCALE,
eventData: this.boardId
});
}
}
}