From 92e0b435a9a0a97fb053c71031c89411493c3b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20D=C3=ADaz?= Date: Wed, 18 Mar 2020 03:32:52 +0100 Subject: [PATCH] [MIG] web_advance_search: Migration to 13.0 --- web_advanced_search/README.rst | 21 ++- web_advanced_search/readme/CONTRIBUTORS.rst | 7 +- web_advanced_search/readme/ROADMAP.rst | 4 + .../static/description/index.html | 18 ++- .../static/src/js/human_domain.js | 55 +++---- .../static/src/js/web_advanced_search.js | 139 +++++++++--------- .../static/src/xml/web_advanced_search.xml | 15 +- 7 files changed, 141 insertions(+), 118 deletions(-) diff --git a/web_advanced_search/README.rst b/web_advanced_search/README.rst index 54d2fcebb..631fb9945 100644 --- a/web_advanced_search/README.rst +++ b/web_advanced_search/README.rst @@ -14,13 +14,13 @@ Advanced search :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github - :target: https://github.com/OCA/web/tree/12.0/web_advanced_search + :target: https://github.com/OCA/web/tree/13.0/web_advanced_search :alt: OCA/web .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/web-12-0/web-12-0-web_advanced_search + :target: https://translation.odoo-community.org/projects/web-13-0/web-13-0-web_advanced_search :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/162/12.0 + :target: https://runbot.odoo-community.org/runbot/162/13.0 :alt: Try me on Runbot |badge1| |badge2| |badge3| |badge4| |badge5| @@ -71,6 +71,10 @@ Improvements to the search view in this addon: * Use widgets ``many2many_tags`` when searching ``many2many`` fields * Allow to edit current full search using the advanced domain editor +Issues: + +* Grouped totals can show incorrect values. See https://github.com/odoo/odoo/issues/47950 + Changelog ========= @@ -121,7 +125,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -138,12 +142,15 @@ Contributors ~~~~~~~~~~~~ * Holger Brunn -* Vicent Cubells -* Jairo Llopis * Rami Alwafaie * Jose Mª Bernet * Simone Orsi * Dennis Sluijk +* `Tecnativa `_: + + * Vicent Cubells + * Jairo Llopis + * Alexandre Díaz Maintainers ~~~~~~~~~~~ @@ -158,6 +165,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/web `_ project on GitHub. +This module is part of the `OCA/web `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_advanced_search/readme/CONTRIBUTORS.rst b/web_advanced_search/readme/CONTRIBUTORS.rst index 89b679a1f..d0917fc03 100644 --- a/web_advanced_search/readme/CONTRIBUTORS.rst +++ b/web_advanced_search/readme/CONTRIBUTORS.rst @@ -1,7 +1,10 @@ * Holger Brunn -* Vicent Cubells -* Jairo Llopis * Rami Alwafaie * Jose Mª Bernet * Simone Orsi * Dennis Sluijk +* `Tecnativa `_: + + * Vicent Cubells + * Jairo Llopis + * Alexandre Díaz diff --git a/web_advanced_search/readme/ROADMAP.rst b/web_advanced_search/readme/ROADMAP.rst index 8a0a68717..b71b3c344 100644 --- a/web_advanced_search/readme/ROADMAP.rst +++ b/web_advanced_search/readme/ROADMAP.rst @@ -8,3 +8,7 @@ Improvements to the search view in this addon: * Use widgets ``one2many_tags`` when searching ``one2many`` fields * Use widgets ``many2many_tags`` when searching ``many2many`` fields * Allow to edit current full search using the advanced domain editor + +Issues: + +* Grouped totals can show incorrect values. See https://github.com/odoo/odoo/issues/47950 diff --git a/web_advanced_search/static/description/index.html b/web_advanced_search/static/description/index.html index 0f6d59e61..cb77eb4f1 100644 --- a/web_advanced_search/static/description/index.html +++ b/web_advanced_search/static/description/index.html @@ -367,7 +367,7 @@ ul.auto-toc { !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

+

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

More powerful and easy to use search, especially for related fields.

Table of contents

@@ -426,6 +426,10 @@ to a journal which has a user who is member of a certain group etc.

  • Use widgets many2many_tags when searching many2many fields
  • Allow to edit current full search using the advanced domain editor
  • +

    Issues:

    +

    Changelog

    @@ -473,7 +477,7 @@ search as expected too.

    Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -feedback.

    +feedback.

    Do not contact contributors directly about support or help with technical issues.

    @@ -489,12 +493,16 @@ If you spotted it first, help us smashing it by providing a detailed and welcome

    Contributors

    @@ -504,7 +512,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome

    OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

    -

    This module is part of the OCA/web project on GitHub.

    +

    This module is part of the OCA/web project on GitHub.

    You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

    diff --git a/web_advanced_search/static/src/js/human_domain.js b/web_advanced_search/static/src/js/human_domain.js index 82bfebef9..53772d791 100644 --- a/web_advanced_search/static/src/js/human_domain.js +++ b/web_advanced_search/static/src/js/human_domain.js @@ -1,69 +1,58 @@ /* Copyright 2018 Tecnativa - Jairo Llopis + * Copyright 2020 Tecnativa - Alexandre Díaz * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ -odoo.define("web_advanced_search.human_domain", function(require) { +odoo.define("web_advanced_search.human_domain", function() { "use strict"; - var DomainSelector = require("web.DomainSelector"); - - var join_mapping = { + const join_mapping = { "&": _(" and "), "|": _(" or "), "!": _(" is not "), }; - // HACK I should extend classes, but they are not exposed - // TODO Remove file when merged https://github.com/odoo/odoo/pull/25922 - var human_domain_methods = { + const human_domain_methods = { DomainTree: function() { - var human_domains = []; - _.each(this.children, function(child) { + const human_domains = []; + _.each(this.children, child => { human_domains.push(human_domain_methods[child.template].apply(child)); }); - return _.str.sprintf( - "(%s)", - human_domains.join(join_mapping[this.operator]) - ); + return `(${human_domains.join(join_mapping[this.operator])})`; }, DomainSelector: function() { - var result = human_domain_methods.DomainTree.apply(this, arguments); + const result = human_domain_methods.DomainTree.apply(this, arguments); // Remove surrounding parenthesis return result.slice(1, -1); }, DomainLeaf: function() { - var chain = [], - operator = this.operator_mapping[this.operator], - value = _.str.sprintf('"%s"', this.value); + const chain = []; + let operator = this.operator_mapping[this.operator], + value = `"${this.value}"`; // Humanize chain - this.chain.split(".").forEach(function(element, index) { + const chain_splitted = this.chain.split("."); + const len = chain_splitted.length; + for (let x = 0; x < len; ++x) { + const element = chain_splitted[x]; chain.push( - _.findWhere(this.fieldSelector.pages[index], {name: element}) - .string || element + _.findWhere(this.fieldSelector.pages[x], {name: element}).string || + element ); - }, this); + } // Special beautiness for some values if (this.operator === "=" && _.isBoolean(this.value)) { operator = this.operator_mapping[this.value ? "set" : "not set"]; value = ""; } else if (_.isArray(this.value)) { - value = _.str.sprintf('["%s"]', this.value.join('", "')); + value = `["${this.value.join('", "')}"]`; } - return _.str - .sprintf("%s %s %s", chain.join("→"), operator || this.operator, value) - .trim(); + return `${chain.join("→")} ${operator || this.operator} ${value}`.trim(); }, }; - function getHumanDomain(parent, model, domain, options) { - var domain_selector = new DomainSelector(parent, model, domain, options); - var dummy_parent = $("
    "); - domain_selector.appendTo(dummy_parent); - var result = human_domain_methods.DomainSelector.apply(domain_selector); - domain_selector.destroy(); - dummy_parent.destroy(); - return result; + function getHumanDomain(domain_selector) { + return human_domain_methods.DomainSelector.apply(domain_selector); } return { diff --git a/web_advanced_search/static/src/js/web_advanced_search.js b/web_advanced_search/static/src/js/web_advanced_search.js index 41faf6d88..15724b0c0 100644 --- a/web_advanced_search/static/src/js/web_advanced_search.js +++ b/web_advanced_search/static/src/js/web_advanced_search.js @@ -1,44 +1,27 @@ /* Copyright 2015 Therp BV * Copyright 2017-2018 Jairo Llopis + * Copyright 2020 Alexandre Díaz * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */ odoo.define("web_advanced_search", function(require) { "use strict"; - var core = require("web.core"); - var Domain = require("web.Domain"); - var DomainSelectorDialog = require("web.DomainSelectorDialog"); - var field_registry = require("web.field_registry"); - var FieldManagerMixin = require("web.FieldManagerMixin"); - var FiltersMenu = require("web.FiltersMenu"); - var human_domain = require("web_advanced_search.human_domain"); - var SearchView = require("web.SearchView"); - var Widget = require("web.Widget"); - var Char = core.search_filters_registry.get("char"); - - SearchView.include({ - custom_events: _.extend({}, SearchView.prototype.custom_events, { - get_dataset: "_on_get_dataset", - }), - - /** - * Add or update a `dataset` attribute in event target - * - * The search view dataset includes things such as the model, which - * is required to make some parts of search views smarter. - * - * @param {OdooEvent} event The target will get the dataset. - */ - _on_get_dataset: function(event) { - event.target.dataset = this.dataset; - event.stopPropagation(); - }, - }); + const config = require("web.config"); + const Domain = require("web.Domain"); + const DomainSelector = require("web.DomainSelector"); + const DomainSelectorDialog = require("web.DomainSelectorDialog"); + const field_registry = require("web.field_registry"); + const FieldManagerMixin = require("web.FieldManagerMixin"); + const FilterMenu = require("web.FilterMenu"); + const human_domain = require("web_advanced_search.human_domain"); + const Widget = require("web.Widget"); + const search_filters_registry = require("web.search_filters_registry"); + const Char = search_filters_registry.get("char"); /** * An almost dummy search proposition, to use with domain widget */ - var AdvancedSearchProposition = Widget.extend({ + const AdvancedSearchProposition = Widget.extend({ /** * @override */ @@ -47,38 +30,49 @@ odoo.define("web_advanced_search", function(require) { this.model = model; this.domain = new Domain(domain); this.domain_array = domain; + this._createDomainSelector(); }, /** * Produce a filter descriptor for advanced searches. * - * @returns {Object} In the format expected by `web.FiltersMenu`. + * @returns {Object} In the format expected by `web.FilterMenu`. */ get_filter: function() { return { attrs: { domain: this.domain_array, - // TODO Remove when merged - // https://github.com/odoo/odoo/pull/25922 - string: human_domain.getHumanDomain( - this, - this.model, - this.domain_array - ), + string: human_domain.getHumanDomain(this.domain_selector), }, children: [], tag: "filter", }; }, + + _createDomainSelector: function() { + this.domain_selector = new DomainSelector( + this, + this.model, + this.domain_array + ); + this.dummy_parent = $("
    "); + return this.domain_selector.appendTo(this.dummy_parent); + }, + + destroy: function() { + this.domain_selector.destroy(); + this.dummy_parent.remove(); + return this._super.apply(this, arguments); + }, }); // Add advanced search features - FiltersMenu.include({ - custom_events: _.extend({}, FiltersMenu.prototype.custom_events, { + FilterMenu.include({ + custom_events: _.extend({}, FilterMenu.prototype.custom_events, { domain_selected: "advanced_search_commit", }), - events: _.extend({}, FiltersMenu.prototype.events, { + events: _.extend({}, FilterMenu.prototype.events, { "click .o_add_advanced_search": "advanced_search_open", }), @@ -87,7 +81,9 @@ odoo.define("web_advanced_search", function(require) { */ init: function() { this._super.apply(this, arguments); - this.trigger_up("get_dataset"); + + this._context = this.getParent().context; + this._modelName = this.getParent().getParent().modelName; }, /** @@ -96,16 +92,16 @@ odoo.define("web_advanced_search", function(require) { * @returns {$.Deferred} The opening dialog itself. */ advanced_search_open: function() { - var domain_selector_dialog = new DomainSelectorDialog( + const domain_selector_dialog = new DomainSelectorDialog( this, - this.dataset.model, + this._modelName, "[]", { - debugMode: core.debug, + debugMode: config.debug, readonly: false, } ); - domain_selector_dialog.opened(function() { + domain_selector_dialog.opened(() => { // Add 1st domain node by default domain_selector_dialog.domainSelector._onAddFirstButtonClick(); }); @@ -119,13 +115,19 @@ odoo.define("web_advanced_search", function(require) { */ advanced_search_commit: function(event) { _.invoke(this.propositions, "destroy"); - var proposition = new AdvancedSearchProposition( + const proposition = new AdvancedSearchProposition( this, - this.dataset.model, + this._modelName, event.data.domain ); - this.propositions = [proposition]; - this._commitSearch(); + // Necessary to ensure that the porposition have the 'fieldSelector' + // is filled + _.defer( + function() { + this.propositions = [proposition]; + this._commitSearch(); + }.bind(this) + ); }, }); @@ -139,7 +141,7 @@ odoo.define("web_advanced_search", function(require) { * model implementation, which can only hold fake data, given a search view * has no data on it by definition. */ - var Relational = Char.extend(FieldManagerMixin, { + const Relational = Char.extend(FieldManagerMixin, { tagName: "div", className: "x2x_container", attributes: {}, @@ -153,7 +155,7 @@ odoo.define("web_advanced_search", function(require) { FieldManagerMixin.init.call(this); this.trigger_up("get_dataset"); // Make equal and not equal appear 1st and 2nd - this.operators = _.sortBy(this.operators, function(op) { + this.operators = _.sortBy(this.operators, op => { switch (op.value) { case "=": return -2; @@ -164,26 +166,25 @@ odoo.define("web_advanced_search", function(require) { } }); // Create dummy record with only the field the user is searching - var params = { + const params = { fieldNames: [this.field.name], modelName: this.dataset.model, context: this.dataset.context, - fields: {}, type: "record", viewType: "default", fieldsInfo: { default: {}, }, + fields: { + [this.field.name]: _.omit( + this.field, + // User needs all records, to actually produce a new domain + "domain", + // Onchanges make no sense in this context, there's no record + "onChange" + ), + }, }; - // See https://stackoverflow.com/a/11508530/1468388 - // to know how to include this in the previous step in ES6 - params.fields[this.field.name] = _.omit( - this.field, - // User needs all records, to actually produce a new domain - "domain", - // Onchanges make no sense in this context, there's no record - "onChange" - ); if (this.field.type.endsWith("2many")) { // X2many fields behave like m2o in the search context params.fields[this.field.name].type = "many2one"; @@ -200,7 +201,7 @@ odoo.define("web_advanced_search", function(require) { * @override */ start: function() { - var result = this._super.apply(this, arguments); + const result = this._super.apply(this, arguments); // Render the initial widget result.done($.proxy(this, "show_inputs", $(""))); return result; @@ -240,14 +241,14 @@ odoo.define("web_advanced_search", function(require) { default: this._field_widget_name = "char"; } - var _Widget = field_registry.get(this._field_widget_name); + const _Widget = field_registry.get(this._field_widget_name); // Destroy previous widget, if any if (this._field_widget) { this._field_widget.destroy(); delete this._field_widget; } // Create new widget - var options = { + const options = { mode: "edit", attrs: { options: { @@ -283,7 +284,9 @@ odoo.define("web_advanced_search", function(require) { return FieldManagerMixin._applyChanges.apply(this, arguments); } - return $.Deferred().resolve(); + return new Promise(resolve => { + resolve(); + }); }, /** @@ -335,7 +338,7 @@ odoo.define("web_advanced_search", function(require) { }); // Register search filter widgets - core.search_filters_registry + search_filters_registry .add("many2many", Relational) .add("many2one", Relational) .add("one2many", Relational); diff --git a/web_advanced_search/static/src/xml/web_advanced_search.xml b/web_advanced_search/static/src/xml/web_advanced_search.xml index a7d25e4f3..67f0f6468 100644 --- a/web_advanced_search/static/src/xml/web_advanced_search.xml +++ b/web_advanced_search/static/src/xml/web_advanced_search.xml @@ -2,10 +2,19 @@ - + -