aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/snippets.es6
blob: e273fad1edff2054fe812227aaddb7289604e541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

const codeField = document.querySelector("#code-field");
if (codeField !== null) {
  const origTextarea = codeField.querySelector("textarea[name=\"snippet[code]\"]");
  const codeMirror = CodeMirror.fromTextArea(origTextarea, {
    mode: "ruby",
    lineNumbers: true,
    extraKeys: {
      "Ctrl-Enter": cm => {
        cm.save();
        origTextarea.form.submit();
      }
    }
  });
  codeMirror.on("change", cm => cm.save());
}

const agg = (elm, n) => {
  if (!n) n = 0;
  n++;
  setTimeout(() => {
    $.ajax({
      type: "GET",
      url: "/results/" + elm.getAttribute("data-id"),
      dataType: "text",
      success: (text, st) => {
        elm.outerHTML = text;
        if (n < 3) agg(elm, n);
      }
    });
  }, 1000);
};

const refresher = () => {
  const runnings = document.querySelectorAll(".result-item[data-status=running]");
  for (var i = 0, len = runnings.length; i < len; i++) {
    agg(runnings[i]);
  }
};
refresher();

const notrans = document.querySelectorAll(".result-item[data-status=notran]");
const snippet_id = document.querySelector("#snippet-id").innerHTML;
for (var i = 0, len = notrans.length; i < len; i++) {
  const elm = notrans[i];
  $.ajax({
    type: "POST",
    url: "/results/run",
    dataType: "text",
    data: { compiler_id: elm.getAttribute("data-compiler-id"), snippet_id: snippet_id },
    success: (text, st) => {
      elm.outerHTML = text;
    }
  });
}