OctoPrint-MyStromSwitch/octoprint_mystromswitch/static/js/mystromswitch.js

293 lines
9.8 KiB
JavaScript
Raw Normal View History

2019-11-19 21:29:32 +01:00
$(function() {
2019-11-19 21:55:31 +01:00
function mystromswitchViewModel(parameters) {
2019-11-19 21:29:32 +01:00
var self = this;
self.loginState = parameters[0];
self.settings = parameters[1];
self.printer = parameters[2];
2019-11-19 21:55:31 +01:00
self.mystromswitchEnabled = ko.observable();
2019-11-19 21:29:32 +01:00
self.testButtonChangeStatus = function (stat) {
2019-11-19 21:55:31 +01:00
$("#tester_mystromswitch_gcode").prop("disabled", stat);
$("#tester_mystromswitch_api").prop("disabled", stat);
$("#tester_mystromswitch_api_custom").prop("disabled", stat);
2019-11-19 21:29:32 +01:00
}
self.eventChangeCheckToRadio = function (id, listOff) {
$(id).on("change", function () {
if ($(this).prop("checked") == true)
{
listOff.forEach(function(element) {
if (id != element.id)
{
if ($(element.id).prop("checked") == true)
{
$(element.id).unbind("change");
$(element.id).trigger("click");
self.eventChangeCheckToRadio(element.id, listOff);
}
}
});
}
})
}
2019-11-19 21:55:31 +01:00
$("#tester_mystromswitch_gcode").on("click", function () {
2019-11-19 21:29:32 +01:00
self.settings.saveData();
$(this).children("i").show();
setTimeout(function (current) {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "shutdown",
mode: 1,
eventView : true
}),
contentType: "application/json; charset=UTF-8"
}).done(function() {
current.children("i").hide();
});
}, 1000, $(this));
});
2019-11-19 21:55:31 +01:00
$("#tester_mystromswitch_api").on("click", function () {
2019-11-19 21:29:32 +01:00
self.settings.saveData();
$(this).children("i").show();
setTimeout(function (current) {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "shutdown",
mode: 2,
eventView : true
}),
contentType: "application/json; charset=UTF-8"
}).done(function() {
current.children("i").hide();
});
}, 1000, $(this));
});
2019-11-19 21:55:31 +01:00
$("#tester_mystromswitch_api_custom").on("click", function () {
2019-11-19 21:29:32 +01:00
self.settings.saveData();
$(this).children("i").show();
setTimeout(function (current) {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "shutdown",
mode: 3,
eventView : true
}),
contentType: "application/json; charset=UTF-8"
}).done(function() {
current.children("i").hide();
});
}, 1000, $(this));
});
self.listOffMode = [
2019-11-19 21:55:31 +01:00
{"id" : "#mystromswitch_mode_shutdown_gcode"},
{"id" : "#mystromswitch_mode_shutdown_api"},
{"id" : "#mystromswitch_mode_shutdown_api_custom"},
2019-11-19 21:29:32 +01:00
]
self.listOffHTTPMethode = [
2019-11-19 21:55:31 +01:00
{"id" : "#mystromswitch_api_custom_GET"},
{"id" : "#mystromswitch_api_custom_POST"},
{"id" : "#mystromswitch_api_custom_PUT"}
2019-11-19 21:29:32 +01:00
]
2019-11-19 21:55:31 +01:00
self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_gcode", self.listOffMode);
self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_api", self.listOffMode);
self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_api_custom", self.listOffMode);
2019-11-19 21:29:32 +01:00
2019-11-19 21:55:31 +01:00
self.eventChangeCheckToRadio("#mystromswitch_api_custom_GET", self.listOffHTTPMethode);
self.eventChangeCheckToRadio("#mystromswitch_api_custom_POST", self.listOffHTTPMethode);
self.eventChangeCheckToRadio("#mystromswitch_api_custom_PUT", self.listOffHTTPMethode);
2019-11-19 21:29:32 +01:00
// Hack to remove automatically added Cancel button
// See https://github.com/sciactive/pnotify/issues/141
//PNotify.prototype.options.confirm.buttons = [];
//another way use, add custom style class for hide cancel button
self.timeoutPopupText = gettext('Shutting down printer in ');
self.timeoutPopupOptions = {
title: gettext('Shutdown Printer'),
type: 'notice',
icon: true,
hide: false,
confirm: {
confirm: true,
buttons: [{
text: 'Abort Shutdown Printer',
addClass: 'btn-block btn-danger',
promptTrigger: true,
click: function(notice, value){
notice.remove();
notice.get().trigger("pnotify.cancel", [notice, value]);
}
}, {
2019-11-19 21:55:31 +01:00
addClass: 'mystromswitchHideCancelBtnConfirm',
2019-11-19 21:29:32 +01:00
promptTrigger: true,
click: function(notice, value){
notice.remove();
}
}]
},
buttons: {
closer: false,
sticker: false,
},
history: {
history: false
}
};
//for touch ui
self.touchUIMoveElement = function (self, counter) {
var hash = window.location.hash;
if (hash != "" && hash != "#printer" && hash != "#touch")
{
return;
}
if (counter < 10) {
if (document.getElementById("touch") != null && document.getElementById("printer") != null && document.getElementById("printer") != null && document.getElementById("touch").querySelector("#printer").querySelector("#files_wrapper")) {
var newParent = document.getElementById("files_wrapper").parentNode;
2019-11-19 21:55:31 +01:00
newParent.insertBefore(document.getElementById('sidebar_plugin_mystromswitch_wrapper'), document.getElementById("files_wrapper"));
2019-11-19 21:29:32 +01:00
} else {
setTimeout(self.touchUIMoveElement, 1000, self, ++counter);
}
}
}
//add octoprint event for check finish
self.onStartupComplete = function () {
//self.touchUIMoveElement(self, 0);
if (self.printer.isPrinting())
{
self.testButtonChangeStatus(true);
} else {
self.testButtonChangeStatus(false);
}
};
self.onUserLoggedIn = function() {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "update",
eventView : false
}),
contentType: "application/json; charset=UTF-8"
})
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
data: JSON.stringify({
command: "status"
}),
context:self,
contentType: "application/json; charset=UTF-8"
}).done(function(data, textStatus, jqXHR ){
2019-11-19 21:55:31 +01:00
this.mystromswitchEnabled(data == "True" ? true : false);
2019-11-19 21:29:32 +01:00
})
}
self.onUserLoggedOut = function() {
}
self.onEventPrinterStateChanged = function(payload) {
if (payload.state_id == "PRINTING" || payload.state_id == "PAUSED"){
self.testButtonChangeStatus(true);
} else {
self.testButtonChangeStatus(false);
}
}
2019-11-19 21:55:31 +01:00
self.onmystromswitchEvent = function() {
if (self.mystromswitchEnabled()) {
2019-11-19 21:29:32 +01:00
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "enable",
eventView : false
}),
contentType: "application/json; charset=UTF-8"
})
} else {
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "disable",
eventView : false
}),
contentType: "application/json; charset=UTF-8"
})
}
}
2019-11-19 21:55:31 +01:00
self.mystromswitchEnabled.subscribe(self.onmystromswitchEvent, self);
2019-11-19 21:29:32 +01:00
self.onDataUpdaterPluginMessage = function(plugin, data) {
2019-11-19 21:55:31 +01:00
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {
2019-11-19 21:29:32 +01:00
return;
}
2019-11-19 21:55:31 +01:00
self.mystromswitchEnabled(data.mystromswitchEnabled);
2019-11-19 21:29:32 +01:00
if (data.type == "timeout") {
if ((data.timeout_value != null) && (data.timeout_value > 0)) {
self.timeoutPopupOptions.text = self.timeoutPopupText + data.timeout_value;
if (typeof self.timeoutPopup != "undefined") {
self.timeoutPopup.update(self.timeoutPopupOptions);
} else {
self.timeoutPopup = new PNotify(self.timeoutPopupOptions);
self.timeoutPopup.get().on('pnotify.cancel', function() {self.abortShutdown(true);});
}
} else {
if (typeof self.timeoutPopup != "undefined") {
self.timeoutPopup.remove();
self.timeoutPopup = undefined;
}
}
}
}
self.abortShutdown = function(abortShutdownValue) {
self.timeoutPopup.remove();
self.timeoutPopup = undefined;
$.ajax({
2019-11-19 21:55:31 +01:00
url: API_BASEURL + "plugin/mystromswitch",
2019-11-19 21:29:32 +01:00
type: "POST",
dataType: "json",
data: JSON.stringify({
command: "abort",
eventView : true
}),
contentType: "application/json; charset=UTF-8"
})
}
}
OCTOPRINT_VIEWMODELS.push([
2019-11-19 21:55:31 +01:00
mystromswitchViewModel,
2019-11-19 21:29:32 +01:00
["loginStateViewModel", "settingsViewModel", "printerStateViewModel"],
2019-11-19 21:55:31 +01:00
$(".sidebar_plugin_mystromswitch").get(0)
2019-11-19 21:29:32 +01:00
]);
});