OctoPrint-WebcamTab/octoprint_webcamtab/static/js/webcamtab.js

73 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2017-08-18 12:17:26 +02:00
/*
* View model for OctoPrint-WebcamTab
*
* Author: Sven Lohrmann
* License: AGPLv3
*/
$(function() {
function WebcamTabViewModel(parameters) {
var self = this;
2017-08-18 17:18:30 +02:00
self.control = parameters[0];
2017-08-18 12:17:26 +02:00
2017-08-18 17:18:30 +02:00
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();
}
};
2017-08-18 12:17:26 +02:00
2017-08-18 17:18:30 +02:00
self.control._enableWebcam = function() {
// replaced #control with #tab_plugin_webcamtab
if (OctoPrint.coreui.selectedTab != "#tab_plugin_webcamtab" || !OctoPrint.coreui.browserTabVisible) {
return;
}
2017-08-18 12:17:26 +02:00
2017-08-18 17:18:30 +02:00
if (self.control.webcamDisableTimeout != undefined) {
clearTimeout(self.control.webcamDisableTimeout);
}
var webcamImage = $("#webcam_image");
var currentSrc = webcamImage.attr("src");
2017-08-18 12:17:26 +02:00
2017-08-18 17:18:30 +02:00
// safari bug doesn't release the mjpeg stream, so we just set it up the once
2017-09-05 17:07:02 +02:00
if (OctoPrint.coreui.browser.safari && currentSrc != undefined) {
2017-08-18 17:18:30 +02:00
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.onAfterBinding = function() {
var tab = $("#tab_plugin_webcamtab");
var webcam = $("#webcam_container");
if (webcam) {
var hint = webcam.next();
tab.append(webcam.detach());
if (hint && hint.attr("data-bind") === "visible: keycontrolPossible") {
tab.append(hint.detach());
2017-08-18 17:18:30 +02:00
}
}
};
};
OCTOPRINT_VIEWMODELS.push({
construct: WebcamTabViewModel,
dependencies: ["controlViewModel"],
elements: ["#tab_plugin_webcamtab"]
});
2017-08-18 12:17:26 +02:00
});