[FIX] bus: adapt presence service to work with iframe

Before this PR, the presence service was relying on window events
blur/focus to determine whether or not odoo was focused. The issue is
that in the website backend, an iframe is used. When this iframe is focused,
the window looses the focus and vice versa.

This commit fixes this issue by using `window.parent.hasFocus` method.

task-3003950

closes odoo/odoo#102518

X-original-commit: ce57d4494f2bc6a1504f8a7bddddca2a3d2b794b
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
tsm-odoo
2022-10-06 23:14:16 +02:00
parent 30a9ecc14c
commit fc1613f68a
@@ -23,11 +23,10 @@ export const presenceService = {
lastPresenceTime = new Date().getTime();
browser.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}.lastPresence`, lastPresenceTime);
}
function onFocusChange(isFocused) {
isOdooFocused = isFocused;
function onFocusChange() {
isOdooFocused = parent.document.hasFocus();
browser.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}.focus`, isOdooFocused);
if (isFocused) {
if (isOdooFocused) {
lastPresenceTime = new Date().getTime();
env.bus.trigger('window_focus', isOdooFocused);
}
@@ -43,9 +42,9 @@ export const presenceService = {
}
}
browser.addEventListener('storage', onStorage);
browser.addEventListener('focus', () => onFocusChange(true));
browser.addEventListener('blur', () => onFocusChange(false));
browser.addEventListener('pagehide', () => onFocusChange(false));
browser.addEventListener('focus', () => onFocusChange());
browser.addEventListener('blur', () => onFocusChange());
browser.addEventListener('pagehide', () => onFocusChange());
browser.addEventListener('click', onPresence);
browser.addEventListener('keydown', onPresence);