From 35e2735e2ebc5cee82bcb746b86ca417e9fe4088 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 6 Aug 2020 16:56:46 +0200 Subject: [PATCH] luci-base: form.js: implement AbstractSection.getOption() helper The `getOption()` function allows to easily obtain a reference to another option object instance within the same section. Signed-off-by: Jo-Philipp Wich --- .../htdocs/luci-static/resources/form.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/modules/luci-base/htdocs/luci-static/resources/form.js b/modules/luci-base/htdocs/luci-static/resources/form.js index 32d803d6af..568a4abb6a 100644 --- a/modules/luci-base/htdocs/luci-static/resources/form.js +++ b/modules/luci-base/htdocs/luci-static/resources/form.js @@ -1156,6 +1156,36 @@ var CBIAbstractSection = CBIAbstractElement.extend(/** @lends LuCI.form.Abstract return rv; }, + /** + * Obtain underlying option objects. + * + * This function is sensitive to the amount of arguments passed to it; + * if no option name is specified, all options within this section are + * returned as dictionary. + * + * If an option name is supplied, this function returns the matching + * LuCI.form.AbstractValue instance only. + * + * @param {string} [option] + * The name of the option object to obtain + * + * @returns {null|LuCI.form.AbstractValue|Object} + * Returns either a dictionary of option names and their corresponding + * option instance objects or just a single object instance value, + * depending on the amount of passed arguments. + */ + getOption: function(option) { + var rv = (arguments.length == 0) ? {} : null; + + for (var i = 0, o; (o = this.children[i]) != null; i++) + if (rv) + rv[o.option] = o; + else if (o.option == option) + return o; + + return rv; + }, + /** @private */ renderUCISection: function(section_id) { var renderTasks = []; -- 2.30.2