From ad5365edfea582da681e106ae1f6d9274ee932b3 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 8 Jan 2018 13:21:15 +0100 Subject: [PATCH] MOBILE-2302 core: Implement redirect to another page/site --- src/core/login/providers/helper.ts | 71 ++++++++++++++++++++++++++ src/core/mainmenu/pages/menu/menu.html | 3 +- src/core/mainmenu/pages/menu/menu.ts | 41 +++++++++++---- 3 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/core/login/providers/helper.ts b/src/core/login/providers/helper.ts index 8b6314010..571319de1 100644 --- a/src/core/login/providers/helper.ts +++ b/src/core/login/providers/helper.ts @@ -615,6 +615,42 @@ export class CoreLoginHelperProvider { return code == CoreConstants.loginSSOCode || code == CoreConstants.loginSSOInAppCode; } + /** + * Load a site and load a certain page in that site. + * + * @param {string} page Name of the page to load. + * @param {any} params Params to pass to the page. + * @param {string} siteId Site to load. + */ + protected loadSiteAndPage(page: string, params: any, siteId: string) : void { + if (siteId == CoreConstants.noSiteId) { + // Page doesn't belong to a site, just load the page. + this.navCtrl.setRoot(page, params); + } else { + let modal = this.domUtils.showModalLoading(); + this.sitesProvider.loadSite(siteId).then(() => { + if (!this.isSiteLoggedOut(page, params)) { + this.loadPageInMainMenu(page, params); + } + }).catch(() => { + // Site doesn't exist. + this.navCtrl.setRoot('CoreLoginSitesPage') + }).finally(() => { + modal.dismiss(); + }); + } + } + + /** + * Load a certain page in the main menu page. + * + * @param {string} page Name of the page to load. + * @param {any} params Params to pass to the page. + */ + protected loadPageInMainMenu(page: string, params: any) : void { + this.navCtrl.setRoot('CoreMainMenuPage', {redirectPage: page, redirectParams: params}) + } + /** * Open a browser to perform OAuth login (Google, Facebook, Microsoft). * @@ -773,6 +809,41 @@ export class CoreLoginHelperProvider { return loginUrl; } + /** + * Redirect to a new page, setting it as the root page and loading the right site if needed. + * + * @param {string} page Name of the page to load. + * @param {any} params Params to pass to the page. + * @param {string} [siteId] Site to load. If not defined, current site. + */ + redirect(page: string, params?: any, siteId?: string) : void { + siteId = siteId || this.sitesProvider.getCurrentSiteId(); + + if (this.sitesProvider.isLoggedIn()) { + if (siteId && siteId != this.sitesProvider.getCurrentSiteId()) { + // Target page belongs to a different site. Change site. + // @todo Once we have addon manager. + // if ($mmAddonManager.hasRemoteAddonsLoaded()) { + // // The site has remote addons so the app will be restarted. Store the data and logout. + // this.appProvider.storeRedirect(siteId, page, params); + // this.sitesProvider.logout(); + // } else { + this.sitesProvider.logout().then(() => { + this.loadSiteAndPage(page, params, siteId); + }); + // } + } else { + this.loadPageInMainMenu(page, params); + } + } else { + if (siteId) { + this.loadSiteAndPage(page, params, siteId); + } else { + this.navCtrl.setRoot('CoreLoginSitesPage') + } + } + } + /** * Request a password reset. * diff --git a/src/core/mainmenu/pages/menu/menu.html b/src/core/mainmenu/pages/menu/menu.html index ec3f330e1..4e56c064d 100644 --- a/src/core/mainmenu/pages/menu/menu.html +++ b/src/core/mainmenu/pages/menu/menu.html @@ -1,3 +1,4 @@ - + + \ No newline at end of file diff --git a/src/core/mainmenu/pages/menu/menu.ts b/src/core/mainmenu/pages/menu/menu.ts index 57932bbaa..3e0091f55 100644 --- a/src/core/mainmenu/pages/menu/menu.ts +++ b/src/core/mainmenu/pages/menu/menu.ts @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnDestroy } from '@angular/core'; -import { IonicPage, NavController } from 'ionic-angular'; +import { Component, OnDestroy, ViewChild } from '@angular/core'; +import { IonicPage, NavController, NavParams, Tabs } from 'ionic-angular'; import { CoreEventsProvider } from '../../../../providers/events'; import { CoreSitesProvider } from '../../../../providers/sites'; import { CoreMainMenuProvider } from '../../providers/mainmenu'; @@ -28,8 +28,33 @@ import { CoreMainMenuDelegate, CoreMainMenuHandlerData } from '../../providers/d templateUrl: 'menu.html', }) export class CoreMainMenuPage implements OnDestroy { + // Use a setter to wait for ion-tabs to be loaded because it's inside a ngIf. + @ViewChild('mainTabs') set mainTabs(ionTabs: Tabs) { + if (ionTabs && this.redirectPage && !this.redirectPageLoaded) { + // Tabs ready and there is a redirect page set. Load it. + this.redirectPageLoaded = true; + + // Check if the page is the root page of any of the tabs. + let indexToSelect = 0; + for (let i = 0; i < this.tabs.length; i++) { + if (this.tabs[i].page == this.redirectPage) { + indexToSelect = i + 1; + break; + } + } + + // Use a setTimeout, otherwise loading the first tab opens a new state for some reason. + setTimeout(() => { + ionTabs.select(indexToSelect); + }); + } + }; + tabs: CoreMainMenuHandlerData[] = []; loaded: boolean; + redirectPage: string; + redirectParams: any; + protected subscription; protected moreTabData = { page: 'CoreMainMenuMorePage', @@ -37,15 +62,12 @@ export class CoreMainMenuPage implements OnDestroy { icon: 'more' }; protected moreTabAdded = false; - protected logoutObserver; + protected redirectPageLoaded = false; - constructor(private menuDelegate: CoreMainMenuDelegate, private sitesProvider: CoreSitesProvider, + constructor(private menuDelegate: CoreMainMenuDelegate, private sitesProvider: CoreSitesProvider, navParams: NavParams, private navCtrl: NavController, eventsProvider: CoreEventsProvider) { - - // Go to sites page when user is logged out. - this.logoutObserver = eventsProvider.on(CoreEventsProvider.LOGOUT, () => { - this.navCtrl.setRoot('CoreLoginSitesPage'); - }); + this.redirectPage = navParams.get('redirectPage'); + this.redirectParams = navParams.get('redirectParams'); } /** @@ -93,6 +115,5 @@ export class CoreMainMenuPage implements OnDestroy { */ ngOnDestroy() { this.subscription && this.subscription.unsubscribe(); - this.logoutObserver && this.logoutObserver.off(); } }