| 1 |
// Javascript for custom widgets for standard SODA parameters, and |
| 2 |
// other JS support for the improvised datalink interface. |
| 3 |
// See https://github.com/msdemlei/datalink-xslt.git |
| 4 |
// |
| 5 |
// The needs jquery loaded before it. |
| 6 |
// |
| 7 |
// Distributed by the GAVO project under Creative Commons CC0, |
| 8 |
// see http://creativecommons.org/publicdomain/zero/1.0/ |
| 9 |
|
| 10 |
|
| 11 |
// This may need adaptation on non-DaCHS deploments |
| 12 |
const ALADIN_CSS = "/3rdparty/aladin.min.css"; |
| 13 |
const ALADIN_JS = "/3rdparty/aladin.min.js"; |
| 14 |
const FOOTPRINT_JS = "/static/js/footprintedit.js"; |
| 15 |
|
| 16 |
///////////// Micro templating. |
| 17 |
/// See http://docs.g-vo.org/DaCHS/develNotes.html#built-in-templating |
| 18 |
function htmlEscape(str) { |
| 19 |
return String(str).replace(/&/g, '&').replace(/"/g, '"') |
| 20 |
.replace(/'/g, ''').replace(/</g, '<') |
| 21 |
.replace(/>/g, '>'); |
| 22 |
} |
| 23 |
|
| 24 |
let renderTemplate = function () { |
| 25 |
var _tmplCache = {}; |
| 26 |
let renderTemplate = function (templateId, data) { |
| 27 |
var err = ""; |
| 28 |
var func = _tmplCache[templateId]; |
| 29 |
if (!func) { |
| 30 |
let str = document.getElementById(templateId).innerHTML; |
| 31 |
let strFunc = |
| 32 |
"let p=[],print=function(){p.push.apply(p,arguments);};" |
| 33 |
+ "with(obj){p.push('" |
| 34 |
+ str.replace(/[\r\t\n]/g, " ") |
| 35 |
.split("'").join("\\'") |
| 36 |
.split("\t").join("'") |
| 37 |
.replace(/\$([a-zA-Z_]+)/g, "',htmlEscape($1),'") |
| 38 |
.replace(/\$!([a-zA-Z_]+)/g, "',$1,'") |
| 39 |
+ "');}return $.trim(p.join(''));"; |
| 40 |
func = new Function("obj", strFunc); |
| 41 |
_tmplCache[str] = func; |
| 42 |
} |
| 43 |
return func(data); |
| 44 |
} |
| 45 |
return renderTemplate; |
| 46 |
}() |
| 47 |
|
| 48 |
|
| 49 |
/////////////////// misc. utils |
| 50 |
|
| 51 |
// set the contents of clsid within container to val |
| 52 |
function update_class_elements(container, clsid, val) { |
| 53 |
container.find("."+clsid).map( |
| 54 |
function(i, el) { |
| 55 |
$(el).text(val); |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
// update a SODA (interval) widget for par name form a -low/-high/-unit |
| 60 |
// split widget. |
| 61 |
// soda_name is the name of the SODA parameter to be built. conversions |
| 62 |
// is a mapping going from -unit strings to converter functions to |
| 63 |
// the SODA units. |
| 64 |
function update_SODA_widget(input, soda_name, conversions) { |
| 65 |
var form = input.form; |
| 66 |
var low_element = form[soda_name+"-low"]; |
| 67 |
var high_element = form[soda_name+"-high"]; |
| 68 |
var unit_element = form[soda_name+"-unit"]; |
| 69 |
var converter = conversions[unit_element.value]; |
| 70 |
|
| 71 |
var low_val = low_element.value; |
| 72 |
if (low_val) { |
| 73 |
low_val = converter(parseFloat(low_val)); |
| 74 |
} else { |
| 75 |
low_val = '-Inf'; |
| 76 |
} |
| 77 |
|
| 78 |
var high_val = high_element.value; |
| 79 |
if (high_val) { |
| 80 |
high_val = converter(parseFloat(high_val)); |
| 81 |
} else { |
| 82 |
high_val = '+Inf'; |
| 83 |
} |
| 84 |
|
| 85 |
form[soda_name].value = low_val+" "+high_val; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/////////////////// Unit conversion |
| 90 |
|
| 91 |
let LIGHT_C = 2.99792458e8; |
| 92 |
let PLANCK_H_EV = 4.135667662e-15; |
| 93 |
|
| 94 |
// conversions from meters to |
| 95 |
let TO_SPECTRAL_CONVERSIONS = { |
| 96 |
'm': function(val) { return val; }, |
| 97 |
'µm': function(val) { return val*1e6; }, |
| 98 |
'Ångström': function(val) { return val*1e10; }, |
| 99 |
'MHz': function(val) { return LIGHT_C/val*1e-6; }, |
| 100 |
'keV': function(val) { return LIGHT_C*PLANCK_H_EV/val*1e-3; }}; |
| 101 |
|
| 102 |
// conversions to meters from |
| 103 |
let FROM_SPECTRAL_CONVERSIONS = { |
| 104 |
'm': function(val) { return val; }, |
| 105 |
'µm': function(val) { return val/1e6; }, |
| 106 |
'Ångström': function(val) { return val/1e10; }, |
| 107 |
'MHz': function(val) { return LIGHT_C/val/1e-6; }, |
| 108 |
'keV': function(val) { return LIGHT_C*PLANCK_H_EV/val/1e-3; }}; |
| 109 |
|
| 110 |
|
| 111 |
// set properly marked up limits. |
| 112 |
// this assumes that el is the unit select and the whole widget is |
| 113 |
// within a div. |
| 114 |
function convert_spectral_units(el, low, high) { |
| 115 |
var converter = TO_SPECTRAL_CONVERSIONS[el.value]; |
| 116 |
var input_group = $(el).parents("div").first(); |
| 117 |
update_class_elements(input_group, "low-limit", converter(low)); |
| 118 |
update_class_elements(input_group, "high-limit", converter(high)); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/////////////////// Individual widgets |
| 123 |
|
| 124 |
function add_BAND_widget() { |
| 125 |
var old = $(".BAND-m-em_wl"); |
| 126 |
old.map(function(index, el) { |
| 127 |
el = $(el); |
| 128 |
var form = el.parents("form"); |
| 129 |
var low_limit = parseFloat(el.find(".low-limit").text()); |
| 130 |
var high_limit = parseFloat(el.find(".high-limit").text()); |
| 131 |
// TODO: validate limits? |
| 132 |
|
| 133 |
var new_widget = renderTemplate( |
| 134 |
"fancy-band-widget", { |
| 135 |
low_limit: low_limit, |
| 136 |
high_limit: high_limit}); |
| 137 |
el.parent().prepend(new_widget); |
| 138 |
}); |
| 139 |
old.hide(); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
function _draw_POLYGON_widget(poly_widget) { |
| 144 |
// A callback to draw the aladin light window once the required |
| 145 |
// javascript code is loaded are there |
| 146 |
|
| 147 |
var new_widget = $('#aladin-lite-div').detach(); |
| 148 |
// I'd like to use poly_widget.parent() here, but that silently fails. |
| 149 |
$('div.inputpars').prepend(new_widget); |
| 150 |
new_widget.show(); |
| 151 |
new_widget.before("<p>Adjust the cutout region by clicking and dragging" |
| 152 |
+" the handles."); |
| 153 |
|
| 154 |
var init_vals = $.map( |
| 155 |
poly_widget.find(".high-limit").text().split(" "), |
| 156 |
parseFloat); |
| 157 |
var init_poly = []; |
| 158 |
for (var i=0; i<init_vals.length; i+=2) { |
| 159 |
init_poly.push([init_vals[i], init_vals[i+1]]); |
| 160 |
} |
| 161 |
var input = poly_widget.find("input") |
| 162 |
|
| 163 |
embed_region_editor( |
| 164 |
document.getElementById("aladin-lite-div"), |
| 165 |
init_poly, |
| 166 |
function(poly) { |
| 167 |
input[0].value = poly.map((p) => p[0]+" "+p[1]).join(" "); |
| 168 |
}); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
function add_POLYGON_widget() { |
| 173 |
// An aladin-light-based widget letting people draw polygons to |
| 174 |
// cut out. Yes, this is expensive, but it's hard to make something |
| 175 |
// like this with less tooling and JS madness. |
| 176 |
|
| 177 |
var poly_widget = $(".POLYGON-deg-phys_argArea_obs"); |
| 178 |
|
| 179 |
if (poly_widget.length) { |
| 180 |
$("head").append( |
| 181 |
`<link rel='stylesheet' href='${ALADIN_CSS}'` |
| 182 |
+" type='text/css' />"); |
| 183 |
$.getScript(ALADIN_JS).done( |
| 184 |
function() { |
| 185 |
$.getScript(FOOTPRINT_JS).done( |
| 186 |
function() {_draw_POLYGON_widget(poly_widget);}) |
| 187 |
}) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// call the various handler functions for known three-factor widgets. |
| 192 |
// (this is called from the document's ready handler and thus is the |
| 193 |
// main entry point into the magic here) |
| 194 |
function add_custom_widgets() { |
| 195 |
add_BAND_widget(); |
| 196 |
add_POLYGON_widget(); |
| 197 |
// in order to hide the extra inputs from the browser when sending |
| 198 |
// off the form, we need to override the submit action |
| 199 |
$("form.service-interface").bind("submit", |
| 200 |
function(event) { |
| 201 |
event.preventDefault(); |
| 202 |
window.open( |
| 203 |
build_result_URL(event.target)); |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
//////////////////////////// Semantics/link hierarchy builder |
| 208 |
|
| 209 |
// a datalink row, which is constructed with an HTML TR as produced |
| 210 |
// by the XSLT. |
| 211 |
// These have a link (the URL), description, semantics, and dsid |
| 212 |
// (the ivoid). Semantics strips a leading hash). |
| 213 |
function Datalink(datalink_row) { |
| 214 |
var tds = datalink_row.querySelectorAll("td"); |
| 215 |
|
| 216 |
if (tds.length<3) { |
| 217 |
// this is a malformed line (e.g., the header line). We'll |
| 218 |
// ignore it |
| 219 |
return null; |
| 220 |
} |
| 221 |
|
| 222 |
this.link = tds[0].querySelector("a.datalink")?.href; |
| 223 |
this.errmsg = tds[0].querySelector(".errmsg")?.textContent; |
| 224 |
this.size = tds[0].querySelector(".size")?.textContent || ""; |
| 225 |
this.description = tds[1].textContent; |
| 226 |
this.dsid = tds[2].querySelector(".ivoid").textContent; |
| 227 |
|
| 228 |
this.procref = tds[0].querySelector(".procref")?.attributes |
| 229 |
.href.value || null; |
| 230 |
if (this.procref?.startsWith("#")) { |
| 231 |
this.procref = this.procref.slice(1); |
| 232 |
} |
| 233 |
|
| 234 |
this.semantics = tds[2].querySelector(".semantics").textContent; |
| 235 |
if (this.semantics?.startsWith("#")) { |
| 236 |
this.semantics = this.semantics.slice(1); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
Datalink.prototype = { |
| 241 |
// renders the datalink container (a jquery object). |
| 242 |
render: function(container) { |
| 243 |
// extra handling for services and errors |
| 244 |
if (this.procref) { |
| 245 |
let dest_el = document.getElementById(this.procref); |
| 246 |
if (dest_el) { |
| 247 |
dest_el.parentElement.removeChild(dest_el); |
| 248 |
container.append($(dest_el)); |
| 249 |
} else { |
| 250 |
// the referenced service wasn't rendered into a form. Let's assume |
| 251 |
// it's something we can't do anyway (such as async datalink) and |
| 252 |
// skip it for now. TODO: think of something less implicit. |
| 253 |
} |
| 254 |
|
| 255 |
} else if (this.errmsg) { |
| 256 |
container.append($(renderTemplate("js-datalinkerror", this))); |
| 257 |
|
| 258 |
} else { |
| 259 |
// default action for "normal" datalinks |
| 260 |
container.append($(renderTemplate("js-datalink", this))); |
| 261 |
} |
| 262 |
}, |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
// a container for a node in the link tree. |
| 267 |
// These have |
| 268 |
// * semantics ; the root node has semantics null. |
| 269 |
// * links (An array of Datalinks for the semantics proper in a list) |
| 270 |
// * children (A dict of LinkNodes of narrower resources) |
| 271 |
function LinkNode(semantics) { |
| 272 |
this.init(semantics); |
| 273 |
} |
| 274 |
|
| 275 |
LinkNode.prototype = { |
| 276 |
init: function(semantics) { |
| 277 |
this.semantics = semantics; |
| 278 |
this.links = []; |
| 279 |
this.children = {}; |
| 280 |
}, |
| 281 |
|
| 282 |
// Inserts a datalink into the tree using a "trace", the sequence |
| 283 |
// of node labels from root to the datalink |
| 284 |
insert_by_trace: function(trace, datalink) { |
| 285 |
if (trace.length==0) { |
| 286 |
this.add(datalink); |
| 287 |
} else { |
| 288 |
let child_sem = trace.shift(); |
| 289 |
if (! (child_sem in this.children)) { |
| 290 |
this.add(new LinkNode(child_sem)); |
| 291 |
} |
| 292 |
this.children[child_sem].insert_by_trace(trace, datalink); |
| 293 |
} |
| 294 |
}, |
| 295 |
|
| 296 |
// add a Datalink or a LinkNode to self. |
| 297 |
add: function(thing) { |
| 298 |
if (thing instanceof LinkNode) { |
| 299 |
this._add_child(thing); |
| 300 |
} else { |
| 301 |
// be lenient in what we accept as a datalink |
| 302 |
this._add_link(thing); |
| 303 |
} |
| 304 |
}, |
| 305 |
|
| 306 |
// adds a representation of self to the jquery container; |
| 307 |
// voc is the desise terms from the governing vocabulary. |
| 308 |
render: function(container, voc) { |
| 309 |
let voc_meta = voc[this.semantics]; |
| 310 |
if (!voc_meta) { |
| 311 |
voc_meta = { |
| 312 |
"label": this.semantics, |
| 313 |
"description": "(not in IVOA datalink core)"} |
| 314 |
} |
| 315 |
|
| 316 |
let section = $(renderTemplate("term-section", voc_meta)); |
| 317 |
container.append(section); |
| 318 |
|
| 319 |
let links_container = $("<ul class='links-from-js'/>"); |
| 320 |
section.append(links_container); |
| 321 |
this.links.forEach(dl => dl.render(links_container)); |
| 322 |
|
| 323 |
let child_container = $("<div class='child-terms'/>"); |
| 324 |
section.append(child_container); |
| 325 |
for (child in this.children) { |
| 326 |
this.children[child].render(child_container, voc); |
| 327 |
} |
| 328 |
}, |
| 329 |
|
| 330 |
_add_link: function(datalink) { |
| 331 |
if (datalink.semantics!=this.semantics) { |
| 332 |
throw new Error( |
| 333 |
`Cannot add ${datalink.semantics} link to ${this.semantics}`); |
| 334 |
} else { |
| 335 |
this.links.push(datalink); |
| 336 |
} |
| 337 |
}, |
| 338 |
|
| 339 |
_add_child: function(link_node) { |
| 340 |
if (link_node.semantics in this.children) { |
| 341 |
throw Error(`LinkNode for ${link_node.semantics} already present.`); |
| 342 |
} else { |
| 343 |
this.children[link_node.semantics] = link_node; |
| 344 |
} |
| 345 |
}, |
| 346 |
|
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
// the root node is rendered differently from other link nodes, and |
| 351 |
// its semantics is null (which is forbidden otherwise), so there's |
| 352 |
// and extra class for it. |
| 353 |
function RootNode() { |
| 354 |
this.init(null); |
| 355 |
} |
| 356 |
|
| 357 |
RootNode.prototype = Object.create(LinkNode.prototype); |
| 358 |
|
| 359 |
RootNode.prototype.render = function(container, voc) { |
| 360 |
// We prescribe a sequence for the most common toplevel terms and |
| 361 |
// leave the rest to chance for now |
| 362 |
for (term of [ |
| 363 |
"this", |
| 364 |
"preview", |
| 365 |
"proc", |
| 366 |
"documentation", |
| 367 |
"auxiliary", |
| 368 |
"calibration", |
| 369 |
"coderived", |
| 370 |
"counterpart", |
| 371 |
"derviation", |
| 372 |
"progenitor",]) { |
| 373 |
if (term in this.children) { |
| 374 |
this.children[term].render(container, voc); |
| 375 |
delete this.children[term]; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
for (child in this.children) { |
| 380 |
this.children[child].render(container, voc); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
// add an out-of-vocabulary datalink (this should only ever be called on |
| 386 |
// the root node). |
| 387 |
RootNode.prototype.add_oov = function(dl) { |
| 388 |
if (! (dl.semantics in this.children)) { |
| 389 |
this.children[dl.semantics] = new LinkNode(dl.semantics); |
| 390 |
} |
| 391 |
this.children[dl.semantics].add(dl); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
// A container for all links belonging to a Dataset. These |
| 397 |
// these are constructed with the dataset identifier, the organising vocabulary |
| 398 |
// (i.e., datalink core for us, most likely), and they contain a link to the |
| 399 |
// root LinkNode. |
| 400 |
function LinksFor(dsid, vocab) { |
| 401 |
this.dsid = dsid; |
| 402 |
this.vocab = vocab; |
| 403 |
this.root = new RootNode(); |
| 404 |
} |
| 405 |
|
| 406 |
LinksFor.prototype = { |
| 407 |
// add another datalink for my dataset |
| 408 |
add: function(dl) { |
| 409 |
let sem = this.vocab[dl.semantics]; |
| 410 |
if (!sem) { |
| 411 |
// special handling for out-of-vocabulary terms |
| 412 |
this.root.add_oov(dl); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
// now find a trace to a top-level term... |
| 417 |
let cur_term = dl.semantics; |
| 418 |
let trace = []; |
| 419 |
while (cur_term) { |
| 420 |
trace.push(cur_term); |
| 421 |
cur_term = this.vocab[cur_term].wider?.pop(); |
| 422 |
} |
| 423 |
|
| 424 |
trace.reverse(); |
| 425 |
|
| 426 |
// and have LinksFor sort it in |
| 427 |
this.root.insert_by_trace(trace, dl); |
| 428 |
}, |
| 429 |
|
| 430 |
// renders the links into the jquery container |
| 431 |
render: function(container) { |
| 432 |
let inner = $(renderTemplate("links-for-ds", |
| 433 |
{"ivoid": this.dsid})); |
| 434 |
$(container).append(inner); |
| 435 |
this.root.render(inner, this.vocab); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
// returns all datalinks in our document as a mapping |
| 441 |
// dsid -> list of datalinks |
| 442 |
function get_datalinks() { |
| 443 |
let result = {}; |
| 444 |
for (tr of document.querySelectorAll(".links tbody tr")) { |
| 445 |
let dl = new Datalink(tr); |
| 446 |
if (dl.dsid in result) { |
| 447 |
result[dl.dsid].push(dl); |
| 448 |
} else { |
| 449 |
result[dl.dsid] = [dl]; |
| 450 |
} |
| 451 |
} |
| 452 |
return result; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
// asynchronously load the Datalink vocabulary. |
| 457 |
// This returns the xhr object; add handlers for the load event as required. |
| 458 |
function load_vocabulary() { |
| 459 |
var xhr = new XMLHttpRequest(); |
| 460 |
xhr.open('GET', "https://ivoa.net/rdf/datalink/core"); |
| 461 |
xhr.setRequestHeader("accept", "application/x-desise+json"); |
| 462 |
xhr.send(); |
| 463 |
return xhr; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
// morph the links table to our semantics-based tree. |
| 468 |
// This is being used as a success handler for vocabulary loading and |
| 469 |
// should not called otherwise. |
| 470 |
function _morph_table(load_event) { |
| 471 |
let dlcore = JSON.parse(load_event.currentTarget.responseText)["terms"]; |
| 472 |
let byds = get_datalinks(); |
| 473 |
|
| 474 |
// make a container for the new, hierarchical links; |
| 475 |
// this will replace the existing flat table at the end of this function. |
| 476 |
var outer_container = document.createElement("div"); |
| 477 |
outer_container.id = 'links-container'; |
| 478 |
var links_table = document.querySelectorAll("table.links")[0]; |
| 479 |
|
| 480 |
// make trees out of the datalinks and render them |
| 481 |
for (let dsid in byds) { |
| 482 |
let cur_links = new LinksFor(dsid, dlcore); |
| 483 |
for (let dl of byds[dsid]) { |
| 484 |
cur_links.add(dl); |
| 485 |
} |
| 486 |
cur_links.render(outer_container); |
| 487 |
} |
| 488 |
|
| 489 |
links_table.parentNode.replaceChild( |
| 490 |
outer_container, links_table); |
| 491 |
|
| 492 |
$(".foldable").each((ind, obj) => make_foldable(obj)); |
| 493 |
} |
| 494 |
|
| 495 |
// Initiate the morphing of the links table. |
| 496 |
// This just fires off the retrieval of the vocabulary, the actual work |
| 497 |
// being done in that load's event handler. |
| 498 |
function start_table_morphing() { |
| 499 |
load_vocabulary().addEventListener("load", _morph_table); |
| 500 |
// we ignore errors here; this just means the table will stand |
| 501 |
// un-morphed. |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
// helper function for make_foldable |
| 506 |
function _toggle_fold(target) { |
| 507 |
let jqt = $(target); |
| 508 |
if (jqt.hasClass("folded")) { |
| 509 |
jqt.children("ul,div").show(); |
| 510 |
jqt.removeClass("folded"); |
| 511 |
jqt.children("header").find(".toggler").text('▼'); |
| 512 |
} else { |
| 513 |
jqt.children("ul,div").hide(); |
| 514 |
jqt.addClass("folded"); |
| 515 |
jqt.children("header").find(".toggler").text('▶'); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
// furnish a section with a handle to fold it in and out, |
| 521 |
// and fold it in. |
| 522 |
function make_foldable(section) { |
| 523 |
let toggler = $("<button class='toggler'>▼</span>"); |
| 524 |
$(section).find(".voc-label").first().prepend(toggler); |
| 525 |
$(section).children("header").on("click", _ => _toggle_fold(section)); |
| 526 |
|
| 527 |
if (! ( |
| 528 |
section.classList.contains("this") |
| 529 |
|| section.classList.contains("proc"))) { |
| 530 |
_toggle_fold(section); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
//////////////////////////// SAMP interface/result URL building |
| 536 |
|
| 537 |
// The thing sent to the SAMP clients is a URL built from all input |
| 538 |
// items that have a soda class. The stylesheet must arrange it so |
| 539 |
// all input/select items generated from the declared service parameters |
| 540 |
// have a soda class. |
| 541 |
|
| 542 |
// return a list of selected items for a selection element for URL inclusion |
| 543 |
function get_selected_entries(select_element) { |
| 544 |
var result = new Array(); |
| 545 |
var i; |
| 546 |
|
| 547 |
for (i=0; i<select_element.length; i++) { |
| 548 |
if (select_element.options[i].selected) { |
| 549 |
result.push(select_element.name+"="+encodeURIComponent( |
| 550 |
select_element.options[i].value)) |
| 551 |
} |
| 552 |
} |
| 553 |
return result; |
| 554 |
} |
| 555 |
|
| 556 |
// return a URL fragment for a form item |
| 557 |
function make_query_item(form_element, index) { |
| 558 |
var val = ""; |
| 559 |
|
| 560 |
if (! $(form_element).hasClass("soda")) { |
| 561 |
return; |
| 562 |
} |
| 563 |
switch (form_element.nodeName.toUpperCase()) { |
| 564 |
case "INPUT": |
| 565 |
case "TEXTAREA": |
| 566 |
if (form_element.type=="radio" || form_element.type=="checkbox") { |
| 567 |
if (form_element.checked) { |
| 568 |
val = form_element.name+"="+encodeURIComponent(form_element.value); |
| 569 |
} |
| 570 |
} else if (form_element.name && form_element.value) { |
| 571 |
val = form_element.name+"="+encodeURIComponent(form_element.value); |
| 572 |
} |
| 573 |
break; |
| 574 |
case "SELECT": |
| 575 |
return get_selected_entries(form_element).join("&"); |
| 576 |
break; |
| 577 |
} |
| 578 |
return val; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
// return the URL that sending off cur_form would retrieve |
| 583 |
function build_result_URL(cur_form) { |
| 584 |
var fragments = $.map(cur_form.elements, make_query_item); |
| 585 |
dest_url = cur_form.getAttribute("action")+"?"+fragments.join("&"); |
| 586 |
return dest_url; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
// send the current selection as a FITS image |
| 591 |
function send_SAMP(conn, message, cur_form) { |
| 592 |
var msg = new samp.Message(message, { |
| 593 |
"url": build_result_URL(cur_form), |
| 594 |
"name": "SODA result"}); |
| 595 |
conn.notifyAll([msg]); |
| 596 |
} |
| 597 |
|
| 598 |
function completeURL(uriOrPath) { |
| 599 |
if (uriOrPath[0]=="/") { |
| 600 |
return window.location.protocol+"//"+window.location.host+uriOrPath; |
| 601 |
} |
| 602 |
return uriOrPath; |
| 603 |
} |
| 604 |
|
| 605 |
// return the callback for a successful hub connection |
| 606 |
// (which disables-re-registration and sends out the image link) |
| 607 |
function _make_SAMP_success_handler(samp_button, message, cur_form) { |
| 608 |
return function(conn) { |
| 609 |
conn.declareMetadata([{ |
| 610 |
"samp.description": "SODA processed data from"+document.URL, |
| 611 |
"samp.icon.url": completeURL("/favicon.png") |
| 612 |
}]); |
| 613 |
|
| 614 |
// set the button up so clicks send again without reconnection. |
| 615 |
$(samp_button).unbind("click"); |
| 616 |
$(samp_button).click(function(e) { |
| 617 |
e.preventDefault(); |
| 618 |
send_SAMP(conn, cur_form); |
| 619 |
}); |
| 620 |
|
| 621 |
// make sure we unregister when the user leaves the page |
| 622 |
$(window).on("unload", function() { |
| 623 |
conn.unregister(); |
| 624 |
}); |
| 625 |
|
| 626 |
// send the stuff once (since the connection has been established |
| 627 |
// in response to a click already) |
| 628 |
send_SAMP(conn, message, cur_form); |
| 629 |
}; |
| 630 |
} |
| 631 |
|
| 632 |
// see if there is a content qualifier set |
| 633 |
// connect to a SAMP hub and, when the connection is established, |
| 634 |
// send the current cutout result. |
| 635 |
function connect_and_send_SAMP(samp_button, cur_form) { |
| 636 |
// we need to send different messages per data type. We don't really |
| 637 |
// know the datatype, though. The best we can do for now is find out |
| 638 |
// the content qualifier for #this, which is left by the XSL if it can |
| 639 |
// find one in #content_qualifier. If this fails, fall back to |
| 640 |
// image.load.fits. We should evaluate the product_type vocabulary |
| 641 |
// here once we have sufficiently smart SAMP messages. |
| 642 |
var message = "image.load.fits"; |
| 643 |
var cq = document.querySelector("#content_qualifier").textContent; |
| 644 |
if (cq == '#spectrum') { |
| 645 |
message = "spectrum.load.ssa-generic"; |
| 646 |
} |
| 647 |
|
| 648 |
samp.register("SODA processor", |
| 649 |
_make_SAMP_success_handler(samp_button, message, cur_form), |
| 650 |
function(err) { |
| 651 |
alert("Could not connect to SAMP hub: "+err); |
| 652 |
}); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
// create a samp sending button in a SODA form |
| 657 |
function enable_SAMP_on_form(index, cur_form) { |
| 658 |
try { |
| 659 |
var samp_button = $("#samp-template").clone()[0] |
| 660 |
$(samp_button).show() |
| 661 |
$(samp_button).attr({"id": ""}); |
| 662 |
$(cur_form).prepend(samp_button); |
| 663 |
$(samp_button).click(function (e) { |
| 664 |
e.preventDefault(); |
| 665 |
connect_and_send_SAMP(samp_button, cur_form); |
| 666 |
}); |
| 667 |
} catch (e) { |
| 668 |
throw(e); |
| 669 |
// we don't care if there's no SAMP. Log something? |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
// enable SAMP sending for all forms that look promising |
| 674 |
function enable_SAMP() { |
| 675 |
$("form.service-interface").each(enable_SAMP_on_form); |
| 676 |
} |
| 677 |
|
| 678 |
$(document).ready(add_custom_widgets); |
| 679 |
$(document).ready(start_table_morphing); |
| 680 |
$(document).ready(enable_SAMP); |