[IMP] web_widget_mpld3_chart: Move widget from CharField to Component(Json field)

pull/3140/head
BernatPForgeFlow 2023-11-30 11:57:10 +01:00 committed by ThiagoMForgeFlow
parent c3caf43e5b
commit b965b16c99
5 changed files with 29 additions and 27 deletions

View File

@ -23,7 +23,7 @@ Web Widget mpld3 Chart
:target: https://runbot.odoo-community.org/runbot/162/15.0 :target: https://runbot.odoo-community.org/runbot/162/15.0
:alt: Try me on Runbot :alt: Try me on Runbot
|badge1| |badge2| |badge3| |badge4| |badge5| |badge1| |badge2| |badge3| |badge4| |badge5|
This module adds the possibility to insert mpld3 charts into Odoo standard views. This module adds the possibility to insert mpld3 charts into Odoo standard views.
This is an interactive D3js-based viewer which brings matplotlib graphics to the browser. This is an interactive D3js-based viewer which brings matplotlib graphics to the browser.
@ -59,7 +59,7 @@ To insert a mpld3 chart in a view proceed as follows:
#. Declare a text computed field like this:: #. Declare a text computed field like this::
mpld3_chart = fields.Text( mpld3_chart = fields.Json(
string='Mpld3 Chart', string='Mpld3 Chart',
compute='_compute_mpld3_chart', compute='_compute_mpld3_chart',
) )
@ -138,7 +138,7 @@ promote its widespread use.
Current `maintainers <https://odoo-community.org/page/maintainer-role>`__: Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:
|maintainer-JordiBForgeFlow| |maintainer-ChrisOForgeFlow| |maintainer-JordiBForgeFlow| |maintainer-ChrisOForgeFlow|
This module is part of the `OCA/web <https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart>`_ project on GitHub. This module is part of the `OCA/web <https://github.com/OCA/web/tree/15.0/web_widget_mpld3_chart>`_ project on GitHub.

View File

@ -2,7 +2,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import json
import logging import logging
from odoo import api, models from odoo import api, models
@ -26,8 +25,7 @@ class AbstractMpld3Parser(models.AbstractModel):
html_string = mpld3.fig_to_html(figure, no_extras=True, include_libraries=False) html_string = mpld3.fig_to_html(figure, no_extras=True, include_libraries=False)
soup = BeautifulSoup(html_string, "lxml") soup = BeautifulSoup(html_string, "lxml")
json_data = { json_data = {
"style": soup.style.decode(),
"div": str(soup.div), "div": str(soup.div),
"script": soup.script.decode_contents(), "script": soup.script.decode_contents(),
} }
return json.dumps(json_data) return json_data

View File

@ -9,9 +9,9 @@ To insert a mpld3 chart in a view proceed as follows:
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
#. Declare a text computed field like this:: #. Declare a json computed field like this::
mpld3_chart = fields.Text( mpld3_chart = fields.Json(
string='Mpld3 Chart', string='Mpld3 Chart',
compute='_compute_mpld3_chart', compute='_compute_mpld3_chart',
) )

View File

@ -1,12 +1,22 @@
/** @odoo-module **/ /** @odoo-module **/
import {CharField} from "@web/views/fields/char/char_field";
import {loadBundle} from "@web/core/assets"; import {loadBundle} from "@web/core/assets";
import {registry} from "@web/core/registry"; import {registry} from "@web/core/registry";
const {onWillStart, markup} = owl; const {onWillStart, markup, Component, onMounted, onPatched, useRef} = owl;
class Mpld3ChartWidget extends CharField {
class Mpld3ChartJsonWidget extends Component {
setup() { setup() {
super.setup(); this.widget = useRef("widget");
onPatched(() => {
var script = document.createElement("script");
script.text = this.props.value.script;
this.widget.el.append(script);
});
onMounted(() => {
var script = document.createElement("script");
script.text = this.props.value.script;
this.widget.el.append(script);
});
onWillStart(() => onWillStart(() =>
loadBundle({ loadBundle({
jsLibs: [ jsLibs: [
@ -16,17 +26,12 @@ class Mpld3ChartWidget extends CharField {
}) })
); );
} }
get json_value() { markup(value) {
try { console.log("Marking up...");
var value = JSON.parse(this.props.value); return markup(value);
value.div = markup(value.div.trim());
return value;
} catch (error) {
return {};
}
} }
} }
Mpld3ChartWidget.template = "web_widget_mpld3_chart.Mpld3ChartField"; Mpld3ChartJsonWidget.template = "web_widget_mpld3_chart.Mpld3ChartJsonWidget";
registry.category("fields").add("mpld3_chart", Mpld3ChartWidget); registry.category("fields").add("mpld3_chart", Mpld3ChartJsonWidget);
export default Mpld3ChartWidget; export default Mpld3ChartJsonWidget;

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve"> <templates xml:space="preserve">
<t t-name="web_widget_mpld3_chart.Mpld3ChartField" owl="1"> <div t-ref="widget" t-name="web_widget_mpld3_chart.Mpld3ChartJsonWidget" owl="1">
<t t-out="json_value.div" /> <t t-if="props.value.div" t-out="markup(props.value.div)" />
<script type="text/javascript" t-out="json_value.script" /> </div>
</t>
</templates> </templates>