octoprint-customControl/octoprint_customControl/static/js/customControl.js

475 lines
19 KiB
JavaScript
Raw Normal View History

2015-04-15 23:41:05 +02:00
$(function () {
function CustomControlViewModel(parameters) {
var self = this;
self.loginState = parameters[0];
self.settingsViewModel = parameters[1];
self.controlViewModel = parameters[2];
2015-04-19 21:19:54 +02:00
self.customControlDialogViewModel = parameters[3];
2015-04-18 11:45:57 +02:00
self.popup = undefined;
2015-04-15 23:41:05 +02:00
self.controls = ko.observableArray([]);
self.controlsFromServer = [];
self.additionalControls = [];
2015-04-19 21:19:54 +02:00
self.staticID = 0;
2015-04-18 11:45:57 +02:00
self._showPopup = function (options, eventListeners) {
if (self.popup !== undefined) {
self.popup.remove();
2015-04-19 21:19:54 +02:00
}
self.popup = new PNotify(options);
2015-04-18 11:45:57 +02:00
if (eventListeners) {
2015-04-19 21:19:54 +02:00
var popupObj = self.popup.get();
2015-04-18 11:45:57 +02:00
_.each(eventListeners, function (value, key) {
popupObj.on(key, value);
})
}
};
2015-04-15 23:41:05 +02:00
2015-04-18 11:45:57 +02:00
self.onStartup = function () {
self.requestData();
2015-04-15 23:41:05 +02:00
};
2015-04-18 11:45:57 +02:00
self.requestData = function () {
$.ajax({
url: API_BASEURL + "printer/command/custom",
method: "GET",
dataType: "json",
success: function (response) {
self._fromResponse(response);
2015-04-15 23:41:05 +02:00
}
});
2015-04-18 11:45:57 +02:00
};
self._fromResponse = function (response) {
self.controlsFromServer = response.controls;
self.rerenderControls();
2015-04-15 23:41:05 +02:00
};
self.rerenderControls = function () {
2015-04-18 13:12:06 +02:00
// TODO: Brainstorming about how to handle additionalControls...
2015-04-15 23:41:05 +02:00
self.staticID = 0;
2015-04-18 11:45:57 +02:00
self.controls(self._processControls(undefined, self.controlsFromServer))
2015-04-15 23:41:05 +02:00
};
2015-04-18 11:45:57 +02:00
self._processControls = function (parent, controls) {
2015-04-15 23:41:05 +02:00
for (var i = 0; i < controls.length; i++) {
2015-04-18 11:45:57 +02:00
controls[i] = self._processControl(parent, controls[i]);
2015-04-15 23:41:05 +02:00
}
return controls;
};
2015-04-22 09:30:04 +02:00
self._processInput = function (list) {
var inputs = [];
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 (list[i].hasOwnProperty("slider") && typeof list[i].slider == "object")
input.slider = ko.mapping.fromJS(list[i].slider);
else
input.slider = false;
2015-04-19 21:19:54 +02:00
2015-04-22 09:30:04 +02:00
inputs.push(input);
2015-04-19 21:19:54 +02:00
}
2015-04-22 09:30:04 +02:00
return inputs;
2015-04-19 21:19:54 +02:00
}
2015-04-18 11:45:57 +02:00
self._processControl = function (parent, control) {
control.id = ko.observable("settingsCustomControl_id" + self.staticID++);
control.parent = parent;
2015-04-22 09:30:04 +02:00
if (control.hasOwnProperty("template") && control.hasOwnProperty("regex") && control.hasOwnProperty("defaultValue")) {
control.template = ko.observable(control.template);
control.regex = ko.observable(control.regex);
2015-04-22 09:30:04 +02:00
control.defaultValue = ko.observable(control.defaultValue);
2015-04-19 21:19:54 +02:00
}
2015-04-15 23:41:05 +02:00
if (control.hasOwnProperty("children")) {
2015-04-18 11:45:57 +02:00
if (control.processed) {
control.children(self._processControls(control, control.children()));
if (control.hasOwnProperty("layout") && !(control.layout() == "vertical" || control.layout() == "horizontal" || control.layout() == "horizontal_grid"))
control.layout("vertical");
if (control.hasOwnProperty("name") && control.name() != "") {
if (!control.hasOwnProperty("collapsable"))
control.collapsable = ko.observable(false);
}
2015-04-18 11:45:57 +02:00
}
else {
control.children = ko.observableArray(self._processControls(control, control.children));
if (!control.hasOwnProperty("layout") || !(control.layout == "vertical" || control.layout == "horizontal" || control.layout == "horizontal_grid"))
control.layout = ko.observable("vertical");
else
control.layout = ko.observable(control.layout);
if (control.hasOwnProperty("name") && control.name != "") {
if (control.hasOwnProperty("collapsable"))
control.collapsable = ko.observable(control.collapsable);
else
control.collapsable = ko.observable(false);
}
2015-04-15 23:41:05 +02:00
}
}
2015-04-19 21:19:54 +02:00
if (!control.processed) {
if (control.hasOwnProperty("name"))
control.name = ko.observable(control.name);
else
control.name = ko.observable("");
2015-04-15 23:41:05 +02:00
2015-04-19 21:19:54 +02:00
control.width = ko.observable(control.hasOwnProperty("width") ? control.width : "2");
control.offset = ko.observable(control.hasOwnProperty("offset") ? control.offset : "");
}
2015-04-18 11:45:57 +02:00
2015-04-19 21:19:54 +02:00
if (control.hasOwnProperty("input")) {
2015-04-22 09:30:04 +02:00
if (control.processed)
control.input(self._processInput(control.input()));
else
control.input = ko.observableArray(self._processInput(control.input));
2015-04-15 23:41:05 +02:00
}
var js;
if (control.hasOwnProperty("javascript")) {
js = control.javascript;
// if js is a function everything's fine already, but if it's a string we need to eval that first
2015-04-18 11:45:57 +02:00
/*if (!_.isFunction(js)) {
2015-04-15 23:41:05 +02:00
control.javascript = function (data) {
eval(js);
};
2015-04-18 11:45:57 +02:00
}*/
2015-04-15 23:41:05 +02:00
}
if (control.hasOwnProperty("enabled")) {
js = control.enabled;
// if js is a function everything's fine already, but if it's a string we need to eval that first
2015-04-18 11:45:57 +02:00
/*if (!_.isFunction(js)) {
2015-04-15 23:41:05 +02:00
control.enabled = function (data) {
return eval(js);
}
2015-04-18 11:45:57 +02:00
}*/
2015-04-15 23:41:05 +02:00
}
control.processed = true;
return control;
};
self.displayMode = function (customControl) {
if (customControl.hasOwnProperty("children")) {
return (customControl.hasOwnProperty("name") && customControl.name() != "") ? "settingsCustomControls_containerTemplate_accordion" : "settingsCustomControls_containerTemplate";
2015-04-15 23:41:05 +02:00
} else {
return "settingsCustomControls_controlTemplate";
}
}
2015-04-19 21:19:54 +02:00
2015-04-18 11:45:57 +02:00
self.rowCss = function (customControl) {
2015-04-19 21:19:54 +02:00
var span = "span2";
var offset = "";
2015-04-18 11:45:57 +02:00
if (customControl.hasOwnProperty("width") && customControl.width() != "") {
span = "span" + customControl.width();
2015-04-19 21:19:54 +02:00
}
2015-04-18 11:45:57 +02:00
if (customControl.hasOwnProperty("offset") && customControl.offset() != "") {
offset = "offset" + customControl.offset();
2015-04-19 21:19:54 +02:00
}
2015-04-18 11:45:57 +02:00
return span + " " + offset;
};
2015-04-15 23:41:05 +02:00
self.searchElement = function (list, id) {
for (var i = 0; i < list.length; i++)
{
2015-04-18 11:45:57 +02:00
if (list[i].id() == id)
2015-04-15 23:41:05 +02:00
return list[i];
if (list[i].hasOwnProperty("children")) {
var element = self.searchElement(list[i].children(), id);
if (element != undefined)
return element;
}
}
return undefined;
}
2015-04-19 21:19:54 +02:00
self.createElement = function (invokedOn, contextParent, selectedMenu) {
if (invokedOn.attr('id') == "base") {
2015-04-22 09:30:04 +02:00
self.customControlDialogViewModel.reset();
2015-04-19 21:19:54 +02:00
self.customControlDialogViewModel.show(function (ret) {
self.controlsFromServer.push(ret);
self.rerenderControls();
});
}
else {
var parentElement = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
if (parentElement == undefined) {
self._showPopup({
title: gettext("Something went wrong while creating the new Element"),
type: "error"
});
return;
}
2015-04-22 09:30:04 +02:00
self.customControlDialogViewModel.reset({ parent: parentElement });
2015-04-19 21:19:54 +02:00
self.customControlDialogViewModel.show(function (ret) {
parentElement.children.push(self._processControl(parentElement, ret));
});
}
}
self.deleteElement = function (invokedOn, contextParent, selectedMenu) {
var element = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
if (element == undefined) {
self._showPopup({
title: gettext("Something went wrong while creating the new Element"),
type: "error"
});
return;
}
showConfirmationDialog("", function (e) {
if (element.parent != undefined)
element.parent.children.remove(element);
else {
self.controlsFromServer = _.without(self.controlsFromServer, element);
self.rerenderControls();
}
});
}
self.editElement = function (invokedOn, contextParent, selectedMenu) {
var element = self.element = self.searchElement(self.controlsFromServer, contextParent.attr('id'));
2015-04-19 21:19:54 +02:00
if (element == undefined) {
self._showPopup({
title: gettext("Something went wrong while creating the new Element"),
type: "error"
});
return;
}
var title = "Edit Container";
var type = "container";
var data = {
parent: element.parent,
};
if (element.hasOwnProperty("name")) {
2015-04-19 21:19:54 +02:00
data.name = element.name();
data.collapsable = element.hasOwnProperty("collapsable") ? element.collapsable() : false;
}
2015-04-19 21:19:54 +02:00
if (element.hasOwnProperty("layout")) {
data.layout = element.layout();
title = "Edit Container";
type = "container";
}
if (element.hasOwnProperty("command")) {
data.commands = element.command;
title = "Edit Command";
type = "command";
}
if (element.hasOwnProperty("commands")) {
data.commands = element.commands;
title = "Edit Command";
type = "command";
}
if (element.hasOwnProperty("input"))
{
data.input = [];
_.each(element.input(), function (element, index, list) {
data.input[index] = ko.mapping.toJS(element);
});
}
if (element.hasOwnProperty("template")) {
data.template = element.template();
title = "Edit Output";
type = "output";
}
if (element.hasOwnProperty("regex"))
data.regex = element.regex();
2015-04-22 09:30:04 +02:00
if (element.hasOwnProperty("defaultValue"))
data.defaultValue = element.defaultValue();
2015-04-19 21:19:54 +02:00
if (element.hasOwnProperty("width"))
data.width = element.width();
if (element.hasOwnProperty("offset"))
data.offset = element.offset();
self.customControlDialogViewModel.reset(data);
self.customControlDialogViewModel.title(gettext(title));
self.customControlDialogViewModel.type(type);
self.customControlDialogViewModel.show(function (ret) {
var element = self.element;
2015-04-19 21:19:54 +02:00
switch (self.customControlDialogViewModel.type()) {
case "container": {
if (ret.hasOwnProperty("name"))
element.name(ret.name);
2015-04-19 21:19:54 +02:00
element.layout(ret.layout);
if (ret.hasOwnProperty("collapsable") != "") {
if (element.hasOwnProperty("collapsable")) {
if (!ret.collapsable) {
var e = $('#toggle_' + element.id());
if (e)
e.height("auto");
e = $('#' + element.id() + ' div.accordion-heading a h1');
if (e)
e.removeClass('icon-caret-right');
}
element.collapsable(ret.collapsable);
}
else
element.collapsable = ko.observable(ret.collapsable);
}
else {
if (element.hasOwnProperty("collapsable")) {
element.collapsable(false);
delete element.collapsable;
}
var e = $('#toggle_' + element.id());
if (e)
e.height("auto");
e = $('#' + element.id() + ' div.accordion-heading a h1');
if (e)
e.removeClass('icon-caret-right');
}
break;
2015-04-19 21:19:54 +02:00
}
case "command": {
if (ret.hasOwnProperty("name"))
element.name(ret.name);
delete element.command;
delete element.commands;
if (ret.command != undefined)
element.command = ret.command;
if (ret.commands != undefined)
element.commands = ret.commands;
if (ret.input != undefined) {
2015-04-22 09:30:04 +02:00
element.input(self._processInput(ret.input));
2015-04-19 21:19:54 +02:00
}
2015-04-22 09:30:04 +02:00
else
delete element.input;
2015-04-19 21:19:54 +02:00
break;
}
case "output": {
element.template(ret.template);
element.regex(ret.regex);
element.deflt(ret.deflt);
break;
2015-04-19 21:19:54 +02:00
}
}
if (element.parent && element.parent.layout() == "horizontal_grid") {
2015-04-19 21:19:54 +02:00
if (ret.width != undefined && ret.width != "")
element.width(ret.width);
if (ret.offset != undefined && ret.offset != "")
element.offset(ret.offset);
}
});
}
2015-04-18 11:45:57 +02:00
self.controlContextMenu = function (invokedOn, contextParent, selectedMenu)
2015-04-15 23:41:05 +02:00
{
2015-04-18 11:45:57 +02:00
switch (selectedMenu.attr('cmd')) {
case "createContainer": {
2015-04-19 21:19:54 +02:00
self.customControlDialogViewModel.title(gettext("Create container"));
self.customControlDialogViewModel.type("container");
2015-04-18 11:45:57 +02:00
2015-04-19 21:19:54 +02:00
self.createElement(invokedOn, contextParent, selectedMenu);
2015-04-18 11:45:57 +02:00
break;
2015-04-15 23:41:05 +02:00
}
2015-04-19 21:19:54 +02:00
case "createCommand": {
self.customControlDialogViewModel.title(gettext("Create Command"));
self.customControlDialogViewModel.type("command");
2015-04-18 11:45:57 +02:00
2015-04-19 21:19:54 +02:00
self.createElement(invokedOn, contextParent, selectedMenu);
2015-04-18 11:45:57 +02:00
break;
}
2015-04-19 21:19:54 +02:00
case "createOutput": {
self.customControlDialogViewModel.title(gettext("Create Output"));
self.customControlDialogViewModel.type("output");
2015-04-18 11:45:57 +02:00
2015-04-19 21:19:54 +02:00
self.createElement(invokedOn, contextParent, selectedMenu);
break;
}
case "deleteElement": {
self.deleteElement(invokedOn, contextParent, selectedMenu);
break;
}
case "editElement": {
self.editElement(invokedOn, contextParent, selectedMenu);
break;
2015-04-15 23:41:05 +02:00
}
}
}
self.editStyle = function (type) {
}
2015-04-18 11:45:57 +02:00
self.recursiveDeleteProperties = function (list) {
for (var i = 0; i < list.length; i++) {
if (!list[i].parent || (list[i].parent.hasOwnProperty("layout") && list[i].parent.layout() != "horizontal_grid"))
2015-04-18 11:45:57 +02:00
{
delete list[i].width;
delete list[i].offset;
}
delete list[i].id;
delete list[i].parent;
delete list[i].processed;
2015-04-19 21:19:54 +02:00
delete list[i].output;
delete list[i].key;
delete list[i].template_key;
if (list[i].hasOwnProperty("width") && list[i].width() == "")
delete list[i].width;
if (list[i].hasOwnProperty("offset") && list[i].offset() == "")
delete list[i].offset;
if (!list[i].hasOwnProperty("name") || list[i].name() == "") {
delete list[i].name;
delete list[i].collapsable;
}
2015-04-18 11:45:57 +02:00
if (list[i].hasOwnProperty("children"))
self.recursiveDeleteProperties(list[i].children());
}
}
self.onSettingsBeforeSave = function () {
self.recursiveDeleteProperties(self.controlsFromServer);
self.settingsViewModel.settings.plugins.octoprint_customControl.controls = self.controlsFromServer;
}
2015-04-18 13:12:06 +02:00
self.onEventSettingsUpdated = function (payload) {
2015-04-18 11:45:57 +02:00
self.requestData();
}
2015-04-15 23:41:05 +02:00
}
// view model class, parameters for constructor, container to bind to
OCTOPRINT_VIEWMODELS.push([
CustomControlViewModel,
2015-04-19 21:19:54 +02:00
["loginStateViewModel", "settingsViewModel", "controlViewModel", "customControlDialogViewModel"],
2015-04-18 11:45:57 +02:00
"#settings_plugin_octoprint_customControl"
2015-04-15 23:41:05 +02:00
]);
});