(function (global) { 'use strict'; var C = global.DomeCoords; var RAD = 180 / Math.PI, DEG = Math.PI / 180; var R = 500; function lookupText(lookup, count) { if (lookup === 'pending') return 'Checking the local SPHEREx archive\u2026'; if (lookup === 'error') return 'Lookup unavailable'; if (lookup === 'ok') { return count > 0 ? count + ' local SPHEREx frame' + (count === 1 ? '' : 's') + ' nearby' : 'No local SPHEREx frames near this point'; } return ''; } function DomeSelect(view, options) { this.view = view; this.o = Object.assign({ state: null, radius: 0.25, apiBase: '', timeoutMs: 8000, fetchFn: null, screenToWorld: null, getLayer: null, onChange: null, onGround: null, outlineUrl: 'sky_layers/field_polys.json' }, options || {}); this.selection = null; this.marker = null; this.outline = null; this._seq = 0; this._disposed = false; if (!view.supported) return; var self = this; this._onKey = function (e) { if (e.key === 'Escape' && self.selection) { self.clear(); e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); } }; view.container.addEventListener('keydown', this._onKey); } DomeSelect.lookupText = lookupText; DomeSelect.prototype._fetch = function (url, init) { var f = this.o.fetchFn || global.fetch.bind(global); return f(url, init); }; DomeSelect.prototype._screenToWorld = function (clientX, clientY) { if (this.o.screenToWorld) return this.o.screenToWorld(clientX, clientY); var v = this.view, r = v.container.getBoundingClientRect(); var p = new global.THREE.Vector3(((clientX - r.left) / r.width) * 2 - 1, -((clientY - r.top) / r.height) * 2 + 1, 0.5); v.camera.updateMatrixWorld(); p.unproject(v.camera); return { x: p.x - v.camera.position.x, y: p.y - v.camera.position.y, z: p.z - v.camera.position.z }; }; function clonePlain(v) { if (v === undefined || v === null || typeof v !== 'object') return v; if (Array.isArray(v)) return v.map(clonePlain); var out = {}, keys = Object.keys(v); for (var i = 0; i < keys.length; i++) { var k = keys[i]; if (v[k] !== undefined) out[k] = clonePlain(v[k]); } return out; } DomeSelect.prototype._emit = function (extra) { if (!this.o.onChange) return; var s = this.selection; this.o.onChange(Object.assign({ selection: s ? clonePlain(s) : null, text: s ? lookupText(s.lookup, s.count) : '', ground: false }, extra || {})); }; DomeSelect.prototype.bindLook = function (look) { var self = this; look.o.onClick = function (e) { self.handleClick(e); }; }; DomeSelect.prototype.handleClick = function (e) { if (!this.view.supported || this._disposed) return null; var w = this._screenToWorld(e.clientX, e.clientY); if (!w) return null; var obs = this.view.observer || null, len = Math.sqrt(w.x * w.x + w.y * w.y + w.z * w.z) || 1; if (obs && w.y <= 0) { if (this.o.onGround) this.o.onGround(); this._emit({ ground: true }); return { ground: true }; } var sky = obs ? C.worldToRaDec(w, obs) : C.dirToRaDec(w); var g = C.raDecToGalactic(sky.ra, sky.dec); var sel = { ra: sky.ra, dec: sky.dec, l: g.l, b: g.b, count: null, lookup: 'pending', radius: this.o.radius, layer: this.o.getLayer ? this.o.getLayer() : null }; if (obs) { sel.alt = Math.asin(w.y / len) * RAD; sel.az = C.norm360(Math.atan2(w.x, -w.z) * RAD); } this._set(sel); this._lookup(sel); return JSON.parse(JSON.stringify(this.selection)); }; DomeSelect.prototype.selectAt = function (ra, dec, extra) { if (!this.view.supported || this._disposed) return null; var obs = this.view.observer || null, g = C.raDecToGalactic(ra, dec); var sel = { ra: C.norm360(ra), dec: dec, l: g.l, b: g.b, count: null, lookup: 'pending', radius: this.o.radius, layer: this.o.getLayer ? this.o.getLayer() : null }; if (extra && extra.name) sel.name = String(extra.name); if (obs) { var aa = C.altAz(ra, dec, obs); if (aa.alt <= 0) return null; sel.alt = aa.alt; sel.az = aa.az; } this._set(sel); this._lookup(sel); return JSON.parse(JSON.stringify(this.selection)); }; DomeSelect.prototype._set = function (sel) { this.selection = sel; this._placeMarker(sel.ra, sel.dec); if (this.o.state) { this.o.state.select(sel); this.o.state.set({ targetName: sel.name || null }); } this._emit(); }; DomeSelect.prototype.restore = function (sel) { if (!sel || this._disposed) return; this._seq++; this.selection = JSON.parse(JSON.stringify(sel)); this._placeMarker(sel.ra, sel.dec); if (this.o.state) this.o.state.select(this.selection); this._emit(); if (sel.lookup === 'pending' || sel.lookup === 'error' || !sel.lookup) { this.selection.lookup = 'pending'; this._lookup(this.selection); } }; DomeSelect.prototype.clear = function () { this._seq++; this.selection = null; this._removeMarker(); if (this.o.state) this.o.state.clearSelection(); this._emit(); if (this.view.requestRender) this.view.requestRender(); }; DomeSelect.prototype._lookup = function (sel) { var self = this, seq = ++this._seq; var ctrl = typeof AbortController !== 'undefined' ? new AbortController() : null; var timer = setTimeout(function () { if (ctrl) ctrl.abort(); }, this.o.timeoutMs); var body = JSON.stringify({ ra: sel.ra, dec: sel.dec, radius: sel.radius, include_remote: false }); function finish(lookup, count) { clearTimeout(timer); if (seq !== self._seq || !self.selection) return; self.selection.lookup = lookup; self.selection.count = count; if (self.o.state) self.o.state.updateSelection({ lookup: lookup, count: count }); self._emit(); } this._fetch(this.o.apiBase + '/api/observations/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: body, signal: ctrl ? ctrl.signal : undefined }) .then(function (r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); }) .then(function (d) { var n = Number.isInteger(d.count) ? d.count : (Array.isArray(d.observations) ? d.observations.length : NaN); if (!(n >= 0)) throw new Error('unexpected response'); finish('ok', n); }) .catch(function () { finish('error', null); }); }; DomeSelect.prototype._placeMarker = function (ra, dec) { var THREE = global.THREE, v = this.view; if (!v.supported) return; this._removeMarker(); var d = C.raDecToDir(ra, dec), rr = R * 0.985, ang = 0.8 * DEG, rad = rr * Math.tan(ang), i; var ring = [], ticks = []; for (i = 0; i < 48; i++) { var a = (i / 48) * Math.PI * 2; ring.push(new THREE.Vector3(Math.cos(a) * rad, Math.sin(a) * rad, 0)); } [[1, 0], [0, 1], [-1, 0], [0, -1]].forEach(function (u) { ticks.push(new THREE.Vector3(u[0] * rad * 1.35, u[1] * rad * 1.35, 0), new THREE.Vector3(u[0] * rad * 2.1, u[1] * rad * 2.1, 0)); }); var mat = new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.95, fog: false, depthTest: false }); var g = new THREE.Group(); g.name = 'selection.marker'; g.add(new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(ring), mat)); g.add(new THREE.LineSegments(new THREE.BufferGeometry().setFromPoints(ticks), mat)); g.position.set(d.x * rr, d.y * rr, d.z * rr); g.quaternion.setFromUnitVectors(new THREE.Vector3(0, 0, 1), new THREE.Vector3(d.x, d.y, d.z)); g.renderOrder = 5; g.children.forEach(function (c) { c.renderOrder = 5; }); v.skyGroup.add(g); this.marker = g; v.requestRender(); }; DomeSelect.prototype._removeMarker = function () { if (!this.marker) return; var m = this.marker; if (m.parent) m.parent.remove(m); m.traverse(function (o) { if (o.geometry) o.geometry.dispose(); }); if (m.children[0] && m.children[0].material) m.children[0].material.dispose(); this.marker = null; }; DomeSelect.prototype.loadOutline = function (url) { var self = this; return this._fetch(url || this.o.outlineUrl) .then(function (r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); }) .then(function (data) { if (self._disposed || !self.view.supported) return false; self._buildOutline(data); return true; }) .catch(function () { return false; }); }; function edgePoints(THREE, polys, radius, closed) { var pts = []; polys.forEach(function (p) { for (var i = 0; i < p.length; i++) { var a = p[i], b = p[(i + 1) % p.length]; var da = C.raDecToDir(a[0], a[1]), db = C.raDecToDir(b[0], b[1]); pts.push(new THREE.Vector3(da.x * radius, da.y * radius, da.z * radius), new THREE.Vector3(db.x * radius, db.y * radius, db.z * radius)); } }); return pts; } DomeSelect.prototype._buildOutline = function (data) { var THREE = global.THREE, v = this.view; if (this.outline) this._removeOutline(); var frames = (data.frames || []).map(function (f) { return f.p; }); var col = new THREE.Color(0x66b9ef).convertSRGBToLinear(); var group = new THREE.Group(); group.name = 'field.outline'; var fm = new THREE.LineBasicMaterial({ color: col, transparent: true, opacity: 0.22, fog: false }); var hm = new THREE.LineBasicMaterial({ color: col, transparent: true, opacity: 0.9, fog: false }); var fl = new THREE.LineSegments(new THREE.BufferGeometry().setFromPoints(edgePoints(THREE, frames, R * 0.99)), fm); var hl = new THREE.LineSegments(new THREE.BufferGeometry().setFromPoints(edgePoints(THREE, data.hull && data.hull.length ? [data.hull] : [], R * 0.99)), hm); fl.renderOrder = hl.renderOrder = -7; group.add(fl); group.add(hl); v.skyGroup.add(group); this.outline = group; this.fieldInfo = { frames: frames.length, center: data.center || null }; v.requestRender(); }; DomeSelect.prototype._removeOutline = function () { var o = this.outline; if (!o) return; if (o.parent) o.parent.remove(o); o.traverse(function (x) { if (x.geometry) x.geometry.dispose(); if (x.material) x.material.dispose(); }); this.outline = null; }; DomeSelect.prototype.setOutlineVisible = function (on) { if (this.outline) { this.outline.visible = !!on; this.view.requestRender(); } }; DomeSelect.prototype.dispose = function () { if (this._disposed) return; this._disposed = true; this._seq++; if (this._onKey && this.view.container) this.view.container.removeEventListener('keydown', this._onKey); this._removeMarker(); this._removeOutline(); }; global.DomeSelect = DomeSelect; })(window);