[MIG] web_advanced_search: Migration to 16.0

This commit is contained in:
hanamanth.gollar
2022-12-09 16:24:06 +05:30
committed by Raf Ven
parent 1681eef37f
commit c77f504757
12 changed files with 135 additions and 92 deletions

View File

@@ -2,12 +2,29 @@
import BasicModel from "web.BasicModel";
import {ComponentAdapter} from "web.OwlCompatibility";
import {FieldMany2One} from "web.relational_fields";
import {Dropdown} from "@web/core/dropdown/dropdown";
import FieldManagerMixin from "web.FieldManagerMixin";
import {FieldMany2One} from "web.relational_fields";
import {SelectCreateDialog} from "web.view_dialogs";
import {patch} from "@web/core/utils/patch";
import {session} from "@web/session";
const {Component} = owl;
const {xml} = owl.tags;
const {Component, xml} = owl;
patch(Dropdown.prototype, "dropdown", {
onWindowClicked(ev) {
// This patch is created to prevent the closing of the Filter menu
// when a selection is made in the RecordPicker
if (
$(ev.target.closest("ul.dropdown-menu")).attr("id") !== undefined &&
$(ev.target.closest("ul.dropdown-menu")).attr("id") ===
$("body > ul.dropdown-menu").attr("id")
) {
return;
}
this._super(ev);
},
});
export const FakeMany2oneFieldWidget = FieldMany2One.extend(FieldManagerMixin, {
/**
@@ -55,6 +72,7 @@ export const FakeMany2oneFieldWidget = FieldMany2One.extend(FieldManagerMixin, {
* Get record
*
* @param {BasicModel} model
* @returns {String}
*/
_get_record: function (model) {
return model.get(this.dataPointID);
@@ -67,16 +85,6 @@ export const FakeMany2oneFieldWidget = FieldMany2One.extend(FieldManagerMixin, {
this.dataPointID = id;
return this.reset(this._get_record(this.model), event);
},
/**
* Stop propagation of the autocompleteselect event.
* Otherwise, the filter's dropdown will be closed after a selection.
*
* @override to stop propagating autocompleteselect event
*/
start: function () {
this._super(...arguments);
this.$input.on("autocompleteselect", (event) => event.stopPropagation());
},
/**
* Stop propagation of the 'Search more..' dialog click event.
* Otherwise, the filter's dropdown will be closed after a selection.
@@ -102,17 +110,56 @@ export const FakeMany2oneFieldWidget = FieldMany2One.extend(FieldManagerMixin, {
);
return dialog.open();
},
_onFieldChanged: function (event) {
const self = this;
event.stopPropagation();
if (event.data.changes.dummy.display_name === undefined) {
return this._rpc({
model: this.field.relation,
method: "name_get",
args: [event.data.changes.dummy.id],
context: session.user_context,
}).then(function (result) {
event.data.changes.dummy.display_name = result[0][1];
return (
self
._applyChanges(
event.data.dataPointID,
event.data.changes,
event
)
// eslint-disable-next-line no-empty-function
.then(event.data.onSuccess || function () {})
// eslint-disable-next-line no-empty-function
.guardedCatch(event.data.onFailure || function () {})
);
});
}
return (
this._applyChanges(event.data.dataPointID, event.data.changes, event)
// eslint-disable-next-line no-empty-function
.then(event.data.onSuccess || function () {})
// eslint-disable-next-line no-empty-function
.guardedCatch(event.data.onFailure || function () {})
);
},
});
export class FakeMany2oneFieldWidgetAdapter extends ComponentAdapter {
setup() {
this.env = owl.Component.env;
constructor() {
super(...arguments);
this.env = Component.env;
}
async updateWidget() {
/* eslint-disable no-empty-function */
renderWidget() {
this.widget._render();
}
async renderWidget() {
/* eslint-disable no-empty-function */
get widgetArgs() {
if (this.props.widgetArgs) {
return this.props.widgetArgs;
}
return [this.props.attrs];
}
}

View File

@@ -1,68 +0,0 @@
/** @odoo-module **/
import {getHumanDomain} from "../utils.esm";
import config from "web.config";
import DomainSelectorDialog from "web.DomainSelectorDialog";
import Domain from "web.Domain";
import {useModel} from "web.Model";
const {Component, hooks} = owl;
const {useRef} = hooks;
export default class AdvancedFilterItem extends Component {
setup() {
this.itemRef = useRef("dropdown-item");
this.model = useModel("searchModel");
}
/**
* Prevent propagation of dropdown-item-selected event, so that it
* doesn't reaches the FilterMenu onFilterSelected event handler.
*/
mounted() {
$(this.itemRef.el).on("dropdown-item-selected", (event) =>
event.stopPropagation()
);
}
/**
* Open advanced search dialog
*
* @returns {DomainSelectorDialog} The opened dialog itself.
*/
onClick() {
const dialog = new DomainSelectorDialog(
this,
this.model.config.modelName,
"[]",
{
debugMode: config.isDebug(),
readonly: false,
}
);
// Add 1st domain node by default
dialog.opened(() => dialog.domainSelector._onAddFirstButtonClick());
// Configure handler
dialog.on("domain_selected", this, function (e) {
const preFilter = {
description: getHumanDomain(dialog.domainSelector),
domain: Domain.prototype.arrayToString(e.data.domain),
type: "filter",
};
this.model.dispatch("createNewFilters", [preFilter]);
});
return dialog.open();
}
/**
* Mocks _trigger_up to redirect Odoo legacy events to OWL events.
*
* @private
* @param {OdooEvent} event
*/
_trigger_up(event) {
const {name, data} = event;
data.__targetWidget = event.target;
this.trigger(name.replace(/_/g, "-"), data);
}
}
AdvancedFilterItem.template = "web_advanced_search.AdvancedFilterItem";

View File

@@ -1,93 +0,0 @@
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import CustomFilterItem from "web.CustomFilterItem";
import {RecordPicker} from "../RecordPicker.esm";
/**
* Patches the CustomFilterItem for legacy widgets.
*
* Tree views still use this old legacy widget, so we need to patch it.
* This is likely to dissapear in 16.0
*/
patch(CustomFilterItem.prototype, "web_advanced_search.legacy.CustomFilterItem", {
/**
* Ideally we'd want this in setup, but CustomFilterItem does its initialization
* in the constructor, which can't be patched.
*
* Doing it here works just as well.
*
* @override
*/
async willStart() {
this.OPERATORS.relational = this.OPERATORS.char;
this.FIELD_TYPES.many2one = "relational";
return this._super(...arguments);
},
/**
* @override
*/
_setDefaultValue(condition) {
const res = this._super(...arguments);
const fieldType = this.fields[condition.field].type;
const genericType = this.FIELD_TYPES[fieldType];
if (genericType === "relational") {
condition.value = 0;
condition.displayedValue = "";
}
return res;
},
/**
* Add displayed value to preFilters for "relational" types.
*
* @override
*/
onApply() {
// To avoid the complete override, we patch this.conditions.map()
const originalMapFn = this.conditions.map;
const self = this;
this.conditions.map = function () {
const preFilters = originalMapFn.apply(this, arguments);
for (const condition of this) {
const field = self.fields[condition.field];
const type = self.FIELD_TYPES[field.type];
if (type === "relational") {
const idx = this.indexOf(condition);
const preFilter = preFilters[idx];
const operator = self.OPERATORS[type][condition.operator];
const descriptionArray = [
field.string,
operator.description,
`"${condition.displayedValue}"`,
];
preFilter.description = descriptionArray.join(" ");
}
}
return preFilters;
};
const res = this._super(...arguments);
// Restore original map()
this.conditions.map = originalMapFn;
return res;
},
/**
* @private
* @param {Object} condition
* @param {OwlEvent} ev
*/
onRelationalChanged(condition, ev) {
if (ev.detail) {
condition.value = ev.detail.id;
condition.displayedValue = ev.detail.display_name;
}
},
});
patch(CustomFilterItem, "web_advanced_search.legacy.CustomFilterItem", {
components: {
...CustomFilterItem.components,
RecordPicker,
},
});
export default CustomFilterItem;

View File

@@ -1,20 +0,0 @@
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import FilterMenu from "web.FilterMenu";
import AdvancedFilterItem from "./AdvancedFilterItem.esm";
/**
* Patches the FilterMenu for legacy widgets.
*
* Tree views still use this old legacy widget, so we need to patch it.
* This is likely to dissapear in 16.0
*/
patch(FilterMenu, "web_advanced_search.legacy.FilterMenu", {
components: {
...FilterMenu.components,
AdvancedFilterItem,
},
});
export default FilterMenu;

View File

@@ -1,57 +0,0 @@
/** @odoo-module **/
import {getHumanDomain} from "../utils.esm";
import config from "web.config";
import DomainSelectorDialog from "web.DomainSelectorDialog";
import Domain from "web.Domain";
import {ComponentAdapter} from "web.OwlCompatibility";
const {Component, hooks} = owl;
const {useRef} = hooks;
export default class AdvancedFilterItem extends Component {
setup() {
this.itemRef = useRef("dropdown-item");
}
/**
* Prevent propagation of dropdown-item-selected event, so that it
* doesn't reaches the FilterMenu onFilterSelected event handler.
*/
mounted() {
$(this.itemRef.el).on("dropdown-item-selected", (event) =>
event.stopPropagation()
);
}
/**
* Open advanced search dialog
*
* @returns {DomainSelectorDialog} The opened dialog itself.
*/
onClick() {
const adapterParent = new ComponentAdapter(null, {Component});
const dialog = new DomainSelectorDialog(
adapterParent,
this.env.searchModel.resModel,
"[]",
{
debugMode: config.isDebug(),
readonly: false,
}
);
// Add 1st domain node by default
dialog.opened(() => dialog.domainSelector._onAddFirstButtonClick());
// Configure handler
dialog.on("domain_selected", this, function (e) {
const preFilter = {
description: getHumanDomain(dialog.domainSelector),
domain: Domain.prototype.arrayToString(e.data.domain),
type: "filter",
};
this.env.searchModel.createNewFilters([preFilter]);
});
return dialog.open();
}
}
AdvancedFilterItem.template = "web_advanced_search.AdvancedFilterItem";

View File

@@ -1,89 +0,0 @@
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import {CustomFilterItem} from "@web/search/filter_menu/custom_filter_item";
import {RecordPicker} from "../RecordPicker.esm";
/**
* Patches the CustomFilterItem for owl widgets.
*
* Pivot and Graph views use this new owl widget, so we need to patch it.
* Other views like Tree use the old legacy widget that will probably dissapear
* in 16.0. Until then, we need to patch both.
*/
patch(CustomFilterItem.prototype, "web_advanced_search.CustomFilterItem", {
/**
* @override
*/
setup() {
this._super.apply(this, arguments);
this.OPERATORS.relational = this.OPERATORS.char;
this.FIELD_TYPES.many2one = "relational";
},
/**
* @override
*/
setDefaultValue(condition) {
const res = this._super.apply(this, arguments);
const fieldType = this.fields[condition.field].type;
const genericType = this.FIELD_TYPES[fieldType];
if (genericType === "relational") {
condition.value = 0;
condition.displayedValue = "";
}
return res;
},
/**
* Add displayed value to preFilters for "relational" types.
*
* @override
*/
onApply() {
// To avoid the complete override, we patch this.conditions.map()
const originalMapFn = this.conditions.map;
const self = this;
this.conditions.map = function () {
const preFilters = originalMapFn.apply(this, arguments);
for (const condition of this) {
const field = self.fields[condition.field];
const type = self.FIELD_TYPES[field.type];
if (type === "relational") {
const idx = this.indexOf(condition);
const preFilter = preFilters[idx];
const operator = self.OPERATORS[type][condition.operator];
const descriptionArray = [
field.string,
operator.description,
`"${condition.displayedValue}"`,
];
preFilter.description = descriptionArray.join(" ");
}
}
return preFilters;
};
const res = this._super.apply(this, arguments);
// Restore original map()
this.conditions.map = originalMapFn;
return res;
},
/**
* @private
* @param {Object} condition
* @param {OwlEvent} ev
*/
onRelationalChanged(condition, ev) {
if (ev.detail) {
condition.value = ev.detail.id;
condition.displayedValue = ev.detail.display_name;
}
},
});
patch(CustomFilterItem, "web_advanced_search.CustomFilterItem", {
components: {
...CustomFilterItem.components,
RecordPicker,
},
});
export default CustomFilterItem;

View File

@@ -1,21 +0,0 @@
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import {FilterMenu} from "@web/search/filter_menu/filter_menu";
import AdvancedFilterItem from "./AdvancedFilterItem.esm";
/**
* Patches the FilterMenu for owl widgets.
*
* Pivot and Graph views use this new owl widget, so we need to patch it.
* Other views like Tree use the old legacy widget that will probably dissapear
* in 16.0. Until then, we need to patch both.
*/
patch(FilterMenu, "web_advanced_search.FilterMenu", {
components: {
...FilterMenu.components,
AdvancedFilterItem,
},
});
export default FilterMenu;

View File

@@ -7,7 +7,6 @@
*/
import {_t} from "web.core";
const JOIN_MAPPING = {
"&": _t(" and "),
"|": _t(" or "),
@@ -39,8 +38,8 @@ const HUMAN_DOMAIN_METHODS = {
for (let x = 0; x < len; ++x) {
const element = chain_splitted[x];
chain.push(
_.findWhere(this.fieldSelector.pages[x], {name: element}).string ||
element
_.findWhere(this.fieldSelector.popover.pages[x], {name: element})
.string || element
);
}
// Special beautiness for some values