mirror of https://github.com/OCA/web.git
[IMP] web_widget_mpld3_chart: Move widget from CharField to Component(Json field)
parent
972f688dbe
commit
4384f709b7
|
@ -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',
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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',
|
||||||
)
|
)
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue