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

261 lines
10 KiB
JavaScript
Raw Normal View History

2015-04-19 21:19:54 +02:00
$(function () {
function customControlDialogViewModel(parameters) {
var self = this;
self.element = ko.observable();
2015-04-19 21:19:54 +02:00
self.title = ko.observable(gettext("Create Container"));
self.type = ko.observable("container");
self.useInputs = ko.observable(false);
self.useConfirm = ko.observable(false);
self.useOutput = ko.observable(false);
2015-04-19 21:19:54 +02:00
self.useJavaScript = ko.observable(false);
self.useEnabled = ko.observable(false);
self.layouts = ko.observableArray([
{ name: gettext("Vertical"), key: "vertical" },
{ name: gettext("Horizontal"), key: "horizontal" },
{ name: gettext("Horizontal grid"), key: "horizontal_grid" }
]);
self.types = ko.observableArray([
{ name: gettext("Container"), key: "container" },
{ name: gettext("Command"), key: "command" },
{ name: gettext("Script"), key: "script" },
{ name: gettext("Output"), key: "output" },
2015-04-19 21:19:54 +02:00
]);
self.hasSlider = ko.computed(function () {
if (self.element() == undefined || self.element().input == undefined)
return false;
2015-04-22 09:30:04 +02:00
var inputs = self.element().input()
for(var i = 0; i < inputs.length; i++)
2015-04-22 09:30:04 +02:00
{
if (inputs[i].hasOwnProperty("slider")) {
if (typeof inputs[i].slider == "object")
return true;
}
}
2015-04-19 21:19:54 +02:00
return false;
});
self.span = function(parameter) {
return ko.computed(function () {
if (self.hasSlider())
return "span2";
switch (parameter) {
case "name":
case "parameter":
return "span4";
case "default":
return "span3";
}
return "span2";
});
}
self.reset = function (data) {
var element = {
name: undefined,
2018-02-22 09:09:59 +01:00
cusotmClass: "",
collapsed: false,
2015-04-19 21:19:54 +02:00
commands: "",
confirm: "",
2015-04-22 09:30:04 +02:00
defaultValue: "",
2015-04-19 21:19:54 +02:00
script: "",
javascript: "",
enabled: "",
input: [],
layout: "vertical",
regex: "",
template: "",
confirm: "",
width: "2",
offset: "",
parent: undefined
};
if (typeof data == "object") {
2015-04-19 21:19:54 +02:00
element = _.extend(element, data);
self.useConfirm(data.hasOwnProperty("confirm"));
self.useInputs(data.hasOwnProperty("input"));
self.useOutput(data.hasOwnProperty("template"));
2015-04-22 09:30:04 +02:00
}
self.element(ko.mapping.fromJS(element));
2015-04-22 09:30:04 +02:00
}
2015-04-19 21:19:54 +02:00
self.show = function (f) {
var dialog = $("#customControlDialog");
2015-04-22 09:30:04 +02:00
var primarybtn = $('div.modal-footer .btn-primary', dialog);
2015-04-19 21:19:54 +02:00
primarybtn.unbind('click').bind('click', function (e) {
var obj = ko.mapping.toJS(self.element());
var el = {};
switch (self.type()) {
case "container": {
el.name = obj.name;
el.layout = obj.layout;
el.collapsed = obj.collapsed;
2015-04-19 21:19:54 +02:00
el.children = [];
break;
}
case "command": {
el.name = obj.name;
2018-02-22 09:09:59 +01:00
el.customClass = "btn";
2015-04-19 21:19:54 +02:00
if (obj.commands.indexOf('\n') == -1)
el.command = obj.commands;
else
el.commands = obj.commands.split('\n');
2015-04-19 21:19:54 +02:00
if (self.useConfirm()) {
el.confirm = obj.confirm;
}
2015-04-19 21:19:54 +02:00
if (self.useInputs()) {
var attributeToInt = function (obj, key, def) {
if (obj.hasOwnProperty(key)) {
var val = obj[key];
if (_.isNumber(val)) {
return val;
}
var parsedVal = parseInt(val);
if (!isNaN(parsedVal)) {
return parsedVal;
}
}
return def;
};
2015-04-19 21:19:54 +02:00
el.input = [];
_.each(obj.input, function (element, index, list) {
var input = {
name: element.name,
parameter: element.parameter,
default: element.defaultValue
2015-04-19 21:19:54 +02:00
};
2015-04-22 09:30:04 +02:00
if (element.hasOwnProperty("slider") && element.slider != false) {
2015-04-19 21:19:54 +02:00
input["slider"] = {
};
input.default = attributeToInt(element, "defaultValue", undefined);
2015-04-26 22:42:43 +02:00
2015-04-19 21:19:54 +02:00
if (element.slider.hasOwnProperty("min") && element.slider.min != "")
input.slider.min = element.slider.min;
if (element.slider.hasOwnProperty("max") && element.slider.max != "")
input.slider.max = element.slider.max;
if (element.slider.hasOwnProperty("step") && element.slider.step != "")
input.slider.step = element.slider.step;
}
2015-04-22 09:30:04 +02:00
el.input.push(input);
2015-04-19 21:19:54 +02:00
});
}
if (self.useOutput()) {
el.template = obj.template;
el.regex = obj.regex;
el.default = obj.defaultValue;
}
2015-04-19 21:19:54 +02:00
break;
}
case "script":
{
el.name = obj.name;
2018-02-22 09:09:59 +01:00
el.customClass = "btn";
el.script = obj.script;
if (self.useConfirm()) {
el.confirm = obj.confirm;
}
if (self.useInputs()) {
el.input = [];
_.each(obj.input, function (element, index, list) {
var input = {
name: element.name,
parameter: element.parameter,
2015-04-26 22:42:43 +02:00
defaultValue: !isNaN(element.defaultValue) ? element.defaultValue : undefined
};
if (element.hasOwnProperty("slider") && element.slider != false) {
input["slider"] = {
};
2015-04-26 22:42:43 +02:00
input.defaultValue = !isNaN(element.defaultValue) && element.defaultValue != undefined && element.defaultValue != "" ? parseInt(element.defaultValue) : undefined;
if (element.slider.min != "")
input.slider.min = parseInt(element.slider.min);
if (element.slider.max != "")
input.slider.max = parseInt(element.slider.max);
if (element.slider.step != "")
input.slider.step = parseInt(element.slider.step);
}
el.input.push(input);
});
}
break;
}
2015-04-19 21:19:54 +02:00
case "output": {
el.template = obj.template;
el.regex = obj.regex;
2015-04-22 09:30:04 +02:00
el.defaultValue = obj.defaultValue;
2015-04-19 21:19:54 +02:00
break;
}
}
el.width = obj.width;
el.offset = obj.offset;
2015-04-19 21:19:54 +02:00
f(el);
});
dialog.modal({
show: 'true',
backdrop: 'static',
keyboard: false
});
}
2015-04-22 09:30:04 +02:00
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(""),
2015-04-26 22:42:43 +02:00
defaultValue: ko.observable(),
2015-04-22 09:30:04 +02:00
slider: {
2015-04-26 22:42:43 +02:00
min: ko.observable(),
max: ko.observable(),
step: ko.observable()
2015-04-22 09:30:04 +02:00
}
}
self.element().input.push(obj);
}
2015-04-19 21:19:54 +02:00
}
// view model class, parameters for constructor, container to bind to
OCTOPRINT_VIEWMODELS.push([
customControlDialogViewModel,
[],
"#customControlDialog"
]);
});