/*
* Log Stream
*/
-$(".jobs-log-stream").each(function() {
+document.querySelectorAll(".jobs-log-stream").forEach((log) => {
// Fetch the UUID of the job
- const uuid = $(this).data("uuid");
+ const uuid = log.dataset.uuid;
// Fetch the limit
- const limit = $(this).data("limit");
+ const limit = log.dataset.limit;
- // Find where we are appending lines to
- const log = $(this);
+ // Automatically scroll to the bottom
+ var autoscroll = true;
// Make the URL
var url = "wss://" + window.location.host + "/api/v1/jobs/" + uuid + "/log/stream";
// Parse message as JSON
const data = JSON.parse(event.data);
- console.log("Message from server: ", data);
+ console.debug("Message from server: ", data);
// If there is a limit, reduce the number of rows first
- while (limit > 0 && log.children().length >= limit) {
- log.children().first().remove();
+ while (limit > 0 && log.children.length >= limit) {
+ log.removeChild(log.firstChild);
}
// Create a new line
- var line = $("<li></li>");
+ var line = document.createElement("li");
// Set the log level
- line.addClass(data.level);
+ line.classList.add(data.level);
// Set the content
- line.text(data.message);
+ line.textContent = data.message;
// Append it to the log window
- log.append(line);
+ log.appendChild(line);
+
+ // Scroll to the bottom
+ if (autoscroll) {
+ log.scrollTop = log.scrollHeight;
+ }
+ });
+
+ // Disable auto-scroll when the user scrolls away and enabled it again
+ // when the user scrolls to the bottom.
+ log.addEventListener("scroll", (event) => {
+ autoscroll = log.scrollTop + log.clientHeight >= log.scrollHeight - 50;
});
});