MOBILE-2333 siteaddons: Implement call WS directives
parent
6d9df59e67
commit
2c858bc9d2
|
@ -0,0 +1,123 @@
|
|||
// (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 { Input, OnInit, OnDestroy, ElementRef } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
||||
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
|
||||
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
/**
|
||||
* Base class for directives to call a WS when the element is clicked.
|
||||
*
|
||||
* The directives that inherit from this class will call a WS method when the element is clicked.
|
||||
*/
|
||||
export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy {
|
||||
@Input() name: string; // The name of the WS to call.
|
||||
@Input() params: any; // The params for the WS call.
|
||||
@Input() preSets: any; // The preSets for the WS call.
|
||||
@Input() confirmMessage: string; // Message to confirm the action. If not supplied, no confirmation. If empty, default message.
|
||||
@Input() useOtherDataForWS: any[]; // Whether to include other data in the params for the WS.
|
||||
// @see CoreSiteAddonsProvider.loadOtherDataInArgs.
|
||||
|
||||
protected element: HTMLElement;
|
||||
protected invalidateObserver: Subscription;
|
||||
|
||||
constructor(element: ElementRef, protected translate: TranslateService, protected domUtils: CoreDomUtilsProvider,
|
||||
protected siteAddonsProvider: CoreSiteAddonsProvider, protected parentContent: CoreSiteAddonsAddonContentComponent) {
|
||||
this.element = element.nativeElement || element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit(): void {
|
||||
this.element.addEventListener('click', (ev: Event): void => {
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
|
||||
if (typeof this.confirmMessage != 'undefined') {
|
||||
// Ask for confirm.
|
||||
this.domUtils.showConfirm(this.confirmMessage || this.translate.instant('core.areyousure')).then(() => {
|
||||
this.callWS();
|
||||
}).catch(() => {
|
||||
// User cancelled, ignore.
|
||||
});
|
||||
} else {
|
||||
this.callWS();
|
||||
}
|
||||
});
|
||||
|
||||
if (this.parentContent && this.parentContent.invalidateObservable) {
|
||||
this.invalidateObserver = this.parentContent.invalidateObservable.subscribe(() => {
|
||||
this.invalidate();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a WS.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
protected callWS(): Promise<any> {
|
||||
const modal = this.domUtils.showModalLoading();
|
||||
|
||||
let params = this.params;
|
||||
|
||||
if (this.parentContent) {
|
||||
params = this.siteAddonsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
|
||||
}
|
||||
|
||||
return this.siteAddonsProvider.callWS(this.name, params, this.preSets).then((result) => {
|
||||
return this.wsCallSuccess(result);
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true);
|
||||
}).finally(() => {
|
||||
modal.dismiss();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called when the WS call is successful.
|
||||
*
|
||||
* @param {any} result Result of the WS call.
|
||||
*/
|
||||
protected wsCallSuccess(result: any): void {
|
||||
// Function to be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate the WS call.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
invalidate(): Promise<any> {
|
||||
let params = this.params;
|
||||
|
||||
if (this.parentContent) {
|
||||
params = this.siteAddonsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
|
||||
}
|
||||
|
||||
return this.siteAddonsProvider.invalidateCallWS(this.name, params, this.preSets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Directive destroyed.
|
||||
*/
|
||||
ngOnDestroy(): void {
|
||||
this.invalidateObserver && this.invalidateObserver.unsubscribe();
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
|
||||
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
|
||||
import { CoreSiteAddonsProvider } from '../../providers/siteaddons';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
/**
|
||||
* Component to render a site addon content.
|
||||
|
@ -32,20 +33,21 @@ export class CoreSiteAddonsAddonContentComponent implements OnInit {
|
|||
|
||||
content: string; // Content.
|
||||
javascript: string; // Javascript to execute.
|
||||
otherData: any; // Other data of the content.
|
||||
dataLoaded: boolean;
|
||||
invalidateObservable: Subject<void>; // An observable to notify observers when to invalidate data.
|
||||
|
||||
constructor(protected domUtils: CoreDomUtilsProvider, protected siteAddonsProvider: CoreSiteAddonsProvider) {
|
||||
this.onContentLoaded = new EventEmitter();
|
||||
this.onLoadingContent = new EventEmitter();
|
||||
this.invalidateObservable = new Subject<void>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit(): void {
|
||||
this.fetchContent().finally(() => {
|
||||
this.dataLoaded = true;
|
||||
});
|
||||
this.fetchContent();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,17 +62,28 @@ export class CoreSiteAddonsAddonContentComponent implements OnInit {
|
|||
return this.siteAddonsProvider.getContent(this.component, this.method, this.args).then((result) => {
|
||||
this.content = result.html;
|
||||
this.javascript = result.javascript;
|
||||
this.otherData = result.otherdata;
|
||||
|
||||
this.onContentLoaded.emit(refresh);
|
||||
}).catch((error) => {
|
||||
this.domUtils.showErrorModalDefault(error, 'core.errorloadingcontent', true);
|
||||
}).finally(() => {
|
||||
this.dataLoaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the data.
|
||||
*
|
||||
* @param {boolean} [showSpinner] Whether to show spinner while refreshing.
|
||||
*/
|
||||
refreshData(): Promise<any> {
|
||||
refreshData(showSpinner?: boolean): Promise<any> {
|
||||
if (showSpinner) {
|
||||
this.dataLoaded = false;
|
||||
}
|
||||
|
||||
this.invalidateObservable.next(); // Notify observers.
|
||||
|
||||
return this.siteAddonsProvider.invalidateContent(this.component, this.method, this.args).finally(() => {
|
||||
return this.fetchContent(true);
|
||||
});
|
||||
|
@ -89,8 +102,6 @@ export class CoreSiteAddonsAddonContentComponent implements OnInit {
|
|||
this.args = args;
|
||||
this.dataLoaded = false;
|
||||
|
||||
this.fetchContent().finally(() => {
|
||||
this.dataLoaded = true;
|
||||
});
|
||||
this.fetchContent();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
// (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 { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
|
||||
import { NavController } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
||||
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
||||
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
|
||||
import { CoreSiteAddonsCallWSBaseDirective } from '../classes/call-ws-directive';
|
||||
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
|
||||
|
||||
/**
|
||||
* Directive to call a WS when the element is clicked and load a new content passing the WS result as args. This new content
|
||||
* can be displayed in a new page or in the same page (only if current page is already displaying a site addon content).
|
||||
*
|
||||
* If you don't need to load some new content when done, @see CoreSiteAddonsCallWSDirective.
|
||||
*
|
||||
* @see CoreSiteAddonsCallWSBaseDirective.
|
||||
*
|
||||
* Example usages:
|
||||
*
|
||||
* A button to get some data from the server without using cache, showing default confirm and displaying a new page:
|
||||
*
|
||||
* <button ion-button core-site-addons-call-ws-new-content name="mod_certificate_get_issued_certificates"
|
||||
* [params]="{certificateid: <% certificate.id %>}" [preSets]="{getFromCache: 0, saveToCache: 0}" confirmMessage
|
||||
* title="<% certificate.name %>" component="mod_certificate" method="mobile_issues_view"
|
||||
* [args]="{cmid: <% cmid %>, courseid: <% courseid %>}">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.getissued' | translate }}
|
||||
* </button>
|
||||
*
|
||||
* A button to get some data from the server using cache, without confirm, displaying new content in same page and using
|
||||
* userid from otherdata:
|
||||
*
|
||||
* <button ion-button core-site-addons-call-ws-new-content name="mod_certificate_get_issued_certificates"
|
||||
* [params]="{certificateid: <% certificate.id %>}" component="mod_certificate" method="mobile_issues_view"
|
||||
* [args]="{cmid: <% cmid %>, courseid: <% courseid %>}" samePage="true" [useOtherData]="['userid']">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.getissued' | translate }}
|
||||
* </button>
|
||||
*/
|
||||
@Directive({
|
||||
selector: '[core-site-addons-call-ws-new-content]'
|
||||
})
|
||||
export class CoreSiteAddonsCallWSNewContentDirective extends CoreSiteAddonsCallWSBaseDirective {
|
||||
@Input() component: string; // The component of the new content.
|
||||
@Input() method: string; // The method to get the new content.
|
||||
@Input() args: any; // The params to get the new content.
|
||||
@Input() title: string; // The title to display with the new content. Only if samePage=false.
|
||||
@Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
|
||||
@Input() useOtherData: any[]; // Whether to include other data in the args. @see CoreSiteAddonsProvider.loadOtherDataInArgs.
|
||||
|
||||
protected element: HTMLElement;
|
||||
|
||||
constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider,
|
||||
siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent,
|
||||
protected utils: CoreUtilsProvider, protected navCtrl: NavController) {
|
||||
super(element, translate, domUtils, siteAddonsProvider, parentContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called when the WS call is successful.
|
||||
*
|
||||
* @param {any} result Result of the WS call.
|
||||
*/
|
||||
protected wsCallSuccess(result: any): void {
|
||||
let args = this.args || {};
|
||||
|
||||
if (this.parentContent) {
|
||||
args = this.siteAddonsProvider.loadOtherDataInArgs(this.args, this.parentContent.otherData, this.useOtherData);
|
||||
}
|
||||
|
||||
// Add the properties from the WS call result to the args.
|
||||
args = Object.assign(args, result);
|
||||
|
||||
if (this.utils.isTrueOrOne(this.samePage)) {
|
||||
// Update the parent content (if it exists).
|
||||
if (this.parentContent) {
|
||||
this.parentContent.updateContent(this.component, this.method, args);
|
||||
}
|
||||
} else {
|
||||
this.navCtrl.push('CoreSiteAddonsAddonPage', {
|
||||
title: this.title,
|
||||
component: this.component,
|
||||
method: this.method,
|
||||
args: args
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
// (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 { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
|
||||
import { NavController } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
||||
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
||||
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
|
||||
import { CoreSiteAddonsCallWSBaseDirective } from '../classes/call-ws-directive';
|
||||
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
|
||||
|
||||
/**
|
||||
* Directive to call a WS when the element is clicked. The action to do when the WS call is successful depends on the input data:
|
||||
* display a message, go back or refresh current view.
|
||||
*
|
||||
* If you want to load a new content when the WS call is done, @see CoreSiteAddonsCallWSNewContentDirective.
|
||||
*
|
||||
* @see CoreSiteAddonsCallWSBaseDirective.
|
||||
*
|
||||
* Example usages:
|
||||
*
|
||||
* A button to send some data to the server without using cache, displaying default messages and refreshing on success:
|
||||
*
|
||||
* <button ion-button core-site-addons-call-ws name="mod_certificate_view_certificate"
|
||||
* [params]="{certificateid: <% certificate.id %>}" [preSets]="{getFromCache: 0, saveToCache: 0}" confirmMessage
|
||||
* successMessage refreshOnSuccess="true">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.senddata' | translate }}
|
||||
* </button>
|
||||
*
|
||||
* A button to send some data to the server using cache, without confirm, going back on success and using userid from otherdata:
|
||||
*
|
||||
* <button ion-button core-site-addons-call-ws name="mod_certificate_view_certificate"
|
||||
* [params]="{certificateid: <% certificate.id %>}" goBackOnSuccess="true" [useOtherData]="['userid']">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.senddata' | translate }}
|
||||
* </button>
|
||||
*/
|
||||
@Directive({
|
||||
selector: '[core-site-addons-call-ws]'
|
||||
})
|
||||
export class CoreSiteAddonsCallWSDirective extends CoreSiteAddonsCallWSBaseDirective {
|
||||
@Input() successMessage: string; // Message to show on success. If not supplied, no message. If empty, default message.
|
||||
@Input() goBackOnSuccess: boolean | string; // Whether to go back if the WS call is successful.
|
||||
@Input() refreshOnSuccess: boolean | string; // Whether to refresh the current view if the WS call is successful.
|
||||
|
||||
protected element: HTMLElement;
|
||||
|
||||
constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider,
|
||||
siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent,
|
||||
protected utils: CoreUtilsProvider, protected navCtrl: NavController) {
|
||||
super(element, translate, domUtils, siteAddonsProvider, parentContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called when the WS call is successful.
|
||||
*
|
||||
* @param {any} result Result of the WS call.
|
||||
*/
|
||||
protected wsCallSuccess(result: any): void {
|
||||
if (typeof this.successMessage != 'undefined') {
|
||||
// Display the success message.
|
||||
this.domUtils.showToast(this.successMessage || this.translate.instant('core.success'));
|
||||
}
|
||||
|
||||
if (this.utils.isTrueOrOne(this.goBackOnSuccess)) {
|
||||
this.navCtrl.pop();
|
||||
} else if (this.utils.isTrueOrOne(this.refreshOnSuccess) && this.parentContent) {
|
||||
this.parentContent.refreshData(true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,14 +13,20 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CoreSiteAddonsCallWSDirective } from './call-ws';
|
||||
import { CoreSiteAddonsCallWSNewContentDirective } from './call-ws-new-content';
|
||||
import { CoreSiteAddonsNewContentDirective } from './new-content';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
CoreSiteAddonsCallWSDirective,
|
||||
CoreSiteAddonsCallWSNewContentDirective,
|
||||
CoreSiteAddonsNewContentDirective
|
||||
],
|
||||
imports: [],
|
||||
exports: [
|
||||
CoreSiteAddonsCallWSDirective,
|
||||
CoreSiteAddonsCallWSNewContentDirective,
|
||||
CoreSiteAddonsNewContentDirective
|
||||
]
|
||||
})
|
||||
|
|
|
@ -15,11 +15,28 @@
|
|||
import { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
|
||||
import { NavController } from 'ionic-angular';
|
||||
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
||||
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
|
||||
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
|
||||
|
||||
/**
|
||||
* Directive to display a new site addon content when clicked. This new content can be displayed in a new page or in the
|
||||
* current page (only if the current page is already displaying a site addon content).
|
||||
*
|
||||
* Example usages:
|
||||
*
|
||||
* A button to go to a new content page:
|
||||
*
|
||||
* <button ion-button core-site-addons-new-content title="<% certificate.name %>" component="mod_certificate"
|
||||
* method="mobile_issues_view" [args]="{cmid: <% cmid %>, courseid: <% courseid %>}">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.viewissued' | translate }}
|
||||
* </button>
|
||||
*
|
||||
* A button to load new content in current page using a param from otherdata:
|
||||
*
|
||||
* <button ion-button core-site-addons-new-content component="mod_certificate" method="mobile_issues_view"
|
||||
* [args]="{cmid: <% cmid %>, courseid: <% courseid %>}" samePage="true" [useOtherData]="['userid']">
|
||||
* {{ 'addon.mod_certificate_coursecertificate.viewissued' | translate }}
|
||||
* </button>
|
||||
*/
|
||||
@Directive({
|
||||
selector: '[core-site-addons-new-content]'
|
||||
|
@ -29,12 +46,14 @@ export class CoreSiteAddonsNewContentDirective implements OnInit {
|
|||
@Input() method: string; // The method to get the new content.
|
||||
@Input() args: any; // The params to get the new content.
|
||||
@Input() title: string; // The title to display with the new content. Only if samePage=false.
|
||||
@Input() samePage?: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
|
||||
@Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
|
||||
@Input() useOtherData: any[]; // Whether to include other data in the args. @see CoreSiteAddonsProvider.loadOtherDataInArgs.
|
||||
|
||||
protected element: HTMLElement;
|
||||
|
||||
constructor(element: ElementRef, protected utils: CoreUtilsProvider, protected navCtrl: NavController,
|
||||
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent) {
|
||||
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent,
|
||||
protected siteAddonsProvider: CoreSiteAddonsProvider) {
|
||||
this.element = element.nativeElement || element;
|
||||
}
|
||||
|
||||
|
@ -46,17 +65,23 @@ export class CoreSiteAddonsNewContentDirective implements OnInit {
|
|||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
|
||||
let args = this.args;
|
||||
|
||||
if (this.parentContent) {
|
||||
args = this.siteAddonsProvider.loadOtherDataInArgs(this.args, this.parentContent.otherData, this.useOtherData);
|
||||
}
|
||||
|
||||
if (this.utils.isTrueOrOne(this.samePage)) {
|
||||
// Update the parent content (if it exists).
|
||||
if (this.parentContent) {
|
||||
this.parentContent.updateContent(this.component, this.method, this.args);
|
||||
this.parentContent.updateContent(this.component, this.method, args);
|
||||
}
|
||||
} else {
|
||||
this.navCtrl.push('CoreSiteAddonsAddonPage', {
|
||||
title: this.title,
|
||||
component: this.component,
|
||||
method: this.method,
|
||||
args: this.args
|
||||
args: args
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Platform } from 'ionic-angular';
|
||||
import { CoreAppProvider } from '../../../providers/app';
|
||||
import { CoreLangProvider } from '../../../providers/lang';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSite, CoreSiteWSPreSets } from '../../../classes/site';
|
||||
|
@ -80,10 +82,58 @@ export class CoreSiteAddonsProvider {
|
|||
protected moduleSiteAddons: {[modName: string]: CoreSiteAddonsModuleHandler} = {};
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private utils: CoreUtilsProvider,
|
||||
private langProvider: CoreLangProvider) {
|
||||
private langProvider: CoreLangProvider, private appProvider: CoreAppProvider, private platform: Platform) {
|
||||
this.logger = logger.getInstance('CoreUserProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some params that will always be sent for get content.
|
||||
*
|
||||
* @param {any} args Original params.
|
||||
* @param {CoreSite} [site] Site. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved with the new params.
|
||||
*/
|
||||
protected addDefaultArgs(args: any, site?: CoreSite): Promise<any> {
|
||||
args = args || {};
|
||||
site = site || this.sitesProvider.getCurrentSite();
|
||||
|
||||
return this.langProvider.getCurrentLanguage().then((lang) => {
|
||||
|
||||
// Clone the object so the original one isn't modified.
|
||||
const argsToSend = this.utils.clone(args);
|
||||
|
||||
argsToSend.userid = args.userid || site.getUserId();
|
||||
argsToSend.appid = CoreConfigConstants.app_id;
|
||||
argsToSend.appversioncode = CoreConfigConstants.versioncode;
|
||||
argsToSend.appversionname = CoreConfigConstants.versionname;
|
||||
argsToSend.applang = lang;
|
||||
argsToSend.appcustomurlscheme = CoreConfigConstants.customurlscheme;
|
||||
argsToSend.appisdesktop = this.appProvider.isDesktop();
|
||||
argsToSend.appismobile = this.appProvider.isMobile();
|
||||
argsToSend.appiswide = this.appProvider.isWide();
|
||||
|
||||
if (argsToSend.appisdevice) {
|
||||
if (this.platform.is('ios')) {
|
||||
argsToSend.appplatform = 'ios';
|
||||
} else {
|
||||
argsToSend.appplatform = 'android';
|
||||
}
|
||||
} else if (argsToSend.appisdesktop) {
|
||||
if (this.appProvider.isMac()) {
|
||||
argsToSend.appplatform = 'mac';
|
||||
} else if (this.appProvider.isLinux()) {
|
||||
argsToSend.appplatform = 'linux';
|
||||
} else {
|
||||
argsToSend.appplatform = 'windows';
|
||||
}
|
||||
} else {
|
||||
argsToSend.appplatform = 'browser';
|
||||
}
|
||||
|
||||
return argsToSend;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a WS for a site addon.
|
||||
*
|
||||
|
@ -120,7 +170,7 @@ export class CoreSiteAddonsProvider {
|
|||
* @return {string} Cache key.
|
||||
*/
|
||||
protected getCallWSCommonCacheKey(method: string): string {
|
||||
return this.ROOT_CACHE_KEY + method;
|
||||
return this.ROOT_CACHE_KEY + 'ws:' + method;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,15 +186,9 @@ export class CoreSiteAddonsProvider {
|
|||
this.logger.debug(`Get content for component '${component}' and method '${method}'`);
|
||||
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
// Get current language to be added to params.
|
||||
return this.langProvider.getCurrentLanguage().then((lang) => {
|
||||
// Add some params that will always be sent. Clone the object so the original one isn't modified.
|
||||
const argsToSend = this.utils.clone(args);
|
||||
argsToSend.userid = args.userid || site.getUserId();
|
||||
argsToSend.appid = CoreConfigConstants.app_id;
|
||||
argsToSend.versionname = CoreConfigConstants.versionname;
|
||||
argsToSend.lang = lang;
|
||||
|
||||
// Add some params that will always be sent.
|
||||
return this.addDefaultArgs(args, site).then((argsToSend) => {
|
||||
// Now call the WS.
|
||||
const data = {
|
||||
component: component,
|
||||
|
@ -209,12 +253,13 @@ export class CoreSiteAddonsProvider {
|
|||
*
|
||||
* @param {string} method WS method to use.
|
||||
* @param {any} data Data to send to the WS.
|
||||
* @param {CoreSiteWSPreSets} [preSets] Extra options.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
||||
*/
|
||||
invalidateCallWS(method: string, data: any, siteId?: string): Promise<any> {
|
||||
invalidateCallWS(method: string, data: any, preSets?: CoreSiteWSPreSets, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
return site.invalidateWsCacheForKey(this.getCallWSCacheKey(method, data));
|
||||
return site.invalidateWsCacheForKey(preSets.cacheKey || this.getCallWSCacheKey(method, data));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -244,6 +289,44 @@ export class CoreSiteAddonsProvider {
|
|||
return site.wsAvailable('tool_mobile_get_content');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load other data into args as determined by useOtherData list.
|
||||
* If useOtherData is undefined, it won't add any data.
|
||||
* If useOtherData is defined but empty (null, false or empty string) it will copy all the data from otherData to args.
|
||||
* If useOtherData is an array, it will only copy the properties whose names are in the array.
|
||||
*
|
||||
* @param {any} args The current args.
|
||||
* @param {any} otherData All the other data.
|
||||
* @param {any[]} useOtherData Names of the attributes to include.
|
||||
* @return {any} New args.
|
||||
*/
|
||||
loadOtherDataInArgs(args: any, otherData: any, useOtherData: any[]): any {
|
||||
if (!args) {
|
||||
args = {};
|
||||
} else {
|
||||
args = this.utils.clone(args);
|
||||
}
|
||||
|
||||
otherData = otherData || {};
|
||||
|
||||
if (typeof useOtherData == 'undefined') {
|
||||
// No need to add other data, return args as they are.
|
||||
return args;
|
||||
} else if (!useOtherData) {
|
||||
// Use other data is defined but empty. Add all the data to args.
|
||||
for (const name in otherData) {
|
||||
args[name] = otherData[name];
|
||||
}
|
||||
} else {
|
||||
for (const i in useOtherData) {
|
||||
const name = useOtherData[i];
|
||||
args[name] = otherData[name];
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the site addon handler for a certain module.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue