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 254dccc commit 914cf12
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mis_builder/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MIS Builder
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ad8a16269072f6f33f616ae2dc9b91ad0a77d135f607c4a664429781f28572c1
!! source digest: sha256:1cbdc7c822a2d4f34e29edb7301fdf8d3d884c1292e467736a94235030cbf8ec
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand Down
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
2 changes: 1 addition & 1 deletion mis_builder/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">MIS Builder</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ad8a16269072f6f33f616ae2dc9b91ad0a77d135f607c4a664429781f28572c1
!! source digest: sha256:1cbdc7c822a2d4f34e29edb7301fdf8d3d884c1292e467736a94235030cbf8ec
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/mis-builder/tree/16.0/mis_builder"><img alt="OCA/mis-builder" src="https://img.shields.io/badge/github-OCA%2Fmis--builder-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/mis-builder-16-0/mis-builder-16-0-mis_builder"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/mis-builder&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows you to build Management Information Systems dashboards.
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 914cf12

Please sign in to comment.