summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/main.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/js/main.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/mail/static/src/js/main.js')
-rw-r--r--addons/mail/static/src/js/main.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/addons/mail/static/src/js/main.js b/addons/mail/static/src/js/main.js
new file mode 100644
index 00000000..d19a8edb
--- /dev/null
+++ b/addons/mail/static/src/js/main.js
@@ -0,0 +1,126 @@
+odoo.define('mail/static/src/js/main.js', function (require) {
+'use strict';
+
+const ModelManager = require('mail/static/src/model/model_manager.js');
+
+const env = require('web.commonEnv');
+
+const { Store } = owl;
+const { EventBus } = owl.core;
+
+async function createMessaging() {
+ await new Promise(resolve => {
+ /**
+ * Called when all JS resources are loaded. This is useful in order
+ * to do some processing after other JS files have been parsed, for
+ * example new models or patched models that are coming from
+ * other modules, because some of those patches might need to be
+ * applied before messaging initialization.
+ */
+ window.addEventListener('load', resolve);
+ });
+ /**
+ * All JS resources are loaded, but not necessarily processed.
+ * We assume no messaging-related modules return any Promise,
+ * therefore they should be processed *at most* asynchronously at
+ * "Promise time".
+ */
+ await new Promise(resolve => setTimeout(resolve));
+ /**
+ * Some models require session data, like locale text direction (depends on
+ * fully loaded translation).
+ */
+ await env.session.is_bound;
+
+ env.modelManager.start();
+ /**
+ * Create the messaging singleton record.
+ */
+ env.messaging = env.models['mail.messaging'].create();
+}
+
+/**
+ * Messaging store
+ */
+const store = new Store({
+ env,
+ state: {
+ messagingRevNumber: 0,
+ },
+});
+
+/**
+ * Registry of models.
+ */
+env.models = {};
+/**
+ * Environment keys used in messaging.
+ */
+Object.assign(env, {
+ autofetchPartnerImStatus: true,
+ destroyMessaging() {
+ if (env.modelManager) {
+ env.modelManager.deleteAll();
+ env.messaging = undefined;
+ }
+ },
+ disableAnimation: false,
+ isMessagingInitialized() {
+ if (!this.messaging) {
+ return false;
+ }
+ return this.messaging.isInitialized;
+ },
+ /**
+ * States whether the environment is in QUnit test or not.
+ *
+ * Useful to prevent some behaviour in QUnit tests, like applying
+ * style of attachment that uses url.
+ */
+ isQUnitTest: false,
+ loadingBaseDelayDuration: 400,
+ messaging: undefined,
+ messagingBus: new EventBus(),
+ /**
+ * Promise which becomes resolved when messaging is created.
+ *
+ * Useful for discuss widget to know when messaging is created, because this
+ * is an essential condition to make it work.
+ */
+ messagingCreatedPromise: createMessaging(),
+ modelManager: new ModelManager(env),
+ store,
+});
+
+/**
+ * Components cannot use web.bus, because they cannot use
+ * EventDispatcherMixin, and webclient cannot easily access env.
+ * Communication between webclient and components by core.bus
+ * (usable by webclient) and messagingBus (usable by components), which
+ * the messaging service acts as mediator since it can easily use both
+ * kinds of buses.
+ */
+env.bus.on(
+ 'hide_home_menu',
+ null,
+ () => env.messagingBus.trigger('hide_home_menu')
+);
+env.bus.on(
+ 'show_home_menu',
+ null,
+ () => env.messagingBus.trigger('show_home_menu')
+);
+env.bus.on(
+ 'will_hide_home_menu',
+ null,
+ () => env.messagingBus.trigger('will_hide_home_menu')
+);
+env.bus.on(
+ 'will_show_home_menu',
+ null,
+ () => env.messagingBus.trigger('will_show_home_menu')
+);
+
+env.messagingCreatedPromise.then(() => env.messaging.start());
+
+});