diff --git a/octoprint/static/js/app/main.js b/octoprint/static/js/app/main.js index a8f4d0e5..b61c8cbf 100644 --- a/octoprint/static/js/app/main.js +++ b/octoprint/static/js/app/main.js @@ -42,67 +42,15 @@ $(function() { 'margin-left': function() { return -($(this).width() /2); } }); return false; - }) + }); - //~~ Temperature control (should really move to knockout click binding) + //~~ Temperature - $("#temp_newTemp_set").click(function() { - var newTemp = $("#temp_newTemp").val(); - $.ajax({ - url: AJAX_BASEURL + "control/temperature", - type: "POST", - dataType: "json", - data: { temp: newTemp }, - success: function() {$("#temp_newTemp").val("")} - }) - }) - $("#temp_newBedTemp_set").click(function() { - var newBedTemp = $("#temp_newBedTemp").val(); - $.ajax({ - url: AJAX_BASEURL + "control/temperature", - type: "POST", - dataType: "json", - data: { bedTemp: newBedTemp }, - success: function() {$("#temp_newBedTemp").val("")} - }) - }) $('#tabs a[data-toggle="tab"]').on('shown', function (e) { temperatureViewModel.updatePlot(); terminalViewModel.updateOutput(); }); - //~~ Terminal - - $("#terminal-send").click(function () { - var command = $("#terminal-command").val(); - - /* - var re = /^([gm][0-9]+)(\s.*)?/; - var commandMatch = command.match(re); - if (commandMatch != null) { - command = commandMatch[1].toUpperCase() + ((commandMatch[2] !== undefined) ? commandMatch[2] : ""); - } - */ - - if (command) { - $.ajax({ - url: AJAX_BASEURL + "control/command", - type: "POST", - dataType: "json", - contentType: "application/json; charset=UTF-8", - data: JSON.stringify({"command": command}) - }) - $("#terminal-command").val('') - } - - }) - - $("#terminal-command").keyup(function (event) { - if (event.keyCode == 13) { - $("#terminal-send").click() - } - }) - //~~ Gcode upload function gcode_upload_done(e, data) { diff --git a/octoprint/static/js/app/viewmodels/loginstate.js b/octoprint/static/js/app/viewmodels/loginstate.js index 7859665e..75882b68 100644 --- a/octoprint/static/js/app/viewmodels/loginstate.js +++ b/octoprint/static/js/app/viewmodels/loginstate.js @@ -18,6 +18,7 @@ function LoginStateViewModel() { self.subscribers = []; self.subscribe = function(callback) { + if (callback === undefined) return; self.subscribers.push(callback); } diff --git a/octoprint/static/js/app/viewmodels/temperature.js b/octoprint/static/js/app/viewmodels/temperature.js index 30b1a47d..7924cd4c 100644 --- a/octoprint/static/js/app/viewmodels/temperature.js +++ b/octoprint/static/js/app/viewmodels/temperature.js @@ -8,6 +8,9 @@ function TemperatureViewModel(loginStateViewModel, settingsViewModel) { self.targetTemp = ko.observable(undefined); self.bedTargetTemp = ko.observable(undefined); + self.newTemp = ko.observable(undefined); + self.newBedTemp = ko.observable(undefined); + self.isErrorOrClosed = ko.observable(undefined); self.isOperational = ko.observable(undefined); self.isPrinting = ko.observable(undefined); @@ -18,38 +21,6 @@ function TemperatureViewModel(loginStateViewModel, settingsViewModel) { self.temperature_profiles = settingsViewModel.temperature_profiles; - self.setTempFromProfile = function(profile) { - if (!profile) - return; - self.setTemp(profile.extruder); - } - - self.setTemp = function(temp) { - $.ajax({ - url: AJAX_BASEURL + "control/temperature", - type: "POST", - dataType: "json", - data: { temp: temp }, - success: function() {$("#temp_newTemp").val("")} - }) - }; - - self.setBedTempFromProfile = function(profile) { - if (!profile) - return; - self.setBedTemp(profile.bed); - } - - self.setBedTemp = function(bedTemp) { - $.ajax({ - url: AJAX_BASEURL + "control/temperature", - type: "POST", - dataType: "json", - data: { bedTemp: bedTemp }, - success: function() {$("#temp_newBedTemp").val("")} - }) - }; - self.tempString = ko.computed(function() { if (!self.temp()) return "-"; @@ -172,4 +143,45 @@ function TemperatureViewModel(loginStateViewModel, settingsViewModel) { ] $.plot($("#temperature-graph"), data, self.plotOptions); } + + self.setTempFromProfile = function(profile) { + if (!profile) + return; + self._updateTemperature(profile.extruder, "temp"); + } + + self.setTemp = function() { + self._updateTemperature(self.newTemp(), "temp", function(){self.targetTemp(self.newTemp()); self.newTemp("");}); + }; + + self.setBedTempFromProfile = function(profile) { + self._updateTemperature(profile.bed, "bedTemp"); + } + + self.setBedTemp = function() { + self._updateTemperature(self.newBedTemp(), "bedTemp", function() {self.bedTargetTemp(self.newBedTemp()); self.newBedTemp("");}); + }; + + self._updateTemperature = function(temp, type, callback) { + var data = {}; + data[type] = temp; + + $.ajax({ + url: AJAX_BASEURL + "control/temperature", + type: "POST", + dataType: "json", + data: data, + success: function() { if (callback !== undefined) callback(); } + }) + } + + self.handleEnter = function(event, type) { + if (event.keyCode == 13) { + if (type == "temp") { + self.setTemp(); + } else if (type == "bedTemp") { + self.setBedTemp(); + } + } + } } diff --git a/octoprint/static/js/app/viewmodels/terminal.js b/octoprint/static/js/app/viewmodels/terminal.js index 14c8ef0a..68947374 100644 --- a/octoprint/static/js/app/viewmodels/terminal.js +++ b/octoprint/static/js/app/viewmodels/terminal.js @@ -5,6 +5,8 @@ function TerminalViewModel(loginStateViewModel) { self.log = []; + self.command = ko.observable(undefined); + self.isErrorOrClosed = ko.observable(undefined); self.isOperational = ko.observable(undefined); self.isPrinting = ko.observable(undefined); @@ -80,4 +82,32 @@ function TerminalViewModel(loginStateViewModel) { container.scrollTop(container[0].scrollHeight - container.height()) } } + + self.sendCommand = function() { + /* + var re = /^([gm][0-9]+)(\s.*)?/; + var commandMatch = command.match(re); + if (commandMatch != null) { + command = commandMatch[1].toUpperCase() + ((commandMatch[2] !== undefined) ? commandMatch[2] : ""); + } + */ + + var command = self.command(); + if (command) { + $.ajax({ + url: AJAX_BASEURL + "control/command", + type: "POST", + dataType: "json", + contentType: "application/json; charset=UTF-8", + data: JSON.stringify({"command": command}) + }); + self.command(""); + } + } + + self.handleEnter = function(event) { + if (event.keyCode == 13) { + self.sendCommand(); + } + } } diff --git a/octoprint/templates/index.jinja2 b/octoprint/templates/index.jinja2 index 2bc71d28..6d077eef 100644 --- a/octoprint/templates/index.jinja2 +++ b/octoprint/templates/index.jinja2 @@ -259,11 +259,11 @@