From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- .../components/dialog_manager/dialog_manager.js | 69 ++++++++++++++++++ .../components/dialog_manager/dialog_manager.xml | 17 +++++ .../dialog_manager/dialog_manager_tests.js | 82 ++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 addons/mail/static/src/components/dialog_manager/dialog_manager.js create mode 100644 addons/mail/static/src/components/dialog_manager/dialog_manager.xml create mode 100644 addons/mail/static/src/components/dialog_manager/dialog_manager_tests.js (limited to 'addons/mail/static/src/components/dialog_manager') diff --git a/addons/mail/static/src/components/dialog_manager/dialog_manager.js b/addons/mail/static/src/components/dialog_manager/dialog_manager.js new file mode 100644 index 00000000..69b64a27 --- /dev/null +++ b/addons/mail/static/src/components/dialog_manager/dialog_manager.js @@ -0,0 +1,69 @@ +odoo.define('mail/static/src/components/dialog_manager/dialog_manager.js', function (require) { +'use strict'; + +const components = { + Dialog: require('mail/static/src/components/dialog/dialog.js'), +}; +const useShouldUpdateBasedOnProps = require('mail/static/src/component_hooks/use_should_update_based_on_props/use_should_update_based_on_props.js'); +const useStore = require('mail/static/src/component_hooks/use_store/use_store.js'); + +const { Component } = owl; + +class DialogManager extends Component { + + /** + * @override + */ + constructor(...args) { + super(...args); + useShouldUpdateBasedOnProps(); + useStore(props => { + const dialogManager = this.env.messaging && this.env.messaging.dialogManager; + return { + dialogManager: dialogManager ? dialogManager.__state : undefined, + }; + }); + } + + mounted() { + this._checkDialogOpen(); + } + + patched() { + this._checkDialogOpen(); + } + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ + _checkDialogOpen() { + if (!this.env.messaging) { + /** + * Messaging not created, which means essential models like + * dialog manager are not ready, so open status of dialog in DOM + * is omitted during this (short) period of time. + */ + return; + } + if (this.env.messaging.dialogManager.dialogs.length > 0) { + document.body.classList.add('modal-open'); + } else { + document.body.classList.remove('modal-open'); + } + } + +} + +Object.assign(DialogManager, { + components, + props: {}, + template: 'mail.DialogManager', +}); + +return DialogManager; + +}); diff --git a/addons/mail/static/src/components/dialog_manager/dialog_manager.xml b/addons/mail/static/src/components/dialog_manager/dialog_manager.xml new file mode 100644 index 00000000..035e543e --- /dev/null +++ b/addons/mail/static/src/components/dialog_manager/dialog_manager.xml @@ -0,0 +1,17 @@ + + + + +
+ + + + + +
+
+ +
diff --git a/addons/mail/static/src/components/dialog_manager/dialog_manager_tests.js b/addons/mail/static/src/components/dialog_manager/dialog_manager_tests.js new file mode 100644 index 00000000..f377ec17 --- /dev/null +++ b/addons/mail/static/src/components/dialog_manager/dialog_manager_tests.js @@ -0,0 +1,82 @@ +odoo.define('mail/static/src/components/dialog_manager/dialog_manager_tests.js', function (require) { +'use strict'; + +const { makeDeferred } = require('mail/static/src/utils/deferred/deferred.js'); +const { + afterEach, + beforeEach, + nextAnimationFrame, + start, +} = require('mail/static/src/utils/test_utils.js'); + +QUnit.module('mail', {}, function () { +QUnit.module('components', {}, function () { +QUnit.module('dialog_manager', {}, function () { +QUnit.module('dialog_manager_tests.js', { + beforeEach() { + beforeEach(this); + + this.start = async params => { + const { env, widget } = await start(Object.assign( + { hasDialog: true }, + params, + { data: this.data } + )); + this.env = env; + this.widget = widget; + }; + }, + afterEach() { + afterEach(this); + }, +}); + +QUnit.test('[technical] messaging not created', async function (assert) { + /** + * Creation of messaging in env is async due to generation of models being + * async. Generation of models is async because it requires parsing of all + * JS modules that contain pieces of model definitions. + * + * Time of having no messaging is very short, almost imperceptible by user + * on UI, but the display should not crash during this critical time period. + */ + assert.expect(2); + + const messagingBeforeCreationDeferred = makeDeferred(); + await this.start({ + messagingBeforeCreationDeferred, + waitUntilMessagingCondition: 'none', + }); + assert.containsOnce( + document.body, + '.o_DialogManager', + "should have dialog manager even when messaging is not yet created" + ); + + // simulate messaging being created + messagingBeforeCreationDeferred.resolve(); + await nextAnimationFrame(); + + assert.containsOnce( + document.body, + '.o_DialogManager', + "should still contain dialog manager after messaging has been created" + ); +}); + +QUnit.test('initial mount', async function (assert) { + assert.expect(1); + + await this.start(); + assert.containsOnce( + document.body, + '.o_DialogManager', + "should have dialog manager" + ); +}); + +}); +}); +}); + +}); -- cgit v1.2.3