[MOV] web: moving the RootWidget definition after PublicWidget

In the next commit, the PublicWidget class will be made independent of
the legacy Widget class.

Therefore, the RootWidget cannot be defined before the PublicWidget as
it will extend the PublicWidget rather than the legacy Widget.

This commit moves its definition after the PublicWidget definition.

task-3249625

Part-of: odoo/odoo#117210
This commit is contained in:
Arthur Detroux (ard)
2023-07-13 19:27:52 +02:00
committed by qsm-odoo
parent db6b6880d6
commit afbbf75ec9
@@ -7,96 +7,6 @@
import dom from 'web.dom';
import Widget from 'web.Widget';
/**
* Specialized Widget which automatically instantiates child widgets to attach
* to internal DOM elements once it is started. The widgets to instantiate are
* known thanks to a linked registry which contains info about the widget
* classes and jQuery selectors to use to find the elements to attach them to.
*
* @todo Merge with 'PublicWidget' ?
*/
var RootWidget = Widget.extend({
/**
* @constructor
*/
init: function () {
this._super.apply(this, arguments);
this._widgets = [];
},
/**
* @override
* @see _attachComponents
*/
start: function () {
var defs = [this._super.apply(this, arguments)];
defs.push(this._attachComponents());
this._getRegistry().on("UPDATE", this, ({ operation, value }) => {
if (operation === "add") {
this._attachComponent(value);
}
});
return Promise.all(defs);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Instantiates a child widget according to the given registry data.
*
* @private
* @param {Object} childInfo
* @param {function} childInfo.Widget - the widget class to instantiate
* @param {string} childInfo.selector
* the jQuery selector to use to find the internal DOM element which
* needs to be attached to the instantiated widget
* @param {jQuery} [$from] - only check DOM elements which are descendant of
* the given one. If not given, use this.$el.
* @returns {Deferred}
*/
_attachComponent: function (childInfo, $from) {
var self = this;
var $elements = dom.cssFind($from || this.$el, childInfo.selector);
var defs = Array.from($elements).map((element) => {
var w = new childInfo.Widget(self);
self._widgets.push(w);
return w.attachTo(element);
});
return Promise.all(defs);
},
/**
* Instantiates the child widgets that need to be according to the linked
* registry.
*
* @private
* @param {jQuery} [$from] - only check DOM elements which are descendant of
* the given one. If not given, use this.$el.
* @returns {Deferred}
*/
_attachComponents: function ($from) {
var self = this;
var childInfos = this._getRegistry().getAll();
var defs = childInfos.map((childInfo) => {
return self._attachComponent(childInfo, $from);
});
return Promise.all(defs);
},
/**
* Returns the `RootWidgetRegistry` instance that is linked to this
* `RootWidget` instance.
*
* @abstract
* @private
* @returns {RootWidgetRegistry}
*/
_getRegistry: function () {},
});
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/**
* Provides a way for executing code once a website DOM element is loaded in the
* dom.
@@ -257,6 +167,96 @@ var PublicWidget = Widget.extend({
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/**
* Specialized Widget which automatically instantiates child widgets to attach
* to internal DOM elements once it is started. The widgets to instantiate are
* known thanks to a linked registry which contains info about the widget
* classes and jQuery selectors to use to find the elements to attach them to.
*
* @todo Merge with 'PublicWidget' ?
*/
var RootWidget = Widget.extend({
/**
* @constructor
*/
init: function () {
this._super.apply(this, arguments);
this._widgets = [];
},
/**
* @override
* @see _attachComponents
*/
start: function () {
var defs = [this._super.apply(this, arguments)];
defs.push(this._attachComponents());
this._getRegistry().on("UPDATE", this, ({ operation, value }) => {
if (operation === "add") {
this._attachComponent(value);
}
});
return Promise.all(defs);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Instantiates a child widget according to the given registry data.
*
* @private
* @param {Object} childInfo
* @param {function} childInfo.Widget - the widget class to instantiate
* @param {string} childInfo.selector
* the jQuery selector to use to find the internal DOM element which
* needs to be attached to the instantiated widget
* @param {jQuery} [$from] - only check DOM elements which are descendant of
* the given one. If not given, use this.$el.
* @returns {Deferred}
*/
_attachComponent: function (childInfo, $from) {
var self = this;
var $elements = dom.cssFind($from || this.$el, childInfo.selector);
var defs = Array.from($elements).map((element) => {
var w = new childInfo.Widget(self);
self._widgets.push(w);
return w.attachTo(element);
});
return Promise.all(defs);
},
/**
* Instantiates the child widgets that need to be according to the linked
* registry.
*
* @private
* @param {jQuery} [$from] - only check DOM elements which are descendant of
* the given one. If not given, use this.$el.
* @returns {Deferred}
*/
_attachComponents: function ($from) {
var self = this;
var childInfos = this._getRegistry().getAll();
var defs = childInfos.map((childInfo) => {
return self._attachComponent(childInfo, $from);
});
return Promise.all(defs);
},
/**
* Returns the `RootWidgetRegistry` instance that is linked to this
* `RootWidget` instance.
*
* @abstract
* @private
* @returns {RootWidgetRegistry}
*/
_getRegistry: function () {},
});
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/**
* The registry object contains the list of widgets that should be instantiated
* thanks to their selector property if any.