diff --git a/setup/web_widget_dropdown_dynamic/odoo/addons/web_widget_dropdown_dynamic b/setup/web_widget_dropdown_dynamic/odoo/addons/web_widget_dropdown_dynamic new file mode 120000 index 000000000..df3532d14 --- /dev/null +++ b/setup/web_widget_dropdown_dynamic/odoo/addons/web_widget_dropdown_dynamic @@ -0,0 +1 @@ +../../../../web_widget_dropdown_dynamic \ No newline at end of file diff --git a/setup/web_widget_dropdown_dynamic/setup.py b/setup/web_widget_dropdown_dynamic/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_widget_dropdown_dynamic/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_widget_dropdown_dynamic/README.rst b/web_widget_dropdown_dynamic/README.rst new file mode 100644 index 000000000..6abcf43e0 --- /dev/null +++ b/web_widget_dropdown_dynamic/README.rst @@ -0,0 +1,128 @@ +======================= +Dynamic Dropdown Widget +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e2b8eb236b4dc206f3a4ab9b5932f86c2e46c33bdac31c385725384f3fe970e3 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :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/16.0/web_widget_dropdown_dynamic + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_widget_dropdown_dynamic + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Dynamic dropdown widget that supports resolving options from backend of: + + * ``fields.Char`` + * ``fields.Integer`` + * ``fields.Selection`` + +**NOTE:** This widget is not intended to *extend* ``fields.Selection``, but to +filter selection values. For fully-dynamic set of options, use ``fields.Char`` +instead. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +.. code-block:: python + + @api.model + def method_name(self): + values = [ + ('value_a', 'Title A'), + ] + if self.env.context.get('depending_on') == True: + values += [ + ('value_b', 'Title B'), + ] + return values + +.. code-block:: xml + + + + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* CorporateHub + +Contributors +~~~~~~~~~~~~ + +* `CorporateHub `__ + + * Alexey Pelykh + +* `Therp BV `__ + + * Ronald Portier + +* Thanakrit Pintana +* `Trobz `_: + + * Son Ho + +Other credits +~~~~~~~~~~~~~ + +The migration of this module from 15.0 to 16.0 was financially supported by Camptocamp + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_widget_dropdown_dynamic/__init__.py b/web_widget_dropdown_dynamic/__init__.py new file mode 100644 index 000000000..c71289ab1 --- /dev/null +++ b/web_widget_dropdown_dynamic/__init__.py @@ -0,0 +1 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). diff --git a/web_widget_dropdown_dynamic/__manifest__.py b/web_widget_dropdown_dynamic/__manifest__.py new file mode 100644 index 000000000..ad6fd5d23 --- /dev/null +++ b/web_widget_dropdown_dynamic/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2020 CorporateHub (https://corporatehub.eu) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Dynamic Dropdown Widget", + "summary": "This module adds support for dynamic dropdown widget", + "category": "Web", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "CorporateHub, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/web", + "depends": ["web"], + "installable": True, + "assets": { + "web.assets_backend": [ + "web_widget_dropdown_dynamic/**/*", + ], + }, +} diff --git a/web_widget_dropdown_dynamic/i18n/de.po b/web_widget_dropdown_dynamic/i18n/de.po new file mode 100644 index 000000000..e78a6411c --- /dev/null +++ b/web_widget_dropdown_dynamic/i18n/de.po @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_dropdown_dynamic +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2021-10-13 20:46+0000\n" +"Last-Translator: Corneliuus \n" +"Language-Team: none\n" +"Language: de\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.3.2\n" + +#. module: web_widget_dropdown_dynamic +#. openerp-web +#: code:addons/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js:0 +#, python-format +msgid "Dynamic Dropdown" +msgstr "Dynamisches Dropdown" diff --git a/web_widget_dropdown_dynamic/i18n/es.po b/web_widget_dropdown_dynamic/i18n/es.po new file mode 100644 index 000000000..39577a6ef --- /dev/null +++ b/web_widget_dropdown_dynamic/i18n/es.po @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_dropdown_dynamic +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2021-02-17 14:45+0000\n" +"Last-Translator: claudiagn \n" +"Language-Team: none\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.3.2\n" + +#. module: web_widget_dropdown_dynamic +#. openerp-web +#: code:addons/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js:0 +#, python-format +msgid "Dynamic Dropdown" +msgstr "MenĂº desplegable dinĂ¡mico" diff --git a/web_widget_dropdown_dynamic/i18n/web_widget_dropdown_dynamic.pot b/web_widget_dropdown_dynamic/i18n/web_widget_dropdown_dynamic.pot new file mode 100644 index 000000000..decac6482 --- /dev/null +++ b/web_widget_dropdown_dynamic/i18n/web_widget_dropdown_dynamic.pot @@ -0,0 +1,21 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_dropdown_dynamic +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: web_widget_dropdown_dynamic +#. openerp-web +#: code:addons/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js:0 +#, python-format +msgid "Dynamic Dropdown" +msgstr "" diff --git a/web_widget_dropdown_dynamic/readme/CONTRIBUTORS.rst b/web_widget_dropdown_dynamic/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..081566b1c --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/CONTRIBUTORS.rst @@ -0,0 +1,12 @@ +* `CorporateHub `__ + + * Alexey Pelykh + +* `Therp BV `__ + + * Ronald Portier + +* Thanakrit Pintana +* `Trobz `_: + + * Son Ho diff --git a/web_widget_dropdown_dynamic/readme/CREDITS.rst b/web_widget_dropdown_dynamic/readme/CREDITS.rst new file mode 100644 index 000000000..70bc307ad --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/CREDITS.rst @@ -0,0 +1 @@ +The migration of this module from 15.0 to 16.0 was financially supported by Camptocamp diff --git a/web_widget_dropdown_dynamic/readme/DESCRIPTION.rst b/web_widget_dropdown_dynamic/readme/DESCRIPTION.rst new file mode 100644 index 000000000..1ce39c5f7 --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/DESCRIPTION.rst @@ -0,0 +1,9 @@ +Dynamic dropdown widget that supports resolving options from backend of: + + * ``fields.Char`` + * ``fields.Integer`` + * ``fields.Selection`` + +**NOTE:** This widget is not intended to *extend* ``fields.Selection``, but to +filter selection values. For fully-dynamic set of options, use ``fields.Char`` +instead. diff --git a/web_widget_dropdown_dynamic/readme/USAGE.rst b/web_widget_dropdown_dynamic/readme/USAGE.rst new file mode 100644 index 000000000..0d74a1ece --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/USAGE.rst @@ -0,0 +1,24 @@ +.. code-block:: python + + @api.model + def method_name(self): + values = [ + ('value_a', 'Title A'), + ] + if self.env.context.get('depending_on') == True: + values += [ + ('value_b', 'Title B'), + ] + return values + +.. code-block:: xml + + + diff --git a/web_widget_dropdown_dynamic/static/description/icon.png b/web_widget_dropdown_dynamic/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/web_widget_dropdown_dynamic/static/description/icon.png differ diff --git a/web_widget_dropdown_dynamic/static/description/index.html b/web_widget_dropdown_dynamic/static/description/index.html new file mode 100644 index 000000000..99d63ad65 --- /dev/null +++ b/web_widget_dropdown_dynamic/static/description/index.html @@ -0,0 +1,481 @@ + + + + + + +Dynamic Dropdown Widget + + + +
+

Dynamic Dropdown Widget

+ + +

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

+

Dynamic dropdown widget that supports resolving options from backend of:

+
+
    +
  • fields.Char
  • +
  • fields.Integer
  • +
  • fields.Selection
  • +
+
+

NOTE: This widget is not intended to extend fields.Selection, but to +filter selection values. For fully-dynamic set of options, use fields.Char +instead.

+

Table of contents

+ +
+

Usage

+
+@api.model
+def method_name(self):
+    values = [
+        ('value_a', 'Title A'),
+    ]
+    if self.env.context.get('depending_on') == True:
+        values += [
+            ('value_b', 'Title B'),
+        ]
+    return values
+
+
+<field
+    name="other_field"
+/>
+<field
+    name="char_field"
+    widget="dynamic_dropdown"
+    options="{'values':'method_name'}"
+    context="{'depending_on': other_field}"
+/>
+
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • CorporateHub
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

The migration of this module from 15.0 to 16.0 was financially supported by Camptocamp

+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

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

+
+
+
+ + diff --git a/web_widget_dropdown_dynamic/static/src/js/basic_model.esm.js b/web_widget_dropdown_dynamic/static/src/js/basic_model.esm.js new file mode 100644 index 000000000..fa16964c6 --- /dev/null +++ b/web_widget_dropdown_dynamic/static/src/js/basic_model.esm.js @@ -0,0 +1,50 @@ +/** @odoo-module **/ +import BasicModel from "web.BasicModel"; + +BasicModel.include({ + /** + * Fetches all the values associated to the given fieldName. + * + * @param {Object} record - an element from the localData + * @param {Object} fieldName - the name of the field + * @param {Object} fieldInfo + * @returns {Promise} + * The promise is resolved with the fetched special values. + * If this data is the same as the previously fetched one + * (for the given parameters), no RPC is done and the promise + * is resolved with the undefined value. + */ + _fetchDynamicDropdownValues: function (record, fieldName, fieldInfo) { + var model = fieldInfo.options.model || record.model; + var method = fieldInfo.values || fieldInfo.options.values; + if (!method) { + return Promise.resolve(); + } + + var context = record.getContext({fieldName: fieldName}); + + // Avoid rpc if not necessary + var hasChanged = this._saveSpecialDataCache(record, fieldName, { + context: context, + }); + if (!hasChanged) { + return Promise.resolve(); + } + + return this._rpc({ + model: model, + method: method, + context: context, + }).then(function (result) { + var new_result = result.map((val_updated) => { + return val_updated.map((e) => { + if (typeof e !== "string") { + return String(e); + } + return e; + }); + }); + return new_result; + }); + }, +}); diff --git a/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.esm.js b/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.esm.js new file mode 100644 index 000000000..2f1a44880 --- /dev/null +++ b/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.esm.js @@ -0,0 +1,88 @@ +/** @odoo-module **/ +import core from "web.core"; +import {registry} from "@web/core/registry"; +import {standardFieldProps} from "@web/views/fields/standard_field_props"; +import {Component} from "@odoo/owl"; + +var _lt = core._lt; + +export class FieldDynamicDropdown extends Component { + get options() { + var field_type = this.props.record.fields[this.props.name].type || ""; + if (["char", "integer", "selection"].includes(field_type)) { + this._setValues(); + return this.props.record.fields[this.props.name].selection.filter( + (option) => option[0] !== false && option[1] !== "" + ); + } + return []; + } + + get value() { + const rawValue = this.props.value; + this.props.setDirty(false); + return this.props.type === "many2one" && rawValue ? rawValue[0] : rawValue; + } + + parseInteger(value) { + return Number(value); + } + /** + * @param {Event} ev + */ + onChange(ev) { + let lastSetValue = null; + let isInvalid = false; + var isDirty = ev.target.value !== lastSetValue; + const field = this.props.record.fields[this.props.name]; + let value = JSON.parse(ev.target.value); + if (isDirty) { + if (value && field.type === "integer") { + value = Number(value); + if (!value) { + if (this.props.record) { + this.props.record.setInvalidField(this.props.name); + } + isInvalid = true; + } + } + if (!isInvalid) { + Promise.resolve(this.props.update(value)); + lastSetValue = ev.target.value; + } + } + if (this.props.setDirty) { + this.props.setDirty(isDirty); + } + } + stringify(value) { + return JSON.stringify(value); + } + + _setValues() { + if (this.props.record.preloadedData[this.props.name]) { + var sel_value = this.props.record.preloadedData[this.props.name]; + // Convert string element to integer if field is integer + if (this.props.record.fields[this.props.name].type === "integer") { + sel_value = sel_value.map((val_updated) => { + return val_updated.map((e) => { + if (typeof e === "string" && !isNaN(Number(e))) { + return Number(e); + } + return e; + }); + }); + } + this.props.record.fields[this.props.name].selection = sel_value; + } + } +} + +FieldDynamicDropdown.description = _lt("Dynamic Dropdown"); +FieldDynamicDropdown.template = "web.SelectionField"; +FieldDynamicDropdown.legacySpecialData = "_fetchDynamicDropdownValues"; +FieldDynamicDropdown.props = { + ...standardFieldProps, +}; +FieldDynamicDropdown.supportedTypes = ["char", "integer", "selection"]; +registry.category("fields").add("dynamic_dropdown", FieldDynamicDropdown); diff --git a/web_widget_dropdown_dynamic/static/tests/web_widget_dropdown_dynamic_tests.esm.js b/web_widget_dropdown_dynamic/static/tests/web_widget_dropdown_dynamic_tests.esm.js new file mode 100644 index 000000000..295d6ad3e --- /dev/null +++ b/web_widget_dropdown_dynamic/static/tests/web_widget_dropdown_dynamic_tests.esm.js @@ -0,0 +1,182 @@ +/** @odoo-module **/ + +import {editInput, getFixture} from "@web/../tests/helpers/utils"; +import {makeView, setupViewRegistries} from "@web/../tests/views/helpers"; +const {QUnit} = window; + +let serverData = {}; +let target = getFixture(); + +QUnit.module("web_widget_dropdown_dynamic", (hooks) => { + hooks.beforeEach(() => { + target = getFixture(); + serverData = { + models: { + "sale.order": { + fields: { + content_string: {string: "Content", type: "char"}, + bool_field: {string: "Boolean", type: "boolean"}, + content_integer: {string: "Integer", type: "integer"}, + change_field: {string: "Change", type: "char"}, + content_selection: { + string: "Selection", + type: "selection", + selection: [["default", "Default"]], + }, + }, + records: [ + {id: 1, bool_field: false, change_field: ""}, + {id: 2, bool_field: true, change_field: ""}, + ], + methods: { + method_name() { + return Promise.resolve([["value a", "Value A"]]); + }, + }, + }, + }, + }; + setupViewRegistries(); + }); + QUnit.test("values are fetched with changing context", async function (assert) { + assert.expect(13); + + await makeView({ + type: "form", + resModel: "sale.order", + serverData, + arch: ` +
+ + + `, + resId: 1, + mockRPC: function (route, args) { + assert.step(args.method); + if (args.method === "method_name") { + if (args.kwargs.context.depending_on === "step-1") { + return Promise.resolve([["value", "Title"]]); + } else if (args.kwargs.context.depending_on === "step-2") { + return Promise.resolve([ + ["value", "Title"], + ["value_2", "Title 2"], + ]); + } + return Promise.resolve([]); + } + }, + }); + + await editInput(target, ".o_field_widget[name='change_field'] input", "step-1"); + assert.containsN(target, "option", 2); + assert.containsOnce(target, "option[value='\"value\"']"); + await editInput(target, ".o_field_widget[name='change_field'] input", "step-2"); + + assert.containsN(target, "option", 3); + assert.containsOnce(target, "option[value='\"value\"']"); + assert.containsOnce(target, "option[value='\"value_2\"']"); + await editInput( + target, + ".o_field_widget[name='change_field'] input", + "step-other" + ); + + assert.containsN(target, "option", 1); + assert.verifySteps([ + "get_views", + "read", + "method_name", + "method_name", + "method_name", + "method_name", + ]); + }); + QUnit.test("values are fetched w/o context (char)", async (assert) => { + assert.expect(6); + console.log("Start assert", serverData); + console.log("Start makeView"); + await makeView({ + type: "form", + resModel: "sale.order", + serverData, + arch: ` +
+ + + `, + resId: 2, + mockRPC(route, args) { + assert.step(args.method); + if (args.method === "method_name") { + if (args.kwargs.context.depending_on) { + return Promise.resolve([["value b", "Value B"]]); + } + } + }, + }); + const field_target = target.querySelector("div[name='content_string']"); + assert.verifySteps(["get_views", "read", "method_name"]); + assert.containsN(field_target, "option", 2); + assert.containsOnce( + field_target, + "option[value='\"value b\"']", + "got `value b` " + ); + + console.log("Ending makeView", target); + }); + + QUnit.test("values are fetched w/o context (integer)", async (assert) => { + assert.expect(6); + await makeView({ + type: "form", + resModel: "sale.order", + serverData, + arch: ` +
+ + + `, + resId: 2, + mockRPC(route, args) { + assert.step(args.method); + if (args.method === "method_name") { + if (args.kwargs.context.depending_on) { + return Promise.resolve([["10", "Value B"]]); + } + } + }, + }); + const field_target = target.querySelector("div[name='content_integer']"); + assert.verifySteps(["get_views", "read", "method_name"]); + assert.containsN(field_target, "option", 2); + assert.containsOnce(field_target, 'option[value="10"]'); + }); + + QUnit.test("values are fetched w/o context (selection)", async (assert) => { + assert.expect(6); + await makeView({ + type: "form", + resModel: "sale.order", + serverData, + arch: ` +
+ + + `, + resId: 2, + mockRPC(route, args) { + assert.step(args.method); + if (args.method === "method_name") { + if (args.kwargs.context.depending_on) { + return Promise.resolve([["choice b", "Choice B"]]); + } + } + }, + }); + const field_target = target.querySelector("div[name='content_selection']"); + assert.verifySteps(["get_views", "read", "method_name"]); + assert.containsN(field_target, "option", 2); + assert.containsOnce(field_target, "option[value='\"choice b\"']"); + }); +});