lbry.tech/app/dist/scripts/sockets.js

149 lines
5 KiB
JavaScript
Raw Normal View History

2019-07-10 23:48:45 +02:00
"use strict"; /* global document, initializeSmoothScroll, location, runScriptsInDynamicallyInsertedHTML, WebSocket, window */
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() {
2019-02-05 00:42:52 +01:00
if (!ws || ws.readyState === 3)
initializeWebSocketConnection();
2018-09-27 18:11:26 +02:00
}
function initializeWebSocketConnection() {
ws = new WebSocket(location.origin.replace(/^http/, "ws"));
2019-02-14 00:08:59 +01:00
// ws.onopen = () => console.log("WebSocket connection established"); // eslint-disable-line no-console
2018-07-12 23:07:16 +02:00
2018-09-27 18:11:26 +02:00
ws.onmessage = socket => {
2019-02-21 20:28:09 +01:00
let data = JSON.parse(socket.data);
2018-07-17 21:40:25 +02:00
2018-09-28 21:09:45 +02:00
switch(true) {
2019-02-21 20:28:09 +01:00
case data.message === "github token status":
data = data.data;
syncWithApi(data); // eslint-disable-line no-undef
break;
2019-02-12 00:47:01 +01: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>`;
setTimeout(() => {
document.getElementById("flash-container").innerHTML = "";
}, 2100);
break;
case data.message === "redirect":
window.location.href = data.url;
break;
2019-07-10 23:48:45 +02:00
case data.message === "replace html":
// create placeholder
var placeholder = document.createElement("div");
placeholder.setAttribute("id", "__placeholder"); // eslint-disable-line padding-line-between-statements
document.querySelector(data.selector).insertAdjacentElement("afterend", placeholder);
// remove original element
document.querySelector(data.selector).remove();
// add new element and remove placeholder
document.getElementById("__placeholder").insertAdjacentHTML("afterend", data.html);
document.getElementById("__placeholder").remove();
// make our scripts work
runScriptsInDynamicallyInsertedHTML(document.querySelector(data.selector), document.querySelector(data.selector).innerHTML);
// make smooth scroll work on our new content
initializeSmoothScroll();
break;
2018-09-28 21:09:45 +02:00
case data.message === "show result":
2019-02-05 00:42:52 +01:00
if (!data.example)
return;
2018-09-28 21:09:45 +02:00
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) {
2019-02-05 00:42:52 +01:00
detectLanguageAndUpdate(); // eslint-disable-line no-undef
initCanvas(); // eslint-disable-line no-undef
2018-09-27 20:00:34 +02:00
2018-09-28 21:09:45 +02:00
setTimeout(() => {
2018-12-05 00:12:39 +01: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 =
2018-12-05 00:12:39 +01:00
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
default:
2019-02-05 00:42:52 +01:00
console.log(data); // eslint-disable-line no-console
2018-09-27 18:11:26 +02:00
break;
}
};
2019-02-05 00:42:52 +01:00
ws.onclose = () => checkWebSocketConnection(); // reconnect now
2018-09-27 18:11:26 +02:00
}
2018-07-16 23:06:37 +02:00
2019-02-05 00:42:52 +01:00
function send(msg) { // eslint-disable-line no-unused-vars
socketReady(ws, () => ws.send(JSON.stringify(msg)));
2018-07-16 23:06:37 +02:00
}
function socketReady(socket, callback) {
setTimeout(() => {
2018-09-27 18:11:26 +02:00
if (socket && socket.readyState === 1) {
2019-02-05 00:42:52 +01:00
if (callback !== undefined)
callback();
2018-07-16 23:06:37 +02:00
return;
}
2018-09-27 18:11:26 +02:00
return socketReady(socket, callback);
2018-07-16 23:06:37 +02:00
}, 5);
}