diff --git a/octoprint_customControl/static/js/customControl.js b/octoprint_customControl/static/js/customControl.js index 41eb88f..f140d70 100644 --- a/octoprint_customControl/static/js/customControl.js +++ b/octoprint_customControl/static/js/customControl.js @@ -66,30 +66,34 @@ return controls; }; - self._processInput = function (control) { - for (var i = 0; i < control.input.length; i++) { - if (!control.processed) { - control.input[i].value = ko.observable(control.input[i].default); - control.input[i].default = ko.observable(control.input[i].default); - } + self._processInput = function (list) { + var inputs = []; - if (control.processed) - control.input[i].value(control.input[i].default()); + for (var i = 0; i < list.length; i++) { + var input = { + name: ko.observable(list[i].name), + parameter: ko.observable(list[i].parameter), + defaultValue: ko.observable(list[i].defaultValue) + }; - if (!control.input[i].hasOwnProperty("slider")) - control.input[i].slider = ko.observable(false); - else if (!control.processed) - control.input[i].slider = ko.observable(control.input[i].slider); + if (list[i].hasOwnProperty("slider") && typeof list[i].slider == "object") + input.slider = ko.mapping.fromJS(list[i].slider); + else + input.slider = false; + + inputs.push(input); } + + return inputs; } self._processControl = function (parent, control) { control.id = ko.observable("settingsCustomControl_id" + self.staticID++); control.parent = parent; - if (control.hasOwnProperty("template") && control.hasOwnProperty("regex") && control.hasOwnProperty("deflt")) { + if (control.hasOwnProperty("template") && control.hasOwnProperty("regex") && control.hasOwnProperty("defaultValue")) { control.template = ko.observable(control.template); control.regex = ko.observable(control.regex); - control.deflt = ko.observable(control.deflt); + control.defaultValue = ko.observable(control.defaultValue); } if (control.hasOwnProperty("children")) { @@ -130,7 +134,10 @@ } if (control.hasOwnProperty("input")) { - self._processInput(control); + if (control.processed) + control.input(self._processInput(control.input())); + else + control.input = ko.observableArray(self._processInput(control.input)); } var js; @@ -198,6 +205,8 @@ self.createElement = function (invokedOn, contextParent, selectedMenu) { if (invokedOn.attr('id') == "base") { + self.customControlDialogViewModel.reset(); + self.customControlDialogViewModel.show(function (ret) { self.controlsFromServer.push(ret); self.rerenderControls(); @@ -213,6 +222,8 @@ return; } + self.customControlDialogViewModel.reset({ parent: parentElement }); + self.customControlDialogViewModel.show(function (ret) { parentElement.children.push(self._processControl(parentElement, ret)); }); @@ -286,8 +297,8 @@ } if (element.hasOwnProperty("regex")) data.regex = element.regex(); - if (element.hasOwnProperty("deflt")) - data.deflt = element.deflt(); + if (element.hasOwnProperty("defaultValue")) + data.defaultValue = element.defaultValue(); if (element.hasOwnProperty("width")) data.width = element.width(); @@ -347,7 +358,6 @@ delete element.command; delete element.commands; - delete element.input; if (ret.command != undefined) element.command = ret.command; @@ -355,9 +365,10 @@ element.commands = ret.commands; if (ret.input != undefined) { - element.input = ret.input; - self._processInput(element); + element.input(self._processInput(ret.input)); } + else + delete element.input; break; } case "output": { @@ -382,7 +393,6 @@ { switch (selectedMenu.attr('cmd')) { case "createContainer": { - self.customControlDialogViewModel.reset(); self.customControlDialogViewModel.title(gettext("Create container")); self.customControlDialogViewModel.type("container"); @@ -390,7 +400,6 @@ break; } case "createCommand": { - self.customControlDialogViewModel.reset(); self.customControlDialogViewModel.title(gettext("Create Command")); self.customControlDialogViewModel.type("command"); @@ -398,7 +407,6 @@ break; } case "createOutput": { - self.customControlDialogViewModel.reset(); self.customControlDialogViewModel.title(gettext("Create Output")); self.customControlDialogViewModel.type("output"); diff --git a/octoprint_customControl/static/js/customControlDialog.js b/octoprint_customControl/static/js/customControlDialog.js index 2b7f395..c5c7c2f 100644 --- a/octoprint_customControl/static/js/customControlDialog.js +++ b/octoprint_customControl/static/js/customControlDialog.js @@ -26,10 +26,14 @@ if (self.element() == undefined || self.element().input == undefined) return false; - _.each(self.element().input(), function (element, index, list) { - if (element.hasOwnProperty("slider")) - return true; - }); + var inputs = self.element().input() + for(var i = 0; i < inputs.length; i++) + { + if (inputs[i].hasOwnProperty("slider")) { + if (typeof inputs[i].slider == "object") + return true; + } + } return false; }); self.span = function(parameter) { @@ -54,7 +58,7 @@ name: undefined, collapsable: true, commands: "", - deflt: "", + defaultValue: "", script: "", javascript: "", enabled: "", @@ -71,12 +75,21 @@ if (typeof data == "object") element = _.extend(element, data); - self.element(ko.mapping.fromJS(element)); - } + var mapped = ko.mapping.fromJS(element); + if (data.hasOwnProperty("input")) { + self.useInputs(true); + //_.each(mapped.input(), function (e, index, list) { + // if (e.hasOwnProperty("slider") && !$.isFunction(e.slider)) + // e.slider = ko.observable(e.slider); + //}); + } + + self.element(mapped); + } self.show = function (f) { var dialog = $("#customControlDialog"); - var primarybtn = $('.btn-primary', dialog); + var primarybtn = $('div.modal-footer .btn-primary', dialog); primarybtn.unbind('click').bind('click', function (e) { var obj = ko.mapping.toJS(self.element()); @@ -112,9 +125,9 @@ var input = { name: element.name, parameter: element.parameter, - default: element.default + defaultValue: element.defaultValue }; - if (element.hasOwnProperty("slider")) { + if (element.hasOwnProperty("slider") && element.slider != false) { input["slider"] = { }; @@ -125,6 +138,8 @@ if (element.slider.hasOwnProperty("step") && element.slider.step != "") input.slider.step = element.slider.step; } + + el.input.push(input); }); } break; @@ -132,7 +147,7 @@ case "output": { el.template = obj.template; el.regex = obj.regex; - el.deflt = obj.deflt; + el.defaultValue = obj.defaultValue; el.width = obj.width; el.offset = obj.offset; @@ -149,6 +164,34 @@ keyboard: false }); } + + self.removeInput = function (data) { + self.element().input.remove(data); + } + self.addInput = function () { + var obj = { + name: ko.observable(""), + parameter: ko.observable(""), + defaultValue: ko.observable(""), + slider: false + } + + self.element().input.push(obj); + } + self.addSliderInput = function () { + var obj = { + name: ko.observable(""), + parameter: ko.observable(""), + defaultValue: ko.observable(""), + slider: { + min: ko.observable(""), + max: ko.observable(""), + step: ko.observable("") + } + } + + self.element().input.push(obj); + } } // view model class, parameters for constructor, container to bind to diff --git a/octoprint_customControl/templates/octoprint_customControl.jinja2 b/octoprint_customControl/templates/octoprint_customControl.jinja2 index 117f708..b807670 100644 --- a/octoprint_customControl/templates/octoprint_customControl.jinja2 +++ b/octoprint_customControl/templates/octoprint_customControl.jinja2 @@ -16,7 +16,7 @@
- +
@@ -35,7 +35,7 @@
- +
@@ -57,16 +57,18 @@
- -
-
-
+ +
+
+
+
-
+
-
+
+
@@ -88,7 +90,7 @@
- +
diff --git a/octoprint_customControl/templates/octoprint_customControl_settings.jinja2 b/octoprint_customControl/templates/octoprint_customControl_settings.jinja2 index 939636e..eecb7ce 100644 --- a/octoprint_customControl/templates/octoprint_customControl_settings.jinja2 +++ b/octoprint_customControl/templates/octoprint_customControl_settings.jinja2 @@ -2,8 +2,9 @@
- - + + + -