// Javascript for custom widgets for standard SODA parameters, and // other JS support for the improvised datalink interface. // See https://github.com/msdemlei/datalink-xslt.git // // The needs jquery loaded before it. // // Distributed by the GAVO project under Creative Commons CC0, // see http://creativecommons.org/publicdomain/zero/1.0/ // This may need adaptation on non-DaCHS deploments const ALADIN_CSS = "/3rdparty/aladin.min.css"; const ALADIN_JS = "/3rdparty/aladin.min.js"; const FOOTPRINT_JS = "/static/js/footprintedit.js"; ///////////// Micro templating. /// See http://docs.g-vo.org/DaCHS/develNotes.html#built-in-templating function htmlEscape(str) { return String(str).replace(/&/g, '&').replace(/"/g, '"') .replace(/'/g, ''').replace(//g, '>'); } let renderTemplate = function () { var _tmplCache = {}; let renderTemplate = function (templateId, data) { var err = ""; var func = _tmplCache[templateId]; if (!func) { let str = document.getElementById(templateId).innerHTML; let strFunc = "let p=[],print=function(){p.push.apply(p,arguments);};" + "with(obj){p.push('" + str.replace(/[\r\t\n]/g, " ") .split("'").join("\\'") .split("\t").join("'") .replace(/\$([a-zA-Z_]+)/g, "',htmlEscape($1),'") .replace(/\$!([a-zA-Z_]+)/g, "',$1,'") + "');}return $.trim(p.join(''));"; func = new Function("obj", strFunc); _tmplCache[str] = func; } return func(data); } return renderTemplate; }() /////////////////// misc. utils // set the contents of clsid within container to val function update_class_elements(container, clsid, val) { container.find("."+clsid).map( function(i, el) { $(el).text(val); }); } // update a SODA (interval) widget for par name form a -low/-high/-unit // split widget. // soda_name is the name of the SODA parameter to be built. conversions // is a mapping going from -unit strings to converter functions to // the SODA units. function update_SODA_widget(input, soda_name, conversions) { var form = input.form; var low_element = form[soda_name+"-low"]; var high_element = form[soda_name+"-high"]; var unit_element = form[soda_name+"-unit"]; var converter = conversions[unit_element.value]; var low_val = low_element.value; if (low_val) { low_val = converter(parseFloat(low_val)); } else { low_val = '-Inf'; } var high_val = high_element.value; if (high_val) { high_val = converter(parseFloat(high_val)); } else { high_val = '+Inf'; } form[soda_name].value = low_val+" "+high_val; } /////////////////// Unit conversion let LIGHT_C = 2.99792458e8; let PLANCK_H_EV = 4.135667662e-15; // conversions from meters to let TO_SPECTRAL_CONVERSIONS = { 'm': function(val) { return val; }, 'µm': function(val) { return val*1e6; }, 'Ångström': function(val) { return val*1e10; }, 'MHz': function(val) { return LIGHT_C/val*1e-6; }, 'keV': function(val) { return LIGHT_C*PLANCK_H_EV/val*1e-3; }}; // conversions to meters from let FROM_SPECTRAL_CONVERSIONS = { 'm': function(val) { return val; }, 'µm': function(val) { return val/1e6; }, 'Ångström': function(val) { return val/1e10; }, 'MHz': function(val) { return LIGHT_C/val/1e-6; }, 'keV': function(val) { return LIGHT_C*PLANCK_H_EV/val/1e-3; }}; // set properly marked up limits. // this assumes that el is the unit select and the whole widget is // within a div. function convert_spectral_units(el, low, high) { var converter = TO_SPECTRAL_CONVERSIONS[el.value]; var input_group = $(el).parents("div").first(); update_class_elements(input_group, "low-limit", converter(low)); update_class_elements(input_group, "high-limit", converter(high)); } /////////////////// Individual widgets function add_BAND_widget() { var old = $(".BAND-m-em_wl"); old.map(function(index, el) { el = $(el); var form = el.parents("form"); var low_limit = parseFloat(el.find(".low-limit").text()); var high_limit = parseFloat(el.find(".high-limit").text()); // TODO: validate limits? var new_widget = renderTemplate( "fancy-band-widget", { low_limit: low_limit, high_limit: high_limit}); el.parent().prepend(new_widget); }); old.hide(); } function _draw_POLYGON_widget(poly_widget) { // A callback to draw the aladin light window once the required // javascript code is loaded are there var new_widget = $('#aladin-lite-div').detach(); // I'd like to use poly_widget.parent() here, but that silently fails. $('div.inputpars').prepend(new_widget); new_widget.show(); new_widget.before("

Adjust the cutout region by clicking and dragging" +" the handles."); var init_vals = $.map( poly_widget.find(".high-limit").text().split(" "), parseFloat); var init_poly = []; for (var i=0; i p[0]+" "+p[1]).join(" "); }); } function add_POLYGON_widget() { // An aladin-light-based widget letting people draw polygons to // cut out. Yes, this is expensive, but it's hard to make something // like this with less tooling and JS madness. var poly_widget = $(".POLYGON-deg-phys_argArea_obs"); if (poly_widget.length) { $("head").append( `"); $.getScript(ALADIN_JS).done( function() { $.getScript(FOOTPRINT_JS).done( function() {_draw_POLYGON_widget(poly_widget);}) }) } } // call the various handler functions for known three-factor widgets. // (this is called from the document's ready handler and thus is the // main entry point into the magic here) function add_custom_widgets() { add_BAND_widget(); add_POLYGON_widget(); // in order to hide the extra inputs from the browser when sending // off the form, we need to override the submit action $("form.service-interface").bind("submit", function(event) { event.preventDefault(); window.open( build_result_URL(event.target)); }); } //////////////////////////// Semantics/link hierarchy builder // a datalink row, which is constructed with an HTML TR as produced // by the XSLT. // These have a link (the URL), description, semantics, and dsid // (the ivoid). Semantics strips a leading hash). function Datalink(datalink_row) { var tds = datalink_row.querySelectorAll("td"); if (tds.length<3) { // this is a malformed line (e.g., the header line). We'll // ignore it return null; } this.link = tds[0].querySelector("a.datalink")?.href; this.errmsg = tds[0].querySelector(".errmsg")?.textContent; this.size = tds[0].querySelector(".size")?.textContent || ""; this.description = tds[1].textContent; this.dsid = tds[2].querySelector(".ivoid").textContent; this.procref = tds[0].querySelector(".procref")?.attributes .href.value || null; if (this.procref?.startsWith("#")) { this.procref = this.procref.slice(1); } this.semantics = tds[2].querySelector(".semantics").textContent; if (this.semantics?.startsWith("#")) { this.semantics = this.semantics.slice(1); } } Datalink.prototype = { // renders the datalink container (a jquery object). render: function(container) { // extra handling for services and errors if (this.procref) { let dest_el = document.getElementById(this.procref); if (dest_el) { dest_el.parentElement.removeChild(dest_el); container.append($(dest_el)); } else { // the referenced service wasn't rendered into a form. Let's assume // it's something we can't do anyway (such as async datalink) and // skip it for now. TODO: think of something less implicit. } } else if (this.errmsg) { container.append($(renderTemplate("js-datalinkerror", this))); } else { // default action for "normal" datalinks container.append($(renderTemplate("js-datalink", this))); } }, } // a container for a node in the link tree. // These have // * semantics ; the root node has semantics null. // * links (An array of Datalinks for the semantics proper in a list) // * children (A dict of LinkNodes of narrower resources) function LinkNode(semantics) { this.init(semantics); } LinkNode.prototype = { init: function(semantics) { this.semantics = semantics; this.links = []; this.children = {}; }, // Inserts a datalink into the tree using a "trace", the sequence // of node labels from root to the datalink insert_by_trace: function(trace, datalink) { if (trace.length==0) { this.add(datalink); } else { let child_sem = trace.shift(); if (! (child_sem in this.children)) { this.add(new LinkNode(child_sem)); } this.children[child_sem].insert_by_trace(trace, datalink); } }, // add a Datalink or a LinkNode to self. add: function(thing) { if (thing instanceof LinkNode) { this._add_child(thing); } else { // be lenient in what we accept as a datalink this._add_link(thing); } }, // adds a representation of self to the jquery container; // voc is the desise terms from the governing vocabulary. render: function(container, voc) { let voc_meta = voc[this.semantics]; if (!voc_meta) { voc_meta = { "label": this.semantics, "description": "(not in IVOA datalink core)"} } let section = $(renderTemplate("term-section", voc_meta)); container.append(section); let links_container = $("