diff --git a/CHANGELOG.md b/CHANGELOG.md index d47d900e45b9e2e8bf8441273d54ff723c88b185..40e11b2023ddc868ea16ab8384f9c8c28e3f5602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `form_elements.make_form_modal` and `form_elements.make_scripting_submission_button` functions to create a form modal and an SSS submission button, respectively. +* Allow to supply a function that creates the form instead of a config to + create_show_form_callback * `BUILD_MODULE_EXT_ENTITY_ACL_USER_MANAGEMENT_BUTTON` build variable with which a button to the user management is added to the navbar for users with role administration if `BUILD_MODULE_EXT_ENTITY_ACL=ENABLED`. diff --git a/src/core/js/form_panel.js b/src/core/js/form_panel.js index 9728a4ccea54c36d85399a3148373b7372108db0..a745f949f98d6219e377783d7ca6c854908df573 100644 --- a/src/core/js/form_panel.js +++ b/src/core/js/form_panel.js @@ -65,15 +65,33 @@ var form_panel = new function () { }; /** - * Creates a callback function that toggles the form panel which + * Creates a callback function that creates the form inside + * the form panel when called for the first time. + * + * You may supply 'undefined' as form_config and use form_creator + * instead which is supposed to be a function without argument that + * return the form to be added to the panel */ - this.create_show_form_callback = function (panel_id, title, form_config) { + this.create_show_form_callback = function ( + panel_id, title, form_config, form_creator=undefined + ) { return (e) => { logger.trace("enter show_form_panel", e); const panel = $(form_panel.get_form_panel(panel_id, title)); if (panel.find("form").length === 0) { - const form = form_elements.make_form(form_config); + if (form_config != undefined && form_creator!=undefined){ + throw new Error("create_show_form_callback takes either a FormConfig or a function that creates the form"); + } + + var form; + if (form_config != undefined ){ + form = form_elements.make_form(form_config); + } else if (form_creator != undefined ){ + form = form_creator(); + } else { + throw new Error("create_show_form_callback takes a FormConfig or a function that creates the form"); + } panel.append(form); $(form).find(".selectpicker").selectpicker(); diff --git a/test/core/js/modules/form_panel.js.js b/test/core/js/modules/form_panel.js.js index be4c4ad8e66402adedf8c386ff086be4d7cb7955..a810fb8711f14bc3b3637297c9f937884022fc15 100644 --- a/test/core/js/modules/form_panel.js.js +++ b/test/core/js/modules/form_panel.js.js @@ -55,9 +55,18 @@ QUnit.test("create_show_form_callback ", function (assert) { help: help_text, }, ], }; - const cb = form_panel.create_show_form_callback( panel_id, title, csv_form_config); + const cb = form_panel.create_show_form_callback(panel_id, title, csv_form_config); assert.equal(typeof cb, "function", "function created"); cb() + + const creator = function() { + return form_elements.make_form(csv_form_config); + } + const cb2 = form_panel.create_show_form_callback( + panel_id, title, undefined, creator + ); + assert.equal(typeof cb2, "function", "function created"); + cb2() });