summaryrefslogtreecommitdiff
path: root/addons/web/static/src
diff options
context:
space:
mode:
authorSiddarth Gajjar <sga@odoo.com>2018-05-30 14:52:17 +0200
committerThibault Delavallée <tde@odoo.com>2018-05-30 15:39:15 +0200
commitc75c2fa9e3da007f080a4d4691d83d4c29112b1f (patch)
tree62c1a4e36ad59afce79d77bba3eeeed9ea9f36b7 /addons/web/static/src
parentcda8bdc8b371429c7f88326a99cb5ebf6c446a1d (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')
-rw-r--r--addons/web/static/src/js/fields/field_registry.js1
-rw-r--r--addons/web/static/src/js/fields/relational_fields.js70
-rw-r--r--addons/web/static/src/scss/fields.scss18
-rw-r--r--addons/web/static/src/xml/base.xml7
4 files changed, 96 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,
diff --git a/addons/web/static/src/scss/fields.scss b/addons/web/static/src/scss/fields.scss
index 3de64e866ac..0fd69037b0b 100644
--- a/addons/web/static/src/scss/fields.scss
+++ b/addons/web/static/src/scss/fields.scss
@@ -229,6 +229,24 @@
cursor: ns-resize;
}
+ &.o_field_selection_badge {
+ .o_selection_badge {
+ display: inline-block;
+ margin: 0px 0px 4px 4px;
+ padding: 1px 6px;
+ color: $o-main-color-muted;
+ border: 1px solid $o-color-silver-dark;
+ cursor: pointer;
+ &.active {
+ color: $o-brand-optional;
+ border-color: $o-brand-optional;
+ }
+ &:hover:not(.active) {
+ color: darken($o-color-silver-dark, 30%);
+ border-color: darken($o-color-silver-dark, 30%);
+ }
+ }
+ }
// Radio buttons
&.o_field_radio {
.o_radio_input {
diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml
index 0debc65dc54..458e15dd2be 100644
--- a/addons/web/static/src/xml/base.xml
+++ b/addons/web/static/src/xml/base.xml
@@ -792,6 +792,13 @@
<label class="o_form_label" t-att-for="id"><t t-esc="value[1]"/></label>
</div>
</t>
+
+<t t-name="FieldSelectionBadge">
+ <span t-foreach="values" t-as="value" t-attf-class="o_selection_badge #{value[0] == current_value ? 'active' : ''}" t-att-data-index="value_index">
+ <t t-esc="value[1]"/>
+ </span>
+</t>
+
<t t-name="FieldMany2One">
<t t-if="widget.mode === 'readonly'">
<a t-if="!widget.nodeOptions.no_open" class="o_form_uri" href="#"/>