Small Bugfixes, and defaultValue => default

This commit is contained in:
Marc 2015-04-30 09:00:32 +02:00
parent 0e131e8b00
commit 75cb6dda74
3 changed files with 65 additions and 40 deletions

View File

@ -69,51 +69,48 @@
self._processInput = function (list) { self._processInput = function (list) {
var inputs = []; var inputs = [];
correctlyDefined = function (e, key) { var attributeToInt = function (obj, key, def) {
return e.hasOwnProperty(key) && !isNaN(e[key]) && e[key] != undefined && e[key] != ""; if (obj.hasOwnProperty(key)) {
var val = obj[key];
if (_.isNumber(val)) {
return val;
}
var parsedVal = parseInt(val);
if (!isNaN(parsedVal)) {
return parsedVal;
}
}
return def;
}; };
_.each(list, function (element, index, l) { _.each(list, function (element, index, l) {
var input = { var input = {
name: ko.observable(element.name), name: ko.observable(element.name),
parameter: ko.observable(element.parameter), parameter: ko.observable(element.parameter),
defaultValue: ko.observable(element.defaultValue != "" ? element.defaultValue : undefined), default: ko.observable(element.default != "" ? element.default : undefined)
}; }
if (element.hasOwnProperty("slider") && typeof element.slider == "object") { if (element.hasOwnProperty("slider") && typeof element.slider == "object") {
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 correctMin = correctlyDefined(element.slider, "min"); var defaultValue = attributeToInt(element, "default", attributeToInt(element.slider, "min", 0));
var correctMax = correctlyDefined(element.slider, "max");
var param = 0; // if default value is not within range of min and max, correct that
if (correctlyDefined(element, "defaultValue")) { if (!_.inRange(defaultValue, element.slider.min, element.slider.max)) {
param = element.defaultValue; // use bound closer to configured default value
defaultValue = defaultValue < element.slider.min ? element.slider.min : element.slider.max;
} }
else if (correctMin)
param = element.slider.min;
if (typeof param == "string") input.value = ko.observable(defaultValue);
param = parseInt(param);
if (correctMin && param < element.slider.min)
param = element.slider.min;
if (correctMax && param > element.slider.max)
param = element.slider.max;
if (typeof param == "string")
param = parseInt(param);
input.value = ko.observable(param);
} }
else { else {
input.slider = false; input.slider = false;
input.value = ko.observable(correctlyDefined(element, "defaultValue") ? element.defaultValue : undefined); input.value = input.default;
} }
inputs.push(input); inputs.push(input);
@ -141,7 +138,12 @@
if (control.hasOwnProperty("template") && control.hasOwnProperty("regex")) { if (control.hasOwnProperty("template") && control.hasOwnProperty("regex")) {
control.template = ko.observable(control.template); control.template = ko.observable(control.template);
control.regex = ko.observable(control.regex); control.regex = ko.observable(control.regex);
control.defaultValue = ko.observable(control.defaultValue || ""); control.default = ko.observable(control.default || "");
control.value = control.default;
delete control.key;
delete control.template_key;
} }
if (control.hasOwnProperty("children")) { if (control.hasOwnProperty("children")) {
@ -282,7 +284,7 @@
if (element.hasOwnProperty("template")) { if (element.hasOwnProperty("template")) {
data.template = element.template(); data.template = element.template();
data.regex = element.regex(); data.regex = element.regex();
data.defaultValue = element.defaultValue() || ""; data.default = element.default() || "";
title = "Edit Output"; title = "Edit Output";
type = "output"; type = "output";
@ -325,6 +327,9 @@
{ {
data.input = []; data.input = [];
_.each(element.input(), function (element, index, list) { _.each(element.input(), function (element, index, list) {
if (element.hasOwnProperty("default")) {
data.input[index].defaultValue = element.default;
}
data.input[index] = ko.mapping.toJS(element); data.input[index] = ko.mapping.toJS(element);
}); });
} }
@ -367,6 +372,10 @@
} }
if (ret.input != undefined) { if (ret.input != undefined) {
_.each(ret.input, function (element, index, list) {
data.input[index] = ko.mapping.toJS(element);
});
element.input(self._processInput(ret.input)); element.input(self._processInput(ret.input));
} }
else else
@ -384,19 +393,19 @@
else else
element.regex = ko.observable(ret.regex); element.regex = ko.observable(ret.regex);
if (element.hasOwnProperty("defaultValue")) if (element.hasOwnProperty("default"))
element.defaultValue(ret.defaultValue); element.default(ret.defaultValue);
else else
element.defaultValue = ko.observable(ret.defaultValue); element.default = ko.observable(ret.defaultValue);
} }
else else
{ {
if (element.hasOwnProperty("defaultValue")) if (element.hasOwnProperty("default"))
element.defaultValue(undefined); element.default(undefined);
delete element.template; delete element.template;
delete element.regex; delete element.regex;
delete element.defaultValue; delete element.default;
} }
break; break;
} }
@ -419,7 +428,7 @@
case "output": { case "output": {
element.template(ret.template); element.template(ret.template);
element.regex(ret.regex); element.regex(ret.regex);
element.defaultValue(ret.defaultValue); element.default(ret.defaultValue);
break; break;
} }
} }
@ -494,6 +503,7 @@
delete list[i].output; delete list[i].output;
delete list[i].key; delete list[i].key;
delete list[i].template_key; delete list[i].template_key;
delete list[i].value;
if (list[i].hasOwnProperty("width") && list[i].width() == "") if (list[i].hasOwnProperty("width") && list[i].width() == "")
delete list[i].width; delete list[i].width;

View File

@ -115,18 +115,33 @@
} }
if (self.useInputs()) { 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;
};
el.input = []; el.input = [];
_.each(obj.input, function (element, index, list) { _.each(obj.input, function (element, index, list) {
var input = { var input = {
name: element.name, name: element.name,
parameter: element.parameter, parameter: element.parameter,
defaultValue: !isNaN(element.defaultValue) ? element.defaultValue : undefined default: element.defaultValue
}; };
if (element.hasOwnProperty("slider") && element.slider != false) { if (element.hasOwnProperty("slider") && element.slider != false) {
input["slider"] = { input["slider"] = {
}; };
input.defaultValue = !isNaN(element.defaultValue) && element.defaultValue != undefined && element.defaultValue != "" ? parseInt(element.defaultValue) : undefined; input.default = attributeToInt(element, "defaultValue", undefined);
if (element.slider.hasOwnProperty("min") && element.slider.min != "") if (element.slider.hasOwnProperty("min") && element.slider.min != "")
input.slider.min = element.slider.min; input.slider.min = element.slider.min;
@ -143,7 +158,7 @@
if (self.useOutput()) { if (self.useOutput()) {
el.template = obj.template; el.template = obj.template;
el.regex = obj.regex; el.regex = obj.regex;
el.defaultValue = obj.defaultValue; el.default = obj.defaultValue;
} }
break; break;
} }

View File

@ -88,12 +88,12 @@
<input type="number" style="width: 100px" data-bind="slider: {value: value, min: slider.min(), max: slider.max(), step: slider.step(), enabled: false }"> <input type="number" style="width: 100px" data-bind="slider: {value: value, min: slider.min(), max: slider.max(), step: slider.step(), enabled: false }">
<!-- /ko --> <!-- /ko -->
<!-- ko ifnot: slider --> <!-- ko ifnot: slider -->
<input type="text" class="input-small" data-bind="attr: {placeholder: name}, value: defaultValue, disable: true"> <input type="text" class="input-small" data-bind="attr: {placeholder: name}, value: value, disable: true">
<!-- /ko --> <!-- /ko -->
<!-- /ko --> <!-- /ko -->
</script> </script>
<script type="text/html" id="settingsCustomControls_controlTemplate_output"> <script type="text/html" id="settingsCustomControls_controlTemplate_output">
<label style="cursor: default" data-bind="text: defaultValue"></label> <label style="cursor: default" data-bind="text: value"></label>
</script> </script>
<script type="text/html" id="settingsCustomControls_controlTemplate_command"> <script type="text/html" id="settingsCustomControls_controlTemplate_command">
<button class="btn" data-bind="text: name" onclick="event.preventDefault()"></button> <button class="btn" data-bind="text: name" onclick="event.preventDefault()"></button>