Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Replace custom message switch by a dictionary
Browse files Browse the repository at this point in the history
Xen0Xys committed Apr 24, 2024
1 parent dd08e8a commit 875984c
Showing 2 changed files with 41 additions and 60 deletions.
60 changes: 17 additions & 43 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
@@ -75,7 +75,6 @@ export default class EventHandler {
});

/* Div control */

this.model.on("change:height", () => {
let height = this.model.get("height");
aladinDiv.style.height = `${height}px`;
@@ -161,49 +160,24 @@ export default class EventHandler {
.setAlpha(this.model.get("overlay_survey_opacity"));
});

this.eventHandlers = {
goto_ra_dec: this.messageHandler.handleGotoRaDec,
add_catalog_from_URL: this.messageHandler.handleAddCatalogFromURL,
add_MOC_from_URL: this.messageHandler.handleAddMOCFromURL,
add_MOC_from_dict: this.messageHandler.handleAddMOCFromDict,
add_overlay_from_stcs: this.messageHandler.handleAddOverlayFromSTCS,
change_colormap: this.messageHandler.handleChangeColormap,
get_JPG_thumbnail: this.messageHandler.handleGetJPGThumbnail,
trigger_rectangular_selection:
this.messageHandler.handleTriggerRectangularSelection,
add_table: this.messageHandler.handleAddTable,
};

this.model.on("msg:custom", (msg, buffers) => {
let options = {};
switch (msg["event_name"]) {
case "goto_ra_dec":
this.messageHandler.handleGotoRaDec(msg["ra"], msg["dec"]);
break;
case "add_catalog_from_URL":
this.messageHandler.handleAddCatalogFromURL(
msg["votable_URL"],
msg["options"],
);
break;
case "add_MOC_from_URL":
this.messageHandler.handleAddMOCFromURL(
msg["moc_URL"],
msg["options"],
);
break;
case "add_MOC_from_dict":
this.messageHandler.handleAddMOCFromDict(
msg["moc_dict"],
msg["options"],
);
break;
case "add_overlay_from_stcs":
this.messageHandler.handleAddOverlayFromSTCS(
msg["overlay_options"],
msg["stc_string"],
);
break;
case "change_colormap":
this.messageHandler.handleChangeColormap(msg["colormap"]);
break;
case "get_JPG_thumbnail":
this.messageHandler.handleGetJPGThumbnail();
break;
case "trigger_rectangular_selection":
this.messageHandler.handleTriggerRectangularSelection();
break;
case "add_table":
this.messageHandler.handleAddTable(buffers[0].buffer, msg.options);
break;
}
const eventName = msg["event_name"];
const handler = this.eventHandlers[eventName];
if (handler) handler.call(this, msg, buffers);
else throw new Error(`Unknown event name: ${eventName}`);
});
}

41 changes: 24 additions & 17 deletions js/models/message_handler.js
Original file line number Diff line number Diff line change
@@ -4,36 +4,42 @@ export default class MessageHandler {
this.aladin = aladin;
}

handleGotoRaDec(ra, dec) {
this.aladin.gotoRaDec(ra, dec);
handleGotoRaDec(msg) {
this.aladin.gotoRaDec(msg["ra"], msg["dec"]);
}

handleAddCatalogFromURL(votableURL, options) {
this.aladin.addCatalog(this.A.catalogFromURL(votableURL, options));
handleAddCatalogFromURL(msg) {
this.aladin.addCatalog(
this.A.catalogFromURL(msg["votable_URL"], msg["options"]),
);
}

handleAddMOCFromURL(mocURL, options) {
handleAddMOCFromURL(msg) {
const options = msg["options"] || {};
if (options["lineWidth"] === undefined) {
options["lineWidth"] = 3;
}
this.aladin.addMOC(this.A.MOCFromURL(mocURL, options));
this.aladin.addMOC(this.A.MOCFromURL(msg["moc_URL"], options));
}

handleAddMOCFromDict(mocDict, options) {
handleAddMOCFromDict(msg) {
const options = msg["options"] || {};
if (options["lineWidth"] === undefined) {
options["lineWidth"] = 3;
}
this.aladin.addMOC(this.A.MOCFromJSON(mocDict, options));
this.aladin.addMOC(this.A.MOCFromJSON(msg["moc_dict"], options));
}

handleAddOverlayFromSTCS(overlayOptions, stcString) {
let overlay = this.A.graphicOverlay(overlayOptions);
handleAddOverlayFromSTCS(msg) {
const overlayOptions = msg["overlay_options"];
const stcString = msg["stc_string"];
const overlay = this.A.graphicOverlay(overlayOptions);
this.aladin.addOverlay(overlay);
overlay.addFootprints(this.A.footprintsFromSTCS(stcString));
}

handleChangeColormap(colormap) {
this.aladin.getBaseImageLayer().setColormap(colormap);
handleChangeColormap(msg) {
this.aladin.getBaseImageLayer().setColormap(msg["colormap"]);
}

handleGetJPGThumbnail() {
@@ -44,11 +50,12 @@ export default class MessageHandler {
this.aladin.select();
}

handleAddTable(buffer, options) {
let tableBytes = buffer;
let decoder = new TextDecoder("utf-8");
let blob = new Blob([decoder.decode(tableBytes)]);
let url = URL.createObjectURL(blob);
handleAddTable(msg, buffers) {
const options = msg["options"] || {};
const buffer = buffers[0].buffer;
const decoder = new TextDecoder("utf-8");
const blob = new Blob([decoder.decode(buffer)]);
const url = URL.createObjectURL(blob);
this.A.catalogFromURL(
url,
Object.assign(options, { onClick: "showTable" }),

0 comments on commit 875984c

Please sign in to comment.