UNPKG

@rcsb/rcsb-saguaro

Version:
104 lines (103 loc) 3.9 kB
import { EventType } from "../RcsbFvContextManager/RcsbFvContextManager"; /** * Board Selection and Scale Manager * */ export class RcsbFvStateManager { constructor(config) { this.boardId = config.boardId; this.contextManager = config.contextManager; this.xScale = config.xScale; this.selection = config.selection; this.subscription = this.subscribe(); } unsubscribe() { this.subscription.unsubscribe(); } /**Subscribe className to rxjs events (adding tracks, change scale, update board config) * @return rxjs Subscription object * */ subscribe() { return this.contextManager.subscribe((obj) => { 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 * */ setSelection(newSelection) { var _a; if ((newSelection === null || newSelection === void 0 ? void 0 : newSelection.elements) != null) { const list = 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 === null || newSelection === void 0 ? void 0 : newSelection.mode); } this.select((_a = newSelection === null || newSelection === void 0 ? void 0 : newSelection.mode) !== null && _a !== void 0 ? _a : 'select'); } /**Add elements to selection object * @param newSelection new selection elements to be added * */ addSelection(newSelection) { var _a; if ((newSelection === null || newSelection === void 0 ? void 0 : newSelection.elements) != null) { const list = 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((_a = newSelection === null || newSelection === void 0 ? void 0 : newSelection.mode) !== null && _a !== void 0 ? _a : 'select'); } } /**Force current selection in all tracks.*/ select(mode) { this.contextManager.next({ eventType: EventType.SELECTION, eventData: { trackId: this.boardId, mode: mode } }); } /**Update d3 xScale domain * @param domainData new xScale domain * */ setDomain(domainData) { this.xScale.domain(domainData.domain); this.setScale(); } /**Force all board track annotation cells to set xScale. Called when a new track has been added*/ setScale() { if (this.xScale != null) { this.contextManager.next({ eventType: EventType.SCALE, eventData: this.boardId }); } } }