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/handlers/configs.py | 14 +++++++++++--- fg21sim/webui/static/js/configs.js | 17 +++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'fg21sim/webui') diff --git a/fg21sim/webui/handlers/configs.py b/fg21sim/webui/handlers/configs.py index 296bd11..857138b 100644 --- a/fg21sim/webui/handlers/configs.py +++ b/fg21sim/webui/handlers/configs.py @@ -99,7 +99,7 @@ class ConfigsAJAXHandler(BaseRequestHandler): if action == "set": # Set the values of the specified options try: - errors = self._set_configs(data=request["data"]) + data, errors = self._set_configs(request["data"]) success = True except KeyError: success = False @@ -198,10 +198,15 @@ class ConfigsAJAXHandler(BaseRequestHandler): Returns ------- + data_orig : dict + When the supplied value failed to pass the specification + validation, then its original value was returned to the client + to reset its value. errors : dict When error occurs (e.g., invalid key, invalid values), then the specific errors with details are stored in this dictionary. """ + data_orig = {} errors = {} for key, value in data.items(): if key in ["workdir", "configfile"]: @@ -215,10 +220,13 @@ class ConfigsAJAXHandler(BaseRequestHandler): else: try: self.configs.setn(key, value) - except (KeyError, ConfigError) as e: + except KeyError as e: + errors[key] = str(e) + except ConfigError as e: + data_orig[key] = self.configs.getn(key) errors[key] = str(e) # - return errors + return (data_orig, errors) def _reset_configs(self): """Reset the configurations to the defaults.""" 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