From ad18c92ade6145893c603316d610f2109e5a5f67 Mon Sep 17 00:00:00 2001 From: Oleg Zhuk Date: Mon, 1 Jul 2024 16:29:01 +0200 Subject: [PATCH] VCST-1448: Added the ability to unregister meta fields by name, templateUrl or a custom search predicate callback. (#2813) feat: Added the ability to unregister meta fields by name, templateUrl or a custom search predicate callback. --- .../wwwroot/js/common/directives/metaform.js | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives/metaform.js b/src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives/metaform.js index 8580a12f53d..2c7392e943b 100644 --- a/src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives/metaform.js +++ b/src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives/metaform.js @@ -13,13 +13,33 @@ angular.module('platformWebApp') getMetaFields: function (metaFormName) { return registeredMetaFields[metaFormName]; }, - unregisterMetaFields: function (metaFormName, metaField) { - if (registeredMetaFields[metaFormName]) { - var index = registeredMetaFields[metaFormName].indexOf(metaField); - if (index !== -1) { - registeredMetaFields[metaFormName].splice(index, 1); - } + unregisterMetaFields: function (metaFormName, ...metaFieldsToFind) { + metaFieldsToFind.forEach(metaFieldToFind => this.unregisterMetaField(metaFormName, metaFieldToFind)); + }, + unregisterMetaFieldWithTemplateUrl: function (metaFormName, templateUrl) { + this.unregisterMetaField(metaFormName, x => x.templateUrl === templateUrl, `template URL '${templateUrl}'`); + }, + unregisterMetaField: function (metaFormName, metaFieldToFind, metaFieldDescription) { + let metaFieldIndex; + const metaFields = registeredMetaFields[metaFormName]; + + // metaFieldToFind can be a search filter, a metaField name or the metaField object itself (which is the previous way of unregistering a metaField) + if (_.isFunction(metaFieldToFind)) { + metaFieldIndex = _.findIndex(metaFields, metaFieldToFind); + } else if (typeof metaFieldToFind === 'string') { + metaFieldIndex = _.findIndex(metaFields, x => x.name === metaFieldToFind); + metaFieldDescription ??= `name '${metaFieldToFind}'`; + } else { + metaFieldIndex = _.findIndex(metaFields, metaFieldToFind); } + + if (metaFieldIndex <= 0) { + throw new Error(metaFieldDescription + ? `The metaForm '${metaFormName}' doesn't contain a field with ${metaFieldDescription}, the field could not be removed.` + : `The metaForm '${metaFormName}' doesn't contain the field to be removed.`); + } + + metaFields.splice(metaFieldIndex, 1); }, clearMetaFields: function (metaFormName) { registeredMetaFields[metaFormName] = [];