From 875984cdf51bbd8b0bc68ce125c1fae220b31194 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Wed, 24 Apr 2024 09:05:53 +0200 Subject: [PATCH] :art: Replace custom message switch by a dictionary --- js/models/event_handler.js | 60 ++++++++++-------------------------- js/models/message_handler.js | 41 ++++++++++++++---------- 2 files changed, 41 insertions(+), 60 deletions(-) diff --git a/js/models/event_handler.js b/js/models/event_handler.js index 636f60f7..ed7e82b0 100644 --- a/js/models/event_handler.js +++ b/js/models/event_handler.js @@ -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}`); }); } diff --git a/js/models/message_handler.js b/js/models/message_handler.js index cf5c4796..7ef0c380 100644 --- a/js/models/message_handler.js +++ b/js/models/message_handler.js @@ -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" }),