diff --git a/web_widget_dropdown_dynamic/README.rst b/web_widget_dropdown_dynamic/README.rst new file mode 100644 index 000000000..21d266392 --- /dev/null +++ b/web_widget_dropdown_dynamic/README.rst @@ -0,0 +1,114 @@ +======================= +Dynamic Dropdown Widget +======================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/12.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-12-0/web-12-0-web_widget_dropdown_dynamic + :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 + :alt: Try me on Runbot + +|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 + + + + +Known issues / Roadmap +====================== + + * In v13, ``$.when`` is going to become `Promise.resolve` + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Brainbean Apps OU + +Contributors +~~~~~~~~~~~~ + +* Alexey Pelykh + +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..8ede2ec51 --- /dev/null +++ b/web_widget_dropdown_dynamic/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# 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': '12.0.1.0.0', + 'license': 'AGPL-3', + 'author': + 'Brainbean Apps OU, ' + 'Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/web/', + 'depends': [ + 'web', + ], + 'data': [ + 'templates/assets.xml', + ], + 'installable': True, +} 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..673c92c99 --- /dev/null +++ b/web_widget_dropdown_dynamic/i18n/web_widget_dropdown_dynamic.pot @@ -0,0 +1,22 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * web_widget_dropdown_dynamic +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.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:15 +#, 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..1c6a35a1e --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Alexey Pelykh 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/ROADMAP.rst b/web_widget_dropdown_dynamic/readme/ROADMAP.rst new file mode 100644 index 000000000..7186b62cf --- /dev/null +++ b/web_widget_dropdown_dynamic/readme/ROADMAP.rst @@ -0,0 +1 @@ + * In v13, ``$.when`` is going to become `Promise.resolve` diff --git a/web_widget_dropdown_dynamic/readme/USAGE.rst b/web_widget_dropdown_dynamic/readme/USAGE.rst new file mode 100644 index 000000000..f30491bc4 --- /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..d17e254a2 --- /dev/null +++ b/web_widget_dropdown_dynamic/static/description/index.html @@ -0,0 +1,465 @@ + + + + + + +Dynamic Dropdown Widget + + + +
+

Dynamic Dropdown Widget

+ + +

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

+

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"
+    values="method_name"
+    context="{'depending_on': other_field}"
+/>
+
+
+
+

Known issues / Roadmap

+
+
    +
  • In v13, $.when is going to become Promise.resolve
  • +
+
+
+
+

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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Brainbean Apps OU
  • +
+
+ +
+

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.js b/web_widget_dropdown_dynamic/static/src/js/basic_model.js new file mode 100644 index 000000000..a059366ba --- /dev/null +++ b/web_widget_dropdown_dynamic/static/src/js/basic_model.js @@ -0,0 +1,47 @@ +/* + * Copyright 2019 Brainbean Apps (https://brainbeanapps.com) + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + */ +odoo.define('web_widget_dropdown_dynamic.basic_model', function (require) { + "use strict"; + + var BasicModel = require('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 $.when(); + } + + var context = record.getContext({fieldName: fieldName}); + + // avoid rpc if not necessary + var hasChanged = this._saveSpecialDataCache(record, fieldName, { + context: context, + }); + if (!hasChanged) { + return $.when(); + } + + return this._rpc({ + model: model, + method: method, + context: context, + }); + }, + }); +}); diff --git a/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js b/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js new file mode 100644 index 000000000..0a9443127 --- /dev/null +++ b/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js @@ -0,0 +1,140 @@ +/* + * Copyright 2019 Brainbean Apps (https://brainbeanapps.com) + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + */ +odoo.define('web_widget_dropdown_dynamic.field_dynamic_dropdown', function (require) { + "use strict"; + + var core = require('web.core'); + var AbstractField = require('web.AbstractField'); + var field_registry = require('web.field_registry'); + + var _lt = core._lt; + + var FieldDynamicDropdown = AbstractField.extend({ + description: _lt('Dynamic Dropdown'), + template: 'FieldSelection', + specialData: '_fetchDynamicDropdownValues', + supportedFieldTypes: ['selection', 'char', 'integer'], + events: _.extend({}, AbstractField.prototype.events, { + 'change': '_onChange', + }), + /** + * @override + */ + init: function () { + this._super.apply(this, arguments); + this._setValues(); + }, + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * @override + * @returns {jQuery} + */ + getFocusableElement: function () { + return this.$el.is('select') ? this.$el : $(); + }, + /** + * @override + */ + isSet: function () { + return this.value !== false; + }, + /** + * Listen to modifiers updates to hide/show the falsy value in the dropdown + * according to the required modifier. + * + * @override + */ + updateModifiersValue: function () { + this._super.apply(this, arguments); + if (!this.attrs.modifiersValue.invisible && this.mode !== 'readonly') { + this._setValues(); + this._renderEdit(); + } + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + * @private + */ + _formatValue: function (value) { + var options = _.extend({}, this.nodeOptions, { data: this.recordData }, this.formatOptions); + var formattedValue = _.find(this.values, function (option) { + return option[0] === value; + }); + if (!formattedValue) { + return value; + } + formattedValue = formattedValue[1]; + if (options && options.escape) { + formattedValue = _.escape(formattedValue); + } + return formattedValue; + }, + /** + * @override + * @private + */ + _renderEdit: function () { + this.$el.empty(); + for (var i = 0 ; i < this.values.length ; i++) { + this.$el.append($('