MOBILE-2309 contentlinks: Implement delegate and helper
parent
cb561cc4a3
commit
1cfd38229f
|
@ -0,0 +1,29 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CoreContentLinksDelegate } from './providers/delegate';
|
||||
import { CoreContentLinksHelperProvider } from './providers/helper';
|
||||
|
||||
@NgModule({
|
||||
declarations: [],
|
||||
imports: [
|
||||
],
|
||||
providers: [
|
||||
CoreContentLinksDelegate,
|
||||
CoreContentLinksHelperProvider
|
||||
],
|
||||
exports: []
|
||||
})
|
||||
export class CoreContentLinksModule {}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"chooseaccount": "Choose account",
|
||||
"chooseaccounttoopenlink": "Choose an account to open the link with.",
|
||||
"confirmurlothersite": "This link belongs to another site. Do you want to open it?",
|
||||
"errornoactions": "Couldn't find an action to perform with this link.",
|
||||
"errornosites": "Couldn't find any site to handle this link."
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<ion-header>
|
||||
<ion-navbar>
|
||||
<ion-title>{{ 'core.contentlinks.chooseaccount' | translate }}</ion-title>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<core-loading [hideUntil]="loaded">
|
||||
<ion-list>
|
||||
<ion-item text-wrap>
|
||||
<p class="item-heading">{{ 'core.contentlinks.chooseaccounttoopenlink' | translate }}</p>
|
||||
<p>{{ url }}</p>
|
||||
</ion-item>
|
||||
<a ion-item *ngFor="let site of sites" (click)="siteClicked(site.id)">
|
||||
<img [src]="site.avatar" item-start>
|
||||
<h2>{{site.fullName}}</h2>
|
||||
<p><core-format-text clean="true" [text]="site.siteName"></core-format-text></p>
|
||||
<p>{{site.siteUrl}}</p>
|
||||
</a>
|
||||
<ion-item>
|
||||
<button ion-button block (click)="cancel()">{{ 'core.login.cancel' | translate }}</button>
|
||||
</ion-item>
|
||||
</ion-list>
|
||||
</core-loading>
|
||||
</ion-content>
|
|
@ -0,0 +1,33 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { IonicPageModule } from 'ionic-angular';
|
||||
import { CoreContentLinksChooseSitePage } from './choose-site';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreComponentsModule } from '../../../../components/components.module';
|
||||
import { CoreDirectivesModule } from '../../../../directives/directives.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
CoreContentLinksChooseSitePage
|
||||
],
|
||||
imports: [
|
||||
CoreComponentsModule,
|
||||
CoreDirectivesModule,
|
||||
IonicPageModule.forChild(CoreContentLinksChooseSitePage),
|
||||
TranslateModule.forChild()
|
||||
]
|
||||
})
|
||||
export class CoreContentLinksChooseSitePageModule {}
|
|
@ -0,0 +1,95 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { IonicPage, NavController, NavParams } from 'ionic-angular';
|
||||
import { CoreSitesProvider } from '../../../../providers/sites';
|
||||
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
|
||||
import { CoreContentLinksDelegate, CoreContentLinksAction } from '../../providers/delegate';
|
||||
import { CoreContentLinksHelperProvider } from '../../providers/helper';
|
||||
|
||||
/**
|
||||
* Page to display the list of sites to choose one to perform a content link action.
|
||||
*/
|
||||
@IonicPage({segment: 'core-content-links-choose-site'})
|
||||
@Component({
|
||||
selector: 'page-core-content-links-choose-site',
|
||||
templateUrl: 'choose-site.html',
|
||||
})
|
||||
export class CoreContentLinksChooseSitePage implements OnInit {
|
||||
|
||||
url: string;
|
||||
sites: any[];
|
||||
loaded: boolean;
|
||||
protected action: CoreContentLinksAction;
|
||||
|
||||
constructor(private navCtrl: NavController, navParams: NavParams, private contentLinksDelegate: CoreContentLinksDelegate,
|
||||
private sitesProvider: CoreSitesProvider, private domUtils: CoreDomUtilsProvider,
|
||||
private contentLinksHelper: CoreContentLinksHelperProvider) {
|
||||
this.url = navParams.get('url');
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit() {
|
||||
if (!this.url) {
|
||||
return this.leaveView();
|
||||
}
|
||||
|
||||
// Get the action to perform.
|
||||
this.contentLinksDelegate.getActionsFor(this.url).then((actions) => {
|
||||
this.action = this.contentLinksHelper.getFirstValidAction(actions);
|
||||
if (!this.action) {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
|
||||
// Get the sites that can perform the action.
|
||||
return this.sitesProvider.getSites(this.action.sites).then((sites) => {
|
||||
this.sites = sites;
|
||||
});
|
||||
}).catch(() => {
|
||||
this.domUtils.showErrorModal('core.contentlinks.errornosites', true);
|
||||
this.leaveView();
|
||||
}).finally(() => {
|
||||
this.loaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel.
|
||||
*/
|
||||
cancel() : void {
|
||||
this.leaveView();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the action on a certain site.
|
||||
*
|
||||
* @param {string} siteId Site ID.
|
||||
*/
|
||||
siteClicked(siteId: string) : void {
|
||||
this.action.action(siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel and leave the view.
|
||||
*/
|
||||
protected leaveView() {
|
||||
this.sitesProvider.logout().finally(() => {
|
||||
this.navCtrl.setRoot('CoreLoginSitesPage');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,310 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreUrlUtilsProvider } from '../../../providers/utils/url';
|
||||
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
||||
|
||||
/**
|
||||
* Interface that all handlers must implement.
|
||||
*/
|
||||
export interface CoreContentLinksHandler {
|
||||
/**
|
||||
* A name to identify the handler.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Handler's priority. The highest priority is treated first.
|
||||
* @type {number}
|
||||
*/
|
||||
priority?: number;
|
||||
|
||||
/**
|
||||
* Whether the isEnabled function should be called for all the users in a site. It should be true only if the isEnabled call
|
||||
* can return different values for different users in same site.
|
||||
* @type {boolean}
|
||||
*/
|
||||
checkAllUsers?: boolean;
|
||||
|
||||
/**
|
||||
* Name of the feature this handler is related to.
|
||||
* It will be used to check if the feature is disabled (@see CoreSite.isFeatureDisabled).
|
||||
* @type {string}
|
||||
*/
|
||||
featureName?: string;
|
||||
|
||||
/**
|
||||
* Get the list of actions for a link (url).
|
||||
*
|
||||
* @param {string[]} siteIds List of sites the URL belongs to.
|
||||
* @param {string} url The URL to treat.
|
||||
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
|
||||
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
|
||||
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
|
||||
*/
|
||||
getActions(siteIds: string[], url: string, params: any, courseId?: number) :
|
||||
CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>;
|
||||
|
||||
/**
|
||||
* Check if a URL is handled by this handler.
|
||||
*
|
||||
* @param {string} url The URL to check.
|
||||
* @return {boolean} Whether the URL is handled by this handler
|
||||
*/
|
||||
handles(url: string) : boolean;
|
||||
|
||||
|
||||
/**
|
||||
* If the URL is handled by this handler, return the site URL.
|
||||
*
|
||||
* @param {string} url The URL to check.
|
||||
* @return {string} Site URL if it is handled, undefined otherwise.
|
||||
*/
|
||||
getSiteUrl(url: string) : string;
|
||||
|
||||
/**
|
||||
* Check if the handler is enabled for a certain site (site + user) and a URL.
|
||||
* If not defined, defaults to true.
|
||||
*
|
||||
* @param {string} siteId The site ID.
|
||||
* @param {string} url The URL to treat.
|
||||
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
|
||||
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
|
||||
* @return {boolean|Promise<boolean>} Whether the handler is enabled for the URL and site.
|
||||
*/
|
||||
isEnabled?(siteId: string, url: string, params: any, courseId?: number) : boolean|Promise<boolean>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Action to perform when a link is clicked.
|
||||
*/
|
||||
export interface CoreContentLinksAction {
|
||||
/**
|
||||
* A message to identify the action. Default: 'core.view'.
|
||||
* @type {string}
|
||||
*/
|
||||
message?: string;
|
||||
|
||||
/**
|
||||
* Name of the icon of the action. Default: 'eye'.
|
||||
* @type {string}
|
||||
*/
|
||||
icon?: string;
|
||||
|
||||
/**
|
||||
* IDs of the sites that support the action.
|
||||
* @type {string[]}
|
||||
*/
|
||||
sites?: string[];
|
||||
|
||||
/**
|
||||
* Action to perform when the link is clicked.
|
||||
*
|
||||
* @param {string} siteId The site ID.
|
||||
*/
|
||||
action(siteId: string) : void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Actions and priority for a handler and URL.
|
||||
*/
|
||||
export interface CoreContentLinksHandlerActions {
|
||||
/**
|
||||
* Handler's priority.
|
||||
* @type {number}
|
||||
*/
|
||||
priority: number;
|
||||
|
||||
/**
|
||||
* List of actions.
|
||||
* @type {CoreContentLinksAction[]}
|
||||
*/
|
||||
actions: CoreContentLinksAction[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Delegate to register handlers to handle links.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreContentLinksDelegate {
|
||||
protected logger;
|
||||
protected handlers: {[s: string]: CoreContentLinksHandler} = {}; // All registered handlers.
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private urlUtils: CoreUrlUtilsProvider,
|
||||
private utils: CoreUtilsProvider) {
|
||||
this.logger = logger.getInstance('CoreContentLinksDelegate');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of possible actions to do for a URL.
|
||||
*
|
||||
* @param {string} url URL to handle.
|
||||
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
|
||||
* @param {string} [username] Username to use to filter sites.
|
||||
* @return {Promise<CoreContentLinksAction[]>} Promise resolved with the actions.
|
||||
*/
|
||||
getActionsFor(url: string, courseId?: number, username?: string) : Promise<CoreContentLinksAction[]> {
|
||||
if (!url) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
// Get the list of sites the URL belongs to.
|
||||
return this.sitesProvider.getSiteIdsFromUrl(url, true, username).then((siteIds) => {
|
||||
const linkActions: CoreContentLinksHandlerActions[] = [],
|
||||
promises = [],
|
||||
params = this.urlUtils.extractUrlParams(url);
|
||||
|
||||
for (let name in this.handlers) {
|
||||
const handler = this.handlers[name],
|
||||
checkAll = handler.checkAllUsers,
|
||||
isEnabledFn = this.isHandlerEnabled.bind(this, handler, url, params, courseId);
|
||||
|
||||
if (!handler.handles(url)) {
|
||||
// Invalid handler or it doesn't handle the URL. Stop.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Filter the site IDs using the isEnabled function.
|
||||
promises.push(this.utils.filterEnabledSites(siteIds, isEnabledFn, checkAll).then((siteIds) => {
|
||||
if (!siteIds.length) {
|
||||
// No sites supported, no actions.
|
||||
return;
|
||||
}
|
||||
|
||||
return Promise.resolve(handler.getActions(siteIds, url, params, courseId)).then((actions) => {
|
||||
if (actions && actions.length) {
|
||||
// Set default values if any value isn't supplied.
|
||||
actions.forEach((action) => {
|
||||
action.message = action.message || 'core.view';
|
||||
action.icon = action.icon || 'eye';
|
||||
action.sites = action.sites || siteIds;
|
||||
});
|
||||
|
||||
// Add them to the list.
|
||||
linkActions.push({
|
||||
priority: handler.priority,
|
||||
actions: actions
|
||||
});
|
||||
}
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
return this.utils.allPromises(promises).catch(() => {
|
||||
// Ignore errors.
|
||||
}).then(() => {
|
||||
// Sort link actions by priority.
|
||||
return this.sortActionsByPriority(linkActions);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the site URL if the URL is supported by any handler.
|
||||
*
|
||||
* @param {string} url URL to handle.
|
||||
* @return {string} Site URL if the URL is supported by any handler, undefined otherwise.
|
||||
*/
|
||||
getSiteUrl(url: string) : string {
|
||||
if (!url) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if any handler supports this URL.
|
||||
for (let name in this.handlers) {
|
||||
const handler = this.handlers[name],
|
||||
siteUrl = handler.getSiteUrl(url);
|
||||
|
||||
if (siteUrl) {
|
||||
return siteUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a handler is enabled for a certain site and URL.
|
||||
*
|
||||
* @param {CoreContentLinksHandler} handler Handler to check.
|
||||
* @param {string} url The URL to check.
|
||||
* @param {any} params The params of the URL
|
||||
* @param {number} courseId Course ID the URL belongs to (can be undefined).
|
||||
* @param {string} siteId The site ID to check.
|
||||
* @return {Promise<boolean>} Promise resolved with boolean: whether the handler is enabled.
|
||||
*/
|
||||
protected isHandlerEnabled(handler: CoreContentLinksHandler, url: string, params: any, courseId: number, siteId: string)
|
||||
: Promise<boolean> {
|
||||
let promise;
|
||||
|
||||
if (handler.featureName) {
|
||||
// Check if the feature is disabled.
|
||||
promise = this.sitesProvider.isFeatureDisabled(handler.featureName, siteId);
|
||||
} else {
|
||||
promise = Promise.resolve(false);
|
||||
}
|
||||
|
||||
return promise.then((disabled) => {
|
||||
if (disabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!handler.isEnabled) {
|
||||
// isEnabled function not provided, assume it's enabled.
|
||||
return true;
|
||||
}
|
||||
|
||||
return handler.isEnabled(siteId, url, params, courseId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreContentLinksHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreContentLinksHandler) : boolean {
|
||||
if (typeof this.handlers[handler.name] !== 'undefined') {
|
||||
this.logger.log(`Addon '${handler.name}' already registered`);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}'`);
|
||||
this.handlers[handler.name] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort actions by priority.
|
||||
*
|
||||
* @param {CoreContentLinksHandlerActions[]} actions Actions to sort.
|
||||
* @return {CoreContentLinksAction[]} Sorted actions.
|
||||
*/
|
||||
protected sortActionsByPriority(actions: CoreContentLinksHandlerActions[]) : CoreContentLinksAction[] {
|
||||
let sorted: CoreContentLinksAction[] = [];
|
||||
|
||||
// Sort by priority.
|
||||
actions = actions.sort((a, b) => {
|
||||
return a.priority >= b.priority ? 1 : -1;
|
||||
});
|
||||
|
||||
// Fill result array.
|
||||
actions.forEach((entry) => {
|
||||
sorted = sorted.concat(entry.actions);
|
||||
});
|
||||
return sorted;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,239 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { NavController } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreAppProvider } from '../../../providers/app';
|
||||
import { CoreEventsProvider } from '../../../providers/events';
|
||||
import { CoreInitDelegate } from '../../../providers/init';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
||||
import { CoreUrlUtilsProvider } from '../../../providers/utils/url';
|
||||
import { CoreLoginHelperProvider } from '../../login/providers/helper';
|
||||
import { CoreContentLinksDelegate, CoreContentLinksAction } from './delegate';
|
||||
import { CoreConstants } from '../../constants';
|
||||
import { CoreConfigConstants } from '../../../configconstants';
|
||||
|
||||
/**
|
||||
* Service that provides some features regarding content links.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreContentLinksHelperProvider {
|
||||
protected logger;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private loginHelper: CoreLoginHelperProvider,
|
||||
private contentLinksDelegate: CoreContentLinksDelegate, private appProvider: CoreAppProvider,
|
||||
private domUtils: CoreDomUtilsProvider, private urlUtils: CoreUrlUtilsProvider, private translate: TranslateService,
|
||||
private initDelegate: CoreInitDelegate, eventsProvider: CoreEventsProvider) {
|
||||
this.logger = logger.getInstance('CoreContentLinksHelperProvider');
|
||||
|
||||
// Listen for app launched URLs. If we receive one, check if it's a content link.
|
||||
eventsProvider.on(CoreEventsProvider.APP_LAUNCHED_URL, this.handleCustomUrl.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first valid action in a list of actions.
|
||||
*
|
||||
* @param {CoreContentLinksAction[]} actions List of actions.
|
||||
* @return {CoreContentLinksAction} First valid action. Returns undefined if no valid action found.
|
||||
*/
|
||||
getFirstValidAction(actions: CoreContentLinksAction[]) : CoreContentLinksAction {
|
||||
if (actions) {
|
||||
for (let i = 0; i < actions.length; i++) {
|
||||
const action = actions[i];
|
||||
if (action && action.sites && action.sites.length) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Goes to a certain page in a certain site. If the site is current site it will perform a regular navigation,
|
||||
* otherwise it will 'redirect' to the other site.
|
||||
*
|
||||
* @param {NavController} navCtrl The NavController instance to use.
|
||||
* @param {string} pageName Name of the page to go.
|
||||
* @param {any} [pageParams] Params to send to the page.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
*/
|
||||
goInSite(navCtrl: NavController, pageName: string, pageParams: any, siteId?: string) : void {
|
||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||
if (siteId == this.sitesProvider.getCurrentSiteId()) {
|
||||
navCtrl.push(pageName, pageParams);
|
||||
} else {
|
||||
this.loginHelper.redirect(pageName, pageParams, siteId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Go to the page to choose a site.
|
||||
*
|
||||
* @param {string} url URL to treat.
|
||||
*/
|
||||
goToChooseSite(url: string) : void {
|
||||
this.appProvider.getRootNavController().setRoot('CoreContentLinksChooseSitePage', {url: url});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a URL received by Custom URL Scheme.
|
||||
*
|
||||
* @param {string} url URL to handle.
|
||||
* @return {boolean} True if the URL should be handled by this component, false otherwise.
|
||||
*/
|
||||
handleCustomUrl(url: string) : boolean {
|
||||
const contentLinksScheme = CoreConfigConstants.customurlscheme + '://link';
|
||||
if (url.indexOf(contentLinksScheme) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
url = decodeURIComponent(url);
|
||||
|
||||
// App opened using custom URL scheme.
|
||||
this.logger.debug('Treating custom URL scheme: ' + url);
|
||||
|
||||
let modal = this.domUtils.showModalLoading(),
|
||||
username;
|
||||
|
||||
// Delete the scheme from the URL.
|
||||
url = url.replace(contentLinksScheme + '=', '');
|
||||
|
||||
// Detect if there's a user specified.
|
||||
username = this.urlUtils.getUsernameFromUrl(url);
|
||||
if (username) {
|
||||
url = url.replace(username + '@', ''); // Remove the username from the URL.
|
||||
}
|
||||
|
||||
// Wait for the app to be ready.
|
||||
this.initDelegate.ready().then(() => {
|
||||
// Check if the site is stored.
|
||||
return this.sitesProvider.getSiteIdsFromUrl(url, false, username);
|
||||
}).then((siteIds) => {
|
||||
if (siteIds.length) {
|
||||
modal.dismiss(); // Dismiss modal so it doesn't collide with confirms.
|
||||
return this.handleLink(url, username).then((treated) => {
|
||||
if (!treated) {
|
||||
this.domUtils.showErrorModal('core.contentlinks.errornoactions', true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Get the site URL.
|
||||
const siteUrl = this.contentLinksDelegate.getSiteUrl(url);
|
||||
if (!siteUrl) {
|
||||
this.domUtils.showErrorModal('core.login.invalidsite', true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check that site exists.
|
||||
return this.sitesProvider.checkSite(siteUrl).then((result) => {
|
||||
// Site exists. We'll allow to add it.
|
||||
let promise,
|
||||
ssoNeeded = this.loginHelper.isSSOLoginNeeded(result.code),
|
||||
hasRemoteAddonsLoaded = false,
|
||||
pageName = 'CoreLoginCredentialsPage',
|
||||
pageParams = {
|
||||
siteUrl: result.siteUrl,
|
||||
username: username,
|
||||
urlToOpen: url,
|
||||
siteConfig: result.config
|
||||
};
|
||||
|
||||
modal.dismiss(); // Dismiss modal so it doesn't collide with confirms.
|
||||
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
// Not logged in, no need to confirm. If SSO the confirm will be shown later.
|
||||
promise = Promise.resolve();
|
||||
} else {
|
||||
// Ask the user before changing site.
|
||||
const confirmMsg = this.translate.instant('core.contentlinks.confirmurlothersite');
|
||||
promise = this.domUtils.showConfirm(confirmMsg).then(() => {
|
||||
if (!ssoNeeded) {
|
||||
// hasRemoteAddonsLoaded = $mmAddonManager.hasRemoteAddonsLoaded(); @todo
|
||||
if (hasRemoteAddonsLoaded) {
|
||||
// Store the redirect since logout will restart the app.
|
||||
this.appProvider.storeRedirect(CoreConstants.NO_SITE_ID, pageName, pageParams);
|
||||
}
|
||||
|
||||
return this.sitesProvider.logout().catch(() => {
|
||||
// Ignore errors (shouldn't happen).
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return promise.then(() => {
|
||||
if (ssoNeeded) {
|
||||
this.loginHelper.confirmAndOpenBrowserForSSOLogin(
|
||||
result.siteUrl, result.code, result.service, result.config && result.config.launchurl);
|
||||
} else if (!hasRemoteAddonsLoaded) {
|
||||
this.appProvider.getRootNavController().setRoot(pageName, pageParams);
|
||||
}
|
||||
});
|
||||
|
||||
}).catch((error) => {
|
||||
if (error) {
|
||||
this.domUtils.showErrorModal(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).finally(() => {
|
||||
modal.dismiss();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a link.
|
||||
*
|
||||
* @param {string} url URL to handle.
|
||||
* @param {string} [username] Username related with the URL. E.g. in 'http://myuser@m.com', url would be 'http://m.com' and
|
||||
* the username 'myuser'. Don't use it if you don't want to filter by username.
|
||||
* @return {Promise<boolean>} Promise resolved with a boolean: true if URL was treated, false otherwise.
|
||||
*/
|
||||
handleLink(url: string, username?: string) : Promise<boolean> {
|
||||
// Check if the link should be treated by some component/addon.
|
||||
return this.contentLinksDelegate.getActionsFor(url, undefined, username).then((actions) => {
|
||||
const action = this.getFirstValidAction(actions);
|
||||
if (action) {
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
// No current site. Perform the action if only 1 site found, choose the site otherwise.
|
||||
if (action.sites.length == 1) {
|
||||
action.action(action.sites[0]);
|
||||
} else {
|
||||
this.goToChooseSite(url);
|
||||
}
|
||||
} else if (action.sites.length == 1 && action.sites[0] == this.sitesProvider.getCurrentSiteId()) {
|
||||
// Current site.
|
||||
action.action(action.sites[0]);
|
||||
} else {
|
||||
// Not current site or more than one site. Ask for confirmation.
|
||||
this.domUtils.showConfirm(this.translate.instant('core.contentlinks.confirmurlothersite')).then(() => {
|
||||
if (action.sites.length == 1) {
|
||||
action.action(action.sites[0]);
|
||||
} else {
|
||||
this.goToChooseSite(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}).catch(() => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -776,7 +776,7 @@ export class CoreLoginHelperProvider {
|
|||
if (siteId) {
|
||||
this.loadSiteAndPage(page, params, siteId);
|
||||
} else {
|
||||
this.appProvider.getRootNavController().setRoot('CoreLoginSitesPage')
|
||||
this.appProvider.getRootNavController().setRoot('CoreLoginSitesPage');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue