Skip to content

Commit ad18c92

Browse files
authored
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.
1 parent 1c9a0f2 commit ad18c92

File tree

1 file changed

+26
-6
lines changed
  • src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives

1 file changed

+26
-6
lines changed

src/VirtoCommerce.Platform.Web/wwwroot/js/common/directives/metaform.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,33 @@ angular.module('platformWebApp')
1313
getMetaFields: function (metaFormName) {
1414
return registeredMetaFields[metaFormName];
1515
},
16-
unregisterMetaFields: function (metaFormName, metaField) {
17-
if (registeredMetaFields[metaFormName]) {
18-
var index = registeredMetaFields[metaFormName].indexOf(metaField);
19-
if (index !== -1) {
20-
registeredMetaFields[metaFormName].splice(index, 1);
21-
}
16+
unregisterMetaFields: function (metaFormName, ...metaFieldsToFind) {
17+
metaFieldsToFind.forEach(metaFieldToFind => this.unregisterMetaField(metaFormName, metaFieldToFind));
18+
},
19+
unregisterMetaFieldWithTemplateUrl: function (metaFormName, templateUrl) {
20+
this.unregisterMetaField(metaFormName, x => x.templateUrl === templateUrl, `template URL '${templateUrl}'`);
21+
},
22+
unregisterMetaField: function (metaFormName, metaFieldToFind, metaFieldDescription) {
23+
let metaFieldIndex;
24+
const metaFields = registeredMetaFields[metaFormName];
25+
26+
// metaFieldToFind can be a search filter, a metaField name or the metaField object itself (which is the previous way of unregistering a metaField)
27+
if (_.isFunction(metaFieldToFind)) {
28+
metaFieldIndex = _.findIndex(metaFields, metaFieldToFind);
29+
} else if (typeof metaFieldToFind === 'string') {
30+
metaFieldIndex = _.findIndex(metaFields, x => x.name === metaFieldToFind);
31+
metaFieldDescription ??= `name '${metaFieldToFind}'`;
32+
} else {
33+
metaFieldIndex = _.findIndex(metaFields, metaFieldToFind);
2234
}
35+
36+
if (metaFieldIndex <= 0) {
37+
throw new Error(metaFieldDescription
38+
? `The metaForm '${metaFormName}' doesn't contain a field with ${metaFieldDescription}, the field could not be removed.`
39+
: `The metaForm '${metaFormName}' doesn't contain the field to be removed.`);
40+
}
41+
42+
metaFields.splice(metaFieldIndex, 1);
2343
},
2444
clearMetaFields: function (metaFormName) {
2545
registeredMetaFields[metaFormName] = [];

0 commit comments

Comments
 (0)