Fixed #14, and some code cleanup

This commit is contained in:
Salandora 2018-03-05 21:29:31 +01:00
parent b684644bbe
commit 7c1dca0459
2 changed files with 61 additions and 60 deletions

View File

@ -12,11 +12,6 @@ import octoprint.plugin
class CustomControlPlugin(octoprint.plugin.SettingsPlugin, class CustomControlPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.TemplatePlugin, octoprint.plugin.TemplatePlugin,
octoprint.plugin.AssetPlugin): octoprint.plugin.AssetPlugin):
def get_settings_defaults(self):
return dict(
controls=[]
)
def get_template_configs(self): def get_template_configs(self):
if "editorcollection" in self._plugin_manager.enabled_plugins: if "editorcollection" in self._plugin_manager.enabled_plugins:
return [ return [
@ -28,9 +23,13 @@ class CustomControlPlugin(octoprint.plugin.SettingsPlugin,
dict(type="settings", template="customControl_hookedsettings.jinja2", custom_bindings=True) dict(type="settings", template="customControl_hookedsettings.jinja2", custom_bindings=True)
] ]
def on_settings_load(self):
return dict(
controls=settings().get(["controls"])
)
def on_settings_save(self, data): def on_settings_save(self, data):
s = settings() settings().set(["controls"], data["controls"])
s.set(["controls"], data["controls"])
def get_assets(self): def get_assets(self):
return dict( return dict(

View File

@ -32,7 +32,9 @@
}; };
self.onSettingsShown = function () { self.onSettingsShown = function () {
self.requestData(); OctoPrint.control.getCustomControls().done(function(response) {
self._fromResponse(response);
});
}; };
self.requestData = function () { self.requestData = function () {
@ -90,10 +92,10 @@
var target = ko.dataFor(this); var target = ko.dataFor(this);
var item = ko.dataFor(ui.item[0]); var item = ko.dataFor(ui.item[0]);
if (target == undefined) { if (target === undefined) {
return; return;
} else { } else {
if (target == self) { if (target === self) {
if (!item.hasOwnProperty("children")) { if (!item.hasOwnProperty("children")) {
return; return;
} }
@ -105,23 +107,23 @@
var position = ko.utils.arrayIndexOf(ui.item.parent().children(), ui.item[0]); var position = ko.utils.arrayIndexOf(ui.item.parent().children(), ui.item[0]);
if (position >= 0) { if (position >= 0) {
if (item.parent != undefined) { if (item.parent !== undefined) {
item.parent.children.remove(item); item.parent.children.remove(item);
if (target == self) if (target === self)
self.controlsFromServer.splice(position, 0, item); self.controlsFromServer.splice(position, 0, item);
else else
target.children.splice(position, 0, item); target.children.splice(position, 0, item);
} else { } else {
self.controlsFromServer = _.without(self.controlsFromServer, item); self.controlsFromServer = _.without(self.controlsFromServer, item);
if (target == self) if (target === self)
self.controlsFromServer.splice(position, 0, item); self.controlsFromServer.splice(position, 0, item);
else else
target.children.splice(position, 0, item); target.children.splice(position, 0, item);
} }
} }
}, },
stop: function(event, ui) { stop: function() {
self.rerenderControls(); self.rerenderControls();
} }
}).disableSelection(); }).disableSelection();
@ -152,19 +154,19 @@
return def; return def;
}; };
_.each(list, function (element, index, l) { _.each(list, function (element) {
var input = { var input = {
name: ko.observable(element.name), name: ko.observable(element.name),
parameter: ko.observable(element.parameter), parameter: ko.observable(element.parameter),
default: ko.observable(element.hasOwnProperty("default") ? element.default : undefined) default: ko.observable(element.hasOwnProperty("default") ? element.default : undefined)
} };
if (element.hasOwnProperty("slider") && _.isObject(element.slider)) { if (element.hasOwnProperty("slider") && _.isObject(element.slider)) {
input.slider = { input.slider = {
min: ko.observable(element.slider.min), min: ko.observable(element.slider.min),
max: ko.observable(element.slider.max), max: ko.observable(element.slider.max),
step: ko.observable(element.slider.step) step: ko.observable(element.slider.step)
} };
var defaultValue = attributeToInt(element, "default", attributeToInt(element.slider, "min", 0)); var defaultValue = attributeToInt(element, "default", attributeToInt(element.slider, "min", 0));
@ -185,7 +187,7 @@
}); });
return inputs; return inputs;
} };
self._processControl = function (parent, control) { self._processControl = function (parent, control) {
if (control.processed) { if (control.processed) {
control.id("settingsCustomControl_id" + self.staticID++); control.id("settingsCustomControl_id" + self.staticID++);
@ -215,7 +217,7 @@
if (control.hasOwnProperty("children")) { if (control.hasOwnProperty("children")) {
control.children = ko.observableArray(self._processControls(control, control.children)); control.children = ko.observableArray(self._processControls(control, control.children));
if (!control.hasOwnProperty("layout") || !(control.layout == "vertical" || control.layout == "horizontal" || control.layout == "horizontal_grid")) if (!control.hasOwnProperty("layout") || !(control.layout === "vertical" || control.layout === "horizontal" || control.layout === "horizontal_grid"))
control.layout = ko.observable("vertical"); control.layout = ko.observable("vertical");
else else
control.layout = ko.observable(control.layout); control.layout = ko.observable(control.layout);
@ -250,19 +252,19 @@
self.displayMode = function (customControl) { self.displayMode = function (customControl) {
if (customControl.hasOwnProperty("children")) { if (customControl.hasOwnProperty("children")) {
return (customControl.hasOwnProperty("name") && customControl.name() != "") ? "settingsCustomControls_containerTemplate_collapsable" : "settingsCustomControls_containerTemplate_nameless"; return (customControl.hasOwnProperty("name") && customControl.name() !== "") ? "settingsCustomControls_containerTemplate_collapsable" : "settingsCustomControls_containerTemplate_nameless";
} else { } else {
return "settingsCustomControls_controlTemplate"; return "settingsCustomControls_controlTemplate";
} }
} };
self.rowCss = function (customControl) { self.rowCss = function (customControl) {
var span = "span2"; var span = "span2";
var offset = ""; var offset = "";
if (customControl.hasOwnProperty("width") && customControl.width() != "") { if (customControl.hasOwnProperty("width") && customControl.width() !== "") {
span = "span" + customControl.width(); span = "span" + customControl.width();
} }
if (customControl.hasOwnProperty("offset") && customControl.offset() != "") { if (customControl.hasOwnProperty("offset") && customControl.offset() !== "") {
offset = "offset" + customControl.offset(); offset = "offset" + customControl.offset();
} }
return "sortable " + span + " " + offset; return "sortable " + span + " " + offset;
@ -271,21 +273,21 @@
self.searchElement = function (list, id) { self.searchElement = function (list, id) {
for (var i = 0; i < list.length; i++) for (var i = 0; i < list.length; i++)
{ {
if (list[i].id() == id) if (list[i].id() === id)
return list[i]; return list[i];
if (list[i].hasOwnProperty("children")) { if (list[i].hasOwnProperty("children")) {
var element = self.searchElement(list[i].children(), id); var element = self.searchElement(list[i].children(), id);
if (element != undefined) if (element !== undefined)
return element; return element;
} }
} }
return undefined; return undefined;
} };
self.createElement = function (invokedOn, contextParent, selectedMenu) { self.createElement = function (invokedOn, contextParent) {
if (contextParent.attr('id') == "base") { if (contextParent.attr('id') === "base") {
self.customControlDialogViewModel.reset(); self.customControlDialogViewModel.reset();
self.customControlDialogViewModel.show(function (ret) { self.customControlDialogViewModel.show(function (ret) {
@ -295,7 +297,7 @@
} }
else { else {
var parentElement = self.searchElement(self.controlsFromServer, contextParent.attr('id')); var parentElement = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
if (parentElement == undefined) { if (parentElement === undefined) {
self._showPopup({ self._showPopup({
title: gettext("Something went wrong while creating the new Element"), title: gettext("Something went wrong while creating the new Element"),
type: "error" type: "error"
@ -309,10 +311,10 @@
parentElement.children.push(self._processControl(parentElement, ret)); parentElement.children.push(self._processControl(parentElement, ret));
}); });
} }
} };
self.deleteElement = function (invokedOn, contextParent, selectedMenu) { self.deleteElement = function (invokedOn, contextParent) {
var element = self.searchElement(self.controlsFromServer, contextParent.attr('id')); var element = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
if (element == undefined) { if (element === undefined) {
self._showPopup({ self._showPopup({
title: gettext("Something went wrong while creating the new Element"), title: gettext("Something went wrong while creating the new Element"),
type: "error" type: "error"
@ -321,17 +323,17 @@
} }
showConfirmationDialog("", function (e) { showConfirmationDialog("", function (e) {
if (element.parent != undefined) if (element.parent !== undefined)
element.parent.children.remove(element); element.parent.children.remove(element);
else { else {
self.controlsFromServer = _.without(self.controlsFromServer, element); self.controlsFromServer = _.without(self.controlsFromServer, element);
self.rerenderControls(); self.rerenderControls();
} }
}); });
} };
self.editElement = function (invokedOn, contextParent, selectedMenu) { self.editElement = function (invokedOn, contextParent) {
var element = self.element = self.searchElement(self.controlsFromServer, contextParent.attr('id')); var element = self.element = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
if (element == undefined) { if (element === undefined) {
self._showPopup({ self._showPopup({
title: gettext("Something went wrong while creating the new Element"), title: gettext("Something went wrong while creating the new Element"),
type: "error" type: "error"
@ -342,7 +344,7 @@
var title = "Edit Container"; var title = "Edit Container";
var type = "container"; var type = "container";
var data = { var data = {
parent: element.parent, parent: element.parent
}; };
if (element.hasOwnProperty("name")) { if (element.hasOwnProperty("name")) {
@ -425,21 +427,21 @@
case "command": { case "command": {
element.name(ret.name); element.name(ret.name);
if (ret.command != undefined) { if (ret.command !== undefined) {
element.command = ret.command; element.command = ret.command;
delete element.commands; delete element.commands;
} }
if (ret.commands != undefined) { if (ret.commands !== undefined) {
element.commands = ret.commands; element.commands = ret.commands;
delete element.command; delete element.command;
} }
if (ret.confirm != "") { if (ret.confirm !== "") {
element.confirm = ret.confirm; element.confirm = ret.confirm;
} }
if (ret.input != undefined) { if (ret.input !== undefined) {
_.each(ret.input, function (element, index, list) { _.each(ret.input, function (element, index) {
data.input[index] = ko.mapping.toJS(element); data.input[index] = ko.mapping.toJS(element);
}); });
@ -480,11 +482,11 @@
element.name(ret.name); element.name(ret.name);
element.script = ret.script; element.script = ret.script;
if (ret.confirm != "") { if (ret.confirm !== "") {
element.confirm = ret.confirm; element.confirm = ret.confirm;
} }
if (ret.input != undefined) { if (ret.input !== undefined) {
element.input(self._processInput(ret.input)); element.input(self._processInput(ret.input));
} }
else else
@ -500,15 +502,15 @@
} }
} }
if (element.parent && element.parent.layout() == "horizontal_grid") { if (element.parent && element.parent.layout() === "horizontal_grid") {
if (ret.width != undefined && ret.width != "") if (ret.width !== undefined && ret.width !== "")
element.width(ret.width); element.width(ret.width);
if (ret.offset != undefined && ret.offset != "") if (ret.offset !== undefined && ret.offset !== "")
element.offset(ret.offset); element.offset(ret.offset);
} }
}); });
} };
self.controlContextMenu = function (invokedOn, contextParent, selectedMenu) self.controlContextMenu = function (invokedOn, contextParent, selectedMenu)
{ {
@ -551,19 +553,19 @@
break; break;
} }
} }
} };
self.editStyle = function (type) { self.editStyle = function (type) {
} };
self.recursiveDeleteProperties = function (list) { self.recursiveDeleteProperties = function (list) {
_.each(list, function (element, index, ll) { _.each(list, function (element) {
if (!element.parent || (element.parent.hasOwnProperty("layout") && element.parent.layout() != "horizontal_grid")) { if (!element.parent || (element.parent.hasOwnProperty("layout") && element.parent.layout() !== "horizontal_grid")) {
delete element.width; delete element.width;
delete element.offset; delete element.offset;
} }
if (element.default == "") if (element.default === "")
delete element.default; delete element.default;
delete element.id; delete element.id;
@ -576,19 +578,19 @@
if (element.hasOwnProperty("input")) { if (element.hasOwnProperty("input")) {
_.each(element.input(), function (e, i, l) { _.each(element.input(), function (e, i, l) {
if (e.default == "") if (e.default === "")
delete e.default; delete e.default;
delete e.value; delete e.value;
}); });
} }
if (element.hasOwnProperty("width") && element.width() == "") if (element.hasOwnProperty("width") && element.width() === "")
delete element.width; delete element.width;
if (element.hasOwnProperty("offset") && element.offset() == "") if (element.hasOwnProperty("offset") && element.offset() === "")
delete element.offset; delete element.offset;
if (!element.hasOwnProperty("name") || element.name() == "") { if (!element.hasOwnProperty("name") || element.name() === "") {
delete element.name; delete element.name;
delete element.collapsed; delete element.collapsed;
} }
@ -601,13 +603,13 @@
self.recursiveDeleteProperties(element.children()); self.recursiveDeleteProperties(element.children());
} }
}); });
} };
self.onSettingsBeforeSave = function () { self.onSettingsBeforeSave = function () {
self.recursiveDeleteProperties(self.controlsFromServer); self.recursiveDeleteProperties(self.controlsFromServer);
self.settingsViewModel.settings.plugins.customControl.controls = self.controlsFromServer; self.settingsViewModel.settings.plugins.customControl.controls = self.controlsFromServer;
} };
self.onEventSettingsUpdated = function (payload) { self.onEventSettingsUpdated = function () {
self.requestData(); self.requestData();
} }
} }