OXYGENHUB
# b349e15a3eab oxygenhub / assets / report-frame.js
2.2 KiB · JavaScript · last changed Reports: the timeline, play, compare, and the frame interface 24 days ago source blame history raw
1
/* OxygenHub report frame (oxygenhub#127): the hub adds this to a report's
2
   HTML pages. It tells the page around the frame how tall the report is,
3
   so the frame grows to fit instead of scrolling, and takes the hub's
4
   theme (data-theme="dark" or "light" on <html>) for report.css. It sends
5
   nothing but sizes, and reads nothing but the theme. */
6
(function () {
7
  if (window.parent === window) return;
8
  var root = document.documentElement, last = 0;
9
  var send = function () {
10
    var h = Math.ceil(Math.max(root.scrollHeight, document.body ? document.body.scrollHeight : 0));
11
    if (h && h !== last) {
12
      last = h;
13
      window.parent.postMessage({ oxygenhubReport: { height: h } }, "*");
14
    }
15
  };
16
  window.addEventListener("message", function (e) {
17
    if (e.source !== window.parent) return;
18
    var t = e.data && e.data.oxygenhubTheme;
19
    if (t === "dark" || t === "light") root.dataset.theme = t;
20
  });
21
  window.addEventListener("load", send);
22
  if (window.ResizeObserver) new ResizeObserver(send).observe(root);
23
  else setInterval(send, 500);
24
  // The frame interface (RP4): report.run() is the run on screen, and
25
  // report.history("frames.csv") that file of data/ across the report's
26
  // runs on its bookmark, oldest first, each { run, created_ms, current,
27
  // text }. Both are promises; outside the hub there is no report object.
28
  var asked = 0, waiting = {};
29
  window.addEventListener("message", function (e) {
30
    var a = e.source === window.parent && e.data && e.data.oxygenhubAnswer;
31
    if (!a || !waiting[a.n]) return;
32
    var w = waiting[a.n]; delete waiting[a.n];
33
    if (a.error) w.reject(new Error(a.error)); else w.resolve(a.value);
34
  });
35
  var ask = function (what, dataset) {
36
    return new Promise(function (resolve, reject) {
37
      var n = ++asked;
38
      waiting[n] = { resolve: resolve, reject: reject };
39
      window.parent.postMessage({ oxygenhubAsk: { n: n, what: what, dataset: dataset } }, "*");
40
      setTimeout(function () { if (waiting[n]) { delete waiting[n]; reject(new Error("no answer from the hub")); } }, 30000);
41
    });
42
  };
43
  window.report = { run: function () { return ask("run"); }, history: function (dataset) { return ask("history", dataset); } };
44
  window.parent.postMessage({ oxygenhubReport: { ready: true } }, "*");
45
})();