From 4a0d91315ce1e18b29c814ab5adf5ab7b860e5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 22 Nov 2016 16:17:00 +0100 Subject: [PATCH] Fix update of webcam streamURL not being recognized We used to track our webcam stream URL by the global variable CONFIG_WEBCAMURL. That's still a left over from the architecture about four years ago and completely obsolete these days. Additionally it causes issues now that anything rendered into the page (as this variable value is through initscript.jinja2) will not be changed unless the page cache is refreshed. Taking the stream URL from the settings view model instead solves that problem and is way cleaner anyhow. --- src/octoprint/static/js/app/dataupdater.js | 4 ---- src/octoprint/static/js/app/viewmodels/control.js | 8 +++++--- src/octoprint/static/js/app/viewmodels/settings.js | 6 ++++++ src/octoprint/templates/initscript.jinja2 | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/octoprint/static/js/app/dataupdater.js b/src/octoprint/static/js/app/dataupdater.js index c8394618..ca25b810 100644 --- a/src/octoprint/static/js/app/dataupdater.js +++ b/src/octoprint/static/js/app/dataupdater.js @@ -136,10 +136,6 @@ function DataUpdater(allViewModels) { hideOfflineOverlay(); callViewModels(self.allViewModels, "onServerReconnect"); callViewModels(self.allViewModels, "onDataUpdaterReconnect"); - - if ($('#tabs li[class="active"] a').attr("href") == "#control") { - $("#webcam_image").attr("src", CONFIG_WEBCAM_STREAM + "?" + new Date().getTime()); - } } else { callViewModels(self.allViewModels, "onServerConnect"); } diff --git a/src/octoprint/static/js/app/viewmodels/control.js b/src/octoprint/static/js/app/viewmodels/control.js index 56d58ca3..3b3a56fa 100644 --- a/src/octoprint/static/js/app/viewmodels/control.js +++ b/src/octoprint/static/js/app/viewmodels/control.js @@ -91,6 +91,8 @@ $(function() { }; self.onEventSettingsUpdated = function (payload) { + // the webcam url might have changed, make sure we replace it now if the tab is focused + self._enableWebcam(); self.requestData(); }; @@ -378,9 +380,9 @@ $(function() { } var webcamImage = $("#webcam_image"); var currentSrc = webcamImage.attr("src"); - if (currentSrc === undefined || currentSrc.trim() == "") { - var newSrc = CONFIG_WEBCAM_STREAM; - if (CONFIG_WEBCAM_STREAM.lastIndexOf("?") > -1) { + var newSrc = self.settings.webcam_streamUrl(); + if (currentSrc != newSrc) { + if (newSrc.lastIndexOf("?") > -1) { newSrc += "&"; } else { newSrc += "?"; diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index 12e4703c..63407add 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -807,6 +807,12 @@ $(function() { $('ul.nav-list a:first', self.settingsDialog).tab("show"); } }; + + self.onServerReconnect = function() { + // the settings might have changed if the server was just restarted, + // better refresh them now + self.requestData(); + }; } OCTOPRINT_VIEWMODELS.push([ diff --git a/src/octoprint/templates/initscript.jinja2 b/src/octoprint/templates/initscript.jinja2 index 2ee922ef..daacfde4 100644 --- a/src/octoprint/templates/initscript.jinja2 +++ b/src/octoprint/templates/initscript.jinja2 @@ -10,7 +10,7 @@ var CONFIG_TIMELAPSEFILESPERPAGE = 10; var CONFIG_LOGFILESPERPAGE = 10; var CONFIG_USERSPERPAGE = 10; - var CONFIG_WEBCAM_STREAM = "{{ webcamStream|e }}"; + var CONFIG_WEBCAM_STREAM = "{{ webcamStream|e }}"; // deprecated, only left for compatibility reasons var CONFIG_ACCESS_CONTROL = {% if enableAccessControl -%} true; {% else %} false; {%- endif %} var CONFIG_ACCESS_CONTROL_ACTIVE = {% if accessControlActive -%} true; {% else %} false; {%- endif %} var CONFIG_SD_SUPPORT = {% if enableSdSupport -%} true; {% else %} false; {%- endif %}