File size: 5,057 Bytes
7f8e190 | 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /* Workspace interactions.
*
* Run and Submit are ordinary form submissions to /problems/<slug>/judge, so
* the workspace works with scripting disabled. This file adds the editor
* affordances the captured pane shows: line numbers, the Ln/Col readout, tab
* insertion, the language switch, the case tabs, the console tabs and the
* stopwatch. No guest code is ever executed here or on the server. */
(function () {
"use strict";
var editor = document.querySelector("[data-editor]");
var gutter = document.querySelector("[data-gutter]");
var cursor = document.querySelector("[data-cursor]");
var saved = document.querySelector("[data-saved-state]");
function lineCount(value) {
return value.split("\n").length;
}
function paintGutter() {
if (!gutter || !editor) {
return;
}
var total = lineCount(editor.value);
var numbers = [];
for (var index = 1; index <= total; index += 1) {
numbers.push(index);
}
gutter.textContent = numbers.join("\n");
}
function paintCursor() {
if (!cursor || !editor) {
return;
}
var upto = editor.value.slice(0, editor.selectionStart);
var lines = upto.split("\n");
cursor.textContent =
"Ln " + lines.length + ", Col " + (lines[lines.length - 1].length + 1);
}
if (editor) {
paintGutter();
paintCursor();
editor.addEventListener("input", function () {
paintGutter();
if (saved) {
saved.textContent = "Unsaved";
}
});
["keyup", "click", "select"].forEach(function (name) {
editor.addEventListener(name, paintCursor);
});
editor.addEventListener("scroll", function () {
if (gutter) {
gutter.style.transform = "translateY(" + -editor.scrollTop + "px)";
}
});
editor.addEventListener("keydown", function (event) {
if (event.key !== "Tab") {
return;
}
event.preventDefault();
var start = editor.selectionStart;
var end = editor.selectionEnd;
editor.value =
editor.value.slice(0, start) + " " + editor.value.slice(end);
editor.selectionStart = editor.selectionEnd = start + 4;
paintGutter();
paintCursor();
});
}
/* ---- language switch: reload with the chosen language -------------- */
var language = document.querySelector("[data-language]");
if (language) {
language.addEventListener("change", function () {
var url = new URL(window.location.href);
url.searchParams.set("lang", language.value);
window.location.href = url.pathname + url.search;
});
}
/* ---- testcase tabs -------------------------------------------------- */
var caseTabs = Array.prototype.slice.call(
document.querySelectorAll("[data-case-tab]")
);
var casePanels = Array.prototype.slice.call(
document.querySelectorAll("[data-case-panel]")
);
caseTabs.forEach(function (tab) {
tab.addEventListener("click", function () {
var wanted = tab.getAttribute("data-case-tab");
caseTabs.forEach(function (other) {
var active = other === tab;
other.classList.toggle("is-active", active);
other.setAttribute("aria-selected", active ? "true" : "false");
});
casePanels.forEach(function (panel) {
panel.classList.toggle(
"is-hidden",
panel.getAttribute("data-case-panel") !== wanted
);
});
});
});
/* ---- console tabs --------------------------------------------------- */
var consoleTabs = Array.prototype.slice.call(
document.querySelectorAll("[data-console-tab]")
);
var consolePanels = Array.prototype.slice.call(
document.querySelectorAll("[data-console-panel]")
);
consoleTabs.forEach(function (tab) {
tab.addEventListener("click", function () {
var wanted = tab.getAttribute("data-console-tab");
consoleTabs.forEach(function (other) {
other.classList.toggle("is-active", other === tab);
});
consolePanels.forEach(function (panel) {
panel.classList.toggle(
"is-hidden",
panel.getAttribute("data-console-panel") !== wanted
);
});
});
});
/* ---- stopwatch ------------------------------------------------------ */
var stopwatch = document.querySelector("[data-stopwatch]");
if (stopwatch) {
var elapsed = 0;
var running = false;
var pad = function (value) {
return value < 10 ? "0" + value : String(value);
};
var paint = function () {
stopwatch.textContent =
pad(Math.floor(elapsed / 3600)) +
":" +
pad(Math.floor((elapsed % 3600) / 60)) +
":" +
pad(elapsed % 60);
};
stopwatch.setAttribute("role", "button");
stopwatch.setAttribute("tabindex", "0");
stopwatch.title = "Start or pause the stopwatch";
stopwatch.addEventListener("click", function () {
running = !running;
});
window.setInterval(function () {
if (running) {
elapsed += 1;
paint();
}
}, 1000);
paint();
}
})();
|