diff --git a/octoprint_webcamtab/__init__.py b/octoprint_webcamtab/__init__.py index fafff9b..e87230d 100644 --- a/octoprint_webcamtab/__init__.py +++ b/octoprint_webcamtab/__init__.py @@ -8,15 +8,9 @@ __copyright__ = "Copyright (C) 2017 Sven Lohrmann - Released under terms of the import octoprint.plugin -class WebcamTabPlugin(octoprint.plugin.SettingsPlugin, - octoprint.plugin.AssetPlugin, +class WebcamTabPlugin(octoprint.plugin.AssetPlugin, octoprint.plugin.TemplatePlugin): - # SettingsPlugin mixin - - def get_settings_defaults(self): - return dict() - # AssetPlugin mixin def get_assets(self): @@ -24,6 +18,13 @@ class WebcamTabPlugin(octoprint.plugin.SettingsPlugin, js=["js/webcamtab.js"] ) + # TemplatePlugin + + def get_template_configs(self): + return [ + dict(type="tab", name="Webcam") + ] + # Softwareupdate hook def get_update_information(self): diff --git a/octoprint_webcamtab/static/js/webcamtab.js b/octoprint_webcamtab/static/js/webcamtab.js index 6ec1aef..80e7e4e 100644 --- a/octoprint_webcamtab/static/js/webcamtab.js +++ b/octoprint_webcamtab/static/js/webcamtab.js @@ -8,21 +8,64 @@ $(function() { function WebcamTabViewModel(parameters) { var self = this; - // assign the injected parameters, e.g.: - // self.loginStateViewModel = parameters[0]; - // self.settingsViewModel = parameters[1]; + self.control = parameters[0]; - // TODO: Implement your plugin's view model here. - } + self.control.onTabChange = function (current, previous) { + // replaced #control with #tab_plugin_webcamtab + if (current == "#tab_plugin_webcamtab") { + self.control._enableWebcam(); + } else if (previous == "#tab_plugin_webcamtab") { + self.control._disableWebcam(); + } + }; - // view model class, parameters for constructor, container to bind to - OCTOPRINT_VIEWMODELS.push([ - WebcamTabViewModel, + self.control._enableWebcam = function() { + // replaced #control with #tab_plugin_webcamtab + if (OctoPrint.coreui.selectedTab != "#tab_plugin_webcamtab" || !OctoPrint.coreui.browserTabVisible) { + return; + } - // e.g. loginStateViewModel, settingsViewModel, ... - [ /* "loginStateViewModel", "settingsViewModel" */ ], + if (self.control.webcamDisableTimeout != undefined) { + clearTimeout(self.control.webcamDisableTimeout); + } + var webcamImage = $("#webcam_image"); + var currentSrc = webcamImage.attr("src"); - // e.g. #settings_plugin_webcamtab, #tab_plugin_webcamtab, ... - [ /* ... */ ] - ]); + // safari bug doesn't release the mjpeg stream, so we just set it up the once + if (self.control._isSafari() && currentSrc != undefined) { + return; + } + + var newSrc = self.control.settings.webcam_streamUrl(); + if (currentSrc != newSrc) { + if (newSrc.lastIndexOf("?") > -1) { + newSrc += "&"; + } else { + newSrc += "?"; + } + newSrc += new Date().getTime(); + + self.control.webcamLoaded(false); + self.control.webcamError(false); + webcamImage.attr("src", newSrc); + } + }; + + self.onStartup = function() { + var container = $("#control #webcam_container"); + if (container.length) { + var hint = container.next(); + if (hint.attr("data-bind") === "visible: keycontrolPossible") { + hint.remove(); + } + container.remove(); + } + }; + }; + + OCTOPRINT_VIEWMODELS.push({ + construct: WebcamTabViewModel, + dependencies: ["controlViewModel"], + elements: ["#tab_plugin_webcamtab"] + }); }); diff --git a/octoprint_webcamtab/templates/webcamtab_tab.jinja2 b/octoprint_webcamtab/templates/webcamtab_tab.jinja2 new file mode 100644 index 0000000..a1c04f4 --- /dev/null +++ b/octoprint_webcamtab/templates/webcamtab_tab.jinja2 @@ -0,0 +1,36 @@ +
{{ _('Webcam stream loading...') }}
+{{ _('Webcam stream not loaded') }}
+{{ _('It might not be correctly configured. You can change the URL of the stream under "Settings" > "Webcam & Timelapse" > "Stream URL". If you don\'t have a webcam just set the URL to an empty value.') }}
+