Skip to content

Commit

Permalink
[IMP] mis-builder: preserve the filters when navigating back from dri…
Browse files Browse the repository at this point in the history
…ll-down

Prior to this commit the state of the searchModel was lost when navigating
back from a drill down.

This commit uses the globalState in order to store and restore the state
of the searchModlel.
  • Loading branch information
Laurent Stukkens committed Dec 27, 2023
1 parent 9a8add9 commit ae2428d
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 8 deletions.
7 changes: 6 additions & 1 deletion mis_builder/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@
"mis_builder/static/src/components/mis_report_widget.esm.js",
"mis_builder/static/src/components/mis_report_widget.xml",
"mis_builder/static/src/components/mis_report_widget.css",
"mis_builder/static/src/views/field/field.esm.js",
"mis_builder/static/src/views/form/mis_report_form_compiler.esm.js",
"mis_builder/static/src/views/form/mis_report_form_controller.esm.js",
"mis_builder/static/src/views/form/mis_report_form_controller.xml",
"mis_builder/static/src/views/form/mis_report_form_renderer.esm.js",
"mis_builder/static/src/views/form/mis_report_form_view.esm.js",
],
"web.report_assets_common": [
"/mis_builder/static/src/css/report.css",
],
},
"qweb": ["static/src/xml/mis_report_widget.xml"],
"installable": True,
"application": True,
"license": "AGPL-3",
Expand Down
37 changes: 31 additions & 6 deletions mis_builder/static/src/components/mis_report_widget.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {SearchBar} from "@web/search/search_bar/search_bar";
import {parseDate} from "@web/core/l10n/dates";
import {registry} from "@web/core/registry";
import {standardFieldProps} from "@web/views/fields/standard_field_props";
import {useSetupAction} from "@web/webclient/actions/action_hook";

export const misReportWidgetClassName = "mis_report_widget";

export class MisReportWidget extends Component {
setup() {
Expand All @@ -33,6 +36,18 @@ export class MisReportWidget extends Component {
this.refresh();
});
onWillStart(this.willStart);
useSetupAction({
getGlobalState: () => {
if (!this.showSearchBar) {
return {};
}
return {
misReportSearchModelState: JSON.stringify(
this.searchModel.exportState()
),
};
},
});
}

// Lifecycle
Expand All @@ -59,11 +74,15 @@ export class MisReportWidget extends Component {
this.widget_show_pivot_date = result.widget_show_pivot_date;
if (this.showSearchBar) {
// Initialize the search model
await this.searchModel.load({
const config = {
resModel: this.source_aml_model_name,
searchViewId: this.widget_search_view_id,
analyticAccountId: this.analyticAccountId,
});
};
if (this.props.misReportSearchModelState) {
config.state = JSON.parse(this.props.misReportSearchModelState);
}
await this.searchModel.load(config);
}

// Compute the report
Expand Down Expand Up @@ -232,9 +251,15 @@ MisReportWidget.props = {
type: String,
optional: true,
},
misReportSearchModelState: {
type: String,
optional: true,
},
};
MisReportWidget.extractProps = ({attrs}) => {
return {
analyticAccountIdField: attrs.analytic_account_id_field,
};
};
MisReportWidget.extractProps = ({attrs}) => ({
analyticAccountIdField: attrs.analytic_account_id_field,
});

registry.category("fields").add("mis_report_widget", MisReportWidget);
registry.category("fields").add(misReportWidgetClassName, MisReportWidget);
23 changes: 23 additions & 0 deletions mis_builder/static/src/views/field/field.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @odoo-module **/

import {Field} from "@web/views/fields/field";
import {misReportWidgetClassName} from "@mis_builder/components/mis_report_widget.esm";

export class MisReportField extends Field {
/**
* @override
*/
get fieldComponentProps() {
const fieldComponentProps = super.fieldComponentProps;
// Ensures that misReportSearchModelState is passed to the props of the
// `mis_report_widget` widget that is mounted on the field.
if (
this.props.fieldInfo.widget &&
this.props.fieldInfo.widget === misReportWidgetClassName
) {
fieldComponentProps.misReportSearchModelState =
this.props.misReportSearchModelState;
}
return fieldComponentProps;
}
}
25 changes: 25 additions & 0 deletions mis_builder/static/src/views/form/mis_report_form_compiler.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @odoo-module **/

import {FormCompiler} from "@web/views/form/form_compiler";
import {misReportWidgetClassName} from "@mis_builder/components/mis_report_widget.esm";

export class MisReportFormCompiler extends FormCompiler {
/**
* @override
*/
compileField(el) {
// Passes misReportSearchModelState to the Field that is compiled in the
// view when the field has the `mis_report_widget` widget set.
const field = super.compileField(el);
if (
el.hasAttribute("widget") &&
el.getAttribute("widget") === misReportWidgetClassName
) {
field.setAttribute(
"misReportSearchModelState",
"props.globalState && props.globalState.misReportSearchModelState"
);
}
return field;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @odoo-module */

import {FormController} from "@web/views/form/form_controller";

export class MisReportFormController extends FormController {}

// Ensures that globalState is passed to the renderer.
MisReportFormController.template = "mis_builder.FormView";
21 changes: 21 additions & 0 deletions mis_builder/static/src/views/form/mis_report_form_controller.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t
t-name="mis_builder.FormView"
t-inherit="web.FormView"
t-inherit-mode="primary"
owl="1"
>
<xpath expr="//Layout/t[@t-component='props.Renderer']" position="attributes">
<attribute name="globalState">props.globalState</attribute>
</xpath>
<xpath
expr="//Layout/t[@t-set-slot='layout-buttons']//t[@t-component='props.Renderer']"
position="attributes"
>
<attribute name="globalState">props.globalState</attribute>
</xpath>
</t>

</templates>
19 changes: 19 additions & 0 deletions mis_builder/static/src/views/form/mis_report_form_renderer.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @odoo-module */

import {FormRenderer} from "@web/views/form/form_renderer";
import {MisReportField} from "@mis_builder/views/field/field.esm";

export class MisReportFormRenderer extends FormRenderer {}

// Gets the global state from the attributes that have been passed by the controller.
MisReportFormRenderer.extractProps = ({attrs}) => {
return {
...FormRenderer.extractProps({attrs}),
globalState: attrs.globalState || {},
};
};

MisReportFormRenderer.components = {
...FormRenderer.components,
Field: MisReportField,
};
18 changes: 18 additions & 0 deletions mis_builder/static/src/views/form/mis_report_form_view.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @odoo-module **/

import {MisReportFormCompiler} from "@mis_builder/views/form/mis_report_form_compiler.esm";
import {MisReportFormController} from "@mis_builder/views/form/mis_report_form_controller.esm";
import {MisReportFormRenderer} from "@mis_builder/views/form/mis_report_form_renderer.esm";
import {formView} from "@web/views/form/form_view";
import {registry} from "@web/core/registry";

// The view to use when mounting `mis_report_widget` widget in order to preserve the
// filters when returning from the drill-down views.
export const misReportFormView = {
...formView,
Compiler: MisReportFormCompiler,
Controller: MisReportFormController,
Renderer: MisReportFormRenderer,
};

registry.category("views").add("mis_report_form_view", misReportFormView);
3 changes: 2 additions & 1 deletion mis_builder/views/mis_report_instance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
edit="false"
create="false"
delete="false"
js_class="mis_report_form_view"
>
<sheet>
<field
name="id"
widget="mis_report_widget"
nolabel="1"
style="width:100%"
class="w-100"
/>
</sheet>
</form>
Expand Down

0 comments on commit ae2428d

Please sign in to comment.