diff --git a/addons/web/static/src/search/search_arch_parser.js b/addons/web/static/src/search/search_arch_parser.js index a229a17074c..b821b52ff27 100644 --- a/addons/web/static/src/search/search_arch_parser.js +++ b/addons/web/static/src/search/search_arch_parser.js @@ -208,9 +208,9 @@ export class SearchArchParser extends XMLParser { preSearchItem.type = "dateFilter"; preSearchItem.fieldName = fieldName; preSearchItem.fieldType = this.fields[fieldName].type; - preSearchItem.defaultGeneratorId = DEFAULT_PERIOD; + preSearchItem.defaultGeneratorIds = [DEFAULT_PERIOD]; if (node.hasAttribute("default_period")) { - preSearchItem.defaultGeneratorId = node.getAttribute("default_period"); + preSearchItem.defaultGeneratorIds = node.getAttribute("default_period").split(','); } } else { let stringRepr = "[]"; diff --git a/addons/web/static/src/search/search_model.js b/addons/web/static/src/search/search_model.js index 583a4761b2b..6d0b97bf412 100644 --- a/addons/web/static/src/search/search_model.js +++ b/addons/web/static/src/search/search_model.js @@ -884,31 +884,33 @@ export class SearchModel extends EventBus { if (searchItem.type !== "dateFilter") { return; } - generatorId = generatorId || searchItem.defaultGeneratorId; - const index = this.query.findIndex( - (queryElem) => - queryElem.searchItemId === searchItemId && - "generatorId" in queryElem && - queryElem.generatorId === generatorId - ); - if (index >= 0) { - this.query.splice(index, 1); - if (!yearSelected(this._getSelectedGeneratorIds(searchItemId))) { - // This is the case where generatorId was the last option - // of type 'year' to be there before being removed above. - // Since other options of type 'month' or 'quarter' do - // not make sense without a year we deactivate all options. - this.query = this.query.filter( - (queryElem) => queryElem.searchItemId !== searchItemId - ); - } - } else { - this.query.push({ searchItemId, generatorId }); - if (!yearSelected(this._getSelectedGeneratorIds(searchItemId))) { - // Here we add 'this_year' as options if no option of type - // year is already selected. - const { defaultYearId } = this.optionGenerators.find((o) => o.id === generatorId); - this.query.push({ searchItemId, generatorId: defaultYearId }); + const generatorIds = generatorId ? [generatorId] : searchItem.defaultGeneratorIds; + for (const generatorId of generatorIds) { + const index = this.query.findIndex( + (queryElem) => + queryElem.searchItemId === searchItemId && + "generatorId" in queryElem && + queryElem.generatorId === generatorId + ); + if (index >= 0) { + this.query.splice(index, 1); + if (!yearSelected(this._getSelectedGeneratorIds(searchItemId))) { + // This is the case where generatorId was the last option + // of type 'year' to be there before being removed above. + // Since other options of type 'month' or 'quarter' do + // not make sense without a year we deactivate all options. + this.query = this.query.filter( + (queryElem) => queryElem.searchItemId !== searchItemId + ); + } + } else { + this.query.push({ searchItemId, generatorId }); + if (!yearSelected(this._getSelectedGeneratorIds(searchItemId))) { + // Here we add 'this_year' as options if no option of type + // year is already selected. + const { defaultYearId } = this.optionGenerators.find((o) => o.id === generatorId); + this.query.push({ searchItemId, generatorId: defaultYearId }); + } } } this._checkComparisonStatus(); diff --git a/addons/web/static/tests/search/filter_menu_tests.js b/addons/web/static/tests/search/filter_menu_tests.js index a492d2d68d7..ef756140494 100644 --- a/addons/web/static/tests/search/filter_menu_tests.js +++ b/addons/web/static/tests/search/filter_menu_tests.js @@ -471,6 +471,34 @@ QUnit.module("Search", (hooks) => { assert.deepEqual(getFacetTexts(target), ["Date: June 2019"]); }); + QUnit.test("filter with multiple values in default_period date attribute set as search_default", async function (assert) { + assert.expect(3); + + patchDate(2019, 6, 31, 13, 43, 0); + + await makeWithSearch({ + serverData, + resModel: "foo", + Component: ControlPanel, + searchViewId: false, + searchMenuTypes: ["filter"], + searchViewArch: ` + + + + `, + context: { search_default_date_field: true }, + }); + + await toggleFilterMenu(target); + await toggleMenuItem(target, "Date"); + + assert.ok(isItemSelected(target, "Date")); + assert.ok(isOptionSelected(target, "Date", "2019")); + assert.ok(isOptionSelected(target, "Date", "2018")); + + }); + QUnit.test("filter domains are correcly combined by OR and AND", async function (assert) { assert.expect(2); diff --git a/addons/web/static/tests/search/search_model_tests.js b/addons/web/static/tests/search/search_model_tests.js index 7a8bb020cd1..99cf9a96c29 100644 --- a/addons/web/static/tests/search/search_model_tests.js +++ b/addons/web/static/tests/search/search_model_tests.js @@ -232,7 +232,42 @@ QUnit.module("Search", (hooks) => { ]); }); - QUnit.test("parsing one filter tag with date attribute", async function (assert) { + QUnit.test("parsing one filter tag with default_period date attribute", async function (assert) { + assert.expect(1); + const model = await makeSearchModel({ + serverData, + searchViewArch: ` + + + + `, + }); + const dateFilterId = model.getSearchItems((f) => f.type === "dateFilter")[0].id; + assert.deepEqual(sanitizeSearchItems(model), [ + { + defaultGeneratorIds: ["this_year", "last_year"], + description: "Date", + fieldName: "date_field", + fieldType: "date", + type: "dateFilter", + name: "date_filter", + }, + { + comparisonOptionId: "previous_period", + dateFilterId, + description: "Date: Previous Period", + type: "comparison", + }, + { + comparisonOptionId: "previous_year", + dateFilterId, + description: "Date: Previous Year", + type: "comparison", + }, + ]); + }); + + QUnit.test("parsing one filter tag with date attribute ", async function (assert) { assert.expect(1); const model = await makeSearchModel({ serverData, @@ -245,7 +280,7 @@ QUnit.module("Search", (hooks) => { const dateFilterId = model.getSearchItems((f) => f.type === "dateFilter")[0].id; assert.deepEqual(sanitizeSearchItems(model), [ { - defaultGeneratorId: "this_month", + defaultGeneratorIds: ["this_month"], description: "Date", fieldName: "date_field", fieldType: "date",