Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/apostrophe/lib/compose-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Shared utility for composing filters in doc-type and page modules.
*
* Transforms a filters object (keyed by name) into an array of filter
* definitions with normalized inputType, default values, and null choices.
*
* This was previously duplicated between:
* - @apostrophecms/doc-type/index.js composeFilters()
* - @apostrophecms/page/index.js composeFilters()
*
* @param {Object} filters - An object keyed by filter name, where each value
* is a filter definition object.
* @returns {Array} An array of composed filter objects with `name` property added.
*/
module.exports = function composeFilters(filters) {
const composed = Object.entries(filters)
.map(([ name, filter ]) => ({
name,
...filter,
inputType: filter.inputType || 'select'
}));

// Add a null choice if not already added or set to `required`
composed.forEach((filter) => {
if (Array.isArray(filter.choices)) {
if (
!filter.required &&
!filter.choices.find((choice) => choice.value === null)
) {
filter.def = null;
filter.choices = filter.inputType === 'checkbox'
? filter.choices
: filter.choices.concat({
value: null,
label: 'apostrophe:none'
});
}
} else {
// Dynamic choices from the REST API, but
// we need a label for "no opinion"
filter.nullLabel = filter.inputType === 'radio'
? 'apostrophe:any'
: 'apostrophe:filterMenuChooseOne';
}
if (filter.inputType === 'checkbox') {
filter.def = [];
}
});

return composed;
};
35 changes: 1 addition & 34 deletions packages/apostrophe/modules/@apostrophecms/doc-type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1675,40 +1675,7 @@ module.exports = {
},

composeFilters() {
// TODO: keep in sync with page/index.js composeFilters
self.filters = Object.entries(self.filters)
.map(([ name, filter ]) => ({
name,
...filter,
inputType: filter.inputType || 'select'
}));

// Add a null choice if not already added or set to `required`
self.filters.forEach((filter) => {
if (Array.isArray(filter.choices)) {
if (
!filter.required &&
!filter.choices.find((choice) => choice.value === null)
) {
filter.def = null;
filter.choices = filter.inputType === 'checkbox'
? filter.choices
: filter.choices.concat({
value: null,
label: 'apostrophe:none'
});
}
} else {
// Dynamic choices from the REST API, but
// we need a label for "no opinion"
filter.nullLabel = filter.inputType === 'radio'
? 'apostrophe:any'
: 'apostrophe:filterMenuChooseOne';
}
if (filter.inputType === 'checkbox') {
filter.def = [];
}
});
self.filters = require('../../../lib/compose-filters')(self.filters);
},

composeColumns() {
Expand Down
35 changes: 1 addition & 34 deletions packages/apostrophe/modules/@apostrophecms/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3304,40 +3304,7 @@ database.`);
});
},
composeFilters() {
// TODO: keep in sync with doc-type/index.js composeFilters
self.filters = Object.entries(self.filters)
.map(([ name, filter ]) => ({
name,
...filter,
inputType: filter.inputType || 'select'
}));

// Add a null choice if not already added or set to `required`
self.filters.forEach((filter) => {
if (Array.isArray(filter.choices)) {
if (
!filter.required &&
!filter.choices.find((choice) => choice.value === null)
) {
filter.def = null;
filter.choices = filter.inputType === 'checkbox'
? filter.choices
: filter.choices.concat({
value: null,
label: 'apostrophe:none'
});
}
} else {
// Dynamic choices from the REST API, but
// we need a label for "no opinion"
filter.nullLabel = filter.inputType === 'radio'
? 'apostrophe:any'
: 'apostrophe:filterMenuChooseOne';
}
if (filter.inputType === 'checkbox') {
filter.def = [];
}
});
self.filters = require('../../../lib/compose-filters')(self.filters);
},
async getBatchArchivePatches(req, ids) {
const batchReq = req.clone({
Expand Down
Loading