// 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");
section.append(links_container);
this.links.forEach(dl => dl.render(links_container));
let child_container = $("");
section.append(child_container);
for (child in this.children) {
this.children[child].render(child_container, voc);
}
},
_add_link: function(datalink) {
if (datalink.semantics!=this.semantics) {
throw new Error(
`Cannot add ${datalink.semantics} link to ${this.semantics}`);
} else {
this.links.push(datalink);
}
},
_add_child: function(link_node) {
if (link_node.semantics in this.children) {
throw Error(`LinkNode for ${link_node.semantics} already present.`);
} else {
this.children[link_node.semantics] = link_node;
}
},
};
// the root node is rendered differently from other link nodes, and
// its semantics is null (which is forbidden otherwise), so there's
// and extra class for it.
function RootNode() {
this.init(null);
}
RootNode.prototype = Object.create(LinkNode.prototype);
RootNode.prototype.render = function(container, voc) {
// We prescribe a sequence for the most common toplevel terms and
// leave the rest to chance for now
for (term of [
"this",
"preview",
"proc",
"documentation",
"auxiliary",
"calibration",
"coderived",
"counterpart",
"derviation",
"progenitor",]) {
if (term in this.children) {
this.children[term].render(container, voc);
delete this.children[term];
}
}
for (child in this.children) {
this.children[child].render(container, voc);
}
}
// add an out-of-vocabulary datalink (this should only ever be called on
// the root node).
RootNode.prototype.add_oov = function(dl) {
if (! (dl.semantics in this.children)) {
this.children[dl.semantics] = new LinkNode(dl.semantics);
}
this.children[dl.semantics].add(dl);
}
// A container for all links belonging to a Dataset. These
// these are constructed with the dataset identifier, the organising vocabulary
// (i.e., datalink core for us, most likely), and they contain a link to the
// root LinkNode.
function LinksFor(dsid, vocab) {
this.dsid = dsid;
this.vocab = vocab;
this.root = new RootNode();
}
LinksFor.prototype = {
// add another datalink for my dataset
add: function(dl) {
let sem = this.vocab[dl.semantics];
if (!sem) {
// special handling for out-of-vocabulary terms
this.root.add_oov(dl);
return;
}
// now find a trace to a top-level term...
let cur_term = dl.semantics;
let trace = [];
while (cur_term) {
trace.push(cur_term);
cur_term = this.vocab[cur_term].wider?.pop();
}
trace.reverse();
// and have LinksFor sort it in
this.root.insert_by_trace(trace, dl);
},
// renders the links into the jquery container
render: function(container) {
let inner = $(renderTemplate("links-for-ds",
{"ivoid": this.dsid}));
$(container).append(inner);
this.root.render(inner, this.vocab);
}
}
// returns all datalinks in our document as a mapping
// dsid -> list of datalinks
function get_datalinks() {
let result = {};
for (tr of document.querySelectorAll(".links tbody tr")) {
let dl = new Datalink(tr);
if (dl.dsid in result) {
result[dl.dsid].push(dl);
} else {
result[dl.dsid] = [dl];
}
}
return result;
}
// asynchronously load the Datalink vocabulary.
// This returns the xhr object; add handlers for the load event as required.
function load_vocabulary() {
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://ivoa.net/rdf/datalink/core");
xhr.setRequestHeader("accept", "application/x-desise+json");
xhr.send();
return xhr;
}
// morph the links table to our semantics-based tree.
// This is being used as a success handler for vocabulary loading and
// should not called otherwise.
function _morph_table(load_event) {
let dlcore = JSON.parse(load_event.currentTarget.responseText)["terms"];
let byds = get_datalinks();
// make a container for the new, hierarchical links;
// this will replace the existing flat table at the end of this function.
var outer_container = document.createElement("div");
outer_container.id = 'links-container';
var links_table = document.querySelectorAll("table.links")[0];
// make trees out of the datalinks and render them
for (let dsid in byds) {
let cur_links = new LinksFor(dsid, dlcore);
for (let dl of byds[dsid]) {
cur_links.add(dl);
}
cur_links.render(outer_container);
}
links_table.parentNode.replaceChild(
outer_container, links_table);
$(".foldable").each((ind, obj) => make_foldable(obj));
}
// Initiate the morphing of the links table.
// This just fires off the retrieval of the vocabulary, the actual work
// being done in that load's event handler.
function start_table_morphing() {
load_vocabulary().addEventListener("load", _morph_table);
// we ignore errors here; this just means the table will stand
// un-morphed.
}
// helper function for make_foldable
function _toggle_fold(target) {
let jqt = $(target);
if (jqt.hasClass("folded")) {
jqt.children("ul,div").show();
jqt.removeClass("folded");
jqt.children("header").find(".toggler").text('▼');
} else {
jqt.children("ul,div").hide();
jqt.addClass("folded");
jqt.children("header").find(".toggler").text('▶');
}
}
// furnish a section with a handle to fold it in and out,
// and fold it in.
function make_foldable(section) {
let toggler = $("