Verified Commit 95dfabe6 authored by Štěpán Henek's avatar Štěpán Henek 🌩

javascript: WS handler logic reworked

parent 4c7d24ce
......@@ -6,8 +6,8 @@ Foris.sampleMessages = {
chartValueAxis: "{% trans %}Value axis{% endtrans %}"
}
// Register on websockets events (just reload chart is used
Foris.WS["sample"] = function(msg) {
// Register on websockets events (just reload chart is used)
Foris.addWsHanlder("sample", (msg) => {
switch(msg.action) {
case "reload_chart":
$.get('{{ url("config_ajax", page_name="sample") }}', {action: "get_records"})
......@@ -17,4 +17,4 @@ Foris.WS["sample"] = function(msg) {
})
break;
}
}
});
......@@ -57,6 +57,7 @@ Foris.initialize = function () {
Foris.initClickableHints();
Foris.initSmoothScrolling();
Foris.applySVGFallback();
Foris.initWsHandlers();
Foris.initWebsockets();
Foris.initRebootRequired();
};
......@@ -130,31 +131,43 @@ Foris.applySVGFallback = function() {
}
};
Foris.WsHandlers = {};
Foris.WS = {
maintain: function(msg) {
switch (msg.action) {
case "reboot":
Foris.handleReboot(msg.data.ips, msg.data.remains);
break;
case "reboot_required":
$('#reboot-required-notice').show("slow");
break;;
case "network-restart":
Foris.handleNetworkRestart(msg.data.ips, msg.data.remains);
break;
}
},
router_notifications: function(msg) {
if (msg.action == "create" || msg.action == "mark_as_displayed") {
Foris.handleNotificationsCountUpdate(msg.data.new_count);
}
},
updater: function(msg) {
if (msg.action == "run") {
Foris.handleUpdaterRun(msg.data.status != "exit" && msg.data.status != "failed");
}
}
Foris.addWsHanlder = (module, handler, controller_id) => {
controller_id = controller_id || $('meta[name=controller-id]').attr("content");
let moduleHandler = Foris.WsHandlers[module] || {};
let handlers = moduleHandler[controller_id] || [];
handlers.push(handler);
moduleHandler[controller_id] = handlers;
Foris.WsHandlers[module] = moduleHandler;
};
Foris.initWsHandlers = () => {
Foris.addWsHanlder("maintain", (msg) => {
switch (msg.action) {
case "reboot":
Foris.handleReboot(msg.data.ips, msg.data.remains);
break;
case "reboot_required":
$('#reboot-required-notice').show("slow");
break;;
case "network-restart":
Foris.handleNetworkRestart(msg.data.ips, msg.data.remains);
break;
}
});
Foris.addWsHanlder("router_notifications", (msg) => {
if (msg.action == "create" || msg.action == "mark_as_displayed") {
Foris.handleNotificationsCountUpdate(msg.data.new_count);
}
});
Foris.addWsHanlder("updater", (msg) => {
if (msg.action == "run") {
Foris.handleUpdaterRun(msg.data.status != "exit" && msg.data.status != "failed");
}
});
};
Foris.initWebsockets = function() {
......@@ -170,16 +183,23 @@ Foris.initWebsockets = function() {
ws = new WebSocket(url);
ws.onopen = function () {
var output = JSON.stringify({"action": "subscribe", "params": Object.keys(Foris.WS)});
var output = JSON.stringify({"action": "subscribe", "params": Object.keys(Foris.WsHandlers)});
ws.send(output);
console.log("WS registering for: " + Object.keys(Foris.WS));
console.log("WS registering for: " + Object.keys(Foris.WsHandlers));
};
ws.onmessage = function (e) {
console.log("WS message received: " + e.data);
var parsed = JSON.parse(e.data);
if (Foris.WS.hasOwnProperty(parsed["module"])) {
Foris.WS[parsed["module"]](parsed);
if (Foris.WsHandlers.hasOwnProperty(parsed.module)) {
// filter using controller_id
for (let key in Foris.WsHandlers[parsed.module]) {
if (key == parsed.controller_id || key == "+") { // '+' means perform every time
for (let action of Foris.WsHandlers[parsed.module][key]) {
action(parsed);
}
}
}
}
};
......
......@@ -35,6 +35,7 @@
<meta name="foris-wss-path" content="{{ foris_info.websockets["wss_path"] }}">
{% endif %}
<meta name="csrf" content="{{ get_csrf_token() }}">
<meta name="controller-id" content="{{ foris_info.backend.controller_id if foris_info.backend.controller_id else "" }}">
<script src="{{ static("js/contrib/jquery-3.3.1.min.js") }}"></script>
<script src="{{ static("js/contrib/parsley.min.js") }}"></script>
<script src="{{ static("js/contrib/vex.combined.min.js") }}"></script>
......
......@@ -31,7 +31,7 @@
resultBox.removeClass("test-success").removeClass("test-loading").addClass("test-fail").text(Foris.messages.error);
}
}
Foris.WS["wan"] = function(msg) {
Foris.addWsHanlder("wan", (msg) => {
switch(msg["action"]) {
case "connection_test":
if (msg["data"]["test_id"] != Foris.watched_test) {
......@@ -55,7 +55,7 @@
$("#test-connection").show();
break;
}
}
});
$(document).ready(function() {
$("#test-connection").click(function(e) {
$("#connection-test-fail").hide();
......
......@@ -48,7 +48,7 @@
resultBox.removeClass("test-success").removeClass("test-loading").addClass("test-fail").text(Foris.messages.error);
}
}
Foris.WS["wan"] = function(msg) {
Foris.addWsHanlder("wan", (msg) => {
switch(msg["action"]) {
case "connection_test":
if (msg["data"]["test_id"] != Foris.watched_test) {
......@@ -72,7 +72,7 @@
$("#test-connection").show();
break;
}
}
});
$(document).ready(function() {
$("#test-connection").click(function(e) {
$("#connection-test-fail").hide();
......
......@@ -30,7 +30,7 @@
{% include "config/_dhcp_clients_table.html.j2" %}
{% endif %}
<script>
Foris.WS["networks"] = (msg) => Foris.clearNetworkWarnings("guest_turris", msg.data);
Foris.addWsHanlder("networks", (msg) => Foris.clearNetworkWarnings("guest_turris", msg.data));
</script>
{% endif %}
{% endblock %}
......@@ -34,7 +34,7 @@
{% endif %}
</div>
<script>
Foris.WS["networks"] = (msg) => Foris.clearNetworkWarnings("lan", msg.data);
Foris.addWsHanlder("networks", (msg) => Foris.clearNetworkWarnings("lan", msg.data));
</script>
{% endif %}
{% endblock %}
......@@ -35,8 +35,7 @@
$(document).ready(function() {
Foris.initNotificationReboot();
});
// redefine
Foris.WS["router_notifications"] = function(msg) {
Foris.addWsHanlder("router_notifications", (msg) => {
if (msg.action == "create" || msg.action == "mark_as_displayed") {
// Update nav
Foris.handleNotificationsCountUpdate(msg.data.new_count);
......@@ -51,7 +50,7 @@
location.reload();
});
}
}
});
Foris.initNotifications("{{ get_csrf_token() }}");
</script>
......
......@@ -74,7 +74,7 @@
{% if is_xhr is not defined %}
<script src="{{ static("js/contrib/jquery-qrcode-0.14.0.min.js") }}"></script>
<script>
Foris.WS["remote"] = (msg) => {
Foris.addWsHanlder("remote", (msg) => {
switch(msg["action"]) {
case "generate_ca":
case "delete_ca":
......@@ -85,7 +85,7 @@
Foris.updateTokens();
break;
}
};
});
Foris.updateTokens = () => {
$.get('{{ url("config_ajax", page_name="remote") }}', {action: "list-tokens"})
.done((response) => {
......
......@@ -220,7 +220,7 @@
await state_element.animate({opacity: '1.0'}, 50);
}
};
Foris.WS["subordinates"] = async function(msg) {
Foris.addWsHanlder("subordinates", async (msg) => {
switch(msg.action) {
case "add_sub":
case "add_subsub":
......@@ -229,14 +229,14 @@
await Foris.loadSubordinatesList();
break;
}
}
Foris.WS["remote"] = function(msg) {
});
Foris.addWsHanlder("remote", async (msg) => {
switch(msg.action) {
case "advertize":
Foris.updateSubordinateState(msg.data);
break;
};
};
}, '+');
$(document).ready(function() {
Foris.loadSubordinatesList();
Foris.afterAjaxUpdateFunctions.push(Foris.suboridnatesOverrideEditClose);
......
......@@ -121,7 +121,7 @@
Foris.ntp_override();
});
Foris.watched_ntpdate = null;
Foris.WS["time"] = function(msg) {
Foris.addWsHanlder("time", (msg) => {
switch(msg["action"]) {
case "ntpdate_started":
if (msg.data.id != Foris.watched_ntpdate) {
......@@ -142,7 +142,7 @@
$("#start-ntpdate-button").toggleClass("grayed");
break;
}
}
});
$(document).ready(function() {
Foris.ntp_override();
});
......
......@@ -36,7 +36,7 @@
{% include "config/_connection_test.html.j2" %}
</div>
<script>
Foris.WS["networks"] = (msg) => Foris.clearNetworkWarnings("wan", msg.data);
Foris.addWsHanlder("networks", (msg) => Foris.clearNetworkWarnings("wan", msg.data));
</script>
{% endif %}
{% endblock %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment