MOBILE-2302 core: Implement redirect to another page/site

main
Dani Palou 2018-01-08 13:21:15 +01:00
parent 730631e153
commit ad5365edfe
3 changed files with 104 additions and 11 deletions

View File

@ -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.
*

View File

@ -1,3 +1,4 @@
<ion-tabs *ngIf="loaded">
<ion-tabs *ngIf="loaded" #mainTabs>
<ion-tab [enabled]="false" [show]="false" [root]="redirectPage" [rootParams]="redirectParams"></ion-tab>
<ion-tab *ngFor="let tab of tabs" [root]="tab.page" [tabTitle]="tab.title | translate" [tabIcon]="tab.icon" class="{{tab.class}}"></ion-tab>
</ion-tabs>

View File

@ -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();
}
}