summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/env.js
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2020-05-28 09:55:44 +0000
committerAaron Bohy <aab@odoo.com>2020-06-12 08:04:14 +0000
commit26fe23201bb495176d14c15c8866e8fc72ca044c (patch)
treed464bed68b4a9e5494869db375c0aea103d69350 /addons/web/static/src/js/env.js
parentc4786fccfd23360a02e442c48ae2fb9f1e70aa4c (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/env.js')
-rw-r--r--addons/web/static/src/js/env.js157
1 files changed, 8 insertions, 149 deletions
diff --git a/addons/web/static/src/js/env.js b/addons/web/static/src/js/env.js
index c95cbd3045a..7fd7e1defab 100644
--- a/addons/web/static/src/js/env.js
+++ b/addons/web/static/src/js/env.js
@@ -1,157 +1,16 @@
odoo.define("web.env", function (require) {
"use strict";
- const { jsonRpc } = require('web.ajax');
- const { device, isDebug } = require("web.config");
- const { bus, serviceRegistry } = require("web.core");
+ /**
+ * This file defines the env to use in the webclient.
+ */
+
+ const commonEnv = require('web.commonEnv');
const dataManager = require('web.data_manager');
const { blockUI, unblockUI } = require("web.framework");
- const rpc = require("web.rpc");
- const session = require("web.session");
- const { _t } = require('web.translation');
- const utils = require("web.utils");
-
- const qweb = new owl.QWeb({ translateFn: _t });
-
- function ajaxJsonRPC() {
- return jsonRpc(...arguments);
- }
-
- function getCookie() {
- return utils.get_cookie(...arguments);
- }
-
- function httpRequest(route, params = {}, readMethod = 'json') {
- const info = {
- method: params.method || 'POST',
- };
- if (params.method !== 'GET') {
- const formData = new FormData();
- for (const key in params) {
- if (key === 'method') {
- continue;
- }
- const value = params[key];
- if (Array.isArray(value) && value.length) {
- for (const val of value) {
- formData.append(key, val);
- }
- } else {
- formData.append(key, value);
- }
- }
- info.body = formData;
- }
-
- return fetch(route, info).then(response => response[readMethod]());
- }
-
- function navigate(url, params) {
- window.location = $.param.querystring(url, params);
- }
-
- function performRPC(params, options) {
- const query = rpc.buildQuery(params);
- return session.rpc(query.route, query.params, options);
- }
-
- function reloadPage() {
- window.location.reload();
- }
-
- function setCookie() {
- utils.set_cookie(...arguments);
- }
- // ServiceProvider
- const services = {}; // dict containing deployed service instances
- const UndeployedServices = {}; // dict containing classes of undeployed services
- function _deployServices() {
- let done = false;
- while (!done) {
- const serviceName = _.findKey(UndeployedServices, Service => {
- // no missing dependency
- return !_.some(Service.prototype.dependencies, depName => {
- return !services[depName];
- });
- });
- if (serviceName) {
- const Service = UndeployedServices[serviceName];
- // we created a patched version of the Service in which the 'trigger_up'
- // function directly calls the requested service, instead of triggering
- // a 'call_service' event up, which wouldn't work as services have
- // no parent
- const PatchedService = Service.extend({
- _trigger_up: function (ev) {
- this._super(...arguments);
- if (!ev.is_stopped() && ev.name === 'call_service') {
- 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);
- }
- const service = services[payload.service];
- const result = service[payload.method].apply(service, args);
- payload.callback(result);
- } else {
- // historically, some services could reach the webclient
- // by triggering events up, as the webclient was their
- // parent. Since services have been moved to the env,
- // this is no longer the case, so we re-trigger those
- // events on the bus for now. Eventually, services
- // should stop triggering events up (they can still
- // communicate with other services through the env).
- bus.trigger('legacy_webclient_request', ev);
- }
- },
- });
- const service = new PatchedService();
- services[serviceName] = service;
- delete UndeployedServices[serviceName];
- service.start();
- } else {
- done = true;
- }
- }
- }
- _.each(serviceRegistry.map, (Service, serviceName) => {
- if (serviceName in UndeployedServices) {
- throw new Error(`Service ${serviceName} is already loaded.`);
- }
- UndeployedServices[serviceName] = Service;
- });
- serviceRegistry.onAdd((serviceName, Service) => {
- if (serviceName in services || serviceName in UndeployedServices) {
- throw new Error(`Service ${serviceName} is already loaded.`);
- }
- UndeployedServices[serviceName] = Service;
- _deployServices();
- });
- _deployServices();
+ const env = Object.assign(commonEnv, { dataManager });
+ env.services = Object.assign(env.services, { blockUI, unblockUI });
- // 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 {
- _t,
- bus,
- dataManager,
- device,
- isDebug,
- qweb,
- services: Object.assign(services, {
- ajaxJsonRPC,
- blockUI,
- getCookie,
- httpRequest,
- navigate,
- reloadPage,
- rpc: performRPC,
- setCookie,
- unblockUI,
- }),
- session,
- };
+ return env;
});