From 25d2654b3b4cfa4a4be61fe5843dce13b05cdd24 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Thu, 11 Aug 2022 09:55:08 +0000 Subject: [PATCH] [REF] web: convert field upgradeBoolean This commit rewrites upgradeBoolean using the new framework. closes odoo/odoo#97951 Related: odoo/enterprise#30334 Signed-off-by: Lucas Perais (lpe) --- .../src/legacy/js/fields/upgrade_fields.js | 199 ------------------ addons/web/static/src/legacy/xml/base.xml | 19 -- .../fields/upgrade_boolean_field.js | 37 ++++ .../fields/upgrade_dialog.js | 23 ++ .../fields/upgrade_dialog.xml | 27 +++ .../form_label_highlight_text.js | 9 + .../form_label_highlight_text.xml | 3 + .../legacy/fields/upgrade_fields_tests.js | 66 ------ .../settings_form_view_tests.js | 0 .../settings_upgrade_boolean_field_tests.js | 131 ++++++++++++ 10 files changed, 230 insertions(+), 284 deletions(-) delete mode 100644 addons/web/static/src/legacy/js/fields/upgrade_fields.js create mode 100644 addons/web/static/src/webclient/settings_form_view/fields/upgrade_boolean_field.js create mode 100644 addons/web/static/src/webclient/settings_form_view/fields/upgrade_dialog.js create mode 100644 addons/web/static/src/webclient/settings_form_view/fields/upgrade_dialog.xml delete mode 100644 addons/web/static/tests/legacy/fields/upgrade_fields_tests.js rename addons/web/static/tests/webclient/{ => settings_form_view}/settings_form_view_tests.js (100%) create mode 100644 addons/web/static/tests/webclient/settings_form_view/settings_upgrade_boolean_field_tests.js diff --git a/addons/web/static/src/legacy/js/fields/upgrade_fields.js b/addons/web/static/src/legacy/js/fields/upgrade_fields.js deleted file mode 100644 index 343159cf22a..00000000000 --- a/addons/web/static/src/legacy/js/fields/upgrade_fields.js +++ /dev/null @@ -1,199 +0,0 @@ -odoo.define('web.upgrade_widgets', function (require) { -"use strict"; - -/** - * The upgrade widgets are intended to be used in config settings. - * When checked, an upgrade popup is showed to the user. - */ - -var AbstractField = require('web.AbstractField'); -var basic_fields = require('web.basic_fields'); -var core = require('web.core'); -var Dialog = require('web.Dialog'); -var field_registry = require('web.field_registry'); -var framework = require('web.framework'); -var relational_fields = require('web.relational_fields'); - -var _t = core._t; -var QWeb = core.qweb; - -var FieldBoolean = basic_fields.FieldBoolean; -var FieldRadio = relational_fields.FieldRadio; - - -/** - * Mixin that defines the common functions shared between Boolean and Radio - * upgrade widgets - */ -var AbstractFieldUpgrade = { - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - - /** - * Redirects the user to the odoo-enterprise/uprade page - * - * @private - * @returns {Promise} - */ - _confirmUpgrade: function () { - return this._rpc({ - model: 'res.users', - method: 'search_count', - args: [[["share", "=", false]]], - }) - .then(function (data) { - framework.redirect("https://www.odoo.com/odoo-enterprise/upgrade?num_users=" + data); - }); - }, - /** - * This function is meant to be overridden to insert the 'Enterprise' label - * JQuery node at the right place. - * - * @abstract - * @private - * @param {jQuery} $enterpriseLabel the 'Enterprise' label to insert - */ - _insertEnterpriseLabel: function ($enterpriseLabel) {}, - /** - * Opens the Upgrade dialog. - * - * @private - * @returns {Dialog} the instance of the opened Dialog - */ - _openDialog: function () { - var message = $(QWeb.render('EnterpriseUpgrade')); - - var buttons = [ - { - text: _t("Upgrade now"), - classes: 'btn-primary', - close: true, - click: this._confirmUpgrade.bind(this), - }, - { - text: _t("Cancel"), - close: true, - }, - ]; - - return new Dialog(this, { - size: 'medium', - buttons: buttons, - $content: $('
', { - html: message, - }), - title: _t("Odoo Enterprise"), - }).open(); - }, - /** - * @override - * @private - */ - _render: function () { - this._super.apply(this, arguments); - this._insertEnterpriseLabel($("", { - text: "Enterprise", - 'class': "badge text-bg-primary oe_inline o_enterprise_label" - })); - }, - /** - * This function is meant to be overridden to reset the $el to its initial - * state. - * - * @abstract - * @private - */ - _resetValue: function () {}, - - //-------------------------------------------------------------------------- - // Handlers - //-------------------------------------------------------------------------- - - /** - * @private - * @param {MouseEvent} event - */ - _onInputClicked: function (event) { - if ($(event.currentTarget).prop("checked")) { - this._openDialog().on('closed', this, this._resetValue.bind(this)); - } - }, - -}; - -var UpgradeBoolean = FieldBoolean.extend(AbstractFieldUpgrade, { - supportedFieldTypes: [], - 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 - //-------------------------------------------------------------------------- - - /** - * @override - * @private - */ - _insertEnterpriseLabel: function ($enterpriseLabel) { - var $el = this.$label || this.$el; - $el.append(' ').append($enterpriseLabel); - }, - /** - * @override - * @private - */ - _resetValue: function () { - this.$input.prop("checked", false).change(); - }, -}); - -var UpgradeRadio = FieldRadio.extend(AbstractFieldUpgrade, { - supportedFieldTypes: [], - events: _.extend({}, FieldRadio.prototype.events, { - 'click input:last': '_onInputClicked', - }), - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - isSet: function () { - return true; - }, - - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - - /** - * @override - * @private - */ - _insertEnterpriseLabel: function ($enterpriseLabel) { - this.$('label').last().append(' ').append($enterpriseLabel); - }, - /** - * @override - * @private - */ - _resetValue: function () { - this.$('input').first().prop("checked", true).click(); - }, -}); - -field_registry - .add('upgrade_boolean', UpgradeBoolean) - .add('upgrade_radio', UpgradeRadio); - -}); diff --git a/addons/web/static/src/legacy/xml/base.xml b/addons/web/static/src/legacy/xml/base.xml index 590a46b364c..e9fc5ac9c80 100644 --- a/addons/web/static/src/legacy/xml/base.xml +++ b/addons/web/static/src/legacy/xml/base.xml @@ -1524,25 +1524,6 @@
- -
-
- Get this feature and much more with Odoo Enterprise! -
    -
  • Access to all Enterprise Apps
  • -
  • New design
  • -
  • Mobile support
  • -
  • Upgrade to future versions
  • -
  • Bugfixes guarantee
  • -
  • And more
  • -
-
-
- Upgrade to enterprise -
-
-
- + + + + + diff --git a/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.js b/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.js index 0c088dce01f..6013b368c4d 100644 --- a/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.js +++ b/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.js @@ -5,6 +5,15 @@ import { HighlightText } from "./highlight_text"; export class FormLabelHighlightText extends FormLabel { setup() { super.setup(); + const isEnterprise = odoo.info && odoo.info.isEnterprise; + if ( + this.props.fieldInfo && + this.props.fieldInfo.FieldComponent && + this.props.fieldInfo.FieldComponent.isUpgradeField && + !isEnterprise + ) { + this.upgradeEnterprise = true; + } } get className() { if (this.props.className) { diff --git a/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.xml b/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.xml index 449e5c8ae29..0d591038054 100644 --- a/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.xml +++ b/addons/web/static/src/webclient/settings_form_view/highlight_text/form_label_highlight_text.xml @@ -3,6 +3,9 @@ diff --git a/addons/web/static/tests/legacy/fields/upgrade_fields_tests.js b/addons/web/static/tests/legacy/fields/upgrade_fields_tests.js deleted file mode 100644 index d908fd48b61..00000000000 --- a/addons/web/static/tests/legacy/fields/upgrade_fields_tests.js +++ /dev/null @@ -1,66 +0,0 @@ -odoo.define('web.upgrade_fields_tests', function (require) { -"use strict"; - -var FormView = require('web.FormView'); -var testUtils = require('web.test_utils'); - -var createView = testUtils.createView; - -QUnit.module('fields', {}, function () { - -QUnit.module('upgrade_fields', { - beforeEach: function () { - this.data = { - partner: { - fields: { - bar: {string: "Bar", type: "boolean"}, - }, - } - }; - }, -}, function () { - - QUnit.module('UpgradeBoolean'); - - QUnit.test('widget upgrade_boolean in a form view', async function (assert) { - assert.expect(1); - - var form = await createView({ - View: FormView, - model: 'partner', - data: this.data, - arch: '
', - }); - - await testUtils.dom.click(form.$('input:checkbox')); - assert.strictEqual($('.modal').length, 1, - "the 'Upgrade to Enterprise' dialog should be opened"); - - form.destroy(); - }); - - QUnit.test('widget upgrade_boolean in a form view', async function (assert) { - assert.expect(3); - - var form = await createView({ - View: FormView, - model: 'partner', - data: this.data, - arch: '
' + - '
' + - '
' + - '
', - }); - - assert.containsNone(form, '.o_field .badge', - "the upgrade badge shouldn't be inside the field section"); - assert.containsOnce(form, '.o_label .badge', - "the upgrade badge 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(); - }); - -}); -}); -}); diff --git a/addons/web/static/tests/webclient/settings_form_view_tests.js b/addons/web/static/tests/webclient/settings_form_view/settings_form_view_tests.js similarity index 100% rename from addons/web/static/tests/webclient/settings_form_view_tests.js rename to addons/web/static/tests/webclient/settings_form_view/settings_form_view_tests.js diff --git a/addons/web/static/tests/webclient/settings_form_view/settings_upgrade_boolean_field_tests.js b/addons/web/static/tests/webclient/settings_form_view/settings_upgrade_boolean_field_tests.js new file mode 100644 index 00000000000..e1f6983098c --- /dev/null +++ b/addons/web/static/tests/webclient/settings_form_view/settings_upgrade_boolean_field_tests.js @@ -0,0 +1,131 @@ +/** @odoo-module **/ +import { click, getFixture, patchWithCleanup } from "@web/../tests/helpers/utils"; +import { makeView, setupViewRegistries } from "@web/../tests/views/helpers"; + +let serverData; +let target; + +QUnit.module("SettingsUpgradeBoolean", (hooks) => { + hooks.beforeEach(() => { + serverData = { + models: { + "res.config.settings": { + fields: { + bar: { string: "Bar", type: "boolean" }, + }, + }, + }, + }; + target = getFixture(); + setupViewRegistries(); + }); + QUnit.test("widget upgrade_boolean in a form view - dialog", async function (assert) { + await makeView({ + type: "form", + arch: ` +
+ + `, + serverData, + resModel: "res.config.settings", + }); + + await click(target.querySelector(".o-checkbox .form-check-input")); + + assert.containsOnce( + target, + ".o_dialog .modal", + "the 'Upgrade to Enterprise' dialog should be opened" + ); + }); + + QUnit.test("widget upgrade_boolean in a form view - label", async function (assert) { + await makeView({ + type: "form", + arch: ` +
+
+ +
+
+
`, + serverData, + resModel: "res.config.settings", + }); + + assert.containsNone( + target, + ".o_field .badge", + "the upgrade badge shouldn't be inside the field section" + ); + assert.containsOnce( + target, + ".o_label .badge", + "the upgrade badge should be inside the label section" + ); + assert.strictEqual( + target.querySelector(".o_label").textContent, + "BarEnterpriseCoucou", + "the upgrade label should be inside the label section" + ); + }); + + QUnit.test( + "widget upgrade_boolean in a form view - dialog (enterprise version)", + async function (assert) { + patchWithCleanup(odoo, { info: { isEnterprise: 1 } }); + await makeView({ + type: "form", + arch: ` +
+ + `, + serverData, + resModel: "res.config.settings", + }); + + await click(target.querySelector(".o-checkbox .form-check-input")); + + assert.containsNone( + target, + ".o_dialog .modal", + "the 'Upgrade to Enterprise' dialog shouldn't be opened" + ); + } + ); + + QUnit.test( + "widget upgrade_boolean in a form view - label (enterprise version)", + async function (assert) { + patchWithCleanup(odoo, { info: { isEnterprise: 1 } }); + await makeView({ + type: "form", + arch: ` +
+
+ +
+
+
`, + serverData, + resModel: "res.config.settings", + }); + + assert.containsNone( + target, + ".o_field .badge", + "the upgrade badge shouldn't be inside the field section" + ); + assert.containsNone( + target, + ".o_label .badge", + "the upgrade badge shouldn't be inside the label section" + ); + assert.strictEqual( + target.querySelector(".o_label").textContent, + "BarCoucou", + "the label shouldn't contains the upgrade label" + ); + } + ); +});