forked from Techsystech/web
[ADD] web_widget_dropdown_dynamic
parent
85e7fc31a8
commit
b875727b33
|
@ -0,0 +1 @@
|
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
|
@ -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,
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
* Alexey Pelykh <alexey.pelykh@brainbeanapps.com>
|
|
@ -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.
|
|
@ -0,0 +1 @@
|
|||
* In v13, ``$.when`` is going to become `Promise.resolve`
|
|
@ -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
|
||||
|
||||
<field
|
||||
name="other_field"
|
||||
/>
|
||||
<field
|
||||
name="char_field"
|
||||
widget="dynamic_dropdown"
|
||||
values="method_name"
|
||||
context="{'depending_on': other_field}"
|
||||
/>
|
|
@ -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<any>}
|
||||
* 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,
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
|
@ -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($('<option/>', {
|
||||
value: JSON.stringify(this.values[i][0]),
|
||||
text: this.values[i][1]
|
||||
}));
|
||||
}
|
||||
this.$el.val(JSON.stringify(this.value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_renderReadonly: function () {
|
||||
this.$el.empty().text(this._formatValue(this.value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
_reset: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this._setValues();
|
||||
},
|
||||
/**
|
||||
* Sets the possible field values.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_setValues: function () {
|
||||
this.values = _.reject(this.record.specialData[this.name], function (v) {
|
||||
return v[0] === false && v[1] === '';
|
||||
});
|
||||
if (!this.attrs.modifiersValue || !this.attrs.modifiersValue.required) {
|
||||
this.values = [[false, this.attrs.placeholder || '']].concat(this.values);
|
||||
}
|
||||
},
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Handlers
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_onChange: function () {
|
||||
var value = JSON.parse(this.$el.val());
|
||||
this._setValue(value.toString());
|
||||
},
|
||||
});
|
||||
field_registry.add('dynamic_dropdown', FieldDynamicDropdown);
|
||||
|
||||
return FieldDynamicDropdown;
|
||||
});
|
|
@ -0,0 +1,177 @@
|
|||
odoo.define('web_widget_dropdown_dynamic.web_widget_dropdown_dynamic_tests', function (require) {
|
||||
"use strict";
|
||||
|
||||
var FormView = require('web.FormView');
|
||||
var testUtils = require('web.test_utils');
|
||||
|
||||
QUnit.module('web_widget_dropdown_dynamic', {}, function () {
|
||||
|
||||
QUnit.test('values are fetched w/o context (char)', async function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var form = await testUtils.createView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'char'},
|
||||
},
|
||||
records: [{id: 1, test_field: ''}],
|
||||
},
|
||||
},
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field" widget="dynamic_dropdown" values="_get_test_field_values"/>' +
|
||||
'</form>',
|
||||
mockRPC: function (route, args) {
|
||||
if (args.method === '_get_test_field_values') {
|
||||
return $.when([
|
||||
['value', 'Title'],
|
||||
]);
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
assert.containsN(form, 'option', 2);
|
||||
assert.containsOnce(form, 'option[value=\'"value"\']');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('values are fetched w/o context (integer)', async function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var form = await testUtils.createView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'integer'},
|
||||
},
|
||||
records: [{id: 1, test_field: 0}],
|
||||
},
|
||||
},
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field" widget="dynamic_dropdown" values="_get_test_field_values"/>' +
|
||||
'</form>',
|
||||
mockRPC: function (route, args) {
|
||||
if (args.method === '_get_test_field_values') {
|
||||
return $.when([
|
||||
[0, 'Title'],
|
||||
]);
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
assert.containsN(form, 'option', 2);
|
||||
assert.containsOnce(form, 'option[value=\'0\']');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('values are fetched w/o context (selection)', async function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var form = await testUtils.createView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'selection'},
|
||||
},
|
||||
records: [{id: 1, test_field: ''}],
|
||||
},
|
||||
},
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field" widget="dynamic_dropdown" values="_get_test_field_values"/>' +
|
||||
'</form>',
|
||||
mockRPC: function (route, args) {
|
||||
if (args.method === '_get_test_field_values') {
|
||||
return $.when([
|
||||
['value', 'Title'],
|
||||
]);
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
assert.containsN(form, 'option', 2);
|
||||
assert.containsOnce(form, 'option[value=\'"value"\']');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('values are fetched with changing context', async function (assert) {
|
||||
assert.expect(6);
|
||||
|
||||
var form = await testUtils.createView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
other_field: {string: 'Other Field', type: 'char'},
|
||||
test_field: {string: 'Test Field', type: 'char'},
|
||||
},
|
||||
records: [{id: 1, other_field: '', test_field: ''}],
|
||||
},
|
||||
},
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="other_field" />' +
|
||||
'<field name="test_field" widget="dynamic_dropdown" values="_get_test_field_values" context="{\'step\': other_field}"/>' +
|
||||
'</form>',
|
||||
mockRPC: function (route, args) {
|
||||
if (args.method === '_get_test_field_values') {
|
||||
if (args.kwargs.context.step === 'step-1') {
|
||||
return $.when([
|
||||
['value', 'Title'],
|
||||
]);
|
||||
} else if (args.kwargs.context.step === 'step-2') {
|
||||
return $.when([
|
||||
['value', 'Title'],
|
||||
['value_2', 'Title 2'],
|
||||
]);
|
||||
} else {
|
||||
return $.when([]);
|
||||
}
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
|
||||
await testUtils.fields.editAndTrigger(
|
||||
form.$('.o_field_widget[name="other_field"]'),
|
||||
'step-1',
|
||||
['input']
|
||||
);
|
||||
assert.containsN(form, 'option', 2);
|
||||
assert.containsOnce(form, 'option[value=\'"value"\']');
|
||||
|
||||
await testUtils.fields.editAndTrigger(
|
||||
form.$('.o_field_widget[name="other_field"]'),
|
||||
'step-2',
|
||||
['input']
|
||||
);
|
||||
assert.containsN(form, 'option', 3);
|
||||
assert.containsOnce(form, 'option[value=\'"value"\']');
|
||||
assert.containsOnce(form, 'option[value=\'"value_2"\']');
|
||||
|
||||
await testUtils.fields.editAndTrigger(
|
||||
form.$('.o_field_widget[name="other_field"]'),
|
||||
'step-other',
|
||||
['input']
|
||||
);
|
||||
assert.containsN(form, 'option', 1);
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
-->
|
||||
<odoo>
|
||||
<template id="assets_backend" name="web_widget_dropdown_dynamic assets" inherit_id="web.assets_backend">
|
||||
<xpath expr="." position="inside">
|
||||
<script type="text/javascript" src="/web_widget_dropdown_dynamic/static/src/js/basic_model.js" />
|
||||
<script type="text/javascript" src="/web_widget_dropdown_dynamic/static/src/js/field_dynamic_dropdown.js" />
|
||||
</xpath>
|
||||
</template>
|
||||
|
||||
<template id="qunit_suite" name="web_widget_dropdown_dynamic tests" inherit_id="web.qunit_suite">
|
||||
<xpath expr="//t[@t-set='head']" position="inside">
|
||||
<script type="text/javascript" src="/web_widget_dropdown_dynamic/static/tests/web_widget_dropdown_dynamic_tests.js" />
|
||||
</xpath>
|
||||
</template>
|
||||
</odoo>
|
|
@ -0,0 +1,3 @@
|
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from . import models
|
|
@ -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: Example',
|
||||
'summary': 'Demonstration of web_widget_dropdown_dynamic',
|
||||
'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_widget_dropdown_dynamic',
|
||||
],
|
||||
'data': [
|
||||
'views/web_widget_dropdown_dynamic_example.xml',
|
||||
],
|
||||
'installable': True,
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from . import web_widget_dropdown_dynamic_example
|
|
@ -0,0 +1,87 @@
|
|||
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class WebWidgetDropdownDynamicExample(models.TransientModel):
|
||||
_name = 'web.widget.dropdown.dynamic.example'
|
||||
_description = 'Web Widget Dropdown Dynamic Example'
|
||||
|
||||
name = fields.Char(
|
||||
default='Web Widget Dropdown Dynamic Example',
|
||||
required=True,
|
||||
)
|
||||
|
||||
char_field_options = fields.Text(
|
||||
string='Char field options',
|
||||
default=(
|
||||
'Option A\n'
|
||||
'Option B\n'
|
||||
'Option C\n'
|
||||
),
|
||||
)
|
||||
char_field = fields.Char(
|
||||
string='Char field',
|
||||
)
|
||||
|
||||
int_field_min = fields.Integer(
|
||||
string='Int field (min)',
|
||||
default=0,
|
||||
)
|
||||
int_field_max = fields.Integer(
|
||||
string='Int field (max)',
|
||||
default=9,
|
||||
)
|
||||
int_field = fields.Integer(
|
||||
string='Int field',
|
||||
)
|
||||
|
||||
selection_field_options = fields.Text(
|
||||
string='Selection field options',
|
||||
default=(
|
||||
'Option A\n'
|
||||
'Option B\n'
|
||||
'Option C\n'
|
||||
'Option D\n'
|
||||
),
|
||||
)
|
||||
selection_field = fields.Char(
|
||||
string='Selection field',
|
||||
selection=[
|
||||
('Option A', 'Option A'),
|
||||
('Option B', 'Option B'),
|
||||
('Option C', 'Option C'),
|
||||
]
|
||||
)
|
||||
|
||||
@api.model
|
||||
def values_char_field(self):
|
||||
options = self.env.context.get('options').strip().split('\n')
|
||||
return list(map(
|
||||
lambda option: (option, option),
|
||||
filter(
|
||||
lambda option: bool(option),
|
||||
options
|
||||
)
|
||||
))
|
||||
|
||||
@api.model
|
||||
def values_int_field(self):
|
||||
min_value = int(self.env.context.get('min'))
|
||||
max_value = int(self.env.context.get('max'))
|
||||
options = []
|
||||
for value in range(min_value, max_value + 1):
|
||||
options.append((value, str(value)))
|
||||
return options
|
||||
|
||||
@api.model
|
||||
def values_selection_field(self):
|
||||
options = self.env.context.get('options').strip().split('\n')
|
||||
return list(map(
|
||||
lambda option: (option, option),
|
||||
filter(
|
||||
lambda option: bool(option),
|
||||
options
|
||||
)
|
||||
))
|
|
@ -0,0 +1,58 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
|
||||
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
-->
|
||||
<odoo>
|
||||
|
||||
<record id="web_widget_dropdown_dynamic_example_form" model="ir.ui.view">
|
||||
<field name="name">web.widget.dropdown.dynamic.example.form</field>
|
||||
<field name="model">web.widget.dropdown.dynamic.example</field>
|
||||
<field name="arch" type="xml">
|
||||
<form>
|
||||
<group string="Char field">
|
||||
<field name="char_field_options"/>
|
||||
<field
|
||||
name="char_field"
|
||||
widget="dynamic_dropdown"
|
||||
values="values_char_field"
|
||||
context="{'options': char_field_options}"
|
||||
/>
|
||||
</group>
|
||||
<group string="Int field">
|
||||
<field name="int_field_min"/>
|
||||
<field name="int_field_max"/>
|
||||
<field
|
||||
name="int_field"
|
||||
widget="dynamic_dropdown"
|
||||
values="values_int_field"
|
||||
context="{'min': int_field_min, 'max': int_field_max}"
|
||||
/>
|
||||
</group>
|
||||
<group string="Selection field">
|
||||
<field name="selection_field_options"/>
|
||||
<field
|
||||
name="selection_field"
|
||||
widget="dynamic_dropdown"
|
||||
values="values_selection_field"
|
||||
context="{'options': selection_field_options}"
|
||||
/>
|
||||
</group>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record id="action_web_widget_dropdown_dynamic_example" model="ir.actions.act_window">
|
||||
<field name="name">web_widget_dropdown_dynamic Demo</field>
|
||||
<field name="res_model">web.widget.dropdown.dynamic.example</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">form</field>
|
||||
</record>
|
||||
|
||||
<menuitem
|
||||
id="web_widget_dropdown_dynamic_example_menu"
|
||||
name="web_widget_dropdown_dynamic Demo"
|
||||
action="action_web_widget_dropdown_dynamic_example"
|
||||
/>
|
||||
|
||||
</odoo>
|
Loading…
Reference in New Issue