From cb540c48eb575902018da97f2f541a813bda4d6b Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Fri, 15 Sep 2017 14:18:57 +0200 Subject: [PATCH] [FIX] web: correctly display the enterprise label The `upgrade_boolean` widget should display an `Enterprise` label next to it. There is however an issue when a `label` is specified for this field. In this case, the `Enterprise` label should be placed next to the field label (and not the field itself). The easiest solution would have been to instantiate the field widget with its label, but this can't work because the field widget is instantiated before its label. We thus re-render the widget after its label creation. --- .../static/src/js/fields/upgrade_fields.js | 22 ++++++++++++++----- .../static/src/js/views/form/form_renderer.js | 9 ++++++-- .../tests/fields/upgrade_fields_tests.js | 22 +++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/addons/web/static/src/js/fields/upgrade_fields.js b/addons/web/static/src/js/fields/upgrade_fields.js index ae8cae56ef3..c2c30760abf 100644 --- a/addons/web/static/src/js/fields/upgrade_fields.js +++ b/addons/web/static/src/js/fields/upgrade_fields.js @@ -52,9 +52,9 @@ var AbstractFieldUpgrade = { * * @abstract * @private - * @param {JQuery} the 'Enterprise' label to insert + * @param {jQuery} $enterpriseLabel the 'Enterprise' label to insert */ - _insertEnterpriseLabel: function ($enterprise_label) {}, + _insertEnterpriseLabel: function ($enterpriseLabel) {}, /** * Opens the Upgrade dialog. * @@ -127,6 +127,15 @@ var UpgradeBoolean = FieldBoolean.extend(AbstractFieldUpgrade, { events: _.extend({}, AbstractField.prototype.events, { 'click input': '_onInputClicked', }), + /** + * Re-renders the widget with the label + * + * @param {jQuery} $label + */ + renderWithLabel: function ($label) { + this.$label = $label; + this._render(); + }, //-------------------------------------------------------------------------- // Private @@ -136,8 +145,9 @@ var UpgradeBoolean = FieldBoolean.extend(AbstractFieldUpgrade, { * @override * @private */ - _insertEnterpriseLabel: function ($enterprise_label) { - this.$el.append(' ').append($enterprise_label); + _insertEnterpriseLabel: function ($enterpriseLabel) { + var $el = this.$label || this.$el; + $el.append(' ').append($enterpriseLabel); }, /** * @override @@ -170,8 +180,8 @@ var UpgradeRadio = FieldRadio.extend(AbstractFieldUpgrade, { * @override * @private */ - _insertEnterpriseLabel: function ($enterprise_label) { - this.$('label').last().append(' ').append($enterprise_label); + _insertEnterpriseLabel: function ($enterpriseLabel) { + this.$('label').last().append(' ').append($enterpriseLabel); }, /** * @override diff --git a/addons/web/static/src/js/views/form/form_renderer.js b/addons/web/static/src/js/views/form/form_renderer.js index ac53168df6e..0b00bcd1971 100644 --- a/addons/web/static/src/js/views/form/form_renderer.js +++ b/addons/web/static/src/js/views/form/form_renderer.js @@ -809,11 +809,16 @@ var FormRenderer = BasicRenderer.extend({ // Attach the tooltips on the fields' label _.each(this.allFieldWidgets[this.state.id], function (widget) { + var idForLabel = self.idsForLabels[widget.name]; + var $label = idForLabel ? self.$('label[for=' + idForLabel + ']') : $(); if (core.debug || widget.attrs.help || widget.field.help) { - var idForLabel = self.idsForLabels[widget.name]; - var $label = idForLabel ? self.$('label[for=' + idForLabel + ']') : $(); self._addFieldTooltip(widget, $label); } + if (widget.attrs.widget === 'upgrade_boolean') { + // this widget needs a reference to its $label to be correctly + // rendered + widget.renderWithLabel($label); + } }); }, /** diff --git a/addons/web/static/tests/fields/upgrade_fields_tests.js b/addons/web/static/tests/fields/upgrade_fields_tests.js index babc32b60a8..427ec7b54e3 100644 --- a/addons/web/static/tests/fields/upgrade_fields_tests.js +++ b/addons/web/static/tests/fields/upgrade_fields_tests.js @@ -39,6 +39,28 @@ QUnit.module('upgrade_fields', { form.destroy(); }); + QUnit.test('widget upgrade_boolean in a form view', function (assert) { + assert.expect(3); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '
' + + '
' + + '
', + }); + + assert.strictEqual(form.$('.o_field .label').length, 0, + "the upgrade label shouldn't be inside the field section"); + assert.strictEqual(form.$('.o_label .label').length, 1, + "the upgrade label should be inside the label section"); + assert.strictEqual(form.$('.o_label').text(), "Bar EnterpriseCoucou", + "the upgrade label should be inside the label section"); + form.destroy(); + }); + }); }); });