Skip to content

Commit b102e46

Browse files
committed
DM-7784-7963: refactor code.
1 parent 3bae000 commit b102e46

File tree

1 file changed

+58
-56
lines changed

1 file changed

+58
-56
lines changed

src/firefly/js/templates/lightcurve/LcManager.js

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,23 @@ export const DEF_IMAGE_CNT= 5;
3030
export const MAX_IMAGE_CNT= 7;
3131
const plotIdRoot= 'LC_FRAME-';
3232

33+
34+
var webplotRequestCreator;
35+
/**
36+
* A function to create a WebPlotRequest from the given parameters
37+
* @callback WebplotRequestCreator
38+
* @param {TableModel} tableModel
39+
* @param {number} hlrow
40+
* @param {number} cutoutSize
41+
*/
42+
3343
/**
34-
* This event manager is custom made for light curve viewer.
44+
* This event manager is custom made for light curve viewer.
45+
* @param {Object} params
46+
* @props {WebplotRequestCreator} params.webplotRequestCreator
3547
*/
36-
export function* lcManager() {
48+
export function* lcManager(params={}) {
49+
webplotRequestCreator = params.WebplotRequestCreator || getWebPlotRequestViaUrl;
3750

3851
while (true) {
3952
const action = yield take([
@@ -87,7 +100,7 @@ function handleTableLoad(layoutInfo, action) {
87100
if (isImageEnabledTable(tbl_id)) {
88101
layoutInfo = updateSet(layoutInfo, 'showImages', true);
89102
layoutInfo = updateSet(layoutInfo, 'images.activeTableId', tbl_id);
90-
exec(setupImages, tbl_id);
103+
setupImages(tbl_id);
91104
}
92105
return layoutInfo;
93106
}
@@ -98,15 +111,15 @@ function handleTableActive(layoutInfo, action) {
98111
const {tbl_id} = action.payload;
99112
if (isImageEnabledTable(tbl_id)) {
100113
layoutInfo = updateSet(layoutInfo, 'images.activeTableId', tbl_id);
101-
exec(setupImages, tbl_id);
114+
setupImages(tbl_id);
102115
}
103116
return layoutInfo;
104117
}
105118

106119
function handleTableHighlight(layoutInfo, action) {
107120
const {tbl_id} = action.payload;
108121
if (isImageEnabledTable(tbl_id)) {
109-
exec(setupImages, tbl_id);
122+
setupImages(tbl_id);
110123
}
111124
}
112125

@@ -117,7 +130,7 @@ function isImageEnabledTable(tbl_id) {
117130
function handleChangeMultiViewLayout(layoutInfo, action) {
118131
const activeTableId = get(layoutInfo, 'images.activeTableId');
119132
const tbl= getTblById(activeTableId);
120-
if (get(tbl, 'totalRows',0)>0) exec(setupImages, activeTableId);
133+
if (get(tbl, 'totalRows',0)>0) setupImages(activeTableId);
121134
return layoutInfo;
122135
}
123136

@@ -157,7 +170,7 @@ function getWebPlotRequest(tableModel, hlrow) {
157170

158171
}
159172

160-
function getWebPlotRequestViaUrl(tableModel, hlrow) {
173+
function getWebPlotRequestViaUrl(tableModel, hlrow, cutoutSize) {
161174
const ra = getCellValue(tableModel, hlrow, 'ra');
162175
const dec = getCellValue(tableModel, hlrow, 'dec');
163176
const frameId = getCellValue(tableModel, hlrow, 'frame_id');
@@ -167,21 +180,18 @@ function getWebPlotRequestViaUrl(tableModel, hlrow) {
167180
const scangrp = res[2];
168181
const frame_num = res[3];
169182

170-
let {cutoutSize} = FieldGroupUtils.getGroupFields(grpkey);
171-
const cutoutsize = cutoutSize.value;
172-
173183
/*the following should be from reading in the url column returned from LC search
174184
we are constructing the url for wise as the LC table does
175185
not have the url colume yet
176186
It is only for WISE, using default cutout size 0.3 deg
177187
const url = `http://irsa.ipac.caltech.edu/ibe/data/wise/merge/merge_p1bm_frm/${scangrp}/${scan_id}/${frame_num}/${scan_id}${frame_num}-w1-int-1b.fits`;
178188
*/
179189
const serverinfo = 'http://irsa.ipac.caltech.edu/ibe/data/wise/merge/merge_p1bm_frm/';
180-
const centerandsize = `?center=${ra},${dec}&size=${cutoutsize}&gzip=false`;
190+
const centerandsize = cutoutSize ? `?center=${ra},${dec}&size=${cutoutSize}&gzip=false` : '';
181191
const url = `${serverinfo}${scangrp}/${scan_id}/${frame_num}/${scan_id}${frame_num}-w1-int-1b.fits${centerandsize}`;
182192
const plot_desc = `WISE-${frameId}`;
183193
const reqParams = WebPlotRequest.makeURLPlotRequest(url, plot_desc);
184-
reqParams.setTitle('WISE-'+ frameId + ' size: '+ cutoutsize +'(deg)');
194+
reqParams.setTitle('WISE-'+ frameId + (cutoutSize ? ` size: ${cutoutSize}(deg)` : ''));
185195
reqParams.setTitleOptions(TitleOptions.NONE);
186196
reqParams.setGroupLocked(true);
187197
reqParams.setPlotGroupId('LightCurveGroup');
@@ -193,37 +203,42 @@ function getWebPlotRequestViaUrl(tableModel, hlrow) {
193203
}
194204

195205
export function setupImages(tbl_id) {
196-
const viewer= getViewer(getMultiViewRoot(),IMG_VIEWER_ID);
197-
const count= get(viewer, 'layoutDetail.count',DEF_IMAGE_CNT);
198-
const tableModel = getTblById(tbl_id);
199-
if (!tableModel || isNil(tableModel.highlightedRow)) return;
200-
var vr= visRoot();
201-
const newPlotIdAry= makePlotIds(tableModel.highlightedRow, tableModel.totalRows,count);
202-
const maxPlotIdAry= makePlotIds(tableModel.highlightedRow, tableModel.totalRows,MAX_IMAGE_CNT);
203-
204-
205-
newPlotIdAry.forEach( (plotId) => {
206-
if (!getPlotViewById(vr,plotId)) {
207-
const rowNum= Number(plotId.substring(plotIdRoot.length));
208-
const webPlotReq = getWebPlotRequestViaUrl(tableModel,rowNum);
209-
dispatchPlotImage({plotId, wpRequest:webPlotReq,
210-
setNewPlotAsActive:false,
211-
holdWcsMatch:true,
212-
pvOptions: { userCanDeletePlots: false}});
213-
}
214-
});
215-
216-
217-
dispatchReplaceViewerItems(IMG_VIEWER_ID, newPlotIdAry);
218-
dispatchChangeActivePlotView(plotIdRoot+tableModel.highlightedRow);
219-
220-
vr= visRoot();
221-
222-
vr.plotViewAry
223-
.filter( (pv) => pv.plotId.startsWith(plotIdRoot))
224-
.filter( (pv) => pv.plotId!==vr.mpwWcsPrimId)
225-
.filter( (pv) => !maxPlotIdAry.includes(pv.plotId))
226-
.forEach( (pv) => dispatchDeletePlotView({plotId:pv.plotId, holdWcsMatch:true}));
206+
try {
207+
const viewer= getViewer(getMultiViewRoot(),IMG_VIEWER_ID);
208+
const count= get(viewer, 'layoutDetail.count',DEF_IMAGE_CNT);
209+
const tableModel = getTblById(tbl_id);
210+
if (!tableModel || isNil(tableModel.highlightedRow)) return;
211+
var vr= visRoot();
212+
const newPlotIdAry= makePlotIds(tableModel.highlightedRow, tableModel.totalRows,count);
213+
const maxPlotIdAry= makePlotIds(tableModel.highlightedRow, tableModel.totalRows,MAX_IMAGE_CNT);
214+
215+
const cutoutSize = get(FieldGroupUtils.getGroupFields(grpkey), ['cutoutSize', 'value'], null);
216+
217+
newPlotIdAry.forEach( (plotId) => {
218+
if (!getPlotViewById(vr,plotId)) {
219+
const rowNum= Number(plotId.substring(plotIdRoot.length));
220+
const webPlotReq = webplotRequestCreator(tableModel,rowNum, cutoutSize);
221+
dispatchPlotImage({plotId, wpRequest:webPlotReq,
222+
setNewPlotAsActive:false,
223+
holdWcsMatch:true,
224+
pvOptions: { userCanDeletePlots: false}});
225+
}
226+
});
227+
228+
229+
dispatchReplaceViewerItems(IMG_VIEWER_ID, newPlotIdAry);
230+
dispatchChangeActivePlotView(plotIdRoot+tableModel.highlightedRow);
231+
232+
vr= visRoot();
233+
234+
vr.plotViewAry
235+
.filter( (pv) => pv.plotId.startsWith(plotIdRoot))
236+
.filter( (pv) => pv.plotId!==vr.mpwWcsPrimId)
237+
.filter( (pv) => !maxPlotIdAry.includes(pv.plotId))
238+
.forEach( (pv) => dispatchDeletePlotView({plotId:pv.plotId, holdWcsMatch:true}));
239+
} catch (E){
240+
console.log(E.toString());
241+
}
227242
}
228243

229244

@@ -242,16 +257,3 @@ function makePlotIds(highlightedRow, totalRows, totalPlots) {
242257
}
243258

244259

245-
/**
246-
* A simple wrapper to catch the exception then log it to console
247-
* @param {function} f function to execute
248-
* @param {*} args function's arguments.
249-
*/
250-
function exec(f, args) {
251-
try {
252-
f(args);
253-
} catch (E){
254-
console.log(E.toString());
255-
}
256-
257-
}

0 commit comments

Comments
 (0)