Skip to content

Commit

Permalink
VCST-1448: Added the ability to unregister meta fields by name, templ…
Browse files Browse the repository at this point in the history
…ateUrl or a custom search predicate callback. (#2813)

feat: Added the ability to unregister meta fields by name, templateUrl or a custom search predicate callback.
  • Loading branch information
OlegoO authored Jul 1, 2024
1 parent 1c9a0f2 commit ad18c92
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
Expand Down

0 comments on commit ad18c92

Please sign in to comment.