diff --git a/octoprint_tailscale_funnel/octoprint_tailscale_funnel/static/js/tailscale_funnel_navbar.js b/octoprint_tailscale_funnel/octoprint_tailscale_funnel/static/js/tailscale_funnel_navbar.js index 37f3efd..8d444a0 100644 --- a/octoprint_tailscale_funnel/octoprint_tailscale_funnel/static/js/tailscale_funnel_navbar.js +++ b/octoprint_tailscale_funnel/octoprint_tailscale_funnel/static/js/tailscale_funnel_navbar.js @@ -1,8 +1,11 @@ $(function() { + var lastEnabled = null; + function refreshNavbarStatus() { var $status = $("#tsf_nav_status"); var $openLi = $("#tsf_nav_open_li"); var $open = $("#tsf_nav_open"); + var $toggleText = $("#tsf_nav_toggle_text"); if ($status.length === 0) return; $status.text("Checking..."); $.ajax({ @@ -14,6 +17,8 @@ $(function() { var enabled = !!(resp.data && resp.data.funnel_enabled); var url = resp.data && resp.data.public_url ? resp.data.public_url : ""; $status.text(enabled ? "Enabled" : "Disabled"); + $toggleText.text(enabled ? "Disable" : "Enable"); + lastEnabled = enabled; if (enabled && url) { $open.attr("href", url); $openLi.removeClass("hidden"); @@ -23,11 +28,13 @@ $(function() { } } else { $status.text("Error"); + $toggleText.text("Enable"); $openLi.addClass("hidden"); } }, error: function() { $status.text("Error"); + $toggleText.text("Enable"); $openLi.addClass("hidden"); } }); @@ -39,5 +46,49 @@ $(function() { e.preventDefault(); refreshNavbarStatus(); }); + + $(document).on('click', '#tsf_nav_toggle', function(e) { + e.preventDefault(); + var enable = !(lastEnabled === true); + // Optional simpler confirm: nur beim Aktivieren + if (enable) { + var c = window.confirm("Enabling Funnel will make your OctoPrint instance accessible from the public internet. Continue?"); + if (!c) return; + } + var action = enable ? 'enable' : 'disable'; + var $status = $("#tsf_nav_status"); + var $toggleText = $("#tsf_nav_toggle_text"); + $status.text(enable ? 'Enabling...' : 'Disabling...'); + $.ajax({ + url: PLUGIN_BASEURL + 'tailscale_funnel/' + action, + type: 'POST', + dataType: 'json', + success: function(resp) { + if (resp && resp.status === 'success') { + if (enable) { + $status.text('Enabled'); + $toggleText.text('Disable'); + var url = resp.data && resp.data.public_url ? resp.data.public_url : ''; + if (url) { + $("#tsf_nav_open").attr('href', url); + $("#tsf_nav_open_li").removeClass('hidden'); + } + lastEnabled = true; + } else { + $status.text('Disabled'); + $toggleText.text('Enable'); + $("#tsf_nav_open").attr('href', '#'); + $("#tsf_nav_open_li").addClass('hidden'); + lastEnabled = false; + } + } else { + $status.text('Error'); + } + }, + error: function() { + $status.text('Error'); + } + }); + }); }); diff --git a/octoprint_tailscale_funnel/octoprint_tailscale_funnel/templates/tailscale_funnel_navbar.jinja2 b/octoprint_tailscale_funnel/octoprint_tailscale_funnel/templates/tailscale_funnel_navbar.jinja2 index 28a49e9..2ddbcca 100644 --- a/octoprint_tailscale_funnel/octoprint_tailscale_funnel/templates/tailscale_funnel_navbar.jinja2 +++ b/octoprint_tailscale_funnel/octoprint_tailscale_funnel/templates/tailscale_funnel_navbar.jinja2 @@ -6,6 +6,7 @@