diff --git a/scripts/langindex.json b/scripts/langindex.json index 93ec25362..809db3959 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1465,6 +1465,7 @@ "core.cannotconnecttrouble": "local_moodlemobileapp", "core.cannotconnectverify": "local_moodlemobileapp", "core.cannotdownloadfiles": "local_moodlemobileapp", + "core.cannotlogoutpageblocks": "local_moodlemobileapp", "core.cannotopeninapp": "local_moodlemobileapp", "core.cannotopeninappdownload": "local_moodlemobileapp", "core.captureaudio": "local_moodlemobileapp", diff --git a/src/core/features/mainmenu/components/user-menu/user-menu.ts b/src/core/features/mainmenu/components/user-menu/user-menu.ts index e3cda789c..c20d16bce 100644 --- a/src/core/features/mainmenu/components/user-menu/user-menu.ts +++ b/src/core/features/mainmenu/components/user-menu/user-menu.ts @@ -28,7 +28,7 @@ import { import { CoreNavigator } from '@services/navigator'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; -import { ModalController } from '@singletons'; +import { ModalController, Translate } from '@singletons'; import { Subscription } from 'rxjs'; /** @@ -172,6 +172,12 @@ export class CoreMainMenuUserMenuComponent implements OnInit, OnDestroy { * @param event Click event */ async logout(event: Event): Promise { + if (CoreNavigator.currentRouteCanBlockLeave()) { + await CoreDomUtils.showAlert(undefined, Translate.instant('core.cannotlogoutpageblocks')); + + return; + } + if (this.removeAccountOnLogout) { // Ask confirm. const siteName = this.siteName ? @@ -200,6 +206,12 @@ export class CoreMainMenuUserMenuComponent implements OnInit, OnDestroy { * @param event Click event */ async switchAccounts(event: Event): Promise { + if (CoreNavigator.currentRouteCanBlockLeave()) { + await CoreDomUtils.showAlert(undefined, Translate.instant('core.cannotlogoutpageblocks')); + + return; + } + const thisModal = await ModalController.getTop(); event.preventDefault(); diff --git a/src/core/lang.json b/src/core/lang.json index 7429c930b..a3e73bb03 100644 --- a/src/core/lang.json +++ b/src/core/lang.json @@ -18,6 +18,7 @@ "cannotconnecttrouble": "We're having trouble connecting to your site.", "cannotconnectverify": "Please check the address is correct.", "cannotdownloadfiles": "File downloading is disabled. Please contact your site administrator.", + "cannotlogoutpageblocks": "Please save or discard your changes before continuing.", "cannotopeninapp": "This file may not work as expected on this device. Would you like to open it anyway?", "cannotopeninappdownload": "This file may not work as expected on this device. Would you like to download it anyway?", "captureaudio": "Record audio", diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts index e2613e579..a33abca58 100644 --- a/src/core/services/navigator.ts +++ b/src/core/services/navigator.ts @@ -658,6 +658,15 @@ export class CoreNavigatorService { } } + /** + * Check if the current route page can block leaving the route. + * + * @return Whether the current route page can block leaving the route. + */ + currentRouteCanBlockLeave(): boolean { + return !!this.getCurrentRoute().snapshot.routeConfig?.canDeactivate?.length; + } + } export const CoreNavigator = makeSingleton(CoreNavigatorService);