diff options
| author | Siddarth Gajjar <sga@odoo.com> | 2018-05-30 14:52:17 +0200 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2018-05-30 15:39:15 +0200 |
| commit | c75c2fa9e3da007f080a4d4691d83d4c29112b1f (patch) | |
| tree | 62c1a4e36ad59afce79d77bba3eeeed9ea9f36b7 /addons/web/static/src/js/fields | |
| parent | cda8bdc8b371429c7f88326a99cb5ebf6c446a1d (diff) | |
[IMP] web: add a badge based widget for selection fields
This commit introduces a new widget to be used for selection-based fields.
Valid choices are displayed using rectangular badges and not like the
standard list. This widget is valid for selection and many2one fields
like selection or radio widgets.
Co-authored-by: Siddarth Gajjar <sga@odoo.com>
Co-authored-by: Mohammed Shekha <msh@openerp.com>
Diffstat (limited to 'addons/web/static/src/js/fields')
| -rw-r--r-- | addons/web/static/src/js/fields/field_registry.js | 1 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/relational_fields.js | 70 |
2 files changed, 71 insertions, 0 deletions
diff --git a/addons/web/static/src/js/fields/field_registry.js b/addons/web/static/src/js/fields/field_registry.js index cebfed606fd..8ae7362b75f 100644 --- a/addons/web/static/src/js/fields/field_registry.js +++ b/addons/web/static/src/js/fields/field_registry.js @@ -60,6 +60,7 @@ registry registry .add('selection', relational_fields.FieldSelection) .add('radio', relational_fields.FieldRadio) + .add('selection_badge', relational_fields.FieldSelectionBadge) .add('many2one', relational_fields.FieldMany2One) .add('list.many2one', relational_fields.ListFieldMany2One) .add('kanban.many2one', relational_fields.KanbanFieldMany2One) diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 62cc965cc15..c86411b6cd9 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2301,6 +2301,75 @@ var FieldRadio = FieldSelection.extend({ }, }); + +var FieldSelectionBadge = FieldSelection.extend({ + template: null, + className: 'o_field_selection_badge', + tagName: 'span', + specialData: "_fetchSpecialMany2ones", + events: _.extend({}, AbstractField.prototype.events, { + 'click span.o_selection_badge': '_onBadgeClicked', + }), + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + * @override + */ + _renderEdit: function () { + this.currentValue = this.value; + + if (this.field.type === 'many2one') { + this.currentValue = this.value && this.value.data.id; + } + this.$el.empty(); + this.$el.html(qweb.render('FieldSelectionBadge', {'values': this.values, 'current_value': this.currentValue})); + }, + /** + * Sets the possible field values. If the field is a many2one, those values + * may change during the life cycle of the widget if the domain change (an + * onchange may change the domain). + * + * @private + * @override + */ + _setValues: function () { + // Note: We can make abstract widget for common code in radio and selection badge + if (this.field.type === 'selection') { + this.values = this.field.selection || []; + } else if (this.field.type === 'many2one') { + this.values = _.map(this.record.specialData[this.name], function (val) { + return [val.id, val.display_name]; + }); + } + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + * @param {MouseEvent} event + */ + _onBadgeClicked: function (event) { + var index = $(event.target).data('index'); + var value = this.values[index]; + if (value[0] !== this.currentValue) { + if (this.field.type === 'many2one') { + this._setValue({id: value[0], display_name: value[1]}); + } else { + this._setValue(value[0]); + } + } else { + this._setValue(false); + } + }, +}); + /** * The FieldReference is a combination of a select (for the model) and * a FieldMany2one for its value. @@ -2461,6 +2530,7 @@ return { KanbanFieldMany2ManyTags: KanbanFieldMany2ManyTags, FieldRadio: FieldRadio, + FieldSelectionBadge: FieldSelectionBadge, FieldSelection: FieldSelection, FieldStatus: FieldStatus, |
