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

72 lines
2.3 KiB
JavaScript
Raw 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
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"]
});
2017-08-18 12:17:26 +02:00
});