2015-04-19 21:19:54 +02:00
|
|
|
|
$(function () {
|
|
|
|
|
function customControlDialogViewModel(parameters) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2015-04-21 01:20:21 +02:00
|
|
|
|
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.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("Output"), key: "output" }
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
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,
|
2015-04-21 01:20:21 +02:00
|
|
|
|
collapsable: true,
|
2015-04-19 21:19:54 +02:00
|
|
|
|
commands: "",
|
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")
|
|
|
|
|
element = _.extend(element, data);
|
|
|
|
|
|
2015-04-22 09:30:04 +02:00
|
|
|
|
var mapped = ko.mapping.fromJS(element);
|
|
|
|
|
if (data.hasOwnProperty("input")) {
|
|
|
|
|
self.useInputs(true);
|
2015-04-19 21:19:54 +02:00
|
|
|
|
|
2015-04-22 09:30:04 +02:00
|
|
|
|
//_.each(mapped.input(), function (e, index, list) {
|
|
|
|
|
// if (e.hasOwnProperty("slider") && !$.isFunction(e.slider))
|
|
|
|
|
// e.slider = ko.observable(e.slider);
|
|
|
|
|
//});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.element(mapped);
|
|
|
|
|
}
|
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;
|
2015-04-21 01:20:21 +02:00
|
|
|
|
if (obj.name != "")
|
|
|
|
|
el.collapsable = obj.collapsable;
|
|
|
|
|
|
2015-04-19 21:19:54 +02:00
|
|
|
|
el.children = [];
|
|
|
|
|
|
|
|
|
|
el.width = obj.width;
|
|
|
|
|
el.offset = obj.offset;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "command": {
|
|
|
|
|
el.name = obj.name;
|
|
|
|
|
if (obj.commands.indexOf('\n') == -1)
|
|
|
|
|
el.command = obj.commands;
|
|
|
|
|
else
|
|
|
|
|
el.commands = obj.commands;
|
|
|
|
|
|
|
|
|
|
el.width = obj.width;
|
|
|
|
|
el.offset = obj.offset;
|
|
|
|
|
|
|
|
|
|
if (self.useInputs()) {
|
|
|
|
|
el.input = [];
|
|
|
|
|
_.each(obj.input, function (element, index, list) {
|
|
|
|
|
var input = {
|
|
|
|
|
name: element.name,
|
|
|
|
|
parameter: element.parameter,
|
2015-04-22 09:30:04 +02:00
|
|
|
|
defaultValue: 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"] = {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
el.width = obj.width;
|
|
|
|
|
el.offset = obj.offset;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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(""),
|
|
|
|
|
defaultValue: ko.observable(""),
|
|
|
|
|
slider: {
|
|
|
|
|
min: ko.observable(""),
|
|
|
|
|
max: ko.observable(""),
|
|
|
|
|
step: ko.observable("")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
]);
|
|
|
|
|
});
|