2018-09-27 18:11:26 +02:00
|
|
|
"use strict";
|
2018-07-16 23:06:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
initializeWebSocketConnection();
|
|
|
|
setInterval(checkWebSocketConnection, 5000);
|
|
|
|
});
|
2018-07-12 23:07:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
let ws = null;
|
2018-07-12 23:07:16 +02:00
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
function checkWebSocketConnection() {
|
|
|
|
if (!ws || ws.readyState === 3) initializeWebSocketConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeWebSocketConnection() {
|
|
|
|
ws = new WebSocket(location.origin.replace(/^http/, "ws"));
|
|
|
|
|
|
|
|
ws.onopen = () => {
|
|
|
|
console.log("WebSocket connection established"); // eslint-disable-line
|
|
|
|
};
|
2018-07-12 23:07:16 +02:00
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
ws.onmessage = socket => {
|
|
|
|
const data = JSON.parse(socket.data);
|
2018-07-17 21:40:25 +02:00
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
switch(true) {
|
|
|
|
case data.message === "show result":
|
|
|
|
if (!data.example) return;
|
|
|
|
|
|
|
|
document.querySelector(data.selector).innerHTML = data.html;
|
|
|
|
|
|
|
|
if (!document.querySelector(`[data-example="${data.example}"`).classList.contains("completed"))
|
2018-10-03 22:27:13 +02:00
|
|
|
document.getElementById("playground-example-description").classList.remove("success");
|
2018-09-28 21:09:45 +02:00
|
|
|
|
|
|
|
document.querySelector(`[data-example="${data.example}"`).classList.add("completed");
|
2018-10-03 22:27:13 +02:00
|
|
|
document.getElementById("playground-example-description").classList.add("success");
|
2018-09-28 21:09:45 +02:00
|
|
|
|
2018-10-03 22:27:13 +02:00
|
|
|
document.getElementById("playground-example-description").innerHTML =
|
2018-09-28 21:09:45 +02:00
|
|
|
document.querySelector(`[data-example="${data.example}"`).dataset.success;
|
|
|
|
|
|
|
|
if (document.getElementById("temp-loader"))
|
|
|
|
document.getElementById("temp-loader").style.display = "none";
|
|
|
|
|
2018-10-03 22:27:13 +02:00
|
|
|
if (document.querySelector(".playground"))
|
|
|
|
document.querySelector(".playground").classList.remove("waiting");
|
2018-09-28 21:09:45 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
case data.message === "updated html":
|
|
|
|
document.querySelector(data.selector).innerHTML = data.html;
|
2018-10-10 21:04:16 +02:00
|
|
|
|
|
|
|
if (data.class)
|
|
|
|
document.querySelector(data.selector).classList.add(data.class);
|
|
|
|
|
|
|
|
if (data.selector !== "#emailMessage") {
|
|
|
|
document.getElementById("emailAddress").value = "";
|
|
|
|
document.getElementById("emailMessage").innerHTML = "";
|
|
|
|
}
|
2018-09-27 20:00:34 +02:00
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
if (data.example === 2) {
|
|
|
|
detectLanguageAndUpdate(); // eslint-disable-line
|
|
|
|
initCanvas(); // eslint-disable-line
|
2018-09-27 20:00:34 +02:00
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
setTimeout(() => {
|
2018-10-03 22:27:13 +02:00
|
|
|
document.querySelector(".playground__content__meme__canvas__thumbnail").click();
|
2018-10-03 21:14:38 +02:00
|
|
|
}, 500);
|
2018-09-27 20:00:34 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:27:13 +02:00
|
|
|
if (document.getElementById("playground-example-description")) {
|
|
|
|
document.getElementById("playground-example-description").classList.remove("success");
|
2018-09-27 20:00:34 +02:00
|
|
|
|
2018-10-03 22:27:13 +02:00
|
|
|
document.getElementById("playground-example-description").innerHTML =
|
|
|
|
document.querySelector(".playground__navigation__example.active").dataset.description;
|
2018-09-27 20:00:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (document.getElementById("temp-loader"))
|
|
|
|
document.getElementById("temp-loader").style.display = "none";
|
|
|
|
|
2018-10-03 22:27:13 +02:00
|
|
|
if (document.querySelector(".playground"))
|
|
|
|
document.querySelector(".playground").classList.remove("waiting");
|
2018-09-28 21:09:45 +02:00
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
break;
|
2018-07-17 21:40:25 +02:00
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
case data.message === "notification": // TODO: Make work with appending so multiple notifications can be sent
|
|
|
|
document.getElementById("flash-container").innerHTML =
|
|
|
|
`<div class="flash active${data.type ? " " + data.type : ""}">${data.details}</div>`;
|
2018-07-17 21:40:25 +02:00
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
document.getElementById("flash-container").innerHTML = "";
|
|
|
|
}, 2100);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
console.log(data); // eslint-disable-line
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ws.onclose = () => {
|
|
|
|
console.log("WebSocket connection lost"); // eslint-disable-line
|
|
|
|
checkWebSocketConnection(); // reconnect now
|
|
|
|
};
|
|
|
|
}
|
2018-07-16 23:06:37 +02:00
|
|
|
|
|
|
|
function send(msg) { // eslint-disable-line
|
|
|
|
socketReady(ws, () => ws.send(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
function socketReady(socket, callback) {
|
|
|
|
setTimeout(() => {
|
2018-09-27 18:11:26 +02:00
|
|
|
if (socket && socket.readyState === 1) {
|
2018-07-16 23:06:37 +02:00
|
|
|
if (callback !== undefined) callback();
|
|
|
|
return;
|
|
|
|
}
|
2018-09-27 18:11:26 +02:00
|
|
|
|
|
|
|
return socketReady(socket, callback);
|
2018-07-16 23:06:37 +02:00
|
|
|
}, 5);
|
|
|
|
}
|