[IMP] web: support multiple default_period:

Prior to this commit, `default_period` attribute is taking a single value.
This commit adds the support for comma separated values.

task-2941625

Part-of: odoo/odoo#97321
This commit is contained in:
Laurent Stukkens (LTU)
2022-08-22 18:03:13 +02:00
parent 5efc7bff4b
commit 260def9c6f
4 changed files with 94 additions and 29 deletions
@@ -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 = "[]";
+27 -25
View File
@@ -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();
@@ -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: `
<search>
<filter string="Date" name="date_field" date="date_field" default_period="this_year,last_year"/>
</search>
`,
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);
@@ -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: `
<search>
<filter name="date_filter" string="Date" date="date_field" default_period="this_year,last_year"/>
</search>
`,
});
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",