diff options
| author | Aaron Bohy <aab@odoo.com> | 2020-05-28 09:55:44 +0000 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2020-06-12 08:04:14 +0000 |
| commit | 26fe23201bb495176d14c15c8866e8fc72ca044c (patch) | |
| tree | d464bed68b4a9e5494869db375c0aea103d69350 /addons/web/static/src/js/public | |
| parent | c4786fccfd23360a02e442c48ae2fb9f1e70aa4c (diff) | |
[IMP] web,*: properly define env in the frontend
*bus,mail
This commit extracts the common basis of the env to use both in
the frontend and the backend. The public env now contains most of
the features that were previously (only) in the webclient env.
Moreover, the PublicRoot no longer uses the ServiceProviderMixin,
such that services are only deployed once, in the (public) env.
Owl components can now be defined in the website, and rely on a
properly built env. Legacy widgets still works as they access
services through the PublicRoot (via trigger_up) which redirects
those requests to the env.
Diffstat (limited to 'addons/web/static/src/js/public')
| -rw-r--r-- | addons/web/static/src/js/public/public_env.js | 23 | ||||
| -rw-r--r-- | addons/web/static/src/js/public/public_root.js | 87 | ||||
| -rw-r--r-- | addons/web/static/src/js/public/public_root_instance.js | 7 |
3 files changed, 61 insertions, 56 deletions
diff --git a/addons/web/static/src/js/public/public_env.js b/addons/web/static/src/js/public/public_env.js index 373c7b512b8..581cccd430b 100644 --- a/addons/web/static/src/js/public/public_env.js +++ b/addons/web/static/src/js/public/public_env.js @@ -1,24 +1,11 @@ odoo.define("web.public_env", function (require) { "use strict"; - const { _t } = require("web.core"); - const rpc = require("web.rpc"); + /** + * This file defines the env to use in the public side. + */ - const qweb = new owl.QWeb({ translateFn: _t }); + const commonEnv = require("web.commonEnv"); - function performRPC(params, options) { - const query = rpc.buildQuery(params); - return session.rpc(query.route, query.params, options); - } - - // There should be as much dependencies as possible in the env object. - // This will allow an easier testing of components. - // See https://github.com/odoo/owl/blob/master/doc/reference/environment.md#content-of-an-environment - // for more information on environments. - return { - qweb, - services: { - rpc: performRPC, - }, - }; + return commonEnv; }); diff --git a/addons/web/static/src/js/public/public_root.js b/addons/web/static/src/js/public/public_root.js index 33d442e1d76..f684df2b629 100644 --- a/addons/web/static/src/js/public/public_root.js +++ b/addons/web/static/src/js/public/public_root.js @@ -3,7 +3,7 @@ odoo.define('web.public.root', function (require) { var ajax = require('web.ajax'); var dom = require('web.dom'); -var ServiceProviderMixin = require('web.ServiceProviderMixin'); +const env = require('web.public_env'); var session = require('web.session'); var utils = require('web.utils'); var publicWidget = require('web.public.widget'); @@ -25,16 +25,17 @@ var localeDef = ajax.loadJS('/web/webclient/locale/' + lang.replace('-', '_')); * this Class instance. Its main role will be to retrieve RPC demands from its * children and handle them. */ -var PublicRoot = publicWidget.RootWidget.extend(ServiceProviderMixin, { +var PublicRoot = publicWidget.RootWidget.extend({ events: _.extend({}, publicWidget.RootWidget.prototype.events || {}, { 'submit .js_website_submit_form': '_onWebsiteFormSubmit', 'click .js_disable_on_click': '_onDisableOnClick', }), custom_events: _.extend({}, publicWidget.RootWidget.prototype.custom_events || {}, { - 'context_get': '_onContextGet', - 'main_object_request': '_onMainObjectRequest', - 'widgets_start_request': '_onWidgetsStartRequest', - 'widgets_stop_request': '_onWidgetsStopRequest', + call_service: '_onCallService', + context_get: '_onContextGet', + main_object_request: '_onMainObjectRequest', + widgets_start_request: '_onWidgetsStartRequest', + widgets_stop_request: '_onWidgetsStopRequest', }), /** @@ -42,7 +43,7 @@ var PublicRoot = publicWidget.RootWidget.extend(ServiceProviderMixin, { */ init: function () { this._super.apply(this, arguments); - ServiceProviderMixin.init.call(this); + this.env = env; this.publicWidgets = []; }, /** @@ -91,37 +92,6 @@ var PublicRoot = publicWidget.RootWidget.extend(ServiceProviderMixin, { //-------------------------------------------------------------------------- /** - * Automatically adds the global context to RPCs. - * - * @override - */ - _call_service: function (ev) { - if (ev.data.service === 'ajax' && ev.data.method === 'rpc') { - var route = ev.data.args[0]; - if (_.str.startsWith(route, '/web/dataset/call_kw/')) { - var params = ev.data.args[1]; - var options = ev.data.args[2]; - var noContextKeys = undefined; - if (options) { - noContextKeys = options.noContextKeys; - ev.data.args[2] = _.omit(options, 'noContextKeys'); - } - params.kwargs.context = _computeContext.call(this, params.kwargs.context, noContextKeys); - } - } else if (ev.data.service === 'ajax' && ev.data.method === 'loadLibs') { - ev.data.args[1] = _computeContext.call(this, ev.data.args[1]); - } - return ServiceProviderMixin._call_service.apply(this, arguments); - - function _computeContext(context, noContextKeys) { - context = _.extend({}, this._getContext(), context); - if (noContextKeys) { - context = _.omit(context, noContextKeys); - } - return JSON.parse(JSON.stringify(context)); - } - }, - /** * Retrieves the global context of the public environment. This is the * context which is automatically added to each RPC. * @@ -235,6 +205,47 @@ var PublicRoot = publicWidget.RootWidget.extend(ServiceProviderMixin, { //-------------------------------------------------------------------------- /** + * Calls the requested service from the env. Automatically adds the global + * context to RPCs. + * + * @private + * @param {OdooEvent} event + */ + _onCallService: function (ev) { + function _computeContext(context, noContextKeys) { + context = _.extend({}, this._getContext(), context); + if (noContextKeys) { + context = _.omit(context, noContextKeys); + } + return JSON.parse(JSON.stringify(context)); + } + + const payload = ev.data; + let args = payload.args || []; + if (payload.service === 'ajax' && payload.method === 'rpc') { + // ajax service uses an extra 'target' argument for rpc + args = args.concat(ev.target); + + var route = args[0]; + if (_.str.startsWith(route, '/web/dataset/call_kw/')) { + var params = args[1]; + var options = args[2]; + var noContextKeys; + if (options) { + noContextKeys = options.noContextKeys; + args[2] = _.omit(options, 'noContextKeys'); + } + params.kwargs.context = _computeContext.call(this, params.kwargs.context, noContextKeys); + } + } else if (payload.service === 'ajax' && payload.method === 'loadLibs') { + args[1] = _computeContext.call(this, args[1]); + } + + const service = this.env.services[payload.service]; + const result = service[payload.method].apply(service, args); + payload.callback(result); + }, + /** * Called when someone asked for the global public context. * * @private diff --git a/addons/web/static/src/js/public/public_root_instance.js b/addons/web/static/src/js/public/public_root_instance.js index 988d939bbe1..e368d421fbe 100644 --- a/addons/web/static/src/js/public/public_root_instance.js +++ b/addons/web/static/src/js/public/public_root_instance.js @@ -1,10 +1,17 @@ odoo.define('root.widget', function (require) { 'use strict'; +const env = require('web.public_env'); var lazyloader = require('web.public.lazyloader'); var rootData = require('web.public.root'); /** + * Configure Owl with the public env + */ +owl.config.mode = env.isDebug() ? "dev" : "prod"; +owl.Component.env = env; + +/** * This widget is important, because the tour manager needs a root widget in * order to work. The root widget must be a service provider with the ajax * service, so that the tour manager can let the server know when tours have |
