summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/common_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/common_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/common_env.js')
-rw-r--r--addons/web/static/src/js/common_env.js119
1 files changed, 119 insertions, 0 deletions
diff --git a/addons/web/static/src/js/common_env.js b/addons/web/static/src/js/common_env.js
new file mode 100644
index 00000000000..0e31b8e3ceb
--- /dev/null
+++ b/addons/web/static/src/js/common_env.js
@@ -0,0 +1,119 @@
+odoo.define("web.commonEnv", function (require) {
+ "use strict";
+
+ /**
+ * This file defines the common environment, which contains everything that
+ * is needed in the env for both the backend and the frontend (Odoo
+ * terminology). This module shouldn't be used as is. It should only be
+ * imported by the module defining the final env to use (in the frontend or
+ * in the backend). For instance, module 'web.env' imports it, adds stuff to
+ * it, and exports the final env that is used by the whole webclient
+ * application.
+ *
+ * There should be as much dependencies as possible in the env object. This
+ * will allow an easier testing of components. See [1] for more information
+ * on environments.
+ *
+ * [1] https://github.com/odoo/owl/blob/master/doc/reference/environment.md#content-of-an-environment
+ */
+
+ const { jsonRpc } = require("web.ajax");
+ const { device, isDebug } = require("web.config");
+ const { bus, serviceRegistry } = require("web.core");
+ const rpc = require("web.rpc");
+ const session = require("web.session");
+ const { _t } = require("web.translation");
+ const utils = require("web.utils");
+
+
+ // Build the basic env
+ const env = {
+ _t,
+ bus,
+ device,
+ isDebug,
+ qweb: new owl.QWeb({ translateFn: _t }),
+ services: {
+ ajaxJsonRPC() {
+ return jsonRpc(...arguments);
+ },
+ getCookie() {
+ return utils.get_cookie(...arguments);
+ },
+ 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]());
+ },
+ navigate(url, params) {
+ window.location = $.param.querystring(url, params);
+ },
+ reloadPage() {
+ window.location.reload();
+ },
+ rpc(params, options) {
+ const query = rpc.buildQuery(params);
+ return session.rpc(query.route, query.params, options);
+ },
+ setCookie() {
+ utils.set_cookie(...arguments);
+ },
+ },
+ session,
+ };
+
+
+ // Deploy services in the env (specializations of AbstractService registered
+ // into the serviceRegistry)
+ const UndeployedServices = Object.assign({}, serviceRegistry.map);
+ function _deployServices() {
+ let done = false;
+ while (!done) {
+ // find a service with no missing dependency
+ const serviceName = Object.keys(UndeployedServices).find(serviceName => {
+ const Service = UndeployedServices[serviceName];
+ return Service.prototype.dependencies.every(depName => {
+ return env.services[depName];
+ });
+ });
+ if (serviceName) {
+ const Service = UndeployedServices[serviceName];
+ const service = new Service(env);
+ env.services[serviceName] = service;
+ delete UndeployedServices[serviceName];
+ service.start();
+ } else {
+ done = true;
+ }
+ }
+ }
+ serviceRegistry.onAdd((serviceName, Service) => {
+ if (serviceName in env.services || serviceName in UndeployedServices) {
+ throw new Error(`Service ${serviceName} is already loaded.`);
+ }
+ UndeployedServices[serviceName] = Service;
+ _deployServices();
+ });
+ _deployServices();
+
+
+ return env;
+});