[IMP] stock: add widget popover

Add a popover widget linked to a Char field containing
a JSON string. It used to display flexible content in a Bootstrap
popover which appears when the user have mouse over or focus on the
information icon/button.

task-2082261
This commit is contained in:
Rémy Voet (ryv)
2019-12-30 08:19:40 +00:00
parent d50e2b0010
commit 7fc655f75e
5 changed files with 124 additions and 0 deletions
+1
View File
@@ -79,6 +79,7 @@
'qweb': [
'static/src/xml/inventory_report.xml',
'static/src/xml/inventory_lines.xml',
'static/src/xml/popover_widget.xml',
'static/src/xml/stock_traceability_report_backend.xml',
],
'installable': True,
@@ -0,0 +1,56 @@
odoo.define('stock.popover_widget', function (require) {
'use strict';
var AbstractField = require('web.AbstractField');
var core = require('web.core');
var QWeb = core.qweb;
var fieldRegistry = require('web.field_registry');
/**
* Widget Popover for JSON field, by default render a simple html message
* {
* 'msg': '<CONTENT OF THE POPOVER>',
* 'color': '<COLOR CLASS OF ICON>' (optionnal),
* 'title': '<TITLE OF POPOVER>' (optionnal),
* 'popoverTemplate': '<TEMPLATE OF THE TEMPLATE>' (optionnal)
* }
*/
var PopoverWidgetField = AbstractField.extend({
supportedFieldTypes: ['char'],
buttonTemplape: 'stock.popoverButton',
popoverTemplate: 'stock.popoverContent',
trigger: 'hover focus',
placement: 'top',
html: true,
_render: function () {
var value = JSON.parse(this.value);
if (!value) {
this.$el.html('');
return;
}
this.$el.css('max-width', '17px');
this.$el.html(QWeb.render(this.buttonTemplape, value));
this.$el.find('a').prop('special_click', true);
this.$popover = $(QWeb.render(value.popoverTemplate || this.popoverTemplate, value));
this.$el.find('a').popover({
content: this.$popover,
html: this.html,
placement: this.placement,
title: value.title || this.title,
trigger: this.trigger,
delay: {'show': 0, 'hide': 100},
});
},
destroy: function () {
this.$el.find('a').popover('dispose');
this._super.apply(this, arguments);
},
});
fieldRegistry.add('popover_widget', PopoverWidgetField);
return PopoverWidgetField;
});
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<templates id="template" xml:space="preserve">
<t t-name="stock.popoverButton">
<a tabindex="0" t-attf-class="p-1 fa fa-info-circle #{ color || 'text-primary'}"/>
</t>
<div t-name="stock.popoverContent">
<t t-esc="msg"/>
</div>
</templates>
@@ -0,0 +1,53 @@
odoo.define('web.data_export_tests', function (require) {
"use strict";
var testUtils = require('web.test_utils');
var FormView = require('web.FormView');
var createView = testUtils.createView;
QUnit.module('widgets', {}, function () {
QUnit.module('ModelFieldSelector', {
beforeEach: function () {
this.data = {
partner: {
fields: {
json_data: {string: " ", type: "char"},
},
records: [
{id:1, json_data:'{"color": "text-danger", "msg": "var that = self // why not?", "title": "JS Master"}'}
]
}
};
},
}, function () {
QUnit.test("Test creation/usage popover widget form", async function (assert) {
assert.expect(6);
var form = await createView({
View: FormView,
model: 'partner',
data: this.data,
arch:'<form string="Partners">' +
'<field name="json_data" widget="popover_widget"/>' +
'</form>',
res_id: 1
});
var $popover = $('div.popover');
assert.strictEqual($popover.length, 0, "Shouldn't have a popover container in DOM");
var $popoverButton = form.$('a.fa.fa-info-circle.text-danger');
assert.strictEqual($popoverButton.length, 1, "Should have a popover icon/button in red");
assert.strictEqual($popoverButton.prop('special_click'), true, "Special click properpy should be activated");
await testUtils.dom.triggerEvents($popoverButton, ['focus']);
var $popover = $('div.popover');
assert.strictEqual($popover.length, 1, "Should have a popover container in DOM");
assert.strictEqual($popover.html().includes("var that = self // why not?"), true, "The message should be in DOM");
assert.strictEqual($popover.html().includes("JS Master"), true, "The title should be in DOM");
form.destroy();
});
});
});
});
+2
View File
@@ -12,6 +12,7 @@
<script type="text/javascript" src="/stock/static/src/js/stock_traceability_report_backend.js"></script>
<script type="text/javascript" src="/stock/static/src/js/stock_traceability_report_widgets.js"></script>
<script type="text/javascript" src="/stock/static/src/js/stock_location_hierarchy.js"></script>
<script type="text/javascript" src="/stock/static/src/js/popover_widget.js"></script>
<link rel="stylesheet" type="text/scss" href="/stock/static/src/scss/stock_traceability_report.scss"/>
<link rel="stylesheet" type="text/scss" href="/stock/static/src/scss/stock_location_hierarchy.scss"/>
</xpath>
@@ -20,6 +21,7 @@
<template id="qunit_suite" name="stock tests" inherit_id="web.qunit_suite">
<xpath expr="//t[@t-set='head']" position="inside">
<script type="text/javascript" src="/stock/static/tests/singleton_list_tests.js"/>
<script type="text/javascript" src="/stock/static/tests/popover_widget_tests.js"/>
</xpath>
</template>