[FIX] web: WithSearch: filtering search defaults
Before that commit, the search defaults would not be filtered when the WithSearch is started with a search model state or updated by its parent. closes odoo/odoo#159406 X-original-commit: dac527b7baa699cef0828a9d87a4c814ff3f7f05 Signed-off-by: Francois Georis (fge) <fge@odoo.com> Signed-off-by: Mathieu Duckerts-Antoine (dam) <dam@odoo.com>
This commit is contained in:
@@ -266,6 +266,9 @@ export class SearchModel extends EventBus {
|
||||
this.searchViewId = searchViewDescription.viewId;
|
||||
}
|
||||
|
||||
const { searchDefaults, searchPanelDefaults } =
|
||||
this._extractSearchDefaultsFromGlobalContext();
|
||||
|
||||
if (config.state) {
|
||||
this._importState(config.state);
|
||||
this.__legacyParseSearchPanelArchAnyway(searchViewDescription, searchViewFields);
|
||||
@@ -285,26 +288,6 @@ export class SearchModel extends EventBus {
|
||||
this.nextGroupId = 1;
|
||||
this.nextGroupNumber = 1;
|
||||
|
||||
const searchDefaults = {};
|
||||
const searchPanelDefaults = {};
|
||||
|
||||
for (const key in this.globalContext) {
|
||||
const defaultValue = this.globalContext[key];
|
||||
const searchDefaultMatch = /^search_default_(.*)$/.exec(key);
|
||||
if (searchDefaultMatch) {
|
||||
if (defaultValue) {
|
||||
searchDefaults[searchDefaultMatch[1]] = defaultValue;
|
||||
}
|
||||
delete this.globalContext[key];
|
||||
continue;
|
||||
}
|
||||
const searchPanelDefaultMatch = /^searchpanel_default_(.*)$/.exec(key);
|
||||
if (searchPanelDefaultMatch) {
|
||||
searchPanelDefaults[searchPanelDefaultMatch[1]] = defaultValue;
|
||||
delete this.globalContext[key];
|
||||
}
|
||||
}
|
||||
|
||||
const parser = new SearchArchParser(
|
||||
searchViewDescription,
|
||||
searchViewFields,
|
||||
@@ -389,6 +372,8 @@ export class SearchModel extends EventBus {
|
||||
this.globalGroupBy = groupBy || [];
|
||||
this.globalOrderBy = orderBy || [];
|
||||
|
||||
this._extractSearchDefaultsFromGlobalContext();
|
||||
|
||||
await this._reloadSections();
|
||||
}
|
||||
|
||||
@@ -1455,6 +1440,28 @@ export class SearchModel extends EventBus {
|
||||
}
|
||||
}
|
||||
|
||||
_extractSearchDefaultsFromGlobalContext() {
|
||||
const searchDefaults = {};
|
||||
const searchPanelDefaults = {};
|
||||
for (const key in this.globalContext) {
|
||||
const defaultValue = this.globalContext[key];
|
||||
const searchDefaultMatch = /^search_default_(.*)$/.exec(key);
|
||||
if (searchDefaultMatch) {
|
||||
if (defaultValue) {
|
||||
searchDefaults[searchDefaultMatch[1]] = defaultValue;
|
||||
}
|
||||
delete this.globalContext[key];
|
||||
continue;
|
||||
}
|
||||
const searchPanelDefaultMatch = /^searchpanel_default_(.*)$/.exec(key);
|
||||
if (searchPanelDefaultMatch) {
|
||||
searchPanelDefaults[searchPanelDefaultMatch[1]] = defaultValue;
|
||||
delete this.globalContext[key];
|
||||
}
|
||||
}
|
||||
return { searchDefaults, searchPanelDefaults };
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches values for each category at startup. At reload a category is
|
||||
* only fetched if needed.
|
||||
|
||||
@@ -333,4 +333,56 @@ QUnit.module("Search", (hooks) => {
|
||||
|
||||
await nextTick();
|
||||
});
|
||||
|
||||
QUnit.test("search defaults are removed from context at reload", async function (assert) {
|
||||
assert.expect(4);
|
||||
|
||||
const context = {
|
||||
search_default_x: true,
|
||||
searchpanel_default_y: true,
|
||||
};
|
||||
|
||||
class TestComponent extends Component {
|
||||
setup() {
|
||||
onWillStart(() => {
|
||||
assert.deepEqual(this.props.context, { lang: "en", tz: "taht", uid: 7 });
|
||||
});
|
||||
onWillUpdateProps((nextProps) => {
|
||||
assert.deepEqual(nextProps.context, { lang: "en", tz: "taht", uid: 7 });
|
||||
});
|
||||
}
|
||||
}
|
||||
TestComponent.template = xml`<div class="o_test_component">Test component content</div>`;
|
||||
TestComponent.props = { context: Object };
|
||||
|
||||
const env = await makeTestEnv(serverData);
|
||||
const target = getFixture();
|
||||
|
||||
class Parent extends Component {
|
||||
setup() {
|
||||
useSubEnv({ config: {} });
|
||||
this.searchState = useState({
|
||||
resModel: "animal",
|
||||
domain: [["type", "=", "carnivorous"]],
|
||||
context,
|
||||
});
|
||||
}
|
||||
}
|
||||
Parent.template = xml`
|
||||
<WithSearch t-props="searchState" t-slot-scope="search">
|
||||
<TestComponent
|
||||
context="search.context"
|
||||
/>
|
||||
</WithSearch>
|
||||
`;
|
||||
Parent.components = { WithSearch, TestComponent };
|
||||
|
||||
const parent = await mount(Parent, target, { env });
|
||||
assert.deepEqual(parent.searchState.context, context);
|
||||
|
||||
parent.searchState.domain = [["type", "=", "herbivorous"]];
|
||||
|
||||
await nextTick();
|
||||
assert.deepEqual(parent.searchState.context, context);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,6 +15,8 @@ import {
|
||||
import { listView } from "@web/views/list/list_view";
|
||||
import { companyService } from "@web/webclient/company_service";
|
||||
import { onWillStart } from "@odoo/owl";
|
||||
import { GraphModel } from "@web/views/graph/graph_model";
|
||||
import { switchView } from "../../search/helpers";
|
||||
|
||||
let serverData;
|
||||
let target;
|
||||
@@ -361,6 +363,38 @@ QUnit.module("ActionManager", (hooks) => {
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test(
|
||||
"search defaults are removed from context when switching view",
|
||||
async function (assert) {
|
||||
assert.expect(1);
|
||||
serverData.views["partner,false,graph"] = `<graph/>`;
|
||||
serverData.views["partner,false,list"] = `<list/>`;
|
||||
const context = {
|
||||
search_default_x: true,
|
||||
searchpanel_default_y: true,
|
||||
};
|
||||
patchWithCleanup(GraphModel.prototype, {
|
||||
load(searchParams) {
|
||||
assert.deepEqual(searchParams.context, { lang: "en", tz: "taht", uid: 7 });
|
||||
return super.load(...arguments);
|
||||
},
|
||||
});
|
||||
|
||||
const webClient = await createWebClient({ serverData });
|
||||
await doAction(webClient, {
|
||||
res_model: "partner",
|
||||
type: "ir.actions.act_window",
|
||||
views: [
|
||||
[false, "list"],
|
||||
[false, "graph"],
|
||||
],
|
||||
context,
|
||||
});
|
||||
// list view is loaded, switch to graph view
|
||||
await switchView(target, "graph");
|
||||
}
|
||||
);
|
||||
|
||||
QUnit.test(
|
||||
"retrieving a stored action should remove 'allowed_company_ids' from its context",
|
||||
async function (assert) {
|
||||
|
||||
Reference in New Issue
Block a user