summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/fields
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2018-08-16 12:39:26 +0200
committerAlexandre Kühn <aku@odoo.com>2018-08-16 16:52:56 +0200
commit10f43955d81ff3e7b5ab050656b312ec19f927c5 (patch)
treebba78a14ea795225d9880e9376dd5df5789d26b6 /addons/web/static/src/js/fields
parent2a2ff6eb5c746acec46ebc7fee382dfa78ec8c14 (diff)
[FIX] web: render JournalDashboardGraph when in DOM
The lib 'nvd3' renders graph when they are attached to the DOM. However, widgets are rendered in fragments and appended to the DOM when ready (to prevent flickering). Before this commit, we used a `setTimeout(0)` and cross fingers that the widget was attached to the DOM when the `render` method was called. With this commit, the render method is called when it is attached to the DOM. Task-ID 1868252 Closes #26410
Diffstat (limited to 'addons/web/static/src/js/fields')
-rw-r--r--addons/web/static/src/js/fields/basic_fields.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js
index 9bf432c97fc..71b2a14c434 100644
--- a/addons/web/static/src/js/fields/basic_fields.js
+++ b/addons/web/static/src/js/fields/basic_fields.js
@@ -2379,6 +2379,25 @@ var JournalDashboardGraph = AbstractField.extend({
}
this._super.apply(this, arguments);
},
+ /**
+ * The widget view uses the nv(d3) lib to render the graph. This lib
+ * requires that the rendering is done directly into the DOM (so that it can
+ * correctly compute positions). However, the views are always rendered in
+ * fragments, and appended to the DOM once ready (to prevent them from
+ * flickering). We here use the on_attach_callback hook, called when the
+ * widget is attached to the DOM, to perform the rendering. This ensures
+ * that the rendering is always done in the DOM.
+ */
+ on_attach_callback: function () {
+ this._isInDOM = true;
+ this._renderInDOM();
+ },
+ /**
+ * Called when the field is detached from the DOM.
+ */
+ on_detach_callback: function () {
+ this._isInDOM = false;
+ },
//--------------------------------------------------------------------------
// Private
@@ -2399,9 +2418,25 @@ var JournalDashboardGraph = AbstractField.extend({
}
},
/**
+ * Render the widget only when it is in the DOM. This is because nvd3
+ * renders graph only when it is in DOM, apparently to compute available
+ * height and width for instance.
+ *
+ * @override
* @private
*/
_render: function () {
+ if (this._isInDOM) {
+ return this._renderInDOM();
+ }
+ return $.when();
+ },
+ /**
+ * Render the widget. This function assumes that it is attached to the DOM.
+ *
+ * @private
+ */
+ _renderInDOM: function () {
// note: the rendering of this widget is aynchronous as nvd3 does a
// setTimeout(0) before executing the callback given to addGraph
var self = this;