From b875727b33c85361f8378a0f7bf5675ba77068ea Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Wed, 11 Sep 2019 14:04:03 +0200 Subject: [PATCH] [ADD] web_widget_dropdown_dynamic --- web_widget_dropdown_dynamic/__init__.py | 1 + web_widget_dropdown_dynamic/__manifest__.py | 20 ++ .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 9 + .../readme/ROADMAP.rst | 1 + web_widget_dropdown_dynamic/readme/USAGE.rst | 24 +++ .../static/src/js/basic_model.js | 47 +++++ .../static/src/js/field_dynamic_dropdown.js | 140 ++++++++++++++ .../web_widget_dropdown_dynamic_tests.js | 177 ++++++++++++++++++ .../templates/assets.xml | 19 ++ .../__init__.py | 3 + .../__manifest__.py | 20 ++ .../models/__init__.py | 3 + .../web_widget_dropdown_dynamic_example.py | 87 +++++++++ .../web_widget_dropdown_dynamic_example.xml | 58 ++++++ 15 files changed, 610 insertions(+) create mode 100644 web_widget_dropdown_dynamic/__init__.py create mode 100644 web_widget_dropdown_dynamic/__manifest__.py create mode 100644 web_widget_dropdown_dynamic/readme/CONTRIBUTORS.rst create mode 100644 web_widget_dropdown_dynamic/readme/DESCRIPTION.rst create mode 100644 web_widget_dropdown_dynamic/readme/ROADMAP.rst create mode 100644 web_widget_dropdown_dynamic/readme/USAGE.rst create mode 100644 web_widget_dropdown_dynamic/static/src/js/basic_model.js create mode 100644 web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js create mode 100644 web_widget_dropdown_dynamic/static/tests/web_widget_dropdown_dynamic_tests.js create mode 100644 web_widget_dropdown_dynamic/templates/assets.xml create mode 100644 web_widget_dropdown_dynamic_example/__init__.py create mode 100644 web_widget_dropdown_dynamic_example/__manifest__.py create mode 100644 web_widget_dropdown_dynamic_example/models/__init__.py create mode 100644 web_widget_dropdown_dynamic_example/models/web_widget_dropdown_dynamic_example.py create mode 100644 web_widget_dropdown_dynamic_example/views/web_widget_dropdown_dynamic_example.xml 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/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/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($('