diff options
| author | Géry Debongnie <ged@odoo.com> | 2017-09-13 11:48:02 +0200 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2017-09-13 14:04:34 +0200 |
| commit | 93cf24f73ae79cdfd0346145d0b828ebd1ea07f9 (patch) | |
| tree | 25092bf66b9ab6ff1a8ff4b424aca75db7ca0537 /addons/web/static/src/js/views/basic | |
| parent | c1fa3b8ab3dfa1306dbdd3b6dc910405a3357d16 (diff) | |
[FIX] web: reimplement custom widgets in basic views
With the new views, we lost a (almost unused and undocumented) feature:
the ability to instantiate custom widgets in a form view, not linked to
a particular field. For example:
<widget type="weekly_timesheet" attrs="{'readonly': [['state', 'not in', ['new', 'draft']]]}"/>
We reintroduce this feature in this commit, with a nice twist: it also
works for the list view and the kanban view.
Diffstat (limited to 'addons/web/static/src/js/views/basic')
| -rw-r--r-- | addons/web/static/src/js/views/basic/basic_renderer.js | 39 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/basic/widget_registry.js | 27 |
2 files changed, 66 insertions, 0 deletions
diff --git a/addons/web/static/src/js/views/basic/basic_renderer.js b/addons/web/static/src/js/views/basic/basic_renderer.js index 0e26e2fa19f..ee6e685c3d4 100644 --- a/addons/web/static/src/js/views/basic/basic_renderer.js +++ b/addons/web/static/src/js/views/basic/basic_renderer.js @@ -11,6 +11,7 @@ var AbstractRenderer = require('web.AbstractRenderer'); var config = require('web.config'); var core = require('web.core'); var dom = require('web.dom'); +var widgetRegistry = require('web.widget_registry'); var qweb = core.qweb; @@ -332,6 +333,19 @@ var BasicRenderer = AbstractRenderer.extend({ return _.findWhere(this.allModifiersData, {node: node}); }, /** + * @private + * @param {jQueryElement} $el + * @param {Object} node + */ + _handleAttributes: function ($el, node) { + if (node.attrs.class) { + $el.addClass(node.attrs.class); + } + if (node.attrs.style) { + $el.attr('style', node.attrs.style); + } + }, + /** * Used by list and kanban renderers to determine whether or not to display * the no content helper (if there is no data in the state to display) * @@ -541,6 +555,31 @@ var BasicRenderer = AbstractRenderer.extend({ return $.when(); }, /** + * Instantiate custom widgets + * + * @private + * @param {Object} record + * @param {Object} node + * @returns {jQueryElement} + */ + _renderWidget: function (record, node) { + var Widget = widgetRegistry.get(node.attrs.name); + var widget = new Widget(this, record); + + // Prepare widget rendering and save the related deferred + var def = widget.__widgetRenderAndInsert(function () {}); + if (def.state() === 'pending') { + this.defs.push(def); + } + + // handle other attributes/modifiers + this._handleAttributes(widget.$el, node); + this._registerModifiers(node, record, widget); + widget.$el.addClass('o_widget'); + return widget.$el; + }, + + /** * Rerenders a given widget and make sure the associated data which * referenced the old one is updated. * diff --git a/addons/web/static/src/js/views/basic/widget_registry.js b/addons/web/static/src/js/views/basic/widget_registry.js new file mode 100644 index 00000000000..470127bd12e --- /dev/null +++ b/addons/web/static/src/js/views/basic/widget_registry.js @@ -0,0 +1,27 @@ +odoo.define('web.widget_registry', function (require) { + "use strict"; + + // This registry is supposed to contain all custom widgets that will be + // available in the basic views, with the tag <widget/>. There are + // currently no such widget in the web client, but the functionality is + // certainly useful to be able to cleanly add custom behaviour in basic + // views (and most notably, the form view) + // + // The way custom widgets work is that they register themselves to this + // registry: + // + // widgetRegistry.add('some_name', MyWidget); + // + // Then, they are available with the <widget/> tag (in the arch): + // + // <widget name="some_name"/> + // + // Widgets will be then properly instantiated, rendered and destroyed at the + // appropriate time, with the current state in second argument. + // + // For more examples, look at the tests (grep '<widget' in the test folder) + + var Registry = require('web.Registry'); + + return new Registry(); +}); |
