diff --git a/bemade_hierarchical_tree_view/static/src/components/view.js b/bemade_hierarchical_tree_view/static/src/components/view.js new file mode 100644 index 0000000..cb6cd9b --- /dev/null +++ b/bemade_hierarchical_tree_view/static/src/components/view.js @@ -0,0 +1,35 @@ +/** @odoo-module **/ + +import { View } from "@web/views/view"; +import { patch } from "@web/core/utils/patch"; +import { HierarchicalTreeViewController } from "./hierarchical_tree_view_controller"; +import { onWillRender } from "@odoo/owl"; + +patch(View.prototype, "bemade_hierarchical_tree_view.View", { + setup() { + this._super(); + /* + If we're rendering a hierarchical tree view, we need to remove the parent and + child fields from the groupBy parameters we pass down through the component + tree. This is necessary since this view type will treat the parent and child + groupings differently. We do it here instead of doing it in the controller + because the Controller would have to either modify its props or seriously + redefine the data used within the template, which we are trying to avoid for + code duplication reasons. + */ + if ( this.Controller.prototype === HierarchicalTreeViewController.prototype ) { + onWillRender(() => { + let parentField = this.Controller.parentField + let childField = this.Controller.childField + let parentIndex = this.groupBy.indexOf(parentField); + let childIndex = this.groupBy.indexOf(childField); + if (parentIndex) { + this.groupBy.splice(parentIndex, 1); + } + if (childField) { + this.groupBy.splice(childIndex, 1); + } + }); + } + }, +}); diff --git a/bemade_hierarchical_tree_view/static/src/components/with_search.js b/bemade_hierarchical_tree_view/static/src/components/with_search.js deleted file mode 100644 index 23ff743..0000000 --- a/bemade_hierarchical_tree_view/static/src/components/with_search.js +++ /dev/null @@ -1,34 +0,0 @@ -/** @odoo-module **/ - -import {patch} from "@web/core/utils/patch"; -import {WithSearch} from "@web/search/with_search/with_search"; -import {onWillRender} from "@odoo/owl"; -import {HierarchicalTreeViewController} from "./hierarchical_tree_view_controller"; - -patch(WithSearch.prototype, "bemade_hierarchical_tree_view/with_search", { - setup() { - onWillRender(() => { - let children = Object.hasOwn(this.__owl__, 'children') ? this.__owl__.children : null; - if (children && Object.keys(children).length !== 0) { - for (let childProp in children) { - let child = children[childProp]; - if (Object.hasOwn(child, 'component')) { - let childComponent = child.component; - if (childComponent.prototype == HierarchicalTreeViewController.prototype) { - let parentField, childField = (childComponent.parentField, childComponent.childField) - let parentIndex = this.groupBy.indexOf(parentField); - let childIndex = this.groupBy.indexOf(childField); - if (parentIndex) { - this.groupBy.splice(parentField, 1); - } - if (childField) { - this.groupBy.splice(childField, 1); - } - } - } - } - } - }); - this._super(); - } -}); \ No newline at end of file