[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) <lpe@odoo.com>
This commit is contained in:
Jorge Pinna Puissant
2022-08-12 13:13:02 +02:00
parent 94c9b4c8f1
commit 25d2654b3b
10 changed files with 230 additions and 284 deletions
@@ -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: $('<div>', {
html: message,
}),
title: _t("Odoo Enterprise"),
}).open();
},
/**
* @override
* @private
*/
_render: function () {
this._super.apply(this, arguments);
this._insertEnterpriseLabel($("<span>", {
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('&nbsp;').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('&nbsp;').append($enterpriseLabel);
},
/**
* @override
* @private
*/
_resetValue: function () {
this.$('input').first().prop("checked", true).click();
},
});
field_registry
.add('upgrade_boolean', UpgradeBoolean)
.add('upgrade_radio', UpgradeRadio);
});
-19
View File
@@ -1524,25 +1524,6 @@
</div>
</t>
<t t-name="EnterpriseUpgrade">
<div class="row" role="status">
<div class="col-6">
Get this feature and much more with Odoo Enterprise!
<ul class="list-unstyled">
<li><i class="fa fa-check"></i> Access to all Enterprise Apps</li>
<li><i class="fa fa-check"></i> New design</li>
<li><i class="fa fa-check"></i> Mobile support</li>
<li><i class="fa fa-check"></i> Upgrade to future versions</li>
<li><i class="fa fa-check"></i> Bugfixes guarantee</li>
<li><a href="http://www.odoo.com/editions?utm_source=db&amp;utm_medium=enterprise" target="_blank"><i class="fa fa-plus"></i> And more</a></li>
</ul>
</div>
<div class="col-6">
<img class="img-fluid" t-att-src='_s + "/web/static/img/enterprise_upgrade.jpg"' draggable="false" alt="Upgrade to enterprise"/>
</div>
</div>
</t>
<t t-name="AttachDocument">
<button t-attf-class="btn o_attachment_button #{widget.node.attrs.highlight?'btn-primary':'btn-secondary'}">
<span class="o_attach_document"><t t-esc="widget.node.attrs.string"/></span>
@@ -0,0 +1,37 @@
/** @odoo-module **/
import { registry } from "@web/core/registry";
import { BooleanField } from "@web/views/fields/boolean/boolean_field";
import { useService } from "@web/core/utils/hooks";
import { UpgradeDialog } from "./upgrade_dialog";
/**
* The upgrade boolean field is intended to be used in config settings.
* When checked, an upgrade popup is showed to the user.
*/
export class UpgradeBooleanField extends BooleanField {
setup() {
super.setup();
this.dialogService = useService("dialog");
this.isEnterprise = odoo.info && odoo.info.isEnterprise;
}
async onChange(newValue) {
if (!this.isEnterprise) {
this.dialogService.add(
UpgradeDialog,
{},
{
onClose: () => {
this.props.update(false);
},
}
);
} else {
super.onChange(...arguments);
}
}
}
UpgradeBooleanField.isUpgradeField = true;
registry.category("fields").add("upgrade_boolean", UpgradeBooleanField);
@@ -0,0 +1,23 @@
/** @odoo-module */
import { Dialog } from "@web/core/dialog/dialog";
import { useService } from "@web/core/utils/hooks";
const { Component } = owl;
export class UpgradeDialog extends Component {
setup() {
this.orm = useService("orm");
this.router = useService("router");
}
async _confirmUpgrade() {
const usersCount = await this.orm.call("res.users", "search_count", [
[["share", "=", false]],
]);
this.router.redirect(
"https://www.odoo.com/odoo-enterprise/upgrade?num_users=" + usersCount
);
}
}
UpgradeDialog.template = "web.UpgradeDialog";
UpgradeDialog.components = { Dialog };
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="web.UpgradeDialog" owl="1">
<Dialog size="'md'" title="'Odoo Enterprise'">
<div class="row" role="status">
<div class="col-6">
Get this feature and much more with Odoo Enterprise!
<ul class="list-unstyled">
<li><i class="fa fa-check"></i> Access to all Enterprise Apps</li>
<li><i class="fa fa-check"></i> New design</li>
<li><i class="fa fa-check"></i> Mobile support</li>
<li><i class="fa fa-check"></i> Upgrade to future versions</li>
<li><i class="fa fa-check"></i> Bugfixes guarantee</li>
<li><a href="http://www.odoo.com/editions?utm_source=db&amp;utm_medium=enterprise" target="_blank"><i class="fa fa-plus"></i> And more</a></li>
</ul>
</div>
<div class="col-6">
<img class="img-fluid" t-att-src="'/web/static/img/enterprise_upgrade.jpg'" draggable="false" alt="Upgrade to enterprise"/>
</div>
</div>
<t t-set-slot="footer" owl="1">
<button class="btn btn-primary" t-on-click="_confirmUpgrade">Upgrade now</button>
<button class="btn btn-secondary" t-on-click="this.props.close">Cancel</button>
</t>
</Dialog>
</t>
</templates>
@@ -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) {
@@ -3,6 +3,9 @@
<t t-name="web.FormLabelHighlightText" owl="1">
<label class="o_form_label" t-att-for="props.id" t-att-class="className">
<HighlightText originalText="props.string"/>
<t t-if="upgradeEnterprise">
<span class="badge text-bg-primary oe_inline o_enterprise_label">Enterprise</span>
</t>
</label>
</t>
</templates>
@@ -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: '<form><field name="bar" widget="upgrade_boolean"/></form>',
});
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: '<form>' +
'<div class="o_field"><field name="bar" widget="upgrade_boolean"/></div>' +
'<div class="o_label"><label for="bar"/><div>Coucou</div></div>' +
'</form>',
});
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();
});
});
});
});
@@ -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: `
<form js_class="base_settings">
<field name="bar" widget="upgrade_boolean"/>
</form>`,
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: `
<form js_class="base_settings">
<div class="o_field">
<field name="bar" widget="upgrade_boolean"/>
</div>
<div class="o_label"><label for="bar"/><div>Coucou</div></div>
</form>`,
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: `
<form js_class="base_settings">
<field name="bar" widget="upgrade_boolean"/>
</form>`,
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: `
<form js_class="base_settings">
<div class="o_field">
<field name="bar" widget="upgrade_boolean"/>
</div>
<div class="o_label"><label for="bar"/><div>Coucou</div></div>
</form>`,
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"
);
}
);
});