From b542121fa90041ea457dd605feaca7077a8f6bc4 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 22 Nov 2016 09:34:05 +0800 Subject: webui: configs: Some minor fixes * configs.py/_set_configs(): Also return the original data if the input values failed the validation; * setServerConfigs(): Success callback also reset value if the input value failed pass the server-side validation; * getServerConfigs(): Return "undefined" if key does not exists. --- fg21sim/webui/static/js/configs.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'fg21sim/webui/static/js/configs.js') diff --git a/fg21sim/webui/static/js/configs.js b/fg21sim/webui/static/js/configs.js index f7e3731..f321881 100644 --- a/fg21sim/webui/static/js/configs.js +++ b/fg21sim/webui/static/js/configs.js @@ -118,8 +118,9 @@ var resetFormConfigs = function () { * * @param {String} name - The name of filed name * - * @returns value - value of the option field - * + `null` if the field not exists or empty (no value) + * @returns value - value of the configuration option field + * + `null` if the field is empty (no value) + * + `undefined` if the field does not exists */ var getFormConfigSingle = function (name) { var value = null; @@ -143,8 +144,11 @@ var getFormConfigSingle = function (name) { value = target.filter(":checked").map( function () { return $(this).val(); }).get(); } else if (target.is(":text") && target.data("type") === "array") { - // Convert back to Array + // Convert comma-separated string back to Array value = target.val().split(/\s*,\s*/); + if (value.length === 1 && value[0] === "") { + value = []; // Empty Array + } } else { value = target.val(); } @@ -153,6 +157,7 @@ var getFormConfigSingle = function (name) { value = null; } } else { + value = undefined; console.error("No such element:", selector); } } @@ -241,11 +246,11 @@ var setFormConfigSingle = function (name, value) { var setFormConfigs = function (data, errors) { // Set the values of form field to the input configurations data $.each(data, function (name, value) { - if (value == null) { + if (value === null) { value = ""; // Default to empty string } var val_old = getFormConfigSingle(name); - if (val_old !== value) { + if (typeof val_old !== "undefined" && val_old !== value) { setFormConfigSingle(name, value); console.log("Set input '" + name + "' to:", value, " <-", val_old); } @@ -388,7 +393,7 @@ var setServerConfigs = function (url, data) { data = typeof data !== "undefined" ? data : {}; return $.postJSON(url, {action: "set", data: data}, function (response) { - setFormConfigs({}, response.errors); + setFormConfigs(response.data, response.errors); }) .fail(function (jqxhr) { var modalData = {}; -- cgit v1.2.2