UNPKG

@rcsb/rcsb-saguaro

Version:
149 lines (148 loc) 5.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoardDataState = void 0; const tslib_1 = require("tslib"); const uniqid_1 = tslib_1.__importDefault(require("uniqid")); const RcsbFvContextManager_1 = require("../../RcsbFvContextManager/RcsbFvContextManager"); const array_move_1 = require("array-move"); const RowStatusMap_1 = require("./RowStatusMap"); class BoardDataState { constructor(config) { var _a, _b; this.rowConfigData = []; this.rowStatusMap = new RowStatusMap_1.RowStatusMap(); this.contextManager = config.contextManager; this.boardId = config.boardId; this.rowConfigData = (_b = (_a = config.rowConfigData) === null || _a === void 0 ? void 0 : _a.map(r => this.checkRow(r))) !== null && _b !== void 0 ? _b : []; this.subscription = this.subscribe(); } getBoardData() { return this.rowConfigData; } setBoardData(rowConfigData) { this.rowStatusMap.clear(); this.rowConfigData = rowConfigData.map(r => this.checkRow(r)); } refresh() { this.rowStatusMap.clear(); this.rowConfigData = this.rowConfigData.map(r => this.checkRow(r)); } /**Adds a new track to the board * @param configRow Track configuration object * */ addTrack(configRow) { this.rowConfigData.push(Object.assign(Object.assign({}, this.checkRow(configRow)), { renderSchedule: "sync" })); } /**Modifies visibility of a board track * @param obj Target track id and visibility flag (true/false) * */ changeTrackVisibility(obj) { const row = this.rowConfigData.find(r => r.trackId === this.uniqueTrackId(obj)); if (!row) return; row.trackVisibility = obj.visibility; row.renderSchedule = "sync"; } moveTrack(move) { (0, array_move_1.arrayMoveMutable)(this.rowConfigData, move.oldIndex, move.newIndex); } /**Replace board track rack data * @param obj New track data and target track id * */ updateTrackData(obj) { this.changeTrackData(obj, "replace"); } /**Add new data to a given board track * @param obj Additional track data and target track id * */ addTrackData(obj) { this.changeTrackData(obj, "add"); } /**Rerender the board track * @param trackId Id that identifies the track * */ resetTrack(trackId) { const row = this.rowConfigData.find(r => r.trackId === this.uniqueTrackId({ trackId })); if (!row) return; row.key = generateKey(row.trackId); row.renderSchedule = "sync"; } unsubscribe() { this.subscription.unsubscribe(); } /**Modifies a board track data * @param obj Additional track data and target track id * @param flag Replace track data or add data to the current one * */ changeTrackData(obj, flag) { var _a; const row = this.rowConfigData.find(r => r.trackId === this.uniqueTrackId(obj)); if (!row) return; if (row.displayType != "composite" /* RcsbFvDisplayTypes.COMPOSITE */) { if (flag === "replace") row.trackData = obj.trackData; else if (flag === "add" && row.trackData instanceof Array) row.trackData = row.trackData.concat(obj.trackData); } else if (row.displayType === "composite" /* RcsbFvDisplayTypes.COMPOSITE */ && row.displayConfig instanceof Array) { (_a = row.displayConfig) === null || _a === void 0 ? void 0 : _a.forEach((display) => { var _a; if (display.displayId === obj.displayId) { if (flag === "replace") display.displayData = obj.trackData; else if (flag === "add" && display.displayData instanceof Array) display.displayData = (_a = display.displayData) === null || _a === void 0 ? void 0 : _a.concat(obj.trackData); } }); } row.key = generateKey(row.trackId); row.renderSchedule = "sync"; } checkRow(d) { const trackId = this.uniqueTrackId(d); if (d.trackVisibility === false) this.rowStatusMap.set(trackId, true); else this.rowStatusMap.set(trackId, false); return Object.assign(Object.assign({}, d), { trackVisibility: typeof d.trackVisibility == "boolean" ? d.trackVisibility : true, key: generateKey(trackId), boardId: this.boardId, trackId }); } subscribe() { return this.contextManager.subscribe((o) => { switch (o.eventType) { case RcsbFvContextManager_1.EventType.ROW_READY: this.rowReady(o.eventData); break; } }); } /**Row Track Board Ready Event * @param rowData * */ rowReady(rowData) { this.rowStatusMap.set(rowData.rowId, true); this.contextManager.next({ eventType: RcsbFvContextManager_1.EventType.FRACTION_COMPLETED, eventData: Math.ceil(this.rowStatusMap.completed() / this.rowStatusMap.size() * 100) }); if (this.rowStatusMap.complete()) { this.boardReady(); } } boardReady() { this.contextManager.next({ eventType: RcsbFvContextManager_1.EventType.BOARD_READY, eventData: null }); } uniqueTrackId(d) { var _a; const trackId = (_a = d.trackId) !== null && _a !== void 0 ? _a : (0, uniqid_1.default)("trackId_"); return `${trackId}_${this.boardId}`; } } exports.BoardDataState = BoardDataState; function generateKey(innerTrackId) { return `${innerTrackId}_${(0, uniqid_1.default)("key_")}`; }