/
RcsbSelection.ts
84 lines (74 loc) · 3.12 KB
/
RcsbSelection.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
import {RcsbFvTrackDataElementInterface} from "../RcsbDataManager/RcsbDataManager";
export interface SelectionInterface {
rcsbFvTrackDataElement: RcsbFvTrackDataElementInterface;
domId: string;
}
//TODO add a callback for the 'hover' case
export class RcsbSelection {
private selectedElements: Array<SelectionInterface> = new Array<SelectionInterface>();
private hoverHighlightElements: Array<SelectionInterface> = new Array<SelectionInterface>();
private selectionChangeCallback: (selection: Array<RcsbFvTrackDataElementInterface>)=>void;
public setSelectionChangeCallback(f: (selection: Array<RcsbFvTrackDataElementInterface>)=>void): void{
this.selectionChangeCallback = f;
}
public getSelected(mode:'select'|'hover'):Array<SelectionInterface>{
if(mode == null || mode === 'select')
return this.selectedElements;
else
return this.hoverHighlightElements;
}
public setSelected(elements: Array<SelectionInterface> | SelectionInterface, mode:'select'|'hover'): void{
if(mode == null || mode === 'select') {
if (elements instanceof Array) {
this.selectedElements = elements;
} else {
this.selectedElements = [elements];
}
}else{
if (elements instanceof Array) {
this.hoverHighlightElements = elements;
} else {
this.hoverHighlightElements = [elements];
}
}
this.callback(mode);
}
public addSelected(elements: Array<SelectionInterface> | SelectionInterface, mode:'select'|'hover', replaceLast?:boolean): void{
if(mode == null || mode === 'select') {
if (elements instanceof Array) {
this.selectedElements = this.selectedElements.concat(elements);
} else {
if(replaceLast)
this.selectedElements[this.selectedElements.length-1].rcsbFvTrackDataElement = elements.rcsbFvTrackDataElement;
else
this.selectedElements.push(elements);
}
}else {
if (elements instanceof Array) {
this.hoverHighlightElements = this.selectedElements.concat(elements);
} else {
this.hoverHighlightElements.push(elements);
}
}
this.callback(mode);
}
public clearSelection(mode:'select'|'hover'):void {
if(mode == null || mode === 'select') {
this.selectedElements = new Array<SelectionInterface>();
}else{
this.hoverHighlightElements = new Array<SelectionInterface>();
}
this.callback(mode);
}
public reset(): void {
this.selectedElements = new Array<SelectionInterface>();
this.hoverHighlightElements = new Array<SelectionInterface>();
}
private callback(mode:'select'|'hover'): void{
if(mode == null || mode === 'select') {
if(typeof this.selectionChangeCallback === "function"){
this.selectionChangeCallback(this.selectedElements.map(e=>e.rcsbFvTrackDataElement));
}
}
}
}